Debian Codegraph @

ecs/disks.go

raw symbols SCIP
1 package ecs
2
3 import (
4 "time"
5
6 "github.com/denverdino/aliyungo/common"
7 "github.com/denverdino/aliyungo/util"
8 )
9
10 // Types of disks
11 type DiskType string
12
13 const (
14 DiskTypeAll = DiskType("all") //Default
15 DiskTypeAllSystem = DiskType("system")
16 DiskTypeAllData = DiskType("data")
17 )
18
19 // Categories of disks
20 type DiskCategory string
21
22 const (
23 DiskCategoryAll = DiskCategory("all") //Default
24 DiskCategoryCloud = DiskCategory("cloud")
25 DiskCategoryEphemeral = DiskCategory("ephemeral")
26 DiskCategoryEphemeralSSD = DiskCategory("ephemeral_ssd")
27 DiskCategoryCloudEfficiency = DiskCategory("cloud_efficiency")
28 DiskCategoryCloudSSD = DiskCategory("cloud_ssd")
29 )
30
31 // Status of disks
32 type DiskStatus string
33
34 const (
35 DiskStatusInUse = DiskStatus("In_use")
36 DiskStatusAvailable = DiskStatus("Available")
37 DiskStatusAttaching = DiskStatus("Attaching")
38 DiskStatusDetaching = DiskStatus("Detaching")
39 DiskStatusCreating = DiskStatus("Creating")
40 DiskStatusReIniting = DiskStatus("ReIniting")
41 DiskStatusAll = DiskStatus("All") //Default
42 )
43
44 // Charge type of disks
45 type DiskChargeType string
46
47 const (
48 PrePaid = DiskChargeType("PrePaid")
49 PostPaid = DiskChargeType("PostPaid")
50 )
51
52 // A DescribeDisksArgs defines the arguments to describe disks
53 type DescribeDisksArgs struct {
54 RegionId common.Region
55 ZoneId string
56 DiskIds []string
57 InstanceId string
58 DiskType DiskType //enum for all(default) | system | data
59 Category DiskCategory //enum for all(default) | cloud | ephemeral
60 Status DiskStatus //enum for In_use | Available | Attaching | Detaching | Creating | ReIniting | All(default)
61 SnapshotId string
62 DiskName string
63 Portable *bool //optional
64 DeleteWithInstance *bool //optional
65 DeleteAutoSnapshot *bool //optional
66 EnableAutoSnapshot *bool //optional
67 DiskChargeType DiskChargeType
68 Tag map[string]string
69 common.Pagination
70 }
71
72 //
73 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&diskitemtype
74 type DiskItemType struct {
75 DiskId string
76 RegionId common.Region
77 ZoneId string
78 DiskName string
79 Description string
80 Type DiskType
81 Encrypted bool
82 Category DiskCategory
83 Size int
84 ImageId string
85 SourceSnapshotId string
86 ProductCode string
87 Portable bool
88 Status DiskStatus
89 OperationLocks OperationLocksType
90 InstanceId string
91 Device string
92 DeleteWithInstance bool
93 DeleteAutoSnapshot bool
94 EnableAutoSnapshot bool
95 CreationTime util.ISO6801Time
96 AttachedTime util.ISO6801Time
97 DetachedTime util.ISO6801Time
98 DiskChargeType DiskChargeType
99 }
100
101 type DescribeDisksResponse struct {
102 common.Response
103 common.PaginationResult
104 RegionId common.Region
105 Disks struct {
106 Disk []DiskItemType
107 }
108 }
109
110 // DescribeDisks describes Disks
111 //
112 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&describedisks
113 func (client *Client) DescribeDisks(args *DescribeDisksArgs) (disks []DiskItemType, pagination *common.PaginationResult, err error) {
114 response, err := client.DescribeDisksWithRaw(args)
115 if err != nil {
116 return nil, nil, err
117 }
118
119 return response.Disks.Disk, &response.PaginationResult, err
120 }
121
122 func (client *Client) DescribeDisksWithRaw(args *DescribeDisksArgs) (response *DescribeDisksResponse, err error) {
123 response = &DescribeDisksResponse{}
124
125 err = client.Invoke("DescribeDisks", args, response)
126
127 if err != nil {
128 return nil, err
129 }
130
131 return response, err
132 }
133
134 type CreateDiskArgs struct {
135 RegionId common.Region
136 ZoneId string
137 DiskName string
138 Description string
139 Encrypted bool
140 DiskCategory DiskCategory
141 Size int
142 SnapshotId string
143 ClientToken string
144 }
145
146 type CreateDisksResponse struct {
147 common.Response
148 DiskId string
149 }
150
151 // CreateDisk creates a new disk
152 //
153 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&createdisk
154 func (client *Client) CreateDisk(args *CreateDiskArgs) (diskId string, err error) {
155 response := CreateDisksResponse{}
156 err = client.Invoke("CreateDisk", args, &response)
157 if err != nil {
158 return "", err
159 }
160 return response.DiskId, err
161 }
162
163 type DeleteDiskArgs struct {
164 DiskId string
165 }
166
167 type DeleteDiskResponse struct {
168 common.Response
169 }
170
171 // DeleteDisk deletes disk
172 //
173 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&deletedisk
174 func (client *Client) DeleteDisk(diskId string) error {
175 args := DeleteDiskArgs{
176 DiskId: diskId,
177 }
178 response := DeleteDiskResponse{}
179 err := client.Invoke("DeleteDisk", &args, &response)
180 return err
181 }
182
183 type ReInitDiskArgs struct {
184 DiskId string
185 }
186
187 type ReInitDiskResponse struct {
188 common.Response
189 }
190
191 // ReInitDisk reinitizes disk
192 //
193 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&reinitdisk
194 func (client *Client) ReInitDisk(diskId string) error {
195 args := ReInitDiskArgs{
196 DiskId: diskId,
197 }
198 response := ReInitDiskResponse{}
199 err := client.Invoke("ReInitDisk", &args, &response)
200 return err
201 }
202
203 type AttachDiskArgs struct {
204 InstanceId string
205 DiskId string
206 Device string
207 DeleteWithInstance bool
208 }
209
210 type AttachDiskResponse struct {
211 common.Response
212 }
213
214 // AttachDisk attaches disk to instance
215 //
216 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&attachdisk
217 func (client *Client) AttachDisk(args *AttachDiskArgs) error {
218 response := AttachDiskResponse{}
219 err := client.Invoke("AttachDisk", args, &response)
220 return err
221 }
222
223 type DetachDiskArgs struct {
224 InstanceId string
225 DiskId string
226 }
227
228 type DetachDiskResponse struct {
229 common.Response
230 }
231
232 // DetachDisk detaches disk from instance
233 //
234 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&detachdisk
235 func (client *Client) DetachDisk(instanceId string, diskId string) error {
236 args := DetachDiskArgs{
237 InstanceId: instanceId,
238 DiskId: diskId,
239 }
240 response := DetachDiskResponse{}
241 err := client.Invoke("DetachDisk", &args, &response)
242 return err
243 }
244
245 type ResizeDiskArgs struct {
246 DiskId string
247 NewSize int
248 }
249
250 type ResizeDiskResponse struct {
251 common.Response
252 }
253
254 //
255 // ResizeDisk can only support to enlarge disk size
256 // You can read doc at https://help.aliyun.com/document_detail/25522.html
257 func (client *Client) ResizeDisk(diskId string, sizeGB int) error {
258 args := ResizeDiskArgs{
259 DiskId: diskId,
260 NewSize: sizeGB,
261 }
262 response := ResizeDiskResponse{}
263 err := client.Invoke("ResizeDisk", &args, &response)
264 return err
265 }
266
267 type ResetDiskArgs struct {
268 DiskId string
269 SnapshotId string
270 }
271
272 type ResetDiskResponse struct {
273 common.Response
274 }
275
276 // ResetDisk resets disk to original status
277 //
278 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&resetdisk
279 func (client *Client) ResetDisk(diskId string, snapshotId string) error {
280 args := ResetDiskArgs{
281 SnapshotId: snapshotId,
282 DiskId: diskId,
283 }
284 response := ResetDiskResponse{}
285 err := client.Invoke("ResetDisk", &args, &response)
286 return err
287 }
288
289 type ModifyDiskAttributeArgs struct {
290 DiskId string
291 DiskName string
292 Description string
293 DeleteWithInstance *bool
294 DeleteAutoSnapshot *bool
295 EnableAutoSnapshot *bool
296 }
297
298 type ModifyDiskAttributeResponse struct {
299 common.Response
300 }
301
302 // ModifyDiskAttribute modifies disk attribute
303 //
304 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/disk&modifydiskattribute
305 func (client *Client) ModifyDiskAttribute(args *ModifyDiskAttributeArgs) error {
306 response := ModifyDiskAttributeResponse{}
307 err := client.Invoke("ModifyDiskAttribute", args, &response)
308 return err
309 }
310
311 type ReplaceSystemDiskArgs struct {
312 InstanceId string
313 ImageId string
314 SystemDisk SystemDiskType
315 ClientToken string
316 }
317
318 type ReplaceSystemDiskResponse struct {
319 common.Response
320 DiskId string
321 }
322
323 // ReplaceSystemDisk replace system disk
324 //
325 // You can read doc at https://help.aliyun.com/document_detail/ecs/open-api/disk/replacesystemdisk.html
326 func (client *Client) ReplaceSystemDisk(args *ReplaceSystemDiskArgs) (diskId string, err error) {
327 response := ReplaceSystemDiskResponse{}
328 err = client.Invoke("ReplaceSystemDisk", args, &response)
329 if err != nil {
330 return "", err
331 }
332 return response.DiskId, nil
333 }
334
335 // WaitForDisk waits for disk to given status
336 func (client *Client) WaitForDisk(regionId common.Region, diskId string, status DiskStatus, timeout int) error {
337 if timeout <= 0 {
338 timeout = DefaultTimeout
339 }
340 args := DescribeDisksArgs{
341 RegionId: regionId,
342 DiskIds: []string{diskId},
343 }
344
345 for {
346 disks, _, err := client.DescribeDisks(&args)
347 if err != nil {
348 return err
349 }
350 if disks == nil || len(disks) == 0 {
351 return common.GetClientErrorFromString("Not found")
352 }
353 if disks[0].Status == status {
354 break
355 }
356 timeout = timeout - DefaultWaitForInterval
357 if timeout <= 0 {
358 return common.GetClientErrorFromString("Timeout")
359 }
360 time.Sleep(DefaultWaitForInterval * time.Second)
361 }
362 return nil
363 }