2019-01-28 16:55:28 +03:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2020-04-28 14:59:57 +03:00
|
|
|
"crypto/tls"
|
2019-01-28 16:55:28 +03:00
|
|
|
|
|
|
|
"github.com/Masterminds/semver"
|
|
|
|
"github.com/parnurzeal/gorequest"
|
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.
|
2020-04-28 14:59:57 +03:00
|
|
|
func GetServerState(endpoint, adminSecret string, config *tls.Config, serverVersion *semver.Version, log *logrus.Logger) *ServerState {
|
2019-01-28 16:55:28 +03:00
|
|
|
state := &ServerState{
|
|
|
|
UUID: "00000000-0000-0000-0000-000000000000",
|
|
|
|
}
|
|
|
|
payload := `{
|
|
|
|
"type": "select",
|
|
|
|
"args": {
|
|
|
|
"table": {
|
|
|
|
"schema": "hdb_catalog",
|
|
|
|
"name": "hdb_version"
|
|
|
|
},
|
|
|
|
"columns": [
|
|
|
|
"hasura_uuid",
|
|
|
|
"cli_state"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
req := gorequest.New()
|
2020-04-28 14:59:57 +03:00
|
|
|
if config != nil {
|
|
|
|
req.TLSClientConfig(config)
|
|
|
|
}
|
|
|
|
req.Post(endpoint).Send(payload)
|
2019-02-14 12:37:47 +03:00
|
|
|
req.Set("X-Hasura-Admin-Secret", adminSecret)
|
2019-01-28 16:55:28 +03:00
|
|
|
|
|
|
|
var r []hdbVersion
|
|
|
|
_, _, errs := req.EndStruct(&r)
|
|
|
|
if len(errs) != 0 {
|
|
|
|
log.Debugf("server state: errors: %v", errs)
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r) != 1 {
|
|
|
|
log.Debugf("invalid response: %v", r)
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
state.UUID = r[0].UUID
|
|
|
|
state.CLIState = r[0].CLIState
|
|
|
|
return state
|
|
|
|
}
|