scaling_policy.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. "fmt"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  22. "yunion.io/x/onecloud/pkg/mcclient/options"
  23. )
  24. type Timer struct {
  25. TimingExecTime string `help:"Exectime for 'timing' type trigger, format:'2006-01-02 15:04:05'" json:"exec_time"`
  26. }
  27. type CycleTimer struct {
  28. CycleCycleType string `help:"Cycle type for cycle timer" json:"cycle_type" choices:"day|week|month"`
  29. CycleMinute int `help:"Minute of cycle timer" json:"minute"`
  30. CycleHour int `help:"Hour of cycle timer" json:"hour"`
  31. CycleWeekdays []int `help:"Weekdays for cycle timer" json:"weekdays"`
  32. CycleMonthDays []int `help:"Month days for cycle timer" json:"month_days"`
  33. CycleStartTime string `help:"Start time for cycle timer, format:'2006-01-02 15:04:05'" json:"start_time"`
  34. CycleEndTime string `help:"End time for cycle timer, format:'2006-01-02 15:04:05'" json:"end_time"`
  35. }
  36. func init() {
  37. type ScalingPolicyListOptions struct {
  38. options.BaseListOptions
  39. ScalingGroup string `help:"ScalingGroup ID or Name"`
  40. TriggerType string `help:"Trigger type" choices:"alarm|timing|cycle"`
  41. }
  42. R(&ScalingPolicyListOptions{}, "scaling-policy-list", "List Scaling Policy", func(s *mcclient.ClientSession,
  43. args *ScalingPolicyListOptions) error {
  44. params, err := options.ListStructToParams(args)
  45. policies, err := modules.ScalingPolicy.List(s, params)
  46. if err != nil {
  47. return err
  48. }
  49. printList(policies, modules.ScalingPolicy.GetColumns(s))
  50. return nil
  51. })
  52. type ScalingPolicyShowOptions struct {
  53. ID string `help:"ScalingPolicy ID or Name"`
  54. }
  55. R(&ScalingPolicyShowOptions{}, "scaling-policy-show", "Show Scaling Policy", func(s *mcclient.ClientSession,
  56. args *ScalingPolicyShowOptions) error {
  57. params := jsonutils.NewDict()
  58. params.Set("details", jsonutils.JSONTrue)
  59. policy, err := modules.ScalingPolicy.Get(s, args.ID, params)
  60. if err != nil {
  61. return err
  62. }
  63. printObject(policy)
  64. return nil
  65. })
  66. type ScalingAlarm struct {
  67. AlarmCumulate int `help:"Cumulate times alarm will trigger, for 'alarm' trigger" json:"cumulate"`
  68. AlarmCycle int `help:"Monitoring cycle for indicators, for 'alarm' trigger" json:"cycle"`
  69. AlarmIndicator string `help:"Indicator for 'alarm' trigger" json:"indicator"`
  70. AlarmWrapper string `help:"Wrapper for Indicators" choices:"max|min|average" json:"wrapper"`
  71. AlarmOperator string `help:"Operator between Indicator and Operator" json:"operator"`
  72. AlarmValue float64 `help:"Value of Indicator" json:"value"`
  73. }
  74. type ScalingPolicyCreateOptions struct {
  75. NAME string `help:"ScalingPolicy Name" json:"name"`
  76. ScalingGroup string `help:"ScalingGroup ID or Name" json:"scaling_group"`
  77. TriggerType string `help:"Trigger type" choices:"alarm|timing|cycle" json:"trigger_type"`
  78. Timer
  79. CycleTimer
  80. ScalingAlarm
  81. Action string `help:"Action for scaling policy" choices:"add|remove|set" json:"action"`
  82. Number int `help:"Instance number for action" json:"number"`
  83. Unit string `help:"Unit for Number" choices:"s|%" json:"unit"`
  84. CoolingTime int `help:"Cooling time, unit: s" json:"cooling_time"`
  85. }
  86. R(&ScalingPolicyCreateOptions{}, "scaling-policy-create", "Create Scaling Policy",
  87. func(s *mcclient.ClientSession, args *ScalingPolicyCreateOptions) error {
  88. formatStr := "2006-01-02 15:04:05"
  89. timingExecTime, err := time.Parse(formatStr, args.TimingExecTime)
  90. if err != nil {
  91. return fmt.Errorf("invalid time format for 'exec_time'")
  92. }
  93. cycleStarTime, err := time.Parse(formatStr, args.CycleStartTime)
  94. if err != nil {
  95. return fmt.Errorf("invalid time format for 'start_time'")
  96. }
  97. cycleEndTime, err := time.Parse(formatStr, args.CycleEndTime)
  98. if err != nil {
  99. return fmt.Errorf("invalid time format for 'end_time'")
  100. }
  101. spCreateInput := api.ScalingPolicyCreateInput{
  102. ScalingGroup: args.ScalingGroup,
  103. TriggerType: args.TriggerType,
  104. Timer: api.TimerCreateInput{
  105. ExecTime: timingExecTime,
  106. },
  107. CycleTimer: api.CycleTimerCreateInput{
  108. CycleType: args.CycleCycleType,
  109. Minute: args.CycleMinute,
  110. Hour: args.CycleHour,
  111. WeekDays: args.CycleWeekdays,
  112. MonthDays: args.CycleMonthDays,
  113. StartTime: cycleStarTime,
  114. EndTime: cycleEndTime,
  115. },
  116. Alarm: api.ScalingAlarmCreateInput{
  117. Cumulate: args.AlarmCumulate,
  118. Cycle: args.AlarmCycle,
  119. Indicator: args.AlarmIndicator,
  120. Wrapper: args.AlarmWrapper,
  121. Operator: args.AlarmOperator,
  122. Value: args.AlarmValue,
  123. },
  124. Action: args.Action,
  125. Number: args.Number,
  126. Unit: args.Unit,
  127. CoolingTime: args.CoolingTime,
  128. }
  129. spCreateInput.Name = args.NAME
  130. ret, err := modules.ScalingPolicy.Create(s, jsonutils.Marshal(spCreateInput))
  131. if err != nil {
  132. return err
  133. }
  134. printObject(ret)
  135. return nil
  136. },
  137. )
  138. type ScalingPolicyEnableOptions struct {
  139. ID string `help:"ScalingPolicy ID or Name"`
  140. }
  141. R(&ScalingPolicyEnableOptions{}, "scaling-policy-enable", "Enable ScalingPolicy", func(s *mcclient.ClientSession,
  142. args *ScalingPolicyEnableOptions) error {
  143. ret, err := modules.ScalingPolicy.PerformAction(s, args.ID, "enable", jsonutils.NewDict())
  144. if err != nil {
  145. return err
  146. }
  147. printObject(ret)
  148. return nil
  149. })
  150. R(&ScalingPolicyEnableOptions{}, "scaling-policy-disable", "Disable ScalingPolicy",
  151. func(s *mcclient.ClientSession, args *ScalingPolicyEnableOptions) error {
  152. ret, err := modules.ScalingPolicy.PerformAction(s, args.ID, "disable", jsonutils.NewDict())
  153. if err != nil {
  154. return err
  155. }
  156. printObject(ret)
  157. return nil
  158. },
  159. )
  160. type ScalingPolicyTriggerOptions struct {
  161. ID string `help:"ScalingPolicy ID or Name"`
  162. }
  163. R(&ScalingPolicyTriggerOptions{}, "scaling-policy-trigger", "Trigger ScalingPolicy's action",
  164. func(s *mcclient.ClientSession, args *ScalingPolicyTriggerOptions) error {
  165. ret, err := modules.ScalingPolicy.PerformAction(s, args.ID, "trigger", jsonutils.NewDict())
  166. if err != nil {
  167. return err
  168. }
  169. printObject(ret)
  170. return nil
  171. },
  172. )
  173. type ScalingPolicyDeleteOptions struct {
  174. ID string `help:"ScalingPolicy ID or Name"`
  175. }
  176. R(&ScalingPolicyDeleteOptions{}, "scaling-policy-delete", "Delete SclaingPolicy",
  177. func(s *mcclient.ClientSession, args *ScalingPolicyDeleteOptions) error {
  178. ret, err := modules.ScalingPolicy.Delete(s, args.ID, jsonutils.NewDict())
  179. if err != nil {
  180. return err
  181. }
  182. printObject(ret)
  183. return nil
  184. },
  185. )
  186. }