snapshotpolicy.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 baidu
  15. import (
  16. "fmt"
  17. "net/url"
  18. "time"
  19. "yunion.io/x/cloudmux/pkg/apis"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. "yunion.io/x/jsonutils"
  23. "yunion.io/x/pkg/utils"
  24. )
  25. type SSnapshotPolicy struct {
  26. multicloud.SVirtualResourceBase
  27. SBaiduTag
  28. region *SRegion
  29. Id string
  30. Name string
  31. TimePoints []int
  32. RepeatWeekdays []int
  33. RetentionDays int
  34. Status string
  35. CreatedTime time.Time
  36. UpdatedTime time.Time
  37. DeletedTime time.Time
  38. LastExecuteTime time.Time
  39. VolumeCount int
  40. }
  41. func (self *SSnapshotPolicy) GetId() string {
  42. return self.Id
  43. }
  44. func (self *SSnapshotPolicy) GetName() string {
  45. return self.Name
  46. }
  47. func (self *SSnapshotPolicy) GetStatus() string {
  48. switch self.Status {
  49. case "active":
  50. return apis.STATUS_AVAILABLE
  51. case "deleted":
  52. return apis.STATUS_DELETING
  53. case "paused":
  54. return apis.STATUS_UNKNOWN
  55. default:
  56. return apis.STATUS_UNKNOWN
  57. }
  58. }
  59. func (self *SSnapshotPolicy) Refresh() error {
  60. policy, err := self.region.GetSnapshotPolicy(self.Id)
  61. if err != nil {
  62. return err
  63. }
  64. return jsonutils.Update(self, policy)
  65. }
  66. func (self *SSnapshotPolicy) GetGlobalId() string {
  67. return self.Id
  68. }
  69. func (self *SSnapshotPolicy) GetRetentionDays() int {
  70. return self.RetentionDays
  71. }
  72. func (self *SSnapshotPolicy) GetRepeatWeekdays() ([]int, error) {
  73. return self.RepeatWeekdays, nil
  74. }
  75. func (self *SSnapshotPolicy) GetTimePoints() ([]int, error) {
  76. return self.TimePoints, nil
  77. }
  78. func (self *SSnapshotPolicy) Delete() error {
  79. return self.region.DeletSnapshotPolicy(self.Id)
  80. }
  81. func (self *SSnapshotPolicy) ApplyDisks(ids []string) error {
  82. return self.region.ApplySnapshotPolicy(self.Id, ids)
  83. }
  84. func (self *SSnapshotPolicy) CancelDisks(ids []string) error {
  85. return self.region.CancelSnapshotPolicy(self.Id, ids)
  86. }
  87. func (self *SSnapshotPolicy) GetApplyDiskIds() ([]string, error) {
  88. return nil, cloudprovider.ErrNotSupported
  89. }
  90. func (region *SRegion) GetSnapshotPolicys() ([]SSnapshotPolicy, error) {
  91. params := url.Values{}
  92. ret := []SSnapshotPolicy{}
  93. for {
  94. resp, err := region.bccList("v2/asp", params)
  95. if err != nil {
  96. return nil, err
  97. }
  98. part := struct {
  99. NextMarker string
  100. AutoSnapshotPolicys []SSnapshotPolicy
  101. }{}
  102. err = resp.Unmarshal(&part)
  103. if err != nil {
  104. return nil, err
  105. }
  106. ret = append(ret, part.AutoSnapshotPolicys...)
  107. if len(part.NextMarker) == 0 {
  108. break
  109. }
  110. params.Set("marker", part.NextMarker)
  111. }
  112. return ret, nil
  113. }
  114. func (region *SRegion) GetSnapshotPolicy(id string) (*SSnapshotPolicy, error) {
  115. params := url.Values{}
  116. resp, err := region.bccList(fmt.Sprintf("v2/asp/%s", id), params)
  117. if err != nil {
  118. return nil, err
  119. }
  120. ret := &SSnapshotPolicy{region: region}
  121. err = resp.Unmarshal(ret, "autoSnapshotPolicy")
  122. if err != nil {
  123. return nil, err
  124. }
  125. return ret, nil
  126. }
  127. func (r *SRegion) CreateSnapshotPolicy(opts *cloudprovider.SnapshotPolicyInput) (string, error) {
  128. params := url.Values{}
  129. params.Set("clientToken", utils.GenRequestId(20))
  130. body := map[string]interface{}{
  131. "name": opts.Name,
  132. "timePoints": opts.TimePoints,
  133. "repeatWeekdays": opts.RetentionDays,
  134. "retentionDays": opts.RetentionDays,
  135. }
  136. resp, err := r.bccPost("v2/asp", params, body)
  137. if err != nil {
  138. return "", err
  139. }
  140. return resp.GetString("aspId")
  141. }
  142. func (r *SRegion) GetISnapshotPolicyById(id string) (cloudprovider.ICloudSnapshotPolicy, error) {
  143. policy, err := r.GetSnapshotPolicy(id)
  144. if err != nil {
  145. return nil, err
  146. }
  147. return policy, nil
  148. }
  149. func (self *SRegion) GetISnapshotPolicies() ([]cloudprovider.ICloudSnapshotPolicy, error) {
  150. policys, err := self.GetSnapshotPolicys()
  151. if err != nil {
  152. return nil, err
  153. }
  154. ret := []cloudprovider.ICloudSnapshotPolicy{}
  155. for i := range policys {
  156. policys[i].region = self
  157. ret = append(ret, &policys[i])
  158. }
  159. return ret, nil
  160. }
  161. func (self *SRegion) DeletSnapshotPolicy(id string) error {
  162. _, err := self.bccList(fmt.Sprintf("v2/asp/%s", id), nil)
  163. return err
  164. }
  165. func (self *SRegion) ApplySnapshotPolicy(id string, diskIds []string) error {
  166. params := url.Values{}
  167. params.Set("attach", "")
  168. body := map[string]interface{}{
  169. "volumeIds": diskIds,
  170. }
  171. _, err := self.bccUpdate(fmt.Sprintf("v2/asp/%s", id), params, body)
  172. return err
  173. }
  174. func (self *SRegion) CancelSnapshotPolicy(id string, diskIds []string) error {
  175. params := url.Values{}
  176. params.Set("detach", "")
  177. body := map[string]interface{}{
  178. "volumeIds": diskIds,
  179. }
  180. _, err := self.bccUpdate(fmt.Sprintf("v2/asp/%s", id), params, body)
  181. return err
  182. }