2019-01-28 16:55:28 +03:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2022-02-04 14:10:33 +03:00
|
|
|
"bytes"
|
2021-12-15 20:54:47 +03:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-09-27 17:52:46 +03:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-07-23 12:49:44 +03:00
|
|
|
|
2021-12-15 20:54:47 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/httpc"
|
|
|
|
|
2020-04-28 14:59:57 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-01-28 16:55:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServerState is the state of Hasura stored on the server.
|
|
|
|
type ServerState struct {
|
|
|
|
UUID string
|
|
|
|
CLIState map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type hdbVersion struct {
|
|
|
|
UUID string `json:"hasura_uuid"`
|
|
|
|
CLIState map[string]interface{} `json:"cli_state"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetServerState queries a server for the state.
|
2021-12-15 20:54:47 +03:00
|
|
|
func GetServerState(client *httpc.Client, endpoint string, hasMetadataV3 bool, log *logrus.Logger) *ServerState {
|
2019-01-28 16:55:28 +03:00
|
|
|
state := &ServerState{
|
|
|
|
UUID: "00000000-0000-0000-0000-000000000000",
|
|
|
|
}
|
2021-01-18 20:11:05 +03:00
|
|
|
|
2021-04-05 17:00:21 +03:00
|
|
|
if hasMetadataV3 {
|
2021-12-15 20:54:47 +03:00
|
|
|
payload := []byte(`
|
2021-01-18 20:11:05 +03:00
|
|
|
{
|
|
|
|
"type": "get_catalog_state",
|
|
|
|
"args": {}
|
|
|
|
}
|
2021-12-15 20:54:47 +03:00
|
|
|
`)
|
2021-04-05 17:00:21 +03:00
|
|
|
var r struct {
|
2021-01-18 20:11:05 +03:00
|
|
|
ID string `json:"id"`
|
|
|
|
}
|
2021-12-15 20:54:47 +03:00
|
|
|
var body interface{}
|
|
|
|
err := json.Unmarshal(payload, &body)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("unmarshalling json request to construct server state failed: %v", err)
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
req, err := client.NewRequest(http.MethodPost, endpoint, body)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("constructing http request to construct server state failed: %v", err)
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = client.Do(context.Background(), req, &r)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("http request to construct server state failed: %v", err)
|
2021-01-18 20:11:05 +03:00
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
state.UUID = r.ID
|
2021-04-05 17:00:21 +03:00
|
|
|
} else {
|
2021-12-15 20:54:47 +03:00
|
|
|
payload := []byte(`{
|
2019-01-28 16:55:28 +03:00
|
|
|
"type": "select",
|
|
|
|
"args": {
|
|
|
|
"table": {
|
|
|
|
"schema": "hdb_catalog",
|
|
|
|
"name": "hdb_version"
|
|
|
|
},
|
|
|
|
"columns": [
|
|
|
|
"hasura_uuid",
|
|
|
|
"cli_state"
|
|
|
|
]
|
|
|
|
}
|
2021-12-15 20:54:47 +03:00
|
|
|
}`)
|
|
|
|
var body interface{}
|
|
|
|
err := json.Unmarshal(payload, &body)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("unmarshalling json request to construct server state failed: %v", err)
|
|
|
|
return state
|
2021-01-18 20:11:05 +03:00
|
|
|
}
|
2021-12-15 20:54:47 +03:00
|
|
|
req, err := client.NewRequest(http.MethodPost, endpoint, body)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("constructing http request to construct server state failed: %v", err)
|
2021-01-18 20:11:05 +03:00
|
|
|
return state
|
|
|
|
}
|
2021-12-15 20:54:47 +03:00
|
|
|
var r []hdbVersion
|
|
|
|
_, err = client.Do(context.Background(), req, &r)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("http request to construct server state failed: %v", err)
|
2021-01-18 20:11:05 +03:00
|
|
|
return state
|
|
|
|
}
|
2021-12-15 20:54:47 +03:00
|
|
|
if len(r) >= 1 {
|
|
|
|
state.UUID = r[0].UUID
|
|
|
|
state.CLIState = r[0].CLIState
|
|
|
|
}
|
2021-01-18 20:11:05 +03:00
|
|
|
}
|
2019-01-28 16:55:28 +03:00
|
|
|
return state
|
2021-01-18 20:11:05 +03:00
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
}
|
2021-09-27 17:52:46 +03:00
|
|
|
|
2022-02-04 14:10:33 +03:00
|
|
|
func GetServerStatus(versionEndpoint string, httpClient *httpc.Client) (err error) {
|
|
|
|
req, err := http.NewRequest("GET", versionEndpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create GET request to %s: %w", versionEndpoint, err)
|
|
|
|
}
|
|
|
|
var responseBs bytes.Buffer
|
|
|
|
resp, err := httpClient.Do(context.Background(), req, &responseBs)
|
2021-09-27 17:52:46 +03:00
|
|
|
if err != nil {
|
2021-12-15 20:54:47 +03:00
|
|
|
return fmt.Errorf("making http request failed: %w", err)
|
2021-09-27 17:52:46 +03:00
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2022-02-04 14:10:33 +03:00
|
|
|
return fmt.Errorf("request failed: url: %s status code: %v status: %s \n%s", versionEndpoint, resp.StatusCode, resp.Status, responseBs.String())
|
2021-09-27 17:52:46 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|