snapshot.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 apsara
  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. ApsaraTags
  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. DepartmentInfo
  46. }
  47. func (self *SSnapshot) GetId() string {
  48. return self.SnapshotId
  49. }
  50. func (self *SSnapshot) GetName() string {
  51. return self.SnapshotName
  52. }
  53. func (self *SSnapshot) GetStatus() string {
  54. if self.Status == SnapshotStatusAccomplished {
  55. return api.SNAPSHOT_READY
  56. } else if self.Status == SnapshotStatusProgress {
  57. return api.SNAPSHOT_CREATING
  58. } else { // if self.Status == SnapshotStatusFailed
  59. return api.SNAPSHOT_FAILED
  60. }
  61. }
  62. func (self *SSnapshot) GetSizeMb() int32 {
  63. return self.SourceDiskSize * 1024
  64. }
  65. func (self *SSnapshot) GetDiskId() string {
  66. return self.SourceDiskId
  67. }
  68. func (self *SSnapshot) GetDiskType() string {
  69. if self.SourceDiskType == SnapshotTypeSystem {
  70. return api.DISK_TYPE_SYS
  71. } else if self.SourceDiskType == SnapshotTypeData {
  72. return api.DISK_TYPE_DATA
  73. } else {
  74. return ""
  75. }
  76. }
  77. func (self *SSnapshot) Refresh() error {
  78. if snapshots, total, err := self.region.GetSnapshots("", "", "", []string{self.SnapshotId}, 0, 1); err != nil {
  79. return err
  80. } else if total != 1 {
  81. return cloudprovider.ErrNotFound
  82. } else if err := jsonutils.Update(self, snapshots[0]); err != nil {
  83. return err
  84. }
  85. return nil
  86. }
  87. func (self *SSnapshot) GetGlobalId() string {
  88. return fmt.Sprintf("%s", self.SnapshotId)
  89. }
  90. func (self *SSnapshot) IsEmulated() bool {
  91. return false
  92. }
  93. func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  94. snapshots, total, err := self.GetSnapshots("", "", "", []string{}, 0, 50)
  95. if err != nil {
  96. return nil, err
  97. }
  98. for len(snapshots) < total {
  99. var parts []SSnapshot
  100. parts, total, err = self.GetSnapshots("", "", "", []string{}, len(snapshots), 50)
  101. if err != nil {
  102. return nil, err
  103. }
  104. snapshots = append(snapshots, parts...)
  105. }
  106. ret := make([]cloudprovider.ICloudSnapshot, len(snapshots))
  107. for i := 0; i < len(snapshots); i += 1 {
  108. ret[i] = &snapshots[i]
  109. }
  110. return ret, nil
  111. }
  112. func (self *SSnapshot) Delete() error {
  113. if self.region == nil {
  114. return fmt.Errorf("not init region for snapshot %s", self.SnapshotId)
  115. }
  116. if err := self.region.SnapshotPreDelete(self.SnapshotId); err != nil {
  117. return err
  118. }
  119. return self.region.DeleteSnapshot(self.SnapshotId)
  120. }
  121. func (self *SRegion) GetSnapshots(instanceId string, diskId string, snapshotName string, snapshotIds []string, offset int, limit int) ([]SSnapshot, int, error) {
  122. if limit > 50 || limit <= 0 {
  123. limit = 50
  124. }
  125. params := make(map[string]string)
  126. params["RegionId"] = self.RegionId
  127. params["PageSize"] = fmt.Sprintf("%d", limit)
  128. params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
  129. if len(instanceId) > 0 {
  130. params["InstanceId"] = instanceId
  131. }
  132. if len(diskId) > 0 {
  133. params["diskId"] = diskId
  134. }
  135. if len(snapshotName) > 0 {
  136. params["SnapshotName"] = snapshotName
  137. }
  138. if snapshotIds != nil && len(snapshotIds) > 0 {
  139. params["SnapshotIds"] = jsonutils.Marshal(snapshotIds).String()
  140. }
  141. body, err := self.ecsRequest("DescribeSnapshots", params)
  142. if err != nil {
  143. log.Errorf("GetSnapshots fail %s", err)
  144. return nil, 0, err
  145. }
  146. snapshots := make([]SSnapshot, 0)
  147. if err := body.Unmarshal(&snapshots, "Snapshots", "Snapshot"); err != nil {
  148. log.Errorf("Unmarshal snapshot details fail %s", err)
  149. return nil, 0, err
  150. }
  151. total, _ := body.Int("TotalCount")
  152. for i := 0; i < len(snapshots); i += 1 {
  153. snapshots[i].region = self
  154. }
  155. return snapshots, int(total), nil
  156. }
  157. func (self *SRegion) GetISnapshotById(snapshotId string) (cloudprovider.ICloudSnapshot, error) {
  158. snapshots, total, err := self.GetSnapshots("", "", "", []string{snapshotId}, 0, 1)
  159. if err != nil {
  160. return nil, err
  161. }
  162. if total == 0 {
  163. return nil, cloudprovider.ErrNotFound
  164. } else if total > 1 {
  165. return nil, cloudprovider.ErrDuplicateId
  166. }
  167. return &snapshots[0], nil
  168. }
  169. func (self *SRegion) DeleteSnapshot(snapshotId string) error {
  170. params := make(map[string]string)
  171. params["SnapshotId"] = snapshotId
  172. _, err := self.ecsRequest("DeleteSnapshot", params)
  173. return err
  174. }
  175. // If snapshot linked images can't be delete
  176. // delete images first -- Apsara
  177. func (self *SRegion) SnapshotPreDelete(snapshotId string) error {
  178. images, _, err := self.GetImagesBySnapshot(snapshotId, 0, 0)
  179. if err != nil {
  180. return fmt.Errorf("PreDelete get images by snapshot %s error: %s", snapshotId, err)
  181. }
  182. for _, image := range images {
  183. image.storageCache = &SStoragecache{region: self}
  184. if err := image.Delete(context.Background()); err != nil {
  185. return fmt.Errorf("PreDelete image %s error: %s", image.GetId(), err)
  186. }
  187. if err := cloudprovider.WaitDeleted(&image, 3*time.Second, 300*time.Second); err != nil {
  188. return fmt.Errorf("PreDelete waite image %s deleted error: %s", image.GetId(), err)
  189. }
  190. }
  191. return nil
  192. }