-
Notifications
You must be signed in to change notification settings - Fork 7
/
requests.go
306 lines (232 loc) · 9.06 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
package cloudservers
import (
"encoding/base64"
"github.com/opentelekomcloud/gophertelekomcloud"
)
type CreateOpts struct {
// ImageRef ID the ID of the system image used for creating ECSs.
ImageRef string `json:"imageRef" required:"true"`
// FlavorRef ID of the ECS to be created.
FlavorRef string `json:"flavorRef" required:"true"`
// Name of the ECS instance.
Name string `json:"name" required:"true"`
// UserData to be injected during the ECS creation process.
UserData []byte `json:"-"`
// AdminPass sets the root user password. If not set, a randomly-generated
// password will be created and returned in the response.
AdminPass string `json:"adminPass,omitempty"`
// KeyName of the SSH key used for logging in to the ECS.
KeyName string `json:"key_name,omitempty"`
// VpcId of the VPC to which the ECS belongs.
VpcId string `json:"vpcid" required:"true"`
// Nics information of the ECS.
Nics []Nic `json:"nics" required:"true"`
// PublicIp of the ECS.
PublicIp *PublicIp `json:"publicip,omitempty"`
// Count of ECSs to be created.
// If this parameter is not specified, the default value is 1.
Count int `json:"count,omitempty"`
// ECS RootVolume configurations.
RootVolume RootVolume `json:"root_volume" required:"true"`
// ECS DataVolumes configurations.
DataVolumes []DataVolume `json:"data_volumes,omitempty"`
// SecurityGroups of the ECS.
SecurityGroups []SecurityGroup `json:"security_groups,omitempty"`
// AvailabilityZone specifies name of the AZ where the ECS is located.
AvailabilityZone string `json:"availability_zone" required:"true"`
// ExtendParam provides the supplementary information about the ECS to be created.
ExtendParam *ServerExtendParam `json:"extendparam,omitempty"`
// MetaData specifies the metadata of the ECS to be created.
MetaData *MetaData `json:"metadata,omitempty"`
// SchedulerHints schedules ECSs, for example, by configuring an ECS group.
SchedulerHints *SchedulerHints `json:"os:scheduler_hints,omitempty"`
// ECS Tags.
Tags []string `json:"tags,omitempty"`
ServerTags []ServerTags `json:"server_tags,omitempty"`
}
// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToServerCreateMap() (map[string]interface{}, error)
}
// ToServerCreateMap assembles a request body based on the contents of a
// CreateOpts.
func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
if opts.UserData != nil {
var userData string
if _, err := base64.StdEncoding.DecodeString(string(opts.UserData)); err != nil {
userData = base64.StdEncoding.EncodeToString(opts.UserData)
} else {
userData = string(opts.UserData)
}
b["user_data"] = &userData
}
return map[string]interface{}{"server": b}, nil
}
type Nic struct {
// SubnetId of the ECS.
SubnetId string `json:"subnet_id" required:"true"`
// IpAddress of the NIC used by the ECS.
IpAddress string `json:"ip_address,omitempty"`
// BindingProfile allows you to customize data.
// Configure this parameter when creating a HANA ECS.
BindingProfile BindingProfile `json:"binding:profile,omitempty"`
// ExtraDhcpOpts indicates extended DHCP options.
ExtraDhcpOpts []ExtraDhcpOpts `json:"extra_dhcp_opts,omitempty"`
}
type BindingProfile struct {
// DisableSecurityGroups indicates that a HANA ECS NIC is not added to a security group.
DisableSecurityGroups *bool `json:"disable_security_groups,omitempty"`
}
type ExtraDhcpOpts struct {
// Set the parameter value to 26.
OptName string `json:"opt_name" required:"true"`
// OptValue specifies the NIC MTU, which ranges from 1280 to 8888.
OptValue int `json:"opt_value" required:"true"`
}
type PublicIp struct {
// Id of the existing EIP assigned to the ECS to be created.
Id string `json:"id,omitempty"`
// Eip that will be automatically assigned to an ECS.
Eip *Eip `json:"eip,omitempty"`
}
type Eip struct {
// Specifies the EIP type
IpType string `json:"iptype" required:"true"`
// Specifies the EIP bandwidth.
BandWidth *BandWidth `json:"bandwidth" required:"true"`
}
type BandWidth struct {
// Specifies the bandwidth size.
Size int `json:"size" required:"true"`
// Specifies the bandwidth sharing type
ShareType string `json:"sharetype" required:"true"`
// Specifies the bandwidth billing mode.
ChargeMode string `json:"chargemode" required:"true"`
}
type RootVolume struct {
// VolumeType of the ECS system disk.
VolumeType string `json:"volumetype" required:"true"`
// System disk Size, in GB.
Size int `json:"size,omitempty"`
ExtendParam *VolumeExtendParam `json:"extendparam,omitempty"`
// Pay attention to this parameter if your ECS is SDI-compliant.
// If the value of this parameter is true, the created disk is of SCSI type.
PassThrough *bool `json:"hw:passthrough,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
type DataVolume struct {
// VolumeType of the ECS data disk.
VolumeType string `json:"volumetype" required:"true"`
// The data disk Size, in GB.
Size int `json:"size" required:"true"`
// MultiAttach is the shared disk information.
MultiAttach *bool `json:"multiattach,omitempty"`
// PassThrough indicates whether the data volume uses a SCSI lock.
PassThrough *bool `json:"hw:passthrough,omitempty"`
Extendparam *VolumeExtendParam `json:"extendparam,omitempty"`
// DataImageID If data disks are created using a data disk
// image, this parameter is mandatory and it does not support metadata.
DataImageID string `json:"data_image_id,omitempty"`
// EVS disk Metadata.
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
type VolumeExtendParam struct {
SnapshotId string `json:"snapshotId,omitempty"`
}
type ServerExtendParam struct {
// RegionID is the ID of the region where the ECS resides.
RegionID string `json:"regionID,omitempty"`
// SupportAutoRecovery specifies whether automatic recovery is enabled on the ECS.
SupportAutoRecovery string `json:"support_auto_recovery,omitempty"`
}
type MetaData struct {
// AdminPass specifies the password of user Administrator for logging in to a Windows ECS.
AdminPass string `json:"admin_pass,omitempty"`
// OpSvcUserId specifies the user ID.
OpSvcUserId string `json:"op_svc_userid,omitempty"`
// AgencyName specifies the IAM agency name.
AgencyName string `json:"agency_name,omitempty"`
// If you have an OS or a software license, you can migrate your services to the cloud
// platform in BYOL mode to continue using your existing licenses.
BYOL string `json:"BYOL,omitempty"`
}
type SecurityGroup struct {
// ID of the security group to which an ECS is to be added
ID string `json:"id,omitempty"`
}
type SchedulerHints struct {
// ECS Group ID, which is in UUID format.
Group string `json:"group,omitempty"`
// Specifies whether the ECS is created on a Dedicated Host (DeH) or in a shared pool.
Tenancy string `json:"tenancy,omitempty"`
// DedicatedHostID specifies a DeH ID.
DedicatedHostID string `json:"dedicated_host_id,omitempty"`
}
type ServerTags struct {
Key string `json:"key" required:"true"`
Value string `json:"value,omitempty"`
}
// Create requests a server to be provisioned to the user in the current tenant.
func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) {
b, err := opts.ToServerCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200}},
)
return
}
// DryRun requests a server to be provisioned to the user in the current tenant.
func DryRun(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r DryRunResult) {
b, err := opts.ToServerCreateMap()
if err != nil {
r.Err = err
return
}
b["dry_run"] = true
_, r.Err = client.Post(createURL(client), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{202}},
)
return
}
// Get retrieves a particular Server based on its unique ID.
func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
_, r.Err = c.Get(getURL(c, id), &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200, 203},
})
return
}
type DeleteOpts struct {
// Servers to be deleted
Servers []Server `json:"servers" required:"true"`
// DeletePublicIP specifies whether to delete the EIP bound to the ECS when deleting the ECS.
DeletePublicIP bool `json:"delete_publicip,omitempty"`
// DeleteVolume specifies whether to delete the data disks of the ECS.
DeleteVolume bool `json:"delete_volume,omitempty"`
}
type Server struct {
// ID of the ECS to be deleted.
Id string `json:"id" required:"true"`
}
// ToServerDeleteMap assembles a request body based on the contents of a
// DeleteOpts.
func (opts DeleteOpts) ToServerDeleteMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Delete requests a server to be deleted to the user in the current tenant.
func Delete(client *golangsdk.ServiceClient, opts DeleteOpts) (r JobResult) {
b, err := opts.ToServerDeleteMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(deleteURL(client), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
return
}