graphql-engine/cli/util/server.go
Kali Vara Purushotham Santhati 780d942fad cli: skip tls verfication for all API requests when insecure-skip-tls-verify flag is set
closes: https://github.com/hasura/graphql-engine/issues/4926

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3307
Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com>
GitOrigin-RevId: 90222fc2e5b176fbf5721fea3f5b2af929ca93f4
2022-02-04 11:11:37 +00:00

116 lines
2.8 KiB
Go

package util
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/hasura/graphql-engine/cli/v2/internal/httpc"
"github.com/sirupsen/logrus"
)
// 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.
func GetServerState(client *httpc.Client, endpoint string, hasMetadataV3 bool, log *logrus.Logger) *ServerState {
state := &ServerState{
UUID: "00000000-0000-0000-0000-000000000000",
}
if hasMetadataV3 {
payload := []byte(`
{
"type": "get_catalog_state",
"args": {}
}
`)
var r struct {
ID string `json:"id"`
}
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)
return state
}
state.UUID = r.ID
} else {
payload := []byte(`{
"type": "select",
"args": {
"table": {
"schema": "hdb_catalog",
"name": "hdb_version"
},
"columns": [
"hasura_uuid",
"cli_state"
]
}
}`)
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
}
var r []hdbVersion
_, err = client.Do(context.Background(), req, &r)
if err != nil {
log.Debugf("http request to construct server state failed: %v", err)
return state
}
if len(r) >= 1 {
state.UUID = r[0].UUID
state.CLIState = r[0].CLIState
}
}
return state
}
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)
if err != nil {
return fmt.Errorf("making http request failed: %w", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("request failed: url: %s status code: %v status: %s \n%s", versionEndpoint, resp.StatusCode, resp.Status, responseBs.String())
}
return nil
}