snapshot.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 qcloud
  15. import (
  16. "fmt"
  17. "strconv"
  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 = "NORMAL"
  28. SnapshotStatusProgress SnapshotStatusType = "CREATING"
  29. SnapshotStatusFailed SnapshotStatusType = "failed"
  30. )
  31. type SSnapshot struct {
  32. multicloud.SResourceBase
  33. QcloudTags
  34. region *SRegion
  35. SnapshotId string // 快照ID。
  36. Placement Placement // 快照所在的位置。
  37. DiskUsage string // 创建此快照的云硬盘类型。取值范围:SYSTEM_DISK:系统盘 DATA_DISK:数据盘。
  38. DiskId string // 创建此快照的云硬盘ID。
  39. DiskSize int32 // 创建此快照的云硬盘大小,单位GB。
  40. SnapshotState SnapshotStatusType // 快照的状态。取值范围: NORMAL:正常 CREATING:创建中 ROLLBACKING:回滚中 COPYING_FROM_REMOTE:跨地域复制快照拷贝中。
  41. SnapshotName string // 快照名称,用户自定义的快照别名。调用ModifySnapshotAttribute可修改此字段。
  42. Percent int // 快照创建进度百分比,快照创建成功后此字段恒为100。
  43. CreateTime time.Time // 快照的创建时间。
  44. DeadlineTime time.Time // 快照到期时间。如果快照为永久保留,此字段为空。
  45. Encrypt bool // 是否为加密盘创建的快照。取值范围:true:该快照为加密盘创建的 false:非加密盘创建的快照。
  46. IsPermanent bool // 是否为永久快照。取值范围: true:永久快照 false:非永久快照。
  47. CopyingToRegions []string // 快照正在跨地域复制的目的地域,默认取值为[]。
  48. CopyFromRemote bool // 是否为跨地域复制的快照。取值范围:true:表示为跨地域复制的快照。 false:本地域的快照。
  49. }
  50. func (self *SRegion) GetISnapshotById(snapshotId string) (cloudprovider.ICloudSnapshot, error) {
  51. snapshots, total, err := self.GetSnapshots("", "", "", []string{snapshotId}, 0, 1)
  52. if err != nil {
  53. return nil, err
  54. }
  55. if total > 1 {
  56. return nil, cloudprovider.ErrDuplicateId
  57. }
  58. if total == 0 {
  59. return nil, cloudprovider.ErrNotFound
  60. }
  61. return &snapshots[0], nil
  62. }
  63. func (self *SSnapshot) GetStatus() string {
  64. // NORMAL:正常
  65. // CREATING:创建中
  66. // ROLLBACKING:回滚中
  67. // COPYING_FROM_REMOTE:跨地域复制快照拷贝中。
  68. switch self.SnapshotState {
  69. case "NORMAL", "COPYING_FROM_REMOTE":
  70. return api.SNAPSHOT_READY
  71. case "CREATING":
  72. return api.SNAPSHOT_CREATING
  73. case "ROLLBACKING":
  74. return api.SNAPSHOT_ROLLBACKING
  75. }
  76. return api.SNAPSHOT_UNKNOWN
  77. }
  78. func (self *SSnapshot) IsEmulated() bool {
  79. return false
  80. }
  81. func (self *SSnapshot) Refresh() error {
  82. snapshots, total, err := self.region.GetSnapshots("", "", "", []string{self.SnapshotId}, 0, 1)
  83. if err != nil {
  84. return err
  85. }
  86. if total > 1 {
  87. return cloudprovider.ErrDuplicateId
  88. }
  89. if total == 0 {
  90. return cloudprovider.ErrNotFound
  91. }
  92. return jsonutils.Update(self, snapshots[0])
  93. }
  94. func (self *SRegion) GetSnapshots(instanceId string, diskId string, snapshotName string, snapshotIds []string, offset int, limit int) ([]SSnapshot, int, error) {
  95. if limit > 50 || limit <= 0 {
  96. limit = 50
  97. }
  98. params := make(map[string]string)
  99. params["Limit"] = fmt.Sprintf("%d", limit)
  100. params["Offset"] = fmt.Sprintf("%d", offset)
  101. filter := 0
  102. if len(instanceId) > 0 {
  103. }
  104. if len(diskId) > 0 {
  105. params[fmt.Sprintf("Filters.%d.Name", filter)] = "disk-id"
  106. params[fmt.Sprintf("Filters.%d.Values.0", filter)] = diskId
  107. filter++
  108. }
  109. if len(snapshotName) > 0 {
  110. params[fmt.Sprintf("Filters.%d.Name", filter)] = "snapshot-name"
  111. params[fmt.Sprintf("Filters.%d.Values", filter)] = snapshotName
  112. filter++
  113. }
  114. if snapshotIds != nil && len(snapshotIds) > 0 {
  115. for index, snapshotId := range snapshotIds {
  116. params[fmt.Sprintf("SnapshotIds.%d", index)] = snapshotId
  117. }
  118. }
  119. snapshots := []SSnapshot{}
  120. body, err := self.cbsRequest("DescribeSnapshots", params)
  121. if err != nil {
  122. log.Errorf("GetSnapshots fail %s", err)
  123. return nil, 0, err
  124. }
  125. body.Unmarshal(&snapshots, "SnapshotSet")
  126. if err != nil {
  127. return nil, 0, err
  128. }
  129. total, _ := body.Float("TotalCount")
  130. for i := 0; i < len(snapshots); i++ {
  131. snapshots[i].region = self
  132. }
  133. return snapshots, int(total), nil
  134. }
  135. func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  136. snapshots, total, err := self.GetSnapshots("", "", "", []string{}, 0, 50)
  137. if err != nil {
  138. return nil, err
  139. }
  140. for len(snapshots) < total {
  141. var parts []SSnapshot
  142. parts, total, err = self.GetSnapshots("", "", "", []string{}, len(snapshots), 50)
  143. if err != nil {
  144. return nil, err
  145. }
  146. snapshots = append(snapshots, parts...)
  147. }
  148. ret := make([]cloudprovider.ICloudSnapshot, len(snapshots))
  149. for i := 0; i < len(snapshots); i++ {
  150. ret[i] = &snapshots[i]
  151. }
  152. return ret, nil
  153. }
  154. func (self *SSnapshot) GetRegionId() string {
  155. return self.region.GetId()
  156. }
  157. func (self *SSnapshot) GetSizeMb() int32 {
  158. return self.DiskSize * 1024
  159. }
  160. func (self *SSnapshot) GetDiskId() string {
  161. return self.DiskId
  162. }
  163. func (self *SSnapshot) GetId() string {
  164. return self.SnapshotId
  165. }
  166. func (self *SSnapshot) GetGlobalId() string {
  167. return fmt.Sprintf("%s", self.SnapshotId)
  168. }
  169. func (self *SSnapshot) GetName() string {
  170. return self.SnapshotName
  171. }
  172. func (self *SSnapshot) Delete() error {
  173. if self.region == nil {
  174. return fmt.Errorf("not init region for snapshot %s", self.SnapshotId)
  175. }
  176. return self.region.DeleteSnapshot(self.SnapshotId)
  177. }
  178. func (self *SSnapshot) GetDiskType() string {
  179. switch self.DiskUsage {
  180. case "SYSTEM_DISK":
  181. return api.DISK_TYPE_SYS
  182. case "DATA_DISK":
  183. return api.DISK_TYPE_DATA
  184. }
  185. return api.DISK_TYPE_DATA
  186. }
  187. func (self *SRegion) DeleteSnapshot(snapshotId string) error {
  188. params := map[string]string{"SnapshotIds.0": snapshotId}
  189. _, err := self.cbsRequest("DeleteSnapshots", params)
  190. return err
  191. }
  192. func (self *SRegion) CreateSnapshot(diskId, name, desc string) (string, error) {
  193. params := make(map[string]string)
  194. params["DiskId"] = diskId
  195. params["SnapshotName"] = name
  196. body, err := self.cbsRequest("CreateSnapshot", params)
  197. if err != nil {
  198. log.Errorf("CreateSnapshot fail %s", err)
  199. return "", err
  200. }
  201. return body.GetString("SnapshotId")
  202. }
  203. func (self *SSnapshot) GetProjectId() string {
  204. return strconv.Itoa(self.Placement.ProjectId)
  205. }