snapshot.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 huawei
  15. import (
  16. "fmt"
  17. "net/url"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. /*
  24. 限制:
  25. https://support.huaweicloud.com/api-evs/zh-cn_topic_0058762427.html
  26. 1. 从快照创建云硬盘时,volume_type字段必须和快照源云硬盘保持一致。
  27. 2. 当指定的云硬盘类型在avaliability_zone内不存在时,则创建云硬盘失败。
  28. */
  29. type SnapshotStatusType string
  30. const (
  31. SnapshotStatusCreating SnapshotStatusType = "creating"
  32. SnapshotStatusAvailable SnapshotStatusType = "available" // 云硬盘快照创建成功,可以使用。
  33. SnapshotStatusError SnapshotStatusType = "error" // 云硬盘快照在创建过程中出现错误。
  34. SnapshotStatusDeleting SnapshotStatusType = "deleting" // 云硬盘快照处于正在删除的过程中。
  35. SnapshotStatusErrorDeleting SnapshotStatusType = "error_deleting" // 云硬盘快照在删除过程中出现错误
  36. SnapshotStatusRollbacking SnapshotStatusType = "rollbacking" // 云硬盘快照处于正在回滚数据的过程中。
  37. SnapshotStatusBackingUp SnapshotStatusType = "backing-up" // 通过快照创建备份,快照状态就会变为backing-up
  38. )
  39. type Metadata struct {
  40. SystemEnableActive string `json:"__system__enableActive"` // 如果为true。则表明是系统盘快照
  41. }
  42. type SSnapshot struct {
  43. multicloud.SResourceBase
  44. 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.GetSnapshot(self.GetId())
  85. if err != nil {
  86. return err
  87. }
  88. return jsonutils.Update(self, snapshot)
  89. }
  90. func (self *SSnapshot) IsEmulated() bool {
  91. return false
  92. }
  93. func (self *SSnapshot) GetSizeMb() int32 {
  94. return self.Size * 1024
  95. }
  96. func (self *SSnapshot) GetDiskId() string {
  97. return self.VolumeID
  98. }
  99. func (self *SSnapshot) GetDiskType() string {
  100. if self.Metadata.SystemEnableActive == "true" {
  101. return api.DISK_TYPE_SYS
  102. } else {
  103. return api.DISK_TYPE_DATA
  104. }
  105. }
  106. func (self *SSnapshot) Delete() error {
  107. if self.region == nil {
  108. return fmt.Errorf("not init region for snapshot %s", self.GetId())
  109. }
  110. return self.region.DeleteSnapshot(self.GetId())
  111. }
  112. func (self *SRegion) GetSnapshots(diskId string, snapshotName string) ([]SSnapshot, error) {
  113. params := url.Values{}
  114. if len(diskId) > 0 {
  115. params.Set("volume_id", diskId)
  116. }
  117. if len(snapshotName) > 0 {
  118. params.Set("name", snapshotName)
  119. }
  120. ret := []SSnapshot{}
  121. for {
  122. resp, err := self.list(SERVICE_EVS, "cloudsnapshots/detail", params)
  123. if err != nil {
  124. return nil, err
  125. }
  126. part := struct {
  127. Snapshots []SSnapshot
  128. Count int
  129. }{}
  130. err = resp.Unmarshal(&part)
  131. if err != nil {
  132. return nil, err
  133. }
  134. ret = append(ret, part.Snapshots...)
  135. if len(ret) >= part.Count || len(part.Snapshots) == 0 {
  136. break
  137. }
  138. params.Set("offset", fmt.Sprintf("%d", len(ret)))
  139. }
  140. return ret, nil
  141. }
  142. func (self *SRegion) GetSnapshot(id string) (*SSnapshot, error) {
  143. resp, err := self.list(SERVICE_EVS, "cloudsnapshots/"+id, nil)
  144. if err != nil {
  145. return nil, err
  146. }
  147. ret := &SSnapshot{region: self}
  148. err = resp.Unmarshal(ret, "snapshot")
  149. if err != nil {
  150. return nil, errors.Wrapf(err, "Unmarshal")
  151. }
  152. return ret, nil
  153. }
  154. func (self *SRegion) DeleteSnapshot(id string) error {
  155. _, err := self.delete(SERVICE_EVS, "cloudsnapshots/"+id)
  156. return err
  157. }
  158. func (self *SRegion) CreateSnapshot(diskId, name, desc string) (*SSnapshot, error) {
  159. params := map[string]interface{}{
  160. "name": name,
  161. "description": desc,
  162. "volume_id": diskId,
  163. "force": true,
  164. }
  165. resp, err := self.post(SERVICE_EVS, "cloudsnapshots", map[string]interface{}{"snapshot": params})
  166. if err != nil {
  167. return nil, err
  168. }
  169. ret := &SSnapshot{region: self}
  170. err = resp.Unmarshal(ret, "snapshot")
  171. if err != nil {
  172. return nil, errors.Wrapf(err, "Unmarshal")
  173. }
  174. return ret, nil
  175. }
  176. func (self *SSnapshot) GetProjectId() string {
  177. return ""
  178. }