/
client.go
218 lines (182 loc) · 6.94 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package provisioner
import (
"context"
"fmt"
"reflect"
kebError "github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/error"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/httputil"
gcli "github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/third_party/machinebox/graphql"
schema "github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
)
// 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)
UpgradeRuntime(accountID, runtimeID string, config schema.UpgradeRuntimeInput) (schema.OperationStatus, error)
UpgradeShoot(accountID, runtimeID string, config schema.UpgradeShootInput) (schema.OperationStatus, error)
ReconnectRuntimeAgent(accountID, runtimeID string) (string, error)
RuntimeOperationStatus(accountID, operationID string) (schema.OperationStatus, error)
RuntimeStatus(accountID, runtimeID string) (schema.RuntimeStatus, 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(120, 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{}, fmt.Errorf("failed to convert Provision Runtime Input to query: %w", err)
}
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{}, fmt.Errorf("failed to provision a Runtime: %w", err)
}
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 "", fmt.Errorf("failed to deprovision Runtime: %w", err)
}
return operationId, nil
}
func (c *client) UpgradeRuntime(accountID, runtimeID string, config schema.UpgradeRuntimeInput) (schema.OperationStatus, error) {
upgradeRuntimeIptGQL, err := c.graphqlizer.UpgradeRuntimeInputToGraphQL(config)
if err != nil {
return schema.OperationStatus{}, fmt.Errorf("failed to convert Upgrade Runtime Input to query: %w", err)
}
query := c.queryProvider.upgradeRuntime(runtimeID, upgradeRuntimeIptGQL)
req := gcli.NewRequest(query)
req.Header.Add(accountIDKey, accountID)
var res schema.OperationStatus
err = c.executeRequest(req, &res)
if err != nil {
return schema.OperationStatus{}, fmt.Errorf("failed to upgrade Runtime: %w", err)
}
return res, nil
}
func (c *client) UpgradeShoot(accountID, runtimeID string, config schema.UpgradeShootInput) (schema.OperationStatus, error) {
upgradeShootIptGQL, err := c.graphqlizer.UpgradeShootInputToGraphQL(config)
if err != nil {
return schema.OperationStatus{}, fmt.Errorf("failed to convert Upgrade Shoot Input to query: %w", err)
}
query := c.queryProvider.upgradeShoot(runtimeID, upgradeShootIptGQL)
req := gcli.NewRequest(query)
req.Header.Add(accountIDKey, accountID)
var res schema.OperationStatus
err = c.executeRequest(req, &res)
if err != nil {
return schema.OperationStatus{}, fmt.Errorf("failed to upgrade Shoot: %w", err)
}
return res, 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 "", fmt.Errorf("failed to reconnect Runtime agent: %w", err)
}
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{}, fmt.Errorf("failed to get Runtime operation status: %w", err)
}
return response, nil
}
func (c *client) RuntimeStatus(accountID, runtimeID string) (schema.RuntimeStatus, error) {
query := c.queryProvider.runtimeStatus(runtimeID)
req := gcli.NewRequest(query)
req.Header.Add(accountIDKey, accountID)
var response schema.RuntimeStatus
err := c.executeRequest(req, &response)
if err != nil {
return schema.RuntimeStatus{}, fmt.Errorf("failed to get Runtime status: %w", err)
}
return response, nil
}
func (c *client) executeRequest(req *gcli.Request, respDestination interface{}) error {
if reflect.ValueOf(respDestination).Kind() != reflect.Ptr {
return fmt.Errorf("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)
switch {
case isNotFoundError(err):
return kebError.NotFoundError{}
case isClientError(err):
return err
case err != nil:
return kebError.WrapAsTemporaryError(err, "failed to execute the request")
}
return nil
}
func isClientError(err error) bool {
if ee, ok := err.(gcli.ExtendedError); ok {
code, found := ee.Extensions()["error_code"]
if found {
errCode := code.(float64)
if errCode >= 400 && errCode < 500 {
return true
}
}
}
return false
}
func isNotFoundError(err error) bool {
if ee, ok := err.(gcli.ExtendedError); ok {
reason, found := ee.Extensions()["error_reason"]
if found {
if reason == "err_db_not_found" {
return true
}
}
}
return false
}
func OperationStatusLastError(lastErr *schema.LastError) kebError.ErrorReporter {
var err kebError.LastError
if lastErr == nil {
return err.SetReason(kebError.ErrProvisionerNilLastError).SetComponent(kebError.ErrProvisioner)
}
return err.SetMessage(lastErr.ErrMessage).SetReason(kebError.ErrReason(lastErr.Reason)).SetComponent(kebError.ErrComponent(lastErr.Component))
}