images.go 7.8 KB

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