This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
requests.go
301 lines (248 loc) · 8.69 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
package instances
import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)
type DataStoreOpt struct {
Type string `json:"type" required:"true"`
Version string `json:"version,omitempty"`
}
type BackupStrategyOpt struct {
StartTime string `json:"start_time" required:"true"`
KeepDays int `json:"keep_days,omitempty"`
}
type HaOpt struct {
Mode string `json:"mode" required:"true"`
ReplicationMode string `json:"replication_mode" required:"true"`
Consistency string `json:"consistency,omitempty"`
}
type VolumeOpt struct {
Type string `json:"type" required:"true"`
Size int `json:"size" required:"true"`
}
type RestorePointOpt struct {
InstanceId string `json:"instance_id" required:"true"`
Type string `json:"type" required:"true"`
BackupId string `json:"backup_id,omitempty"`
RestoreTime int `json:"restore_time,omitempty"`
DatabaseName map[string]string `json:"database_name,omitempty"`
}
type CreateGaussDBOpts struct {
Name string `json:"name" required:"true"`
Region string `json:"region,omitempty"`
Flavor string `json:"flavor_ref" required:"true"`
VpcId string `json:"vpc_id,omitempty"`
SubnetId string `json:"subnet_id,omitempty"`
SecurityGroupId string `json:"security_group_id,omitempty"`
Password string `json:"password" required:"true"`
Port string `json:"port,omitempty"`
DiskEncryptionId string `json:"disk_encryption_id,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
AvailabilityZone string `json:"availability_zone" required:"true"`
ConfigurationId string `json:"configuration_id,omitempty"`
DsspoolId string `json:"dsspool_id,omitempty"`
ReplicaOfId string `json:"replica_of_id,omitempty"`
ShardingNum int `json:"sharding_num" required:"true"`
CoordinatorNum int `json:"coordinator_num" required:"true"`
EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
DataStore DataStoreOpt `json:"datastore" required:"true"`
Volume VolumeOpt `json:"volume" required:"true"`
Ha *HaOpt `json:"ha,omitempty"`
BackupStrategy *BackupStrategyOpt `json:"backup_strategy,omitempty"`
RestorePoint *RestorePointOpt `json:"restore_point,omitempty"`
}
type CreateGaussDBBuilder interface {
ToInstancesCreateMap() (map[string]interface{}, error)
}
func (opts CreateGaussDBOpts) 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 CreateGaussDBBuilder) (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{202},
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
})
return
}
type UpdateVolumeOptsBuilder interface {
ToVolumeUpdateMap() (map[string]interface{}, error)
}
type UpdateVolumeOpts struct {
Size int `json:"size" required:"true"`
}
func (opts UpdateVolumeOpts) ToVolumeUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "enlarge_volume")
}
func UpdateVolume(client *golangsdk.ServiceClient, opts UpdateVolumeOptsBuilder, id string) (r UpdateResult) {
b, err := opts.ToVolumeUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(updateURL(client, id, "action"), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{202},
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
})
return
}
type UpdateClusterOptsBuilder interface {
ToClusterUpdateMap() (map[string]interface{}, error)
}
type Shard struct {
Count int `json:"count" required:"true"`
}
type Coordinator struct {
AzCode string `json:"az_code" required:"true"`
}
type UpdateClusterOpts struct {
Shard *Shard `json:"shard,omitempty"`
Coordinators []Coordinator `json:"coordinators,omitempty"`
}
func (opts UpdateClusterOpts) ToClusterUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "expand_cluster")
}
func UpdateCluster(client *golangsdk.ServiceClient, opts UpdateClusterOptsBuilder, id string) (r UpdateResult) {
b, err := opts.ToClusterUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(updateURL(client, id, "action"), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{202},
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
})
return
}
func Delete(client *golangsdk.ServiceClient, instanceId string) (r DeleteResult) {
url := deleteURL(client, instanceId)
_, r.Err = client.Delete(url, &golangsdk.RequestOpts{
OkCodes: []int{202},
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
})
return
}
type ListGaussDBInstanceOpts 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 ListGaussDBBuilder interface {
ToGaussDBListDetailQuery() (string, error)
}
func (opts ListGaussDBInstanceOpts) ToGaussDBListDetailQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), err
}
func List(client *golangsdk.ServiceClient, opts ListGaussDBBuilder) pagination.Pager {
url := listURL(client)
if opts != nil {
query, err := opts.ToGaussDBListDetailQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
pageList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return GaussDBPage{pagination.SinglePageBase(r)}
})
// Headers supplies additional HTTP headers to populate on each paged request
pageList.Headers = map[string]string{"Content-Type": "application/json"}
return pageList
}
func GetInstanceByID(client *golangsdk.ServiceClient, instanceId string) (GaussDBInstance, error) {
var instance GaussDBInstance
opts := ListGaussDBInstanceOpts{
Id: instanceId,
}
pages, err := List(client, &opts).AllPages()
if err != nil {
return instance, err
}
all, err := ExtractGaussDBInstances(pages)
if err != nil {
return instance, err
}
if all.TotalCount == 0 {
return instance, nil
}
instance = all.Instances[0]
return instance, nil
}
func GetInstanceByName(client *golangsdk.ServiceClient, name string) (GaussDBInstance, error) {
var instance GaussDBInstance
opts := ListGaussDBInstanceOpts{
Name: name,
}
pages, err := List(client, &opts).AllPages()
if err != nil {
return instance, err
}
all, err := ExtractGaussDBInstances(pages)
if err != nil {
return instance, err
}
if all.TotalCount == 0 {
return instance, nil
}
instance = all.Instances[0]
return instance, nil
}
type RenameOptsBuilder interface {
ToRenameMap() (map[string]interface{}, error)
}
type RenameOpts struct {
Name string `json:"name" required:"true"`
}
func (opts RenameOpts) ToRenameMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
func Rename(client *golangsdk.ServiceClient, opts RenameOptsBuilder, id string) (r RenameResult) {
b, err := opts.ToRenameMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(updateURL(client, id, "name"), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
})
return
}
type RestorePasswordOptsBuilder interface {
ToRestorePasswordMap() (map[string]interface{}, error)
}
type RestorePasswordOpts struct {
Password string `json:"password" required:"true"`
}
func (opts RestorePasswordOpts) ToRestorePasswordMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
func RestorePassword(client *golangsdk.ServiceClient, opts RestorePasswordOptsBuilder, id string) (r golangsdk.Result) {
b, err := opts.ToRestorePasswordMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(updateURL(client, id, "password"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
})
return
}