forked from denverdino/aliyungo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disks.go
364 lines (314 loc) · 8.82 KB
/
disks.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
package ecs
import (
"time"
"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/util"
)
// Types of disks
type DiskType string
const (
DiskTypeAll = DiskType("all") //Default
DiskTypeAllSystem = DiskType("system")
DiskTypeAllData = DiskType("data")
)
// Categories of disks
type DiskCategory string
const (
DiskCategoryAll = DiskCategory("all") //Default
DiskCategoryCloud = DiskCategory("cloud")
DiskCategoryEphemeral = DiskCategory("ephemeral")
DiskCategoryEphemeralSSD = DiskCategory("ephemeral_ssd")
DiskCategoryCloudEfficiency = DiskCategory("cloud_efficiency")
DiskCategoryCloudSSD = DiskCategory("cloud_ssd")
)
// Status of disks
type DiskStatus string
const (
DiskStatusInUse = DiskStatus("In_use")
DiskStatusAvailable = DiskStatus("Available")
DiskStatusAttaching = DiskStatus("Attaching")
DiskStatusDetaching = DiskStatus("Detaching")
DiskStatusCreating = DiskStatus("Creating")
DiskStatusReIniting = DiskStatus("ReIniting")
DiskStatusAll = DiskStatus("All") //Default
)
// Charge type of disks
type DiskChargeType string
const (
PrePaid = DiskChargeType("PrePaid")
PostPaid = DiskChargeType("PostPaid")
)
// A DescribeDisksArgs defines the arguments to describe disks
type DescribeDisksArgs struct {
RegionId common.Region
ZoneId string
DiskIds []string
InstanceId string
DiskType DiskType //enum for all(default) | system | data
Category DiskCategory //enum for all(default) | cloud | ephemeral
Status DiskStatus //enum for In_use | Available | Attaching | Detaching | Creating | ReIniting | All(default)
SnapshotId string
Name string
Portable *bool //optional
DeleteWithInstance *bool //optional
DeleteAutoSnapshot *bool //optional
EnableAutoSnapshot *bool //optional
DiskChargeType DiskChargeType
Tag map[string]string
common.Pagination
}
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&diskitemtype
type DiskItemType struct {
DiskId string
RegionId common.Region
ZoneId string
DiskName string
Description string
Type DiskType
Category DiskCategory
Size int
ImageId string
SourceSnapshotId string
ProductCode string
Portable bool
Status DiskStatus
OperationLocks OperationLocksType
InstanceId string
Device string
DeleteWithInstance bool
DeleteAutoSnapshot bool
EnableAutoSnapshot bool
CreationTime util.ISO6801Time
AttachedTime util.ISO6801Time
DetachedTime util.ISO6801Time
DiskChargeType DiskChargeType
}
type DescribeDisksResponse struct {
common.Response
common.PaginationResult
RegionId common.Region
Disks struct {
Disk []DiskItemType
}
}
// DescribeDisks describes Disks
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&describedisks
func (client *Client) DescribeDisks(args *DescribeDisksArgs) (disks []DiskItemType, pagination *common.PaginationResult, err error) {
response, err := client.DescribeDisksWithRaw(args)
if err != nil {
return nil, nil, err
}
return response.Disks.Disk, &response.PaginationResult, err
}
func (client *Client) DescribeDisksWithRaw(args *DescribeDisksArgs) (response *DescribeDisksResponse, err error) {
response = &DescribeDisksResponse{}
err = client.Invoke("DescribeDisks", args, response)
if err != nil {
return nil, err
}
return response, err
}
type CreateDiskArgs struct {
RegionId common.Region
ZoneId string
DiskName string
Description string
Encrypted bool
DiskCategory DiskCategory
Size int
SnapshotId string
ClientToken string
}
type CreateDisksResponse struct {
common.Response
DiskId string
}
// CreateDisk creates a new disk
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&createdisk
func (client *Client) CreateDisk(args *CreateDiskArgs) (diskId string, err error) {
response := CreateDisksResponse{}
err = client.Invoke("CreateDisk", args, &response)
if err != nil {
return "", err
}
return response.DiskId, err
}
type DeleteDiskArgs struct {
DiskId string
}
type DeleteDiskResponse struct {
common.Response
}
// DeleteDisk deletes disk
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&deletedisk
func (client *Client) DeleteDisk(diskId string) error {
args := DeleteDiskArgs{
DiskId: diskId,
}
response := DeleteDiskResponse{}
err := client.Invoke("DeleteDisk", &args, &response)
return err
}
type ReInitDiskArgs struct {
DiskId string
}
type ReInitDiskResponse struct {
common.Response
}
// ReInitDisk reinitizes disk
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&reinitdisk
func (client *Client) ReInitDisk(diskId string) error {
args := ReInitDiskArgs{
DiskId: diskId,
}
response := ReInitDiskResponse{}
err := client.Invoke("ReInitDisk", &args, &response)
return err
}
type AttachDiskArgs struct {
InstanceId string
DiskId string
Device string
DeleteWithInstance bool
}
type AttachDiskResponse struct {
common.Response
}
// AttachDisk attaches disk to instance
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&attachdisk
func (client *Client) AttachDisk(args *AttachDiskArgs) error {
response := AttachDiskResponse{}
err := client.Invoke("AttachDisk", args, &response)
return err
}
type DetachDiskArgs struct {
InstanceId string
DiskId string
}
type DetachDiskResponse struct {
common.Response
}
// DetachDisk detaches disk from instance
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&detachdisk
func (client *Client) DetachDisk(instanceId string, diskId string) error {
args := DetachDiskArgs{
InstanceId: instanceId,
DiskId: diskId,
}
response := DetachDiskResponse{}
err := client.Invoke("DetachDisk", &args, &response)
return err
}
type ResizeDiskArgs struct {
DiskId string
NewSize int
}
type ResizeDiskResponse struct {
common.Response
}
//
// ResizeDisk can only support to enlarge disk size
// You can read doc at https://help.aliyun.com/document_detail/25522.html
func (client *Client) ResizeDisk(diskId string, sizeGB int) error {
args := ResizeDiskArgs{
DiskId:diskId,
NewSize:sizeGB,
}
response := ResizeDiskResponse{}
err := client.Invoke("ResizeDisk", &args, &response)
return err
}
type ResetDiskArgs struct {
DiskId string
SnapshotId string
}
type ResetDiskResponse struct {
common.Response
}
// ResetDisk resets disk to original status
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&resetdisk
func (client *Client) ResetDisk(diskId string, snapshotId string) error {
args := ResetDiskArgs{
SnapshotId: snapshotId,
DiskId: diskId,
}
response := ResetDiskResponse{}
err := client.Invoke("ResetDisk", &args, &response)
return err
}
type ModifyDiskAttributeArgs struct {
DiskId string
DiskName string
Description string
DeleteWithInstance *bool
DeleteAutoSnapshot *bool
EnableAutoSnapshot *bool
}
type ModifyDiskAttributeResponse struct {
common.Response
}
// ModifyDiskAttribute modifies disk attribute
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&modifydiskattribute
func (client *Client) ModifyDiskAttribute(args *ModifyDiskAttributeArgs) error {
response := ModifyDiskAttributeResponse{}
err := client.Invoke("ModifyDiskAttribute", args, &response)
return err
}
type ReplaceSystemDiskArgs struct {
InstanceId string
ImageId string
SystemDisk SystemDiskType
ClientToken string
}
type ReplaceSystemDiskResponse struct {
common.Response
DiskId string
}
// ReplaceSystemDisk replace system disk
//
// You can read doc at https://help.aliyun.com/document_detail/ecs/open-api/disk/replacesystemdisk.html
func (client *Client) ReplaceSystemDisk(args *ReplaceSystemDiskArgs) (diskId string, err error) {
response := ReplaceSystemDiskResponse{}
err = client.Invoke("ReplaceSystemDisk", args, &response)
if err != nil {
return "", err
}
return response.DiskId, nil
}
// WaitForDisk waits for disk to given status
func (client *Client) WaitForDisk(regionId common.Region, diskId string, status DiskStatus, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
args := DescribeDisksArgs{
RegionId: regionId,
DiskIds: []string{diskId},
}
for {
disks, _, err := client.DescribeDisks(&args)
if err != nil {
return err
}
if disks == nil || len(disks) == 0 {
return common.GetClientErrorFromString("Not found")
}
if disks[0].Status == status {
break
}
timeout = timeout - DefaultWaitForInterval
if timeout <= 0 {
return common.GetClientErrorFromString("Timeout")
}
time.Sleep(DefaultWaitForInterval * time.Second)
}
return nil
}