Debian Codegraph @

ecs/images.go

raw symbols SCIP
1 package ecs
2
3 import (
4 "net/url"
5 "strconv"
6 "time"
7
8 "github.com/denverdino/aliyungo/common"
9 "github.com/denverdino/aliyungo/util"
10 )
11
12 // ImageOwnerAlias represents image owner
13 type ImageOwnerAlias string
14
15 // Constants of image owner
16 const (
17 ImageOwnerSystem = ImageOwnerAlias("system")
18 ImageOwnerSelf = ImageOwnerAlias("self")
19 ImageOwnerOthers = ImageOwnerAlias("others")
20 ImageOwnerMarketplace = ImageOwnerAlias("marketplace")
21 ImageOwnerDefault = ImageOwnerAlias("") //Return the values for system, self, and others
22 )
23
24 type ImageStatus string
25
26 const (
27 ImageStatusAvailable = ImageStatus("Available")
28 ImageStatusUnAvailable = ImageStatus("UnAvailable")
29 ImageStatusCreating = ImageStatus("Creating")
30 ImageStatusCreateFailed = ImageStatus("CreateFailed")
31 )
32
33 type ImageUsage string
34
35 const (
36 ImageUsageInstance = ImageUsage("instance")
37 ImageUsageNone = ImageUsage("none")
38 )
39
40 type ImageFormatType string
41
42 const (
43 RAW = ImageFormatType("RAW")
44 VHD = ImageFormatType("VHD")
45 )
46
47 // DescribeImagesArgs repsents arguments to describe images
48 type DescribeImagesArgs struct {
49 RegionId common.Region
50 ImageId string
51 SnapshotId string
52 ImageName string
53 Status ImageStatus
54 ImageOwnerAlias ImageOwnerAlias
55 common.Pagination
56 }
57
58 type DescribeImagesResponse struct {
59 common.Response
60 common.PaginationResult
61
62 RegionId common.Region
63 Images struct {
64 Image []ImageType
65 }
66 }
67
68 //
69 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&diskdevicemapping
70 type DiskDeviceMapping struct {
71 SnapshotId string
72 //Why Size Field is string-type.
73 Size string
74 // Now the key Size change to DiskImageSize
75 DiskImageSize string
76 Device string
77 //For import images
78 Format string
79 OSSBucket string
80 OSSObject string
81 }
82
83 //
84 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&imagetype
85 type ImageType struct {
86 ImageId string
87 ImageVersion string
88 Architecture string
89 ImageName string
90 Description string
91 Size int
92 ImageOwnerAlias string
93 OSName string
94 OSType string
95 Platform string
96 DiskDeviceMappings struct {
97 DiskDeviceMapping []DiskDeviceMapping
98 }
99 ProductCode string
100 IsSubscribed bool
101 IsSelfShared string
102 IsCopied bool
103 IsSupportIoOptimized bool
104 Progress string
105 Usage ImageUsage
106 Status ImageStatus
107 CreationTime util.ISO6801Time
108 }
109
110 // DescribeImages describes images
111 //
112 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/image&describeimages
113 func (client *Client) DescribeImages(args *DescribeImagesArgs) (images []ImageType, pagination *common.PaginationResult, err error) {
114 response, err := client.DescribeImagesWithRaw(args)
115 if err != nil {
116 return nil, nil, err
117 }
118 return response.Images.Image, &response.PaginationResult, nil
119 }
120
121 func (client *Client) DescribeImagesWithRaw(args *DescribeImagesArgs) (response *DescribeImagesResponse, err error) {
122 args.Validate()
123 response = &DescribeImagesResponse{}
124 err = client.Invoke("DescribeImages", args, response)
125 if err != nil {
126 return nil, err
127 }
128 return response, nil
129 }
130
131 // CreateImageArgs repsents arguments to create image
132 type CreateImageArgs struct {
133 RegionId common.Region
134 SnapshotId string
135 InstanceId string
136 ImageName string
137 ImageVersion string
138 Description string
139 ClientToken string
140 }
141
142 type CreateImageResponse struct {
143 common.Response
144
145 ImageId string
146 }
147
148 // CreateImage creates a new image
149 //
150 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/image&createimage
151 func (client *Client) CreateImage(args *CreateImageArgs) (imageId string, err error) {
152 response := &CreateImageResponse{}
153 err = client.Invoke("CreateImage", args, &response)
154 if err != nil {
155 return "", err
156 }
157 return response.ImageId, nil
158 }
159
160 type DeleteImageArgs struct {
161 RegionId common.Region
162 ImageId string
163 Force bool
164 }
165
166 type DeleteImageResponse struct {
167 common.Response
168 }
169
170 // DeleteImage deletes Image
171 //
172 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/image&deleteimage
173 func (client *Client) DeleteImage(regionId common.Region, imageId string) error {
174 args := DeleteImageArgs{
175 RegionId: regionId,
176 ImageId: imageId,
177 }
178
179 response := &DeleteImageResponse{}
180 return client.Invoke("DeleteImage", &args, &response)
181 }
182
183 // DeleteImage deletes Image
184 //
185 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/image&deleteimage
186 func (client *Client) DeleteImageWithForce(regionId common.Region, imageId string, force bool) error {
187 args := DeleteImageArgs{
188 RegionId: regionId,
189 ImageId: imageId,
190 Force: force,
191 }
192
193 response := &DeleteImageResponse{}
194 return client.Invoke("DeleteImage", &args, &response)
195 }
196
197 // ModifyImageSharePermission repsents arguments to share image
198 type ModifyImageSharePermissionArgs struct {
199 RegionId common.Region
200 ImageId string
201 AddAccount []string
202 RemoveAccount []string
203 }
204
205 // You can read doc at http://help.aliyun.com/document_detail/ecs/open-api/image/modifyimagesharepermission.html
206 func (client *Client) ModifyImageSharePermission(args *ModifyImageSharePermissionArgs) error {
207 req := url.Values{}
208 req.Add("RegionId", string(args.RegionId))
209 req.Add("ImageId", args.ImageId)
210
211 for i, item := range args.AddAccount {
212 req.Add("AddAccount."+strconv.Itoa(i+1), item)
213 }
214 for i, item := range args.RemoveAccount {
215 req.Add("RemoveAccount."+strconv.Itoa(i+1), item)
216 }
217
218 return client.Invoke("ModifyImageSharePermission", req, &common.Response{})
219 }
220
221 type AccountType struct {
222 AliyunId string
223 }
224 type ImageSharePermissionResponse struct {
225 common.Response
226 ImageId string
227 RegionId string
228 Accounts struct {
229 Account []AccountType
230 }
231 TotalCount int
232 PageNumber int
233 PageSize int
234 }
235
236 func (client *Client) DescribeImageSharePermission(args *ModifyImageSharePermissionArgs) (*ImageSharePermissionResponse, error) {
237 response := ImageSharePermissionResponse{}
238 err := client.Invoke("DescribeImageSharePermission", args, &response)
239 return &response, err
240 }
241
242 type CopyImageArgs struct {
243 RegionId common.Region
244 ImageId string
245 DestinationRegionId common.Region
246 DestinationImageName string
247 DestinationDescription string
248 ClientToken string
249 }
250
251 type CopyImageResponse struct {
252 common.Response
253 ImageId string
254 }
255
256 // You can read doc at https://help.aliyun.com/document_detail/25538.html
257 func (client *Client) CopyImage(args *CopyImageArgs) (string, error) {
258 response := &CopyImageResponse{}
259 err := client.Invoke("CopyImage", args, &response)
260 if err != nil {
261 return "", err
262 }
263 return response.ImageId, nil
264 }
265
266 // ImportImageArgs repsents arguments to import image from oss
267 type ImportImageArgs struct {
268 RegionId common.Region
269 ImageName string
270 ImageVersion string
271 Description string
272 ClientToken string
273 Architecture string
274 OSType string
275 Platform string
276 DiskDeviceMappings struct {
277 DiskDeviceMapping []DiskDeviceMapping
278 }
279 }
280
281 func (client *Client) ImportImage(args *ImportImageArgs) (string, error) {
282 response := &CopyImageResponse{}
283 err := client.Invoke("ImportImage", args, &response)
284 if err != nil {
285 return "", err
286 }
287 return response.ImageId, nil
288 }
289
290 type ImportImageResponse struct {
291 common.Response
292 RegionId common.Region
293 ImageId string
294 ImportTaskId string
295 }
296
297 // Default timeout value for WaitForImageReady method
298 const ImageDefaultTimeout = 120
299
300 //Wait Image ready
301 func (client *Client) WaitForImageReady(regionId common.Region, imageId string, timeout int) error {
302 if timeout <= 0 {
303 timeout = ImageDefaultTimeout
304 }
305 for {
306 args := DescribeImagesArgs{
307 RegionId: regionId,
308 ImageId: imageId,
309 Status: ImageStatusCreating,
310 }
311
312 images, _, err := client.DescribeImages(&args)
313 if err != nil {
314 return err
315 }
316 if images == nil || len(images) == 0 {
317 args.Status = ImageStatusAvailable
318 images, _, er := client.DescribeImages(&args)
319 if er == nil && len(images) == 1 {
320 break
321 } else {
322 return common.GetClientErrorFromString("Not found")
323 }
324 }
325 if images[0].Progress == "100%" {
326 break
327 }
328 timeout = timeout - DefaultWaitForInterval
329 if timeout <= 0 {
330 return common.GetClientErrorFromString("Timeout")
331 }
332 time.Sleep(DefaultWaitForInterval * time.Second)
333 }
334 return nil
335 }
336
337 type CancelCopyImageRequest struct {
338 regionId common.Region
339 ImageId string
340 }
341
342 // You can read doc at https://help.aliyun.com/document_detail/25539.html
343 func (client *Client) CancelCopyImage(regionId common.Region, imageId string) error {
344 response := &common.Response{}
345 err := client.Invoke("CancelCopyImage", &CancelCopyImageRequest{regionId, imageId}, &response)
346 return err
347 }