-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
468 lines (390 loc) · 12.6 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package aiven
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
"code.cloudfoundry.org/lager"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o fakes/fake_client.go . Client
type Client interface {
CreateService(params *CreateServiceInput) (string, error)
GetService(params *GetServiceInput) (*Service, error)
GetServiceTags(params *GetServiceTagsInput) (*ServiceTags, error)
DeleteService(params *DeleteServiceInput) error
CreateServiceUser(params *CreateServiceUserInput) (string, error)
DeleteServiceUser(params *DeleteServiceUserInput) (string, error)
UpdateService(params *UpdateServiceInput) (string, error)
UpdateServiceTags(params *UpdateServiceTagsInput) (string, error)
ForkService(params *ForkServiceInput) (string, error)
}
type HttpClient struct {
BaseURL string
Token string
Project string
logger lager.Logger
HTTPClient *http.Client
}
func NewHttpClient(baseURL, token, project string, logger lager.Logger) *HttpClient {
return &HttpClient{
BaseURL: baseURL,
Token: token,
Project: project,
logger: logger,
HTTPClient: &http.Client{},
}
}
type ErrInvalidUpdate struct {
Message string
}
func (p ErrInvalidUpdate) Error() string {
return p.Message
}
type CreateServiceInput struct {
Cloud string `json:"cloud,omitempty"`
GroupName string `json:"group_name,omitempty"`
Plan string `json:"plan,omitempty"`
ServiceName string `json:"service_name"`
ServiceType string `json:"service_type"`
UserConfig UserConfig `json:"user_config"`
Tags ServiceTags `json:"tags"`
}
type DeleteServiceInput struct {
ServiceName string
}
type CreateServiceUserInput struct {
ServiceName string `json:"-"`
Username string `json:"username"`
}
type CreateServiceUserResponse struct {
Message string `json:"message"`
User User `json:"user"`
}
type User struct {
Password string `json:"password"`
Type string `json:"type"`
Username string `json:"username"`
}
type DeleteServiceUserInput struct {
ServiceName string
Username string
}
type GetServiceInput struct {
ServiceName string
}
type GetServiceResponse struct {
Service Service `json:"service"`
}
type Service struct {
State ServiceStatus `json:"state"`
UpdateTime time.Time `json:"update_time"`
ServiceUriParams ServiceUriParams `json:"service_uri_params"`
ServiceType string `json:"service_type"`
Backups []ServiceBackup `json:"backups"`
Plan string `json:"plan"`
}
type ServiceStatus string
const (
Running ServiceStatus = "RUNNING"
Rebuilding ServiceStatus = "REBUILDING"
Rebalancing ServiceStatus = "REBALANCING"
PowerOff ServiceStatus = "POWEROFF"
Missing ServiceStatus = "MISSING"
)
type ServiceUriParams struct {
Host string `json:"host"`
Password string `json:"password"`
Port string `json:"port"`
User string `json:"user"`
}
type ServiceBackup struct {
Name string `json:"backup_name"`
Time time.Time `json:"backup_time"`
Size int `json:"data_size"`
}
type ServiceTags struct {
DeployEnv string `json:"deploy_env"`
ServiceID string `json:"service_id"`
PlanID string `json:"plan_id"`
OrganizationID string `json:"organization_id"`
SpaceID string `json:"space_id"`
BrokerName string `json:"broker_name"`
RestoredFromBackup string `json:"restored_from_backup"`
OriginServiceID string `json:"restored_from_service"`
RestoredFromTime time.Time `json:"restored_from_time"`
}
type GetServiceTagsInput struct {
ServiceName string
}
type GetServiceTagsResponse struct {
Tags ServiceTags `json:""`
}
type UpdateServiceInput struct {
ServiceName string `json:"-"`
Plan string `json:"plan,omitempty"`
UserConfig UserConfig `json:"user_config"`
}
type UpdateServiceTagsInput struct {
ServiceName string `json:"-"`
Tags ServiceTags `json:"tags"`
}
type ForkServiceInput struct {
Cloud string `json:"cloud,omitempty"`
GroupName string `json:"group_name,omitempty"`
Plan string `json:"plan,omitempty"`
ServiceName string `json:"service_name"`
ServiceType string `json:"service_type"`
UserConfig UserConfig `json:"user_config"`
Tags ServiceTags `json:"tags"`
}
type AivenErrorResponse struct {
Errors []struct {
Message string `json:"message"`
Status int `json:"status"`
} `json:"errors"`
Message string `json:"message"`
}
func (a *HttpClient) CreateService(params *CreateServiceInput) (string, error) {
reqBody, err := json.Marshal(params)
if err != nil {
return "", err
}
a.logger.Info("create-service-body", lager.Data{
"reqBody": reqBody})
res, err := a.do("POST", fmt.Sprintf("/project/%s/service", a.Project), reqBody)
if err != nil {
return "", err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("Error creating service: %d status code returned from Aiven: '%s'", res.StatusCode, b)
}
return string(b), nil
}
func (a *HttpClient) ForkService(params *ForkServiceInput) (string, error) {
reqBody, err := json.Marshal(params)
if err != nil {
return "", err
}
res, err := a.do("POST", fmt.Sprintf("/project/%s/service", a.Project), reqBody)
if err != nil {
return "", err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("Error creating service: %d status code returned from Aiven: '%s'", res.StatusCode, b)
}
return string(b), nil
}
var ErrInstanceDoesNotExist = errors.New("Error: service instance does not exist")
func (a *HttpClient) DeleteService(params *DeleteServiceInput) error {
res, err := a.do("DELETE", fmt.Sprintf("/project/%s/service/%s", a.Project, params.ServiceName), nil)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode == http.StatusOK {
return nil
}
if res.StatusCode == http.StatusNotFound {
return ErrInstanceDoesNotExist
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
return fmt.Errorf("Error deleting service: %d status code returned from Aiven: '%s'", res.StatusCode, b)
}
func (a *HttpClient) CreateServiceUser(params *CreateServiceUserInput) (string, error) {
reqBody, err := json.Marshal(params)
if err != nil {
return "", err
}
res, err := a.do("POST", fmt.Sprintf("/project/%s/service/%s/user", a.Project, params.ServiceName), reqBody)
if err != nil {
return "", err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return "", fmt.Errorf("Error creating service user: %d status code returned from Aiven: '%s'", res.StatusCode, b)
}
createServiceUserResponse := &CreateServiceUserResponse{}
if err := json.NewDecoder(res.Body).Decode(createServiceUserResponse); err != nil {
return "", err
}
if createServiceUserResponse.User.Password == "" {
return "", errors.New("Error creating service user: password was empty")
}
return createServiceUserResponse.User.Password, nil
}
var ErrInstanceUserDoesNotExist = errors.New("Error: service instance user does not exist")
func (a *HttpClient) DeleteServiceUser(params *DeleteServiceUserInput) (string, error) {
res, err := a.do("DELETE", fmt.Sprintf("/project/%s/service/%s/user/%s", a.Project, params.ServiceName, params.Username), nil)
if err != nil {
return "", err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
if res.StatusCode != http.StatusOK {
var errorResponse AivenErrorResponse
jsonErr := json.Unmarshal(b, &errorResponse)
// if this response changes (again), just call it quits and sniff for 404.
// the only reason we're not is in case we mistake "api not here at all"
// for "service user doesn't exist".
expectedMessageIfUserWasAlreadyDeleted := "Service user does not exist"
if jsonErr == nil && errorResponse.Message == expectedMessageIfUserWasAlreadyDeleted {
return "", ErrInstanceUserDoesNotExist
}
return "", fmt.Errorf("Error deleting service user: %d status code returned from Aiven: '%s'", res.StatusCode, b)
}
return string(b), nil
}
func (a *HttpClient) GetService(params *GetServiceInput) (*Service, error) {
res, err := a.do("GET", fmt.Sprintf("/project/%s/service/%s", a.Project, params.ServiceName), nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
switch res.StatusCode {
case http.StatusNotFound:
return &Service{
State: Missing,
UpdateTime: time.Now(),
}, ErrInstanceDoesNotExist
case http.StatusOK:
break
default:
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("Error getting service: %d status code returned from Aiven: '%s'", res.StatusCode, b)
}
getServiceResponse := &GetServiceResponse{}
if err := json.NewDecoder(res.Body).Decode(getServiceResponse); err != nil {
return nil, err
}
service := getServiceResponse.Service
if service.ServiceType == "" {
return nil, errors.New("Error getting service: no service type found in response JSON")
}
if service.State == "" {
return nil, errors.New("Error getting service: no state found in response JSON")
}
defaultTime := time.Time{}
if service.UpdateTime == defaultTime {
return nil, errors.New("Error getting service: no update_time found in response JSON")
}
return &service, nil
}
func (a *HttpClient) GetServiceTags(params *GetServiceTagsInput) (*ServiceTags, error) {
res, err := a.do("GET", fmt.Sprintf("/project/%s/service/%s/tags", a.Project, params.ServiceName), nil)
if err != nil {
return &ServiceTags{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("Error getting service tags: %d status code returned from Aiven: '%s'", res.StatusCode, b)
}
getServiceResponse := &GetServiceTagsResponse{}
if err := json.NewDecoder(res.Body).Decode(getServiceResponse); err != nil {
return nil, err
}
return &getServiceResponse.Tags, nil
}
func (a *HttpClient) UpdateService(params *UpdateServiceInput) (string, error) {
reqBody, err := json.Marshal(params)
if err != nil {
return "", err
}
res, err := a.do("PUT", fmt.Sprintf("/project/%s/service/%s", a.Project, params.ServiceName), reqBody)
if err != nil {
return "", err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
if res.StatusCode == http.StatusBadRequest {
var errorResponse AivenErrorResponse
jsonErr := json.Unmarshal(b, &errorResponse)
if jsonErr != nil {
return "", fmt.Errorf("Error updating service: %d status code returned from Aiven: '%s'", res.StatusCode, b)
}
return "", ErrInvalidUpdate{fmt.Sprintf("Invalid Update: %s", errorResponse.Message)}
}
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("Error updating service: %d status code returned from Aiven: '%s'", res.StatusCode, b)
}
return string(b), nil
}
func (a *HttpClient) UpdateServiceTags(params *UpdateServiceTagsInput) (string, error) {
reqBody, err := json.Marshal(params)
if err != nil {
return "", err
}
res, err := a.do("PUT", fmt.Sprintf("/project/%s/service/%s/tags", a.Project, params.ServiceName), reqBody)
if err != nil {
return "", err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
if res.StatusCode == http.StatusBadRequest {
var errorResponse AivenErrorResponse
jsonErr := json.Unmarshal(b, &errorResponse)
if jsonErr != nil {
return "", fmt.Errorf("Error updating service tags: %d status code returned from Aiven: '%s'", res.StatusCode, b)
}
return "", ErrInvalidUpdate{fmt.Sprintf("Invalid Update: %s", errorResponse.Message)}
}
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("Error updating service: %d status code returned from Aiven: '%s'", res.StatusCode, b)
}
return string(b), nil
}
func (a *HttpClient) do(method, path string, body []byte) (*http.Response, error) {
req, err := a.requestBuilder(method, path, body)
if err != nil {
return nil, err
}
res, err := a.HTTPClient.Do(req)
if err != nil {
return nil, err
}
return res, nil
}
func (a *HttpClient) requestBuilder(method, path string, body []byte) (*http.Request, error) {
req, err := http.NewRequest(method, fmt.Sprintf("%s/v1%s", a.BaseURL, path), bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("aivenv1 %s", a.Token))
return req, err
}