snapshots.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package ecs
  2. import (
  3. "time"
  4. "github.com/denverdino/aliyungo/common"
  5. "github.com/denverdino/aliyungo/util"
  6. )
  7. type DescribeSnapshotsArgs struct {
  8. RegionId common.Region
  9. InstanceId string
  10. DiskId string
  11. SnapshotIds []string //["s-xxxxxxxxx", "s-yyyyyyyyy", ..."s-zzzzzzzzz"]
  12. common.Pagination
  13. }
  14. //
  15. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&snapshottype
  16. type SnapshotType struct {
  17. SnapshotId string
  18. SnapshotName string
  19. Description string
  20. Progress string
  21. SourceDiskId string
  22. SourceDiskSize int
  23. SourceDiskType string //enum for System | Data
  24. ProductCode string
  25. CreationTime util.ISO6801Time
  26. }
  27. type DescribeSnapshotsResponse struct {
  28. common.Response
  29. common.PaginationResult
  30. Snapshots struct {
  31. Snapshot []SnapshotType
  32. }
  33. }
  34. // DescribeSnapshots describe snapshots
  35. //
  36. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/snapshot&describesnapshots
  37. func (client *Client) DescribeSnapshots(args *DescribeSnapshotsArgs) (snapshots []SnapshotType, pagination *common.PaginationResult, err error) {
  38. args.Validate()
  39. response := DescribeSnapshotsResponse{}
  40. err = client.Invoke("DescribeSnapshots", args, &response)
  41. if err != nil {
  42. return nil, nil, err
  43. }
  44. return response.Snapshots.Snapshot, &response.PaginationResult, nil
  45. }
  46. type DeleteSnapshotArgs struct {
  47. SnapshotId string
  48. }
  49. type DeleteSnapshotResponse struct {
  50. common.Response
  51. }
  52. // DeleteSnapshot deletes snapshot
  53. //
  54. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/snapshot&deletesnapshot
  55. func (client *Client) DeleteSnapshot(snapshotId string) error {
  56. args := DeleteSnapshotArgs{SnapshotId: snapshotId}
  57. response := DeleteSnapshotResponse{}
  58. return client.Invoke("DeleteSnapshot", &args, &response)
  59. }
  60. type CreateSnapshotArgs struct {
  61. DiskId string
  62. SnapshotName string
  63. Description string
  64. ClientToken string
  65. }
  66. type CreateSnapshotResponse struct {
  67. common.Response
  68. SnapshotId string
  69. }
  70. // CreateSnapshot creates a new snapshot
  71. //
  72. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/snapshot&createsnapshot
  73. func (client *Client) CreateSnapshot(args *CreateSnapshotArgs) (snapshotId string, err error) {
  74. response := CreateSnapshotResponse{}
  75. err = client.Invoke("CreateSnapshot", args, &response)
  76. if err == nil {
  77. snapshotId = response.SnapshotId
  78. }
  79. return snapshotId, err
  80. }
  81. // Default timeout value for WaitForSnapShotReady method
  82. const SnapshotDefaultTimeout = 120
  83. // WaitForSnapShotReady waits for snapshot ready
  84. func (client *Client) WaitForSnapShotReady(regionId common.Region, snapshotId string, timeout int) error {
  85. if timeout <= 0 {
  86. timeout = SnapshotDefaultTimeout
  87. }
  88. for {
  89. args := DescribeSnapshotsArgs{
  90. RegionId: regionId,
  91. SnapshotIds: []string{snapshotId},
  92. }
  93. snapshots, _, err := client.DescribeSnapshots(&args)
  94. if err != nil {
  95. return err
  96. }
  97. if snapshots == nil || len(snapshots) == 0 {
  98. return common.GetClientErrorFromString("Not found")
  99. }
  100. if snapshots[0].Progress == "100%" {
  101. break
  102. }
  103. timeout = timeout - DefaultWaitForInterval
  104. if timeout <= 0 {
  105. return common.GetClientErrorFromString("Timeout")
  106. }
  107. time.Sleep(DefaultWaitForInterval * time.Second)
  108. }
  109. return nil
  110. }