| 1 |
package ecs |
| 2 |
|
| 3 |
import ( |
| 4 |
"time" |
| 5 |
|
| 6 |
"github.com/denverdino/aliyungo/common" |
| 7 |
"github.com/denverdino/aliyungo/util" |
| 8 |
) |
| 9 |
|
| 10 |
type DescribeSnapshotsArgs struct { |
| 11 |
RegionId common.Region |
| 12 |
InstanceId string |
| 13 |
DiskId string |
| 14 |
SnapshotIds []string |
| 15 |
common.Pagination |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
type SnapshotType struct { |
| 21 |
SnapshotId string |
| 22 |
SnapshotName string |
| 23 |
Description string |
| 24 |
Progress string |
| 25 |
SourceDiskId string |
| 26 |
SourceDiskSize int |
| 27 |
SourceDiskType string |
| 28 |
ProductCode string |
| 29 |
Status string |
| 30 |
Usage string |
| 31 |
CreationTime util.ISO6801Time |
| 32 |
} |
| 33 |
|
| 34 |
type DescribeSnapshotsResponse struct { |
| 35 |
common.Response |
| 36 |
common.PaginationResult |
| 37 |
Snapshots struct { |
| 38 |
Snapshot []SnapshotType |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
func (client *Client) DescribeSnapshots(args *DescribeSnapshotsArgs) (snapshots []SnapshotType, pagination *common.PaginationResult, err error) { |
| 46 |
response, err := client.DescribeSnapshotsWithRaw(args) |
| 47 |
if err != nil { |
| 48 |
return nil, nil, err |
| 49 |
} |
| 50 |
return response.Snapshots.Snapshot, &response.PaginationResult, nil |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
func (client *Client) DescribeSnapshotsWithRaw(args *DescribeSnapshotsArgs) (response *DescribeSnapshotsResponse, err error) { |
| 55 |
args.Validate() |
| 56 |
response = &DescribeSnapshotsResponse{} |
| 57 |
|
| 58 |
err = client.Invoke("DescribeSnapshots", args, response) |
| 59 |
|
| 60 |
if err != nil { |
| 61 |
return nil, err |
| 62 |
} |
| 63 |
return response, nil |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
type DeleteSnapshotArgs struct { |
| 68 |
SnapshotId string |
| 69 |
} |
| 70 |
|
| 71 |
type DeleteSnapshotResponse struct { |
| 72 |
common.Response |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
func (client *Client) DeleteSnapshot(snapshotId string) error { |
| 79 |
args := DeleteSnapshotArgs{SnapshotId: snapshotId} |
| 80 |
response := DeleteSnapshotResponse{} |
| 81 |
|
| 82 |
return client.Invoke("DeleteSnapshot", &args, &response) |
| 83 |
} |
| 84 |
|
| 85 |
type CreateSnapshotArgs struct { |
| 86 |
DiskId string |
| 87 |
SnapshotName string |
| 88 |
Description string |
| 89 |
ClientToken string |
| 90 |
} |
| 91 |
|
| 92 |
type CreateSnapshotResponse struct { |
| 93 |
common.Response |
| 94 |
SnapshotId string |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
func (client *Client) CreateSnapshot(args *CreateSnapshotArgs) (snapshotId string, err error) { |
| 101 |
|
| 102 |
response := CreateSnapshotResponse{} |
| 103 |
|
| 104 |
err = client.Invoke("CreateSnapshot", args, &response) |
| 105 |
if err == nil { |
| 106 |
snapshotId = response.SnapshotId |
| 107 |
} |
| 108 |
return snapshotId, err |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
const SnapshotDefaultTimeout = 120 |
| 113 |
|
| 114 |
|
| 115 |
func (client *Client) WaitForSnapShotReady(regionId common.Region, snapshotId string, timeout int) error { |
| 116 |
if timeout <= 0 { |
| 117 |
timeout = SnapshotDefaultTimeout |
| 118 |
} |
| 119 |
for { |
| 120 |
args := DescribeSnapshotsArgs{ |
| 121 |
RegionId: regionId, |
| 122 |
SnapshotIds: []string{snapshotId}, |
| 123 |
} |
| 124 |
|
| 125 |
snapshots, _, err := client.DescribeSnapshots(&args) |
| 126 |
if err != nil { |
| 127 |
return err |
| 128 |
} |
| 129 |
if snapshots == nil || len(snapshots) == 0 { |
| 130 |
return common.GetClientErrorFromString("Not found") |
| 131 |
} |
| 132 |
if snapshots[0].Progress == "100%" { |
| 133 |
break |
| 134 |
} |
| 135 |
timeout = timeout - DefaultWaitForInterval |
| 136 |
if timeout <= 0 { |
| 137 |
return common.GetClientErrorFromString("Timeout") |
| 138 |
} |
| 139 |
time.Sleep(DefaultWaitForInterval * time.Second) |
| 140 |
} |
| 141 |
return nil |
| 142 |
} |