snapshot.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 volcengine
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/utils"
  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 SSnapshot struct {
  26. multicloud.SResourceBase
  27. VolcEngineTags
  28. region *SRegion
  29. SnapshotId string
  30. ZoneId string
  31. VolumeId string
  32. Status string
  33. SnapshotName string
  34. Description string
  35. CreationTime time.Time
  36. SnapshotType string
  37. VolumeType string
  38. VolumeKind string
  39. VolumeName string
  40. VolumeStatus string
  41. RetentionDays int
  42. ProjectName string
  43. Progress int
  44. SnapshotGroupId string
  45. ImageId string
  46. VolumeSize int32
  47. }
  48. func (self *SSnapshot) GetId() string {
  49. return self.SnapshotId
  50. }
  51. func (self *SSnapshot) GetName() string {
  52. return self.SnapshotName
  53. }
  54. // available creating rollbacking deleting failed
  55. func (self *SSnapshot) GetStatus() string {
  56. switch self.Status {
  57. case "available":
  58. return api.SNAPSHOT_READY
  59. case "creating":
  60. return api.SNAPSHOT_CREATING
  61. case "failed":
  62. return api.SNAPSHOT_FAILED
  63. case "rollbacking":
  64. return api.SNAPSHOT_ROLLBACKING
  65. case "deleting":
  66. return api.SNAPSHOT_DELETING
  67. default:
  68. return self.Status
  69. }
  70. }
  71. func (self *SSnapshot) GetSizeMb() int32 {
  72. return self.VolumeSize * 1024
  73. }
  74. func (self *SSnapshot) GetDiskId() string {
  75. return self.VolumeId
  76. }
  77. func (self *SSnapshot) GetDiskType() string {
  78. if self.VolumeKind == "system" {
  79. return api.DISK_TYPE_SYS
  80. }
  81. return api.DISK_TYPE_DATA
  82. }
  83. func (self *SSnapshot) Refresh() error {
  84. snapshot, err := self.region.GetSnapshot(self.SnapshotId)
  85. if err != nil {
  86. return err
  87. }
  88. return jsonutils.Update(self, snapshot)
  89. }
  90. func (self *SSnapshot) GetGlobalId() string {
  91. return self.SnapshotId
  92. }
  93. func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  94. snapshots, err := self.GetSnapshots("", "", nil)
  95. if err != nil {
  96. return nil, err
  97. }
  98. ret := make([]cloudprovider.ICloudSnapshot, len(snapshots))
  99. for i := 0; i < len(snapshots); i += 1 {
  100. snapshots[i].region = self
  101. ret[i] = &snapshots[i]
  102. }
  103. return ret, nil
  104. }
  105. func (self *SSnapshot) Delete() error {
  106. return self.region.DeleteSnapshot(self.SnapshotId)
  107. }
  108. func (self *SRegion) GetSnapshots(diskId string, snapshotName string, snapshotIds []string) ([]SSnapshot, error) {
  109. params := make(map[string]string)
  110. params["PageSize"] = "100"
  111. pageNum := 1
  112. if len(diskId) > 0 {
  113. params["VolumeId"] = diskId
  114. }
  115. if len(snapshotName) > 0 {
  116. params["SnapshotName"] = snapshotName
  117. }
  118. for i, id := range snapshotIds {
  119. params[fmt.Sprintf("SnapshotIds.%d", i+1)] = id
  120. }
  121. ret := []SSnapshot{}
  122. for {
  123. params["PageNumber"] = fmt.Sprintf("%d", pageNum)
  124. resp, err := self.storageRequest("DescribeSnapshots", params)
  125. if err != nil {
  126. return nil, errors.Wrapf(err, "DescribeSnapshots")
  127. }
  128. part := struct {
  129. Snapshots []SSnapshot
  130. TotalCount int64
  131. }{}
  132. err = resp.Unmarshal(&part)
  133. if err != nil {
  134. return nil, err
  135. }
  136. ret = append(ret, part.Snapshots...)
  137. if len(part.Snapshots) == 0 || len(ret) >= int(part.TotalCount) {
  138. break
  139. }
  140. pageNum++
  141. }
  142. return ret, nil
  143. }
  144. func (self *SRegion) GetSnapshot(id string) (*SSnapshot, error) {
  145. snapshots, err := self.GetSnapshots("", "", []string{id})
  146. if err != nil {
  147. return nil, err
  148. }
  149. for i := range snapshots {
  150. snapshots[i].region = self
  151. if snapshots[i].SnapshotId == id {
  152. return &snapshots[i], nil
  153. }
  154. }
  155. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  156. }
  157. func (self *SRegion) GetISnapshotById(id string) (cloudprovider.ICloudSnapshot, error) {
  158. snapshot, err := self.GetSnapshot(id)
  159. if err != nil {
  160. return nil, err
  161. }
  162. return snapshot, nil
  163. }
  164. func (self *SRegion) DeleteSnapshot(snapshotId string) error {
  165. params := make(map[string]string)
  166. params["SnapshotId"] = snapshotId
  167. params["ClientToken"] = utils.GenRequestId(20)
  168. _, err := self.storageRequest("DeleteSnapshot", params)
  169. return err
  170. }
  171. func (self *SSnapshot) GetProjectId() string {
  172. return self.ProjectName
  173. }
  174. func (self *SRegion) CreateSnapshot(diskId, name, desc string) (*SSnapshot, error) {
  175. params := map[string]string{
  176. "SnapshotName": name,
  177. "ClientToken": utils.GenRequestId(20),
  178. "Description": desc,
  179. "VolumeId": diskId,
  180. }
  181. resp, err := self.storageRequest("CreateSnapshot", params)
  182. if err != nil {
  183. return nil, err
  184. }
  185. id, err := resp.GetString("SnapshotId")
  186. if err != nil {
  187. return nil, err
  188. }
  189. return self.GetSnapshot(id)
  190. }