snapshotpolicy.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 compute
  15. import (
  16. "reflect"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/gotypes"
  19. "yunion.io/x/pkg/utils"
  20. "yunion.io/x/onecloud/pkg/apis"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. )
  23. const (
  24. SNAPSHOT_POLICY_APPLY = "applying"
  25. SNAPSHOT_POLICY_APPLY_FAILED = "apply_failed"
  26. SNAPSHOT_POLICY_CANCEL = "canceling"
  27. SNAPSHOT_POLICY_CANCEL_FAILED = "cancel_failed"
  28. SNAPSHOT_POLICY_TYPE_DISK = "disk"
  29. SNAPSHOT_POLICY_TYPE_SERVER = "server"
  30. )
  31. type SnapshotPolicyDetails struct {
  32. apis.VirtualResourceDetails
  33. ManagedResourceInfo
  34. CloudregionResourceInfo
  35. SSnapshotPolicy
  36. // 绑定磁盘数量
  37. BindingDiskCount int `json:"binding_disk_count"`
  38. // 绑定主机数量
  39. BindingResourceCount int `json:"binding_resource_count"`
  40. // 快照数量
  41. SnapshotCount int `json:"snapshot_count"`
  42. }
  43. type SSnapshotPolicyCreateInput struct {
  44. apis.VirtualResourceCreateInput
  45. CloudproviderResourceInput
  46. CloudregionResourceInput
  47. // 自动快照保留时长 -1: 表示永久保留
  48. RetentionDays int `json:"retention_days"`
  49. // 自动快照保留数量
  50. // 此配置优先级高于 RetentionDays
  51. RetentionCount int `json:"retention_count"`
  52. // 快照类型, 目前支持 disk, server
  53. // disk: 自动磁盘快照策略, 只能关联磁盘
  54. // server: 自动主机快照策略, 只能关联主机
  55. Type string `json:"type"`
  56. // 创建自动快照策略的执行周期
  57. // 1. 周一到周日
  58. // 2. 1~7, 1: Monday, 7: Sunday
  59. RepeatWeekdays []int `json:"repeat_weekdays"`
  60. // 创建自动快照策略的执行时间
  61. // 0:00~23:59 每天, 0~23 每小时
  62. // 创建自动快照策略的时间必须与 RepeatWeekdays 对应的创建周期相一致
  63. TimePoints []int `json:"time_points"`
  64. }
  65. func (self *SSnapshotPolicyCreateInput) Validate() error {
  66. if len(self.RepeatWeekdays) == 0 {
  67. return httperrors.NewMissingParameterError("repeat_weekdays")
  68. }
  69. repeatDays := []int{}
  70. for _, day := range self.RepeatWeekdays {
  71. if day < 1 || day > 7 {
  72. return httperrors.NewOutOfRangeError("repeat_weekdays out of range 1-7")
  73. }
  74. if !utils.IsInArray(day, repeatDays) {
  75. repeatDays = append(repeatDays, day)
  76. }
  77. }
  78. self.RepeatWeekdays = repeatDays
  79. if len(self.TimePoints) == 0 {
  80. return httperrors.NewMissingParameterError("time_points")
  81. }
  82. points := []int{}
  83. for _, point := range self.TimePoints {
  84. if point < 0 || point > 23 {
  85. return httperrors.NewOutOfRangeError("time_points out of range 0-23")
  86. }
  87. if !utils.IsInArray(point, points) {
  88. points = append(points, point)
  89. }
  90. }
  91. self.TimePoints = points
  92. return nil
  93. }
  94. type SSnapshotPolicyUpdateInput struct {
  95. apis.VirtualResourceBaseUpdateInput
  96. // 自动快照保留时长, -1: 表示永久保留
  97. RetentionDays *int
  98. // 自动快照保留数量
  99. // 此配置优先级高于 RetentionDays
  100. RetentionCount *int
  101. // 自动快照执行周期
  102. // 1. 周一到周日
  103. // 2. 1~7, 1: Monday, 7: Sunday
  104. RepeatWeekdays *[]int `json:"repeat_weekdays"`
  105. // 自动快照执行时间
  106. // 0:00~23:59 每天, 0~23 每小时
  107. // 创建自动快照策略的时间必须与 RepeatWeekdays 对应的创建周期相一致
  108. TimePoints *[]int `json:"time_points"`
  109. }
  110. func (self *SSnapshotPolicyUpdateInput) Validate() error {
  111. if self.RepeatWeekdays != nil {
  112. repeatDays := []int{}
  113. for _, day := range *self.RepeatWeekdays {
  114. if day < 1 || day > 7 {
  115. return httperrors.NewOutOfRangeError("repeat_weekdays out of range 1-7")
  116. }
  117. if !utils.IsInArray(day, repeatDays) {
  118. repeatDays = append(repeatDays, day)
  119. }
  120. }
  121. self.RepeatWeekdays = &repeatDays
  122. }
  123. if self.TimePoints != nil {
  124. points := []int{}
  125. for _, point := range *self.TimePoints {
  126. if point < 0 || point > 23 {
  127. return httperrors.NewOutOfRangeError("time_points out of range 0-23")
  128. }
  129. if !utils.IsInArray(point, points) {
  130. points = append(points, point)
  131. }
  132. }
  133. self.TimePoints = &points
  134. }
  135. return nil
  136. }
  137. type SnapshotPolicyDisksInput struct {
  138. // 磁盘ID列表
  139. // 快照策略类型需要时disk
  140. Disks []string `json:"disk"`
  141. }
  142. type SnapshotPolicyResourcesInput struct {
  143. // 绑定的资源列表
  144. // 目前仅支持绑定主机
  145. Resources []struct {
  146. // 绑定资源ID
  147. Id string `json:"id"`
  148. // server: 主机, disk: 磁盘
  149. Type string `json:"type"`
  150. } `json:"resources"`
  151. }
  152. type RepeatWeekdays []int
  153. func (days RepeatWeekdays) String() string {
  154. return jsonutils.Marshal(days).String()
  155. }
  156. func (days RepeatWeekdays) IsZero() bool {
  157. return len(days) == 0
  158. }
  159. type TimePoints []int
  160. func (points TimePoints) String() string {
  161. return jsonutils.Marshal(points).String()
  162. }
  163. func (points TimePoints) IsZero() bool {
  164. return len(points) == 0
  165. }
  166. func init() {
  167. gotypes.RegisterSerializable(reflect.TypeOf(&RepeatWeekdays{}), func() gotypes.ISerializable {
  168. return &RepeatWeekdays{}
  169. })
  170. gotypes.RegisterSerializable(reflect.TypeOf(&TimePoints{}), func() gotypes.ISerializable {
  171. return &TimePoints{}
  172. })
  173. }