snapshot.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package aliyun
  15. import (
  16. "context"
  17. "fmt"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. api "yunion.io/x/cloudmux/pkg/apis/compute"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. "yunion.io/x/cloudmux/pkg/multicloud"
  24. )
  25. type SnapshotStatusType string
  26. const (
  27. SnapshotStatusAccomplished SnapshotStatusType = "accomplished"
  28. SnapshotStatusProgress SnapshotStatusType = "progressing"
  29. SnapshotStatusFailed SnapshotStatusType = "failed"
  30. SnapshotTypeSystem string = "System"
  31. SnapshotTypeData string = "Data"
  32. )
  33. type SSnapshot struct {
  34. multicloud.SResourceBase
  35. AliyunTags
  36. region *SRegion
  37. Progress string
  38. SnapshotId string
  39. SnapshotName string
  40. SourceDiskId string
  41. SourceDiskSize int32
  42. SourceDiskType string
  43. Status SnapshotStatusType
  44. Usage string
  45. ResourceGroupId string
  46. }
  47. func (self *SSnapshot) GetId() string {
  48. return self.SnapshotId
  49. }
  50. func (self *SSnapshot) GetName() string {
  51. if len(self.SnapshotName) > 0 {
  52. return self.SnapshotName
  53. }
  54. return self.SnapshotId
  55. }
  56. func (self *SSnapshot) GetStatus() string {
  57. if self.Status == SnapshotStatusAccomplished {
  58. return api.SNAPSHOT_READY
  59. } else if self.Status == SnapshotStatusProgress {
  60. return api.SNAPSHOT_CREATING
  61. } else { // if self.Status == SnapshotStatusFailed
  62. return api.SNAPSHOT_FAILED
  63. }
  64. }
  65. func (self *SSnapshot) GetSizeMb() int32 {
  66. return self.SourceDiskSize * 1024
  67. }
  68. func (self *SSnapshot) GetDiskId() string {
  69. return self.SourceDiskId
  70. }
  71. func (self *SSnapshot) GetDiskType() string {
  72. if self.SourceDiskType == SnapshotTypeSystem {
  73. return api.DISK_TYPE_SYS
  74. } else if self.SourceDiskType == SnapshotTypeData {
  75. return api.DISK_TYPE_DATA
  76. } else {
  77. return ""
  78. }
  79. }
  80. func (self *SSnapshot) Refresh() error {
  81. if snapshots, total, err := self.region.GetSnapshots("", "", "", []string{self.SnapshotId}, 0, 1); err != nil {
  82. return err
  83. } else if total != 1 {
  84. return cloudprovider.ErrNotFound
  85. } else if err := jsonutils.Update(self, snapshots[0]); err != nil {
  86. return err
  87. }
  88. return nil
  89. }
  90. func (self *SSnapshot) GetGlobalId() string {
  91. return fmt.Sprintf("%s", self.SnapshotId)
  92. }
  93. func (self *SSnapshot) IsEmulated() bool {
  94. return false
  95. }
  96. func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  97. snapshots, total, err := self.GetSnapshots("", "", "", []string{}, 0, 50)
  98. if err != nil {
  99. return nil, err
  100. }
  101. for len(snapshots) < total {
  102. var parts []SSnapshot
  103. parts, total, err = self.GetSnapshots("", "", "", []string{}, len(snapshots), 50)
  104. if err != nil {
  105. return nil, err
  106. }
  107. snapshots = append(snapshots, parts...)
  108. }
  109. ret := make([]cloudprovider.ICloudSnapshot, len(snapshots))
  110. for i := 0; i < len(snapshots); i += 1 {
  111. ret[i] = &snapshots[i]
  112. }
  113. return ret, nil
  114. }
  115. func (self *SSnapshot) Delete() error {
  116. if self.region == nil {
  117. return fmt.Errorf("not init region for snapshot %s", self.SnapshotId)
  118. }
  119. if err := self.region.SnapshotPreDelete(self.SnapshotId); err != nil {
  120. return err
  121. }
  122. return self.region.DeleteSnapshot(self.SnapshotId)
  123. }
  124. func (self *SRegion) GetSnapshots(instanceId string, diskId string, snapshotName string, snapshotIds []string, offset int, limit int) ([]SSnapshot, int, error) {
  125. if limit > 50 || limit <= 0 {
  126. limit = 50
  127. }
  128. params := make(map[string]string)
  129. params["RegionId"] = self.RegionId
  130. params["PageSize"] = fmt.Sprintf("%d", limit)
  131. params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
  132. if len(instanceId) > 0 {
  133. params["InstanceId"] = instanceId
  134. }
  135. if len(diskId) > 0 {
  136. params["diskId"] = diskId
  137. }
  138. if len(snapshotName) > 0 {
  139. params["SnapshotName"] = snapshotName
  140. }
  141. if snapshotIds != nil && len(snapshotIds) > 0 {
  142. params["SnapshotIds"] = jsonutils.Marshal(snapshotIds).String()
  143. }
  144. body, err := self.ecsRequest("DescribeSnapshots", params)
  145. if err != nil {
  146. log.Errorf("GetSnapshots fail %s", err)
  147. return nil, 0, err
  148. }
  149. snapshots := make([]SSnapshot, 0)
  150. if err := body.Unmarshal(&snapshots, "Snapshots", "Snapshot"); err != nil {
  151. log.Errorf("Unmarshal snapshot details fail %s", err)
  152. return nil, 0, err
  153. }
  154. total, _ := body.Int("TotalCount")
  155. for i := 0; i < len(snapshots); i += 1 {
  156. snapshots[i].region = self
  157. }
  158. return snapshots, int(total), nil
  159. }
  160. func (self *SRegion) GetISnapshotById(snapshotId string) (cloudprovider.ICloudSnapshot, error) {
  161. snapshots, total, err := self.GetSnapshots("", "", "", []string{snapshotId}, 0, 1)
  162. if err != nil {
  163. return nil, err
  164. }
  165. if total == 0 {
  166. return nil, cloudprovider.ErrNotFound
  167. } else if total > 1 {
  168. return nil, cloudprovider.ErrDuplicateId
  169. }
  170. return &snapshots[0], nil
  171. }
  172. func (self *SRegion) DeleteSnapshot(snapshotId string) error {
  173. params := make(map[string]string)
  174. params["SnapshotId"] = snapshotId
  175. _, err := self.ecsRequest("DeleteSnapshot", params)
  176. return err
  177. }
  178. func (self *SSnapshot) GetProjectId() string {
  179. return self.ResourceGroupId
  180. }
  181. // If snapshot linked images can't be delete
  182. // delete images first -- Aliyun
  183. func (self *SRegion) SnapshotPreDelete(snapshotId string) error {
  184. images, _, err := self.GetImagesBySnapshot(snapshotId, 0, 0)
  185. if err != nil {
  186. return fmt.Errorf("PreDelete get images by snapshot %s error: %s", snapshotId, err)
  187. }
  188. for _, image := range images {
  189. image.storageCache = &SStoragecache{region: self}
  190. if err := image.Delete(context.Background()); err != nil {
  191. return fmt.Errorf("PreDelete image %s error: %s", image.GetId(), err)
  192. }
  193. if err := cloudprovider.WaitDeleted(&image, 3*time.Second, 300*time.Second); err != nil {
  194. return fmt.Errorf("PreDelete waite image %s deleted error: %s", image.GetId(), err)
  195. }
  196. }
  197. return nil
  198. }