File: images.go

package info (click to toggle)
golang-github-denverdino-aliyungo 0.0~git20180921.13fa8aa-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,824 kB
  • sloc: xml: 1,359; makefile: 3
file content (347 lines) | stat: -rw-r--r-- 8,821 bytes parent folder | download | duplicates (3)
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
package ecs

import (
	"net/url"
	"strconv"
	"time"

	"github.com/denverdino/aliyungo/common"
	"github.com/denverdino/aliyungo/util"
)

// ImageOwnerAlias represents image owner
type ImageOwnerAlias string

// Constants of image owner
const (
	ImageOwnerSystem      = ImageOwnerAlias("system")
	ImageOwnerSelf        = ImageOwnerAlias("self")
	ImageOwnerOthers      = ImageOwnerAlias("others")
	ImageOwnerMarketplace = ImageOwnerAlias("marketplace")
	ImageOwnerDefault     = ImageOwnerAlias("") //Return the values for system, self, and others
)

type ImageStatus string

const (
	ImageStatusAvailable    = ImageStatus("Available")
	ImageStatusUnAvailable  = ImageStatus("UnAvailable")
	ImageStatusCreating     = ImageStatus("Creating")
	ImageStatusCreateFailed = ImageStatus("CreateFailed")
)

type ImageUsage string

const (
	ImageUsageInstance = ImageUsage("instance")
	ImageUsageNone     = ImageUsage("none")
)

type ImageFormatType string

const (
	RAW = ImageFormatType("RAW")
	VHD = ImageFormatType("VHD")
)

// DescribeImagesArgs repsents arguments to describe images
type DescribeImagesArgs struct {
	RegionId        common.Region
	ImageId         string
	SnapshotId      string
	ImageName       string
	Status          ImageStatus
	ImageOwnerAlias ImageOwnerAlias
	common.Pagination
}

type DescribeImagesResponse struct {
	common.Response
	common.PaginationResult

	RegionId common.Region
	Images   struct {
		Image []ImageType
	}
}

//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&diskdevicemapping
type DiskDeviceMapping struct {
	SnapshotId string
	//Why Size Field is string-type.
	Size string
	// Now the key Size change to DiskImageSize
	DiskImageSize string
	Device        string
	//For import images
	Format    string
	OSSBucket string
	OSSObject string
}

//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&imagetype
type ImageType struct {
	ImageId            string
	ImageVersion       string
	Architecture       string
	ImageName          string
	Description        string
	Size               int
	ImageOwnerAlias    string
	OSName             string
	OSType             string
	Platform           string
	DiskDeviceMappings struct {
		DiskDeviceMapping []DiskDeviceMapping
	}
	ProductCode          string
	IsSubscribed         bool
	IsSelfShared         string
	IsCopied             bool
	IsSupportIoOptimized bool
	Progress             string
	Usage                ImageUsage
	Status               ImageStatus
	CreationTime         util.ISO6801Time
}

// DescribeImages describes images
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/image&describeimages
func (client *Client) DescribeImages(args *DescribeImagesArgs) (images []ImageType, pagination *common.PaginationResult, err error) {
	response, err := client.DescribeImagesWithRaw(args)
	if err != nil {
		return nil, nil, err
	}
	return response.Images.Image, &response.PaginationResult, nil
}

func (client *Client) DescribeImagesWithRaw(args *DescribeImagesArgs) (response *DescribeImagesResponse, err error) {
	args.Validate()
	response = &DescribeImagesResponse{}
	err = client.Invoke("DescribeImages", args, response)
	if err != nil {
		return nil, err
	}
	return response, nil
}

// CreateImageArgs repsents arguments to create image
type CreateImageArgs struct {
	RegionId     common.Region
	SnapshotId   string
	InstanceId   string
	ImageName    string
	ImageVersion string
	Description  string
	ClientToken  string
}

type CreateImageResponse struct {
	common.Response

	ImageId string
}

// CreateImage creates a new image
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/image&createimage
func (client *Client) CreateImage(args *CreateImageArgs) (imageId string, err error) {
	response := &CreateImageResponse{}
	err = client.Invoke("CreateImage", args, &response)
	if err != nil {
		return "", err
	}
	return response.ImageId, nil
}

type DeleteImageArgs struct {
	RegionId common.Region
	ImageId  string
	Force    bool
}

type DeleteImageResponse struct {
	common.Response
}

// DeleteImage deletes Image
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/image&deleteimage
func (client *Client) DeleteImage(regionId common.Region, imageId string) error {
	args := DeleteImageArgs{
		RegionId: regionId,
		ImageId:  imageId,
	}

	response := &DeleteImageResponse{}
	return client.Invoke("DeleteImage", &args, &response)
}

// DeleteImage deletes Image
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/image&deleteimage
func (client *Client) DeleteImageWithForce(regionId common.Region, imageId string, force bool) error {
	args := DeleteImageArgs{
		RegionId: regionId,
		ImageId:  imageId,
		Force:    force,
	}

	response := &DeleteImageResponse{}
	return client.Invoke("DeleteImage", &args, &response)
}

// ModifyImageSharePermission repsents arguments to share image
type ModifyImageSharePermissionArgs struct {
	RegionId      common.Region
	ImageId       string
	AddAccount    []string
	RemoveAccount []string
}

// You can read doc at http://help.aliyun.com/document_detail/ecs/open-api/image/modifyimagesharepermission.html
func (client *Client) ModifyImageSharePermission(args *ModifyImageSharePermissionArgs) error {
	req := url.Values{}
	req.Add("RegionId", string(args.RegionId))
	req.Add("ImageId", args.ImageId)

	for i, item := range args.AddAccount {
		req.Add("AddAccount."+strconv.Itoa(i+1), item)
	}
	for i, item := range args.RemoveAccount {
		req.Add("RemoveAccount."+strconv.Itoa(i+1), item)
	}

	return client.Invoke("ModifyImageSharePermission", req, &common.Response{})
}

type AccountType struct {
	AliyunId string
}
type ImageSharePermissionResponse struct {
	common.Response
	ImageId  string
	RegionId string
	Accounts struct {
		Account []AccountType
	}
	TotalCount int
	PageNumber int
	PageSize   int
}

func (client *Client) DescribeImageSharePermission(args *ModifyImageSharePermissionArgs) (*ImageSharePermissionResponse, error) {
	response := ImageSharePermissionResponse{}
	err := client.Invoke("DescribeImageSharePermission", args, &response)
	return &response, err
}

type CopyImageArgs struct {
	RegionId               common.Region
	ImageId                string
	DestinationRegionId    common.Region
	DestinationImageName   string
	DestinationDescription string
	ClientToken            string
}

type CopyImageResponse struct {
	common.Response
	ImageId string
}

// You can read doc at https://help.aliyun.com/document_detail/25538.html
func (client *Client) CopyImage(args *CopyImageArgs) (string, error) {
	response := &CopyImageResponse{}
	err := client.Invoke("CopyImage", args, &response)
	if err != nil {
		return "", err
	}
	return response.ImageId, nil
}

// ImportImageArgs repsents arguments to import image from oss
type ImportImageArgs struct {
	RegionId           common.Region
	ImageName          string
	ImageVersion       string
	Description        string
	ClientToken        string
	Architecture       string
	OSType             string
	Platform           string
	DiskDeviceMappings struct {
		DiskDeviceMapping []DiskDeviceMapping
	}
}

func (client *Client) ImportImage(args *ImportImageArgs) (string, error) {
	response := &CopyImageResponse{}
	err := client.Invoke("ImportImage", args, &response)
	if err != nil {
		return "", err
	}
	return response.ImageId, nil
}

type ImportImageResponse struct {
	common.Response
	RegionId     common.Region
	ImageId      string
	ImportTaskId string
}

// Default timeout value for WaitForImageReady method
const ImageDefaultTimeout = 120

//Wait Image ready
func (client *Client) WaitForImageReady(regionId common.Region, imageId string, timeout int) error {
	if timeout <= 0 {
		timeout = ImageDefaultTimeout
	}
	for {
		args := DescribeImagesArgs{
			RegionId: regionId,
			ImageId:  imageId,
			Status:   ImageStatusCreating,
		}

		images, _, err := client.DescribeImages(&args)
		if err != nil {
			return err
		}
		if images == nil || len(images) == 0 {
			args.Status = ImageStatusAvailable
			images, _, er := client.DescribeImages(&args)
			if er == nil && len(images) == 1 {
				break
			} else {
				return common.GetClientErrorFromString("Not found")
			}
		}
		if images[0].Progress == "100%" {
			break
		}
		timeout = timeout - DefaultWaitForInterval
		if timeout <= 0 {
			return common.GetClientErrorFromString("Timeout")
		}
		time.Sleep(DefaultWaitForInterval * time.Second)
	}
	return nil
}

type CancelCopyImageRequest struct {
	regionId common.Region
	ImageId  string
}

// You can read doc at https://help.aliyun.com/document_detail/25539.html
func (client *Client) CancelCopyImage(regionId common.Region, imageId string) error {
	response := &common.Response{}
	err := client.Invoke("CancelCopyImage", &CancelCopyImageRequest{regionId, imageId}, &response)
	return err
}