-
Notifications
You must be signed in to change notification settings - Fork 7
/
requests.go
590 lines (498 loc) · 17.1 KB
/
requests.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package instances
import (
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/pagination"
)
type CreateOpts struct {
Name string `json:"name" required:"true"`
Datastore *Datastore `json:"datastore" required:"true"`
Ha *Ha `json:"ha,omitempty"`
ConfigurationId string `json:"configuration_id,omitempty"`
Port string `json:"port,omitempty"`
Password string `json:"password" required:"true"`
BackupStrategy *BackupStrategy `json:"backup_strategy,omitempty"`
EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
DiskEncryptionId string `json:"disk_encryption_id,omitempty"`
FlavorRef string `json:"flavor_ref" required:"true"`
Volume *Volume `json:"volume" required:"true"`
Region string `json:"region" required:"true"`
AvailabilityZone string `json:"availability_zone" required:"true"`
VpcId string `json:"vpc_id" required:"true"`
SubnetId string `json:"subnet_id" required:"true"`
SecurityGroupId string `json:"security_group_id" required:"true"`
ChargeInfo *ChargeInfo `json:"charge_info,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
FixedIp string `json:"data_vip,omitempty"`
Collation string `json:"collation,omitempty"`
UnchangeableParam *UnchangeableParam `json:"unchangeable_param,omitempty"`
}
type CreateReplicaOpts struct {
Name string `json:"name" required:"true"`
ReplicaOfId string `json:"replica_of_id" required:"true"`
EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
DiskEncryptionId string `json:"disk_encryption_id,omitempty"`
FlavorRef string `json:"flavor_ref" required:"true"`
Volume *Volume `json:"volume" required:"true"`
Region string `json:"region,omitempty"`
AvailabilityZone string `json:"availability_zone" required:"true"`
ChargeInfo *ChargeInfo `json:"charge_info,omitempty"`
}
type Datastore struct {
Type string `json:"type" required:"true"`
Version string `json:"version" required:"true"`
}
type Ha struct {
Mode string `json:"mode" required:"true"`
ReplicationMode string `json:"replication_mode,omitempty"`
}
type UnchangeableParam struct {
LowerCaseTableNames string `json:"lower_case_table_names"`
}
type BackupStrategy struct {
StartTime string `json:"start_time" required:"true"`
KeepDays int `json:"keep_days,omitempty"`
}
type Volume struct {
Type string `json:"type" required:"true"`
Size int `json:"size" required:"true"`
}
type ChargeInfo struct {
ChargeMode string `json:"charge_mode" required:"true"`
PeriodType string `json:"period_type,omitempty"`
PeriodNum int `json:"period_num,omitempty"`
IsAutoRenew bool `json:"is_auto_renew,omitempty"`
IsAutoPay bool `json:"is_auto_pay,omitempty"`
}
type CreateRdsBuilder interface {
ToInstancesCreateMap() (map[string]interface{}, error)
}
type RestRootPasswordOpts struct {
DbUserPwd string `json:"db_user_pwd" required:"true"`
}
var RequestOpts = golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
}
func (opts CreateOpts) ToInstancesCreateMap() (map[string]interface{}, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
return b, nil
}
func Create(client *golangsdk.ServiceClient, opts CreateRdsBuilder) (r CreateResult) {
b, err := opts.ToInstancesCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200, 202},
})
return
}
func (opts CreateReplicaOpts) ToInstancesCreateMap() (map[string]interface{}, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
return b, nil
}
func CreateReplica(client *golangsdk.ServiceClient, opts CreateRdsBuilder) (r CreateResult) {
b, err := opts.ToInstancesCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200, 202},
})
return
}
type DeleteOpts struct {
InstanceId string `json:"instance_id" required:"true"`
}
type DeleteInstanceBuilder interface {
ToInstancesDeleteMap() (map[string]interface{}, error)
}
func (opts DeleteOpts) ToInstancesDeleteMap() (map[string]interface{}, error) {
b, err := golangsdk.BuildRequestBody(&opts, "")
if err != nil {
return nil, err
}
return b, nil
}
func Delete(client *golangsdk.ServiceClient, instanceId string) (r DeleteResult) {
url := deleteURL(client, instanceId)
_, r.Err = client.Delete(url, &golangsdk.RequestOpts{
JSONResponse: &r.Body,
MoreHeaders: map[string]string{"Content-Type": "application/json"},
})
return
}
type ListOpts struct {
Id string `q:"id"`
Name string `q:"name"`
Type string `q:"type"`
DataStoreType string `q:"datastore_type"`
VpcId string `q:"vpc_id"`
SubnetId string `q:"subnet_id"`
Offset int `q:"offset"`
Limit int `q:"limit"`
}
type ListRdsBuilder interface {
ToRdsListDetailQuery() (string, error)
}
func (opts ListOpts) ToRdsListDetailQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), err
}
func List(client *golangsdk.ServiceClient, opts ListRdsBuilder) pagination.Pager {
url := listURL(client)
if opts != nil {
query, err := opts.ToRdsListDetailQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
pageRdsList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return RdsPage{pagination.SinglePageBase(r)}
})
rdsheader := map[string]string{"Content-Type": "application/json"}
pageRdsList.Headers = rdsheader
return pageRdsList
}
type ActionInstanceBuilder interface {
ToActionInstanceMap() (map[string]interface{}, error)
}
func toActionInstanceMap(opts ActionInstanceBuilder) (map[string]interface{}, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
return b, nil
}
type RestartInstanceOpts struct {
Restart string `json:"restart" required:"true"`
}
func (opts RestartInstanceOpts) ToActionInstanceMap() (map[string]interface{}, error) {
return toActionInstanceMap(opts)
}
func Restart(client *golangsdk.ServiceClient, opts ActionInstanceBuilder, instanceId string) (r RestartResult) {
b, err := opts.ToActionInstanceMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(updateURL(client, instanceId, "action"), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{202},
})
return
}
type RenameInstanceOpts struct {
Name string `json:"name" required:"true"`
}
func (opts RenameInstanceOpts) ToActionInstanceMap() (map[string]interface{}, error) {
return toActionInstanceMap(opts)
}
func Rename(client *golangsdk.ServiceClient, opts ActionInstanceBuilder, instanceId string) (r RenameResult) {
b, err := opts.ToActionInstanceMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(updateURL(client, instanceId, "name"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200, 202},
})
return
}
type SingleToHaRdsOpts struct {
SingleToHa *SingleToHaRds `json:"single_to_ha" required:"true"`
}
type SingleToHaRds struct {
AzCodeNewNode string `json:"az_code_new_node" required:"true"`
Password string `json:"password,omitempty"`
}
func (opts SingleToHaRdsOpts) ToActionInstanceMap() (map[string]interface{}, error) {
return toActionInstanceMap(opts)
}
func SingleToHa(client *golangsdk.ServiceClient, opts ActionInstanceBuilder, instanceId string) (r SingleToHaResult) {
b, err := opts.ToActionInstanceMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(updateURL(client, instanceId, "action"), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{202},
})
return
}
type SpecCode struct {
Speccode string `json:"spec_code" required:"true"`
IsAutoPay bool `json:"is_auto_pay,omitempty"`
}
type ResizeFlavorOpts struct {
ResizeFlavor *SpecCode `json:"resize_flavor" required:"true"`
}
func (opts ResizeFlavorOpts) ToActionInstanceMap() (map[string]interface{}, error) {
return toActionInstanceMap(opts)
}
func Resize(client *golangsdk.ServiceClient, opts ActionInstanceBuilder, instanceId string) (r ResizeFlavorResult) {
b, err := opts.ToActionInstanceMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(updateURL(client, instanceId, "action"), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{202},
})
return
}
type EnlargeVolumeOpts struct {
EnlargeVolume *EnlargeVolumeSize `json:"enlarge_volume" required:"true"`
}
type EnlargeVolumeSize struct {
Size int `json:"size" required:"true"`
}
func (opts EnlargeVolumeOpts) ToActionInstanceMap() (map[string]interface{}, error) {
return toActionInstanceMap(opts)
}
func EnlargeVolume(client *golangsdk.ServiceClient, opts ActionInstanceBuilder, instanceId string) (r EnlargeVolumeResult) {
b, err := opts.ToActionInstanceMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(updateURL(client, instanceId, "action"), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{202},
})
return
}
type DbErrorlogOpts struct {
StartDate string `q:"start_date"`
EndDate string `q:"end_date"`
Offset string `q:"offset"`
Limit string `q:"limit"`
Level string `q:"level"`
}
type DbErrorlogBuilder interface {
DbErrorlogQuery() (string, error)
}
func (opts DbErrorlogOpts) DbErrorlogQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), err
}
func ListErrorLog(client *golangsdk.ServiceClient, opts DbErrorlogBuilder, instanceID string) pagination.Pager {
url := updateURL(client, instanceID, "errorlog")
if opts != nil {
query, err := opts.DbErrorlogQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
pageRdsList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return ErrorLogPage{pagination.SinglePageBase(r)}
})
rdsheader := map[string]string{"Content-Type": "application/json"}
pageRdsList.Headers = rdsheader
return pageRdsList
}
type DbSlowLogOpts struct {
StartDate string `q:"start_date"`
EndDate string `q:"end_date"`
Offset string `q:"offset"`
Limit string `q:"limit"`
Level string `q:"level"`
}
type DbSlowLogBuilder interface {
ToDbSlowLogListQuery() (string, error)
}
func (opts DbSlowLogOpts) ToDbSlowLogListQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), err
}
func ListSlowLog(client *golangsdk.ServiceClient, opts DbSlowLogBuilder, instanceID string) pagination.Pager {
url := updateURL(client, instanceID, "slowlog")
if opts != nil {
query, err := opts.ToDbSlowLogListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
pageRdsList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return ErrorLogPage{pagination.SinglePageBase(r)}
})
rdsheader := map[string]string{"Content-Type": "application/json"}
pageRdsList.Headers = rdsheader
return pageRdsList
}
type RDSJobOpts struct {
JobID string `q:"id"`
}
type RDSJobBuilder interface {
ToRDSJobQuery() (string, error)
}
func (opts RDSJobOpts) ToRDSJobQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), err
}
func GetRDSJob(client *golangsdk.ServiceClient, opts RDSJobBuilder) (r RDSJobResult) {
url := jobURL(client)
if opts != nil {
query, err := opts.ToRDSJobQuery()
if err != nil {
r.Err = err
return
}
url += query
}
_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
})
return
}
func ListEngine(client *golangsdk.ServiceClient, dbName string) (*Engine, error) {
var rst golangsdk.Result
_, err := client.Get(engineURL(client, dbName), &rst.Body, nil)
if err == nil {
var s Engine
err := rst.ExtractInto(&s)
return &s, err
}
return nil, err
}
func RestRootPassword(c *golangsdk.ServiceClient, instanceID string, opts RestRootPasswordOpts) (*ErrorResponse, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
var r ErrorResponse
_, err = c.Post(resetRootPasswordURL(c, instanceID), b, &r, &golangsdk.RequestOpts{
MoreHeaders: RequestOpts.MoreHeaders,
})
return &r, err
}
type ModifyConfigurationOpts struct {
Values map[string]string `json:"values" required:"true"`
}
func ModifyConfiguration(c *golangsdk.ServiceClient, instanceID string, opts ModifyConfigurationOpts) (r ModifyConfigurationResult) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
r.Err = err
return
}
_, r.Err = c.Put(configurationsURL(c, instanceID), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200, 202},
})
return
}
func GetConfigurations(c *golangsdk.ServiceClient, instanceID string) (r GetConfigurationResult) {
_, r.Err = c.Get(configurationsURL(c, instanceID), &r.Body, &golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json"},
})
return
}
func RebootInstance(c *golangsdk.ServiceClient, instanceID string) (r RebootResult) {
b, err := golangsdk.BuildRequestBody(struct{}{}, "restart")
if err != nil {
r.Err = err
return
}
_, r.Err = c.Post(actionURL(c, instanceID), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200, 202},
})
return
}
var (
enableAutoExpand bool = true
disableAutoExpand bool = false
)
// EnableAutoExpandOpts is the structure used to enable the volume automatic expansion of RDS instance.
type EnableAutoExpandOpts struct {
// The instnace ID.
InstanceId string `json:"-" required:"true"`
// The upper limit of automatic expansion of storage, in GB.
// This parameter is mandatory when switch_option is set to true.
// The value ranges from 40 GB to 4,000 GB and must be no less than the current storage of the instance.
LimitSize int `json:"limit_size" required:"true"`
// The threshold to trigger automatic expansion.
// If the available storage drops to this threshold or 10 GB, the automatic expansion is triggered.
// This parameter is mandatory when switch_option is set to true.
// The valid values are as follows:
// + 10
// + 15
// + 20
TriggerThreshold int `json:"trigger_threshold" required:"true"`
}
// autoExpandOpts is the structure used to configure the volume automatic expansion of RDS instance.
type autoExpandOpts struct {
// Whether the auto-expansion is enabled.
SwitchOption *bool `json:"switch_option" required:"true"`
// The upper limit of automatic expansion of storage, in GB.
// This parameter is mandatory when switch_option is set to true.
// The value ranges from 40 GB to 4,000 GB and must be no less than the current storage of the instance.
LimitSize int `json:"limit_size,omitempty"`
// The threshold to trigger automatic expansion.
// If the available storage drops to this threshold or 10 GB, the automatic expansion is triggered.
// This parameter is mandatory when switch_option is set to true.
// The valid values are as follows:
// + 10
// + 15
// + 20
TriggerThreshold int `json:"trigger_threshold,omitempty"`
}
var requestOpts = golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
}
// EnableAutoExpand is a method used to configure the volume automatic expansion of RDS instance.
func EnableAutoExpand(c *golangsdk.ServiceClient, opts EnableAutoExpandOpts) error {
enableOpts := autoExpandOpts{
SwitchOption: &enableAutoExpand,
LimitSize: opts.LimitSize,
TriggerThreshold: opts.TriggerThreshold,
}
b, err := golangsdk.BuildRequestBody(enableOpts, "")
if err != nil {
return err
}
_, err = c.Put(autoExpandURL(c, opts.InstanceId), b, nil, &golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
})
return err
}
// DisableAutoExpand is a method used to remove the volume automatic expansion configuration of RDS instance.
func DisableAutoExpand(c *golangsdk.ServiceClient, instanceId string) error {
autoExpandOpts := autoExpandOpts{
SwitchOption: &disableAutoExpand,
}
b, err := golangsdk.BuildRequestBody(autoExpandOpts, "")
if err != nil {
return err
}
_, err = c.Put(autoExpandURL(c, instanceId), b, nil, &golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
})
return err
}
// GetAutoExpand is a method used to obtain the automatic expansion configuarion of instance storage.
func GetAutoExpand(c *golangsdk.ServiceClient, instanceId string) (*AutoExpansion, error) {
var r AutoExpansion
_, err := c.Get(autoExpandURL(c, instanceId), &r, &golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
})
return &r, err
}