snapshot.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 hcso
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/multicloud"
  20. "yunion.io/x/cloudmux/pkg/multicloud/huawei"
  21. )
  22. /*
  23. 限制:
  24. https://support.huaweicloud.com/api-evs/zh-cn_topic_0058762427.html
  25. 1. 从快照创建云硬盘时,volume_type字段必须和快照源云硬盘保持一致。
  26. 2. 当指定的云硬盘类型在avaliability_zone内不存在时,则创建云硬盘失败。
  27. */
  28. type SnapshotStatusType string
  29. const (
  30. SnapshotStatusCreating SnapshotStatusType = "creating"
  31. SnapshotStatusAvailable SnapshotStatusType = "available" // 云硬盘快照创建成功,可以使用。
  32. SnapshotStatusError SnapshotStatusType = "error" // 云硬盘快照在创建过程中出现错误。
  33. SnapshotStatusDeleting SnapshotStatusType = "deleting" // 云硬盘快照处于正在删除的过程中。
  34. SnapshotStatusErrorDeleting SnapshotStatusType = "error_deleting" // 云硬盘快照在删除过程中出现错误
  35. SnapshotStatusRollbacking SnapshotStatusType = "rollbacking" // 云硬盘快照处于正在回滚数据的过程中。
  36. SnapshotStatusBackingUp SnapshotStatusType = "backing-up" // 通过快照创建备份,快照状态就会变为backing-up
  37. )
  38. type Metadata struct {
  39. SystemEnableActive string `json:"__system__enableActive"` // 如果为true。则表明是系统盘快照
  40. }
  41. // https://support.huaweicloud.com/api-evs/zh-cn_topic_0051408624.html
  42. type SSnapshot struct {
  43. multicloud.SResourceBase
  44. huawei.HuaweiTags
  45. region *SRegion
  46. Metadata Metadata `json:"metadata"`
  47. CreatedAt string `json:"created_at"`
  48. Description string `json:"description"`
  49. ID string `json:"id"`
  50. Name string `json:"name"`
  51. OSExtendedSnapshotAttributesProgress string `json:"os-extended-snapshot-attributes:progress"`
  52. OSExtendedSnapshotAttributesProjectID string `json:"os-extended-snapshot-attributes:project_id"`
  53. Size int32 `json:"size"` // GB
  54. Status string `json:"status"`
  55. UpdatedAt string `json:"updated_at"`
  56. VolumeID string `json:"volume_id"`
  57. }
  58. func (self *SSnapshot) GetId() string {
  59. return self.ID
  60. }
  61. func (self *SSnapshot) GetName() string {
  62. return self.Name
  63. }
  64. func (self *SSnapshot) GetGlobalId() string {
  65. return self.ID
  66. }
  67. func (self *SSnapshot) GetStatus() string {
  68. switch SnapshotStatusType(self.Status) {
  69. case SnapshotStatusAvailable:
  70. return api.SNAPSHOT_READY
  71. case SnapshotStatusCreating:
  72. return api.SNAPSHOT_CREATING
  73. case SnapshotStatusDeleting:
  74. return api.SNAPSHOT_DELETING
  75. case SnapshotStatusErrorDeleting, SnapshotStatusError:
  76. return api.SNAPSHOT_FAILED
  77. case SnapshotStatusRollbacking:
  78. return api.SNAPSHOT_ROLLBACKING
  79. default:
  80. return api.SNAPSHOT_UNKNOWN
  81. }
  82. }
  83. func (self *SSnapshot) Refresh() error {
  84. snapshot, err := self.region.GetSnapshotById(self.GetId())
  85. if err != nil {
  86. return err
  87. }
  88. if err := jsonutils.Update(self, snapshot); err != nil {
  89. return err
  90. }
  91. return nil
  92. }
  93. func (self *SSnapshot) IsEmulated() bool {
  94. return false
  95. }
  96. func (self *SSnapshot) GetSizeMb() int32 {
  97. return self.Size * 1024
  98. }
  99. func (self *SSnapshot) GetDiskId() string {
  100. return self.VolumeID
  101. }
  102. func (self *SSnapshot) GetDiskType() string {
  103. if self.Metadata.SystemEnableActive == "true" {
  104. return api.DISK_TYPE_SYS
  105. } else {
  106. return api.DISK_TYPE_DATA
  107. }
  108. }
  109. func (self *SSnapshot) Delete() error {
  110. if self.region == nil {
  111. return fmt.Errorf("not init region for snapshot %s", self.GetId())
  112. }
  113. return self.region.DeleteSnapshot(self.GetId())
  114. }
  115. // https://support.huaweicloud.com/api-evs/zh-cn_topic_0051408627.html
  116. func (self *SRegion) GetSnapshots(diskId string, snapshotName string) ([]SSnapshot, error) {
  117. params := make(map[string]string)
  118. if len(diskId) > 0 {
  119. params["volume_id"] = diskId
  120. }
  121. if len(snapshotName) > 0 {
  122. params["name"] = snapshotName
  123. }
  124. snapshots := make([]SSnapshot, 0)
  125. err := doListAllWithOffset(self.ecsClient.Snapshots.List, params, &snapshots)
  126. for i := range snapshots {
  127. snapshots[i].region = self
  128. }
  129. return snapshots, err
  130. }
  131. func (self *SRegion) GetSnapshotById(snapshotId string) (SSnapshot, error) {
  132. var snapshot SSnapshot
  133. err := DoGet(self.ecsClient.Snapshots.Get, snapshotId, nil, &snapshot)
  134. snapshot.region = self
  135. return snapshot, err
  136. }
  137. // 不能删除以autobk_snapshot_为前缀的快照。
  138. // 当快照状态为available、error状态时,才可以删除。
  139. func (self *SRegion) DeleteSnapshot(snapshotId string) error {
  140. return DoDelete(self.ecsClient.Snapshots.Delete, snapshotId, nil, nil)
  141. }
  142. // https://support.huaweicloud.com/api-evs/zh-cn_topic_0051408624.html
  143. // 目前已设置force字段。云硬盘处于挂载状态时,能强制创建快照。
  144. func (self *SRegion) CreateSnapshot(diskId, name, desc string) (string, error) {
  145. params := jsonutils.NewDict()
  146. snapshotObj := jsonutils.NewDict()
  147. snapshotObj.Add(jsonutils.NewString(name), "name")
  148. snapshotObj.Add(jsonutils.NewString(desc), "description")
  149. snapshotObj.Add(jsonutils.NewString(diskId), "volume_id")
  150. snapshotObj.Add(jsonutils.JSONTrue, "force")
  151. params.Add(snapshotObj, "snapshot")
  152. snapshot := SSnapshot{}
  153. err := DoCreate(self.ecsClient.Snapshots.Create, params, &snapshot)
  154. return snapshot.ID, err
  155. }
  156. func (self *SSnapshot) GetProjectId() string {
  157. return ""
  158. }