images.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. // DescribeImagesArgs repsents arguements to describe images
  27. type DescribeImagesArgs struct {
  28. RegionId common.Region
  29. ImageId string
  30. SnapshotId string
  31. ImageName string
  32. Status ImageStatus
  33. ImageOwnerAlias ImageOwnerAlias
  34. common.Pagination
  35. }
  36. type DescribeImagesResponse struct {
  37. common.Response
  38. common.PaginationResult
  39. RegionId common.Region
  40. Images struct {
  41. Image []ImageType
  42. }
  43. }
  44. //
  45. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&diskdevicemapping
  46. type DiskDeviceMapping struct {
  47. SnapshotId string
  48. //Why Size Field is string-type.
  49. Size string
  50. Device string
  51. }
  52. //
  53. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&imagetype
  54. type ImageType struct {
  55. ImageId string
  56. ImageVersion string
  57. Architecture string
  58. ImageName string
  59. Description string
  60. Size int
  61. ImageOwnerAlias string
  62. OSName string
  63. DiskDeviceMappings struct {
  64. DiskDeviceMapping []DiskDeviceMapping
  65. }
  66. ProductCode string
  67. IsSubscribed bool
  68. Progress string
  69. Status ImageStatus
  70. CreationTime util.ISO6801Time
  71. }
  72. // DescribeImages describes images
  73. //
  74. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/image&describeimages
  75. func (client *Client) DescribeImages(args *DescribeImagesArgs) (images []ImageType, pagination *common.PaginationResult, err error) {
  76. args.Validate()
  77. response := DescribeImagesResponse{}
  78. err = client.Invoke("DescribeImages", args, &response)
  79. if err != nil {
  80. return nil, nil, err
  81. }
  82. return response.Images.Image, &response.PaginationResult, nil
  83. }
  84. // CreateImageArgs repsents arguements to create image
  85. type CreateImageArgs struct {
  86. RegionId common.Region
  87. SnapshotId string
  88. ImageName string
  89. ImageVersion string
  90. Description string
  91. ClientToken string
  92. }
  93. type CreateImageResponse struct {
  94. common.Response
  95. ImageId string
  96. }
  97. // CreateImage creates a new image
  98. //
  99. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/image&createimage
  100. func (client *Client) CreateImage(args *CreateImageArgs) (imageId string, err error) {
  101. response := &CreateImageResponse{}
  102. err = client.Invoke("CreateImage", args, &response)
  103. if err != nil {
  104. return "", err
  105. }
  106. return response.ImageId, nil
  107. }
  108. type DeleteImageArgs struct {
  109. RegionId common.Region
  110. ImageId string
  111. }
  112. type DeleteImageResponse struct {
  113. common.Response
  114. }
  115. // DeleteImage deletes Image
  116. //
  117. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/image&deleteimage
  118. func (client *Client) DeleteImage(regionId common.Region, imageId string) error {
  119. args := DeleteImageArgs{
  120. RegionId: regionId,
  121. ImageId: imageId,
  122. }
  123. response := &DeleteImageResponse{}
  124. return client.Invoke("DeleteImage", &args, &response)
  125. }
  126. // ModifyImageSharePermission repsents arguements to share image
  127. type ModifyImageSharePermissionArgs struct {
  128. RegionId common.Region
  129. ImageId string
  130. AddAccount []string
  131. RemoveAccount []string
  132. }
  133. // You can read doc at http://help.aliyun.com/document_detail/ecs/open-api/image/modifyimagesharepermission.html
  134. func (client *Client) ModifyImageSharePermission(args *ModifyImageSharePermissionArgs) error {
  135. req := url.Values{}
  136. req.Add("RegionId", string(args.RegionId))
  137. req.Add("ImageId", args.ImageId)
  138. for i, item := range args.AddAccount {
  139. req.Add("AddAccount."+strconv.Itoa(i+1), item)
  140. }
  141. for i, item := range args.RemoveAccount {
  142. req.Add("RemoveAccount."+strconv.Itoa(i+1), item)
  143. }
  144. return client.Invoke("ModifyImageSharePermission", req, &common.Response{})
  145. }
  146. type AccountType struct {
  147. AliyunId string
  148. }
  149. type ImageSharePermissionResponse struct {
  150. common.Response
  151. ImageId string
  152. RegionId string
  153. Accounts struct {
  154. Account []AccountType
  155. }
  156. TotalCount int
  157. PageNumber int
  158. PageSize int
  159. }
  160. func (client *Client) DescribeImageSharePermission(args *ModifyImageSharePermissionArgs) (*ImageSharePermissionResponse, error) {
  161. response := ImageSharePermissionResponse{}
  162. err := client.Invoke("DescribeImageSharePermission", args, &response)
  163. return &response, err
  164. }
  165. type CopyImageArgs struct {
  166. RegionId common.Region
  167. ImageId string
  168. DestinationRegionId common.Region
  169. DestinationImageName string
  170. DestinationDescription string
  171. ClientToken string
  172. }
  173. type CopyImageResponse struct {
  174. common.Response
  175. ImageId string
  176. }
  177. // You can read doc at https://help.aliyun.com/document_detail/25538.html
  178. func (client *Client) CopyImage(args *CopyImageArgs) (string, error) {
  179. response := &CopyImageResponse{}
  180. err := client.Invoke("CopyImage", args, &response)
  181. if err != nil {
  182. return "", err
  183. }
  184. return response.ImageId, nil
  185. }
  186. // Default timeout value for WaitForImageReady method
  187. const ImageDefaultTimeout = 120
  188. //Wait Image ready
  189. func (client *Client) WaitForImageReady(regionId common.Region, imageId string, timeout int) error {
  190. if timeout <= 0 {
  191. timeout = ImageDefaultTimeout
  192. }
  193. for {
  194. args := DescribeImagesArgs{
  195. RegionId: regionId,
  196. ImageId: imageId,
  197. Status: ImageStatusCreating,
  198. }
  199. images, _, err := client.DescribeImages(&args)
  200. if err != nil {
  201. return err
  202. }
  203. if images == nil || len(images) == 0 {
  204. args.Status = ImageStatusAvailable
  205. images, _, er := client.DescribeImages(&args)
  206. if er == nil && len(images) == 1 {
  207. break
  208. } else {
  209. return common.GetClientErrorFromString("Not found")
  210. }
  211. }
  212. if images[0].Progress == "100%" {
  213. break
  214. }
  215. timeout = timeout - DefaultWaitForInterval
  216. if timeout <= 0 {
  217. return common.GetClientErrorFromString("Timeout")
  218. }
  219. time.Sleep(DefaultWaitForInterval * time.Second)
  220. }
  221. return nil
  222. }
  223. type CancelCopyImageRequest struct {
  224. regionId common.Region
  225. ImageId string
  226. }
  227. // You can read doc at https://help.aliyun.com/document_detail/25539.html
  228. func (client *Client) CancelCopyImage(regionId common.Region, imageId string) error {
  229. response := &common.Response{}
  230. err := client.Invoke("CancelCopyImage", &CancelCopyImageRequest{regionId, imageId}, &response)
  231. return err
  232. }