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
282 lines (250 loc) · 9.35 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
package cloudimages
import (
"fmt"
"net/url"
"time"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)
type ListOptsBuilder interface {
ToImageListQuery() (string, error)
}
type ListOpts struct {
Isregistered string `q:"__isregistered"`
Imagetype string `q:"__imagetype"`
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"`
OsBit string `q:"__os_bit"`
Platform string `q:"__platform"`
Marker string `q:"marker"`
Limit int `q:"limit"`
SortKey string `q:"sort_key"`
SortDir string `q:"sort_dir"`
OsType string `q:"__os_type"`
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"`
// 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"`
// the data disks to be converted
DataImages []DataImage `json:"data_images,omitempty"`
// image label "key.value"
Tags []string `json:"tags,omitempty"`
// One or more tag key and value pairs to associate with the image
ImageTags []ImageTag `json:"image_tags,omitempty"`
// the maximum memory of the image in the unit of MB
MaxRam int `json:"max_ram,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"`
}
// 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"`
//whether automatic configuration is enabled,the value can be true or false
IsConfig bool `json:"is_config,omitempty"`
// the master key used for encrypting an image
CmkId string `json:"cmk_id,omitempty"`
// image label "key.value"
Tags []string `json:"tags,omitempty"`
// One or more tag key and value pairs to associate with the image
ImageTags []ImageTag `json:"image_tags,omitempty"`
// the image type, the value can be ECS,BMS,FusionCompute, or Ironic
Type string `json:"type,omitempty"`
// the maximum memory of the image in the unit of MB
MaxRam int `json:"max_ram,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"`
}
// 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"`
}
type ImageTag struct {
Key string `json:"key" required:"true"`
Value string `json:"value,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, "")
}
// 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
}