snapshot.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 baidu
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "time"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. "yunion.io/x/jsonutils"
  24. "yunion.io/x/pkg/utils"
  25. )
  26. type SSnapshot struct {
  27. multicloud.SVirtualResourceBase
  28. region *SRegion
  29. SBaiduTag
  30. Id string
  31. Desc string
  32. VolumeId string
  33. CreateMethod string
  34. Status string
  35. SizeInGb int32
  36. Name string
  37. CreateTime time.Time
  38. }
  39. func (self *SSnapshot) GetId() string {
  40. return self.Id
  41. }
  42. func (self *SSnapshot) GetName() string {
  43. return self.Name
  44. }
  45. func (self *SSnapshot) GetStatus() string {
  46. switch self.Status {
  47. case "Creating":
  48. return api.SNAPSHOT_CREATING
  49. case "CreatedFailed":
  50. return api.SNAPSHOT_FAILED
  51. case "Available":
  52. return api.SNAPSHOT_READY
  53. case "NotAvailable":
  54. return api.SNAPSHOT_UNKNOWN
  55. default:
  56. return strings.ToLower(self.Status)
  57. }
  58. }
  59. func (self *SSnapshot) GetSizeMb() int32 {
  60. return self.SizeInGb * 1024
  61. }
  62. func (self *SSnapshot) GetDiskId() string {
  63. return self.VolumeId
  64. }
  65. func (self *SSnapshot) GetDiskType() string {
  66. return ""
  67. }
  68. func (self *SSnapshot) Refresh() error {
  69. snapshot, err := self.region.GetSnapshot(self.Id)
  70. if err != nil {
  71. return err
  72. }
  73. return jsonutils.Update(self, snapshot)
  74. }
  75. func (self *SSnapshot) GetGlobalId() string {
  76. return self.Id
  77. }
  78. func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  79. snapshots, err := self.GetSnapshots("")
  80. if err != nil {
  81. return nil, err
  82. }
  83. ret := []cloudprovider.ICloudSnapshot{}
  84. for i := 0; i < len(snapshots); i += 1 {
  85. snapshots[i].region = self
  86. ret = append(ret, &snapshots[i])
  87. }
  88. return ret, nil
  89. }
  90. func (self *SRegion) GetISnapshotById(id string) (cloudprovider.ICloudSnapshot, error) {
  91. snapshot, err := self.GetSnapshot(id)
  92. if err != nil {
  93. return nil, err
  94. }
  95. return snapshot, nil
  96. }
  97. func (self *SSnapshot) Delete() error {
  98. return self.region.DeleteSnapshot(self.Id)
  99. }
  100. func (region *SRegion) GetSnapshots(diskId string) ([]SSnapshot, error) {
  101. params := url.Values{}
  102. params.Set("volumeId", diskId)
  103. ret := []SSnapshot{}
  104. for {
  105. resp, err := region.bccList("v2/snapshot", params)
  106. if err != nil {
  107. return nil, err
  108. }
  109. part := struct {
  110. NextMarker string
  111. Snapshots []SSnapshot
  112. }{}
  113. err = resp.Unmarshal(&part)
  114. if err != nil {
  115. return nil, err
  116. }
  117. ret = append(ret, part.Snapshots...)
  118. if len(part.NextMarker) == 0 {
  119. break
  120. }
  121. params.Set("marker", part.NextMarker)
  122. }
  123. return ret, nil
  124. }
  125. func (region *SRegion) GetSnapshot(id string) (*SSnapshot, error) {
  126. resp, err := region.bccList(fmt.Sprintf("v2/snapshot/%s", id), nil)
  127. if err != nil {
  128. return nil, err
  129. }
  130. ret := &SSnapshot{region: region}
  131. err = resp.Unmarshal(ret, "snapshot")
  132. if err != nil {
  133. return nil, err
  134. }
  135. return ret, nil
  136. }
  137. func (region *SRegion) DeleteSnapshot(id string) error {
  138. _, err := region.bccDelete(fmt.Sprintf("v2/snapshot/%s", id), nil)
  139. return err
  140. }
  141. func (region *SRegion) CreateSnapshot(name, desc, diskId string) (*SSnapshot, error) {
  142. params := url.Values{}
  143. params.Set("clicentToken", utils.GenRequestId(20))
  144. body := map[string]interface{}{
  145. "volumeId": diskId,
  146. "snapshotName": name,
  147. "desc": desc,
  148. }
  149. resp, err := region.bccPost("v2/snapshot", params, body)
  150. if err != nil {
  151. return nil, err
  152. }
  153. snapshotId, err := resp.GetString("snapshotId")
  154. if err != nil {
  155. return nil, err
  156. }
  157. return region.GetSnapshot(snapshotId)
  158. }