snapshot.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 ucloud
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. // https://docs.ucloud.cn/api/udisk-api/describe_udisk_snapshot
  24. type SSnapshot struct {
  25. multicloud.SResourceBase
  26. UcloudTags
  27. region *SRegion
  28. Comment string `json:"Comment"`
  29. ChargeType string `json:"ChargeType"`
  30. Name string `json:"Name"`
  31. UDiskName string `json:"UDiskName"`
  32. ExpiredTime int64 `json:"ExpiredTime"`
  33. UDiskID string `json:"UDiskId"`
  34. SnapshotID string `json:"SnapshotId"`
  35. CreateTime int64 `json:"CreateTime"`
  36. SizeGB int32 `json:"Size"`
  37. Status string `json:"Status"`
  38. IsUDiskAvailable bool `json:"IsUDiskAvailable"`
  39. Version string `json:"Version"`
  40. DiskType int `json:"DiskType"`
  41. UHostID string `json:"UHostId"`
  42. }
  43. func (self *SSnapshot) GetProjectId() string {
  44. return self.region.client.projectId
  45. }
  46. func (self *SSnapshot) GetId() string {
  47. return self.SnapshotID
  48. }
  49. func (self *SSnapshot) GetName() string {
  50. if len(self.Name) == 0 {
  51. return self.GetId()
  52. }
  53. return self.Name
  54. }
  55. func (self *SSnapshot) GetGlobalId() string {
  56. return self.GetId()
  57. }
  58. // 快照状态,Normal:正常,Failed:失败,Creating:制作中
  59. func (self *SSnapshot) GetStatus() string {
  60. switch self.Status {
  61. case "Normal":
  62. return api.SNAPSHOT_READY
  63. case "Failed":
  64. return api.SNAPSHOT_FAILED
  65. case "Creating":
  66. return api.SNAPSHOT_CREATING
  67. default:
  68. return api.SNAPSHOT_UNKNOWN
  69. }
  70. }
  71. func (self *SSnapshot) Refresh() error {
  72. disk, err := self.region.GetDisk(self.UDiskID)
  73. if err != nil {
  74. return err
  75. }
  76. snapshot, err := self.region.GetSnapshotById(disk.Zone, self.GetId())
  77. if err != nil {
  78. return err
  79. }
  80. if err := jsonutils.Update(self, snapshot); err != nil {
  81. return err
  82. }
  83. return nil
  84. }
  85. func (self *SSnapshot) IsEmulated() bool {
  86. return false
  87. }
  88. func (self *SSnapshot) GetSizeMb() int32 {
  89. return self.SizeGB * 1024
  90. }
  91. func (self *SSnapshot) GetDiskId() string {
  92. return self.UDiskID
  93. }
  94. // 磁盘类型,0:数据盘,1:系统盘
  95. func (self *SSnapshot) GetDiskType() string {
  96. if self.DiskType == 1 {
  97. return api.DISK_TYPE_SYS
  98. } else {
  99. return api.DISK_TYPE_DATA
  100. }
  101. }
  102. // https://docs.ucloud.cn/api/udisk-api/delete_udisk_snapshot
  103. func (self *SSnapshot) Delete() error {
  104. zoneId := ""
  105. idisk, err := self.region.GetDisk(self.UDiskID)
  106. if err == nil {
  107. zoneId = idisk.Zone
  108. } else if errors.Cause(err) == cloudprovider.ErrNotFound {
  109. zones, err := self.region.GetIZones()
  110. if err != nil {
  111. return errors.Wrap(err, "snapshot.Delete GetIZones")
  112. }
  113. for _, zone := range zones {
  114. if _, err := self.region.GetSnapshotById(zone.GetId(), self.GetId()); err == nil {
  115. zoneId = zone.GetId()
  116. break
  117. }
  118. }
  119. } else {
  120. return errors.Wrap(err, "snapshot.Delete")
  121. }
  122. if len(zoneId) == 0 {
  123. return fmt.Errorf("snapshot.Delete can not found snapshot %s zone id", self.GetId())
  124. }
  125. return self.region.DeleteSnapshot(self.GetId(), zoneId)
  126. }
  127. func (self *SRegion) GetSnapshotById(zoneId string, snapshotId string) (SSnapshot, error) {
  128. snapshots, err := self.GetSnapshots(zoneId, "", snapshotId)
  129. if err != nil {
  130. return SSnapshot{}, err
  131. }
  132. if len(snapshots) == 1 {
  133. return snapshots[0], nil
  134. } else if len(snapshots) == 0 {
  135. return SSnapshot{}, cloudprovider.ErrNotFound
  136. } else {
  137. return SSnapshot{}, fmt.Errorf("GetSnapshotById %s %d found", snapshotId, len(snapshots))
  138. }
  139. }
  140. func (self *SRegion) GetSnapshots(zoneId string, diskId string, snapshotId string) ([]SSnapshot, error) {
  141. params := NewUcloudParams()
  142. if len(diskId) > 0 {
  143. disk, err := self.GetDisk(diskId)
  144. if err != nil {
  145. return nil, err
  146. }
  147. params.Set("UDiskId", diskId)
  148. params.Set("Zone", disk.Zone)
  149. }
  150. if len(zoneId) > 0 {
  151. params.Set("Zone", zoneId)
  152. }
  153. if len(snapshotId) > 0 {
  154. params.Set("SnapshotId", snapshotId)
  155. }
  156. snapshots := make([]SSnapshot, 0)
  157. err := self.DoAction("DescribeUDiskSnapshot", params, &snapshots)
  158. if err != nil {
  159. return nil, err
  160. }
  161. for i := range snapshots {
  162. snapshots[i].region = self
  163. }
  164. return snapshots, nil
  165. }
  166. // https://docs.ucloud.cn/api/udisk-api/delete_udisk_snapshot
  167. func (self *SRegion) DeleteSnapshot(snapshotId string, zoneId string) error {
  168. params := NewUcloudParams()
  169. params.Set("SnapshotId", snapshotId)
  170. params.Set("Zone", zoneId)
  171. return self.DoAction("DeleteUDiskSnapshot", params, nil)
  172. }