123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- package ecs
- import (
- "net/url"
- "strconv"
- "time"
- "github.com/denverdino/aliyungo/common"
- "github.com/denverdino/aliyungo/util"
- )
- type ImageOwnerAlias string
- const (
- ImageOwnerSystem = ImageOwnerAlias("system")
- ImageOwnerSelf = ImageOwnerAlias("self")
- ImageOwnerOthers = ImageOwnerAlias("others")
- ImageOwnerMarketplace = ImageOwnerAlias("marketplace")
- ImageOwnerDefault = ImageOwnerAlias("")
- )
- type ImageStatus string
- const (
- ImageStatusAvailable = ImageStatus("Available")
- ImageStatusUnAvailable = ImageStatus("UnAvailable")
- ImageStatusCreating = ImageStatus("Creating")
- ImageStatusCreateFailed = ImageStatus("CreateFailed")
- )
- 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
- }
- }
- type DiskDeviceMapping struct {
- SnapshotId string
-
- Size string
- Device string
- }
- type ImageType struct {
- ImageId string
- ImageVersion string
- Architecture string
- ImageName string
- Description string
- Size int
- ImageOwnerAlias string
- OSName string
- DiskDeviceMappings struct {
- DiskDeviceMapping []DiskDeviceMapping
- }
- ProductCode string
- IsSubscribed bool
- Progress string
- Status ImageStatus
- CreationTime util.ISO6801Time
- }
- func (client *Client) DescribeImages(args *DescribeImagesArgs) (images []ImageType, pagination *common.PaginationResult, err error) {
- args.Validate()
- response := DescribeImagesResponse{}
- err = client.Invoke("DescribeImages", args, &response)
- if err != nil {
- return nil, nil, err
- }
- return response.Images.Image, &response.PaginationResult, nil
- }
- type CreateImageArgs struct {
- RegionId common.Region
- SnapshotId string
- ImageName string
- ImageVersion string
- Description string
- ClientToken string
- }
- type CreateImageResponse struct {
- common.Response
- ImageId string
- }
- 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
- }
- type DeleteImageResponse struct {
- common.Response
- }
- func (client *Client) DeleteImage(regionId common.Region, imageId string) error {
- args := DeleteImageArgs{
- RegionId: regionId,
- ImageId: imageId,
- }
- response := &DeleteImageResponse{}
- return client.Invoke("DeleteImage", &args, &response)
- }
- type ModifyImageSharePermissionArgs struct {
- RegionId common.Region
- ImageId string
- AddAccount []string
- RemoveAccount []string
- }
- 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
- }
- 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
- }
- const ImageDefaultTimeout = 120
- 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
- }
- func (client *Client) CancelCopyImage(regionId common.Region, imageId string) error {
- response := &common.Response{}
- err := client.Invoke("CancelCopyImage", &CancelCopyImageRequest{regionId, imageId}, &response)
- return err
- }
|