forked from kyma-project/control-plane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
126 lines (103 loc) · 3.78 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package provisioner
import (
"context"
"fmt"
"reflect"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/httputil"
schema "github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
gcli "github.com/machinebox/graphql"
"github.com/pkg/errors"
)
// accountIDKey is a header key name for request send by graphQL client
const (
accountIDKey = "tenant"
subAccountIDKey = "sub-account"
)
//go:generate mockery -name=Client -output=automock -outpkg=automock -case=underscore
type Client interface {
ProvisionRuntime(accountID, subAccountID string, config schema.ProvisionRuntimeInput) (schema.OperationStatus, error)
DeprovisionRuntime(accountID, runtimeID string) (string, error)
ReconnectRuntimeAgent(accountID, runtimeID string) (string, error)
RuntimeOperationStatus(accountID, operationID string) (schema.OperationStatus, error)
}
type client struct {
graphQLClient *gcli.Client
queryProvider queryProvider
graphqlizer Graphqlizer
}
func NewProvisionerClient(endpoint string, queryDumping bool) Client {
graphQlClient := gcli.NewClient(endpoint, gcli.WithHTTPClient(httputil.NewClient(30, false)))
if queryDumping {
graphQlClient.Log = func(s string) {
fmt.Println(s)
}
}
return &client{
graphQLClient: graphQlClient,
queryProvider: queryProvider{},
graphqlizer: Graphqlizer{},
}
}
func (c *client) ProvisionRuntime(accountID, subAccountID string, config schema.ProvisionRuntimeInput) (schema.OperationStatus, error) {
provisionRuntimeIptGQL, err := c.graphqlizer.ProvisionRuntimeInputToGraphQL(config)
if err != nil {
return schema.OperationStatus{}, errors.Wrap(err, "Failed to convert Provision Runtime Input to query")
}
query := c.queryProvider.provisionRuntime(provisionRuntimeIptGQL)
req := gcli.NewRequest(query)
req.Header.Add(accountIDKey, accountID)
req.Header.Add(subAccountIDKey, subAccountID)
var response schema.OperationStatus
err = c.executeRequest(req, &response)
if err != nil {
return schema.OperationStatus{}, errors.Wrap(err, "Failed to provision Runtime")
}
return response, nil
}
func (c *client) DeprovisionRuntime(accountID, runtimeID string) (string, error) {
query := c.queryProvider.deprovisionRuntime(runtimeID)
req := gcli.NewRequest(query)
req.Header.Add(accountIDKey, accountID)
var operationId string
err := c.executeRequest(req, &operationId)
if err != nil {
return "", errors.Wrap(err, "Failed to deprovision Runtime")
}
return operationId, nil
}
func (c *client) ReconnectRuntimeAgent(accountID, runtimeID string) (string, error) {
query := c.queryProvider.reconnectRuntimeAgent(runtimeID)
req := gcli.NewRequest(query)
req.Header.Add(accountIDKey, accountID)
var operationId string
err := c.executeRequest(req, &operationId)
if err != nil {
return "", errors.Wrap(err, "Failed to reconnect Runtime agent")
}
return operationId, nil
}
func (c *client) RuntimeOperationStatus(accountID, operationID string) (schema.OperationStatus, error) {
query := c.queryProvider.runtimeOperationStatus(operationID)
req := gcli.NewRequest(query)
req.Header.Add(accountIDKey, accountID)
var response schema.OperationStatus
err := c.executeRequest(req, &response)
if err != nil {
return schema.OperationStatus{}, errors.Wrap(err, "Failed to get Runtime operation status")
}
return response, nil
}
func (c *client) executeRequest(req *gcli.Request, respDestination interface{}) error {
if reflect.ValueOf(respDestination).Kind() != reflect.Ptr {
return errors.New("destination is not of pointer type")
}
type graphQLResponseWrapper struct {
Result interface{} `json:"result"`
}
wrapper := &graphQLResponseWrapper{Result: respDestination}
err := c.graphQLClient.Run(context.TODO(), req, wrapper)
if err != nil {
return errors.Wrap(err, "Failed to execute request")
}
return nil
}