-
Notifications
You must be signed in to change notification settings - Fork 7
/
requests.go
373 lines (326 loc) · 11.8 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
package cloudimages
import (
"fmt"
"net/url"
"time"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/pagination"
)
type ListOptsBuilder interface {
ToImageListQuery() (string, error)
}
type ListOpts struct {
Isregistered string `q:"__isregistered"`
Imagetype string `q:"__imagetype"`
WholeImage bool `q:"__whole_image"`
Protected bool `q:"protected"`
Visibility string `q:"visibility"`
Owner string `q:"owner"`
ID string `q:"id"`
Status string `q:"status"`
Name string `q:"name"`
ContainerFormat string `q:"container_format"`
DiskFormat string `q:"disk_format"`
MinRam int `q:"min_ram"`
MinDisk int `q:"min_disk"`
Marker string `q:"marker"`
Limit int `q:"limit"`
SortKey string `q:"sort_key"`
SortDir string `q:"sort_dir"`
OsType string `q:"__os_type"`
Platform string `q:"__platform"`
OsVersion string `q:"__os_version"`
OsBit string `q:"__os_bit"`
Tag string `q:"tag"`
MemberStatus string `q:"member_status"`
SupportKvm string `q:"__support_kvm"`
SupportXen string `q:"__support_xen"`
SupportLargeMemory string `q:"__support_largememory"`
SupportDiskintensive string `q:"__support_diskintensive"`
SupportHighperformance string `q:"__support_highperformance"`
SupportXenGpuType string `q:"__support_xen_gpu_type"`
SupportKvmGpuType string `q:"__support_kvm_gpu_type"`
SupportXenHana string `q:"__support_xen_hana"`
SupportKvmInfiniband string `q:"__support_kvm_infiniband"`
VirtualEnvType string `q:"virtual_env_type"`
Architecture string `q:"architecture"`
HwDiskBus string `q:"hw_disk_bus"`
HwFirmwareType string `q:"hw_firmware_type"`
// CreatedAtQuery filters images based on their creation date.
CreatedAtQuery *ImageDateQuery
// UpdatedAtQuery filters images based on their updated date.
UpdatedAtQuery *ImageDateQuery
}
// ImageDateFilter represents a valid filter to use for filtering
// images by their date during a List.
type ImageDateFilter string
const (
FilterGT ImageDateFilter = "gt"
FilterGTE ImageDateFilter = "gte"
FilterLT ImageDateFilter = "lt"
FilterLTE ImageDateFilter = "lte"
FilterNEQ ImageDateFilter = "neq"
FilterEQ ImageDateFilter = "eq"
)
// ImageDateQuery represents a date field to be used for listing images.
// If no filter is specified, the query will act as though FilterEQ was
// set.
type ImageDateQuery struct {
Date time.Time
Filter ImageDateFilter
}
// ToImageListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToImageListQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
params := q.Query()
if opts.CreatedAtQuery != nil {
createdAt := opts.CreatedAtQuery.Date.Format(time.RFC3339)
if v := opts.CreatedAtQuery.Filter; v != "" {
createdAt = fmt.Sprintf("%s:%s", v, createdAt)
}
params.Add("created_at", createdAt)
}
if opts.UpdatedAtQuery != nil {
updatedAt := opts.UpdatedAtQuery.Date.Format(time.RFC3339)
if v := opts.UpdatedAtQuery.Filter; v != "" {
updatedAt = fmt.Sprintf("%s:%s", v, updatedAt)
}
params.Add("updated_at", updatedAt)
}
q = &url.URL{RawQuery: params.Encode()}
return q.String(), err
}
// List implements images list request
func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(client)
if opts != nil {
query, err := opts.ToImageListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
imagePage := ImagePage{
serviceURL: client.ServiceURL(),
LinkedPageBase: pagination.LinkedPageBase{PageResult: r},
}
return imagePage
})
}
// CreateOptsBuilder allows extensions to add parameters to the Create request.
type CreateOptsBuilder interface {
// Returns value that can be passed to json.Marshal
ToImageCreateMap() (map[string]interface{}, error)
}
// CreateOpts represents options used to create an image.
type CreateByServerOpts struct {
// the name of the system disk image
Name string `json:"name" required:"true"`
// Description of the image
Description string `json:"description,omitempty"`
// server id to be converted
InstanceId string `json:"instance_id" required:"true"`
}
// CreateOpts represents options used to create an image.
type CreateByOBSOpts struct {
// the name of the system disk image
Name string `json:"name" required:"true"`
// Description of image
Description string `json:"description,omitempty"`
// the OS version
OsVersion string `json:"os_version,omitempty"`
// the URL of the external image file in the OBS bucket
ImageUrl string `json:"image_url" required:"true"`
// the minimum size of the system disk in the unit of GB
MinDisk int `json:"min_disk" required:"true"`
// the minimum memory of the image in the unit of MB
MinRam int `json:"min_ram,omitempty"`
// whether automatic configuration is enabled,the value can be true or false
IsConfig bool `json:"is_config,omitempty"`
// whether the initial configuration is complete
IsConfigInit bool `json:"is_config_init,omitempty"`
}
// CreateWholeImageOpts represents options used to create an image.
type CreateWholeImageOpts struct {
// the name of the system disk image
Name string `json:"name" required:"true"`
// Description of image
Description string `json:"description,omitempty"`
// the ID of the instance
InstanceId string `json:"instance_id,omitempty"`
// the ID of the CBR backup
BackupId string `json:"backup_id,omitempty"`
// image label "key.value"
Tags []string `json:"tags,omitempty"`
// the minimum memory of the image in the unit of MB
MinRam int `json:"min_ram,omitempty"`
// Enterprise project ID
EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
// the method of creating a full-ECS image
WholeImageType string `json:"whole_image_type,omitempty"`
}
// CreateOpts represents options used to create an image.
type CreateDataImageByServerOpts struct {
// the data disks to be converted
DataImages []DataImage `json:"data_images" required:"true"`
}
// CreateOpts represents options used to create an image.
type CreateDataImageByOBSOpts struct {
// the name of the data disk image
Name string `json:"name" required:"true"`
// Description of image
Description string `json:"description,omitempty"`
// the OS type
OsType string `json:"os_type" required:"true"`
// the URL of the external image file in the OBS bucket
ImageUrl string `json:"image_url" required:"true"`
// the minimum size of the system disk in the unit of GB
MinDisk int `json:"min_disk" required:"true"`
// the master key used for encrypting an image
CmkId string `json:"cmk_id,omitempty"`
// Enterprise project ID
EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
}
type DataImage struct {
// the data disk image name
Name string `json:"name" required:"true"`
// the data disk ID
VolumeId string `json:"volume_id" required:"true"`
// information about the data disk
Description string `json:"description,omitempty"`
// the data disk image tags
Tags []string `json:"tags,omitempty"`
}
// ToImageCreateMap assembles a request body based on the contents of
// a CreateByServerOpts.
func (opts CreateByServerOpts) ToImageCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
func (opts CreateByOBSOpts) ToImageCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
func (opts CreateDataImageByServerOpts) ToImageCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
func (opts CreateDataImageByOBSOpts) ToImageCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
func (opts CreateWholeImageOpts) ToImageCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Create implements create image request.
func CreateImageByServer(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) {
b, err := opts.ToImageCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
return
}
// Create implements create image request.
func CreateImageByOBS(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) {
b, err := opts.ToImageCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
return
}
// Create implements create image request.
func CreateDataImageByServer(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) {
b, err := opts.ToImageCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
return
}
// Create implements create image request.
func CreateDataImageByOBS(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) {
b, err := opts.ToImageCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createDataImageURL(client), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
return
}
// Create implements create whole image request.
func CreateWholeImageByServer(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) {
b, err := opts.ToImageCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createWholeImageURL(client), b, &r.Body, nil)
return
}
// Create implements create image request.
func CreateWholeImageByBackup(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) {
b, err := opts.ToImageCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createWholeImageURL(client), b, &r.Body, nil)
return
}
// Update implements image updated request.
func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToImageUpdateMap()
if err != nil {
r.Err = err
return r
}
_, r.Err = client.Patch(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: map[string]string{"Content-Type": "application/openstack-images-v2.1-json-patch"},
})
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to the Update request.
type UpdateOptsBuilder interface {
ToImageUpdateMap() ([]interface{}, error)
}
// UpdateOpts implements UpdateOpts
type UpdateOpts []Patch
// ToImageUpdateMap assembles a request body based on the contents of UpdateOpts.
func (opts UpdateOpts) ToImageUpdateMap() ([]interface{}, error) {
m := make([]interface{}, len(opts))
for i, patch := range opts {
patchJSON := patch.ToImagePatchMap()
m[i] = patchJSON
}
return m, nil
}
// Patch represents a single update to an existing image. Multiple updates
// to an image can be submitted at the same time.
type Patch interface {
ToImagePatchMap() map[string]interface{}
}
// UpdateOp represents a valid update operation.
type UpdateOp string
const (
AddOp UpdateOp = "add"
ReplaceOp UpdateOp = "replace"
RemoveOp UpdateOp = "remove"
)
// UpdateImageProperty represents an update property request.
type UpdateImageProperty struct {
Op UpdateOp
Name string
Value interface{}
}
// ToImagePatchMap assembles a request body based on UpdateImageProperty.
func (r UpdateImageProperty) ToImagePatchMap() map[string]interface{} {
updateMap := map[string]interface{}{
"op": r.Op,
"path": fmt.Sprintf("/%s", r.Name),
"value": r.Value,
}
return updateMap
}