schedpolicies.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. "yunion.io/x/jsonutils"
  17. "yunion.io/x/onecloud/pkg/mcclient"
  18. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. func init() {
  22. type SchedpoliciesListOptions struct {
  23. options.BaseListOptions
  24. }
  25. R(&SchedpoliciesListOptions{}, "sched-policy-list", "List scheduler policies", func(s *mcclient.ClientSession, args *SchedpoliciesListOptions) error {
  26. var params *jsonutils.JSONDict
  27. {
  28. var err error
  29. params, err = args.BaseListOptions.Params()
  30. if err != nil {
  31. return err
  32. }
  33. }
  34. results, err := modules.Schedpolicies.List(s, params)
  35. if err != nil {
  36. return err
  37. }
  38. printList(results, modules.Schedpolicies.GetColumns(s))
  39. return nil
  40. })
  41. type SchedpoliciesCreateOptions struct {
  42. NAME string `help:"name of the sched policy"`
  43. STRATEGY string `help:"strategy for the schedtag" choices:"require|prefer|avoid|exclude"`
  44. SCHEDTAG string `help:"ID or name of schedtag"`
  45. CONDITION string `help:"condition that assign schedtag to hosts"`
  46. Enable bool `help:"create the policy with enabled status"`
  47. Disable bool `help:"create the policy with disabled status"`
  48. }
  49. R(&SchedpoliciesCreateOptions{}, "sched-policy-create", "create a sched policty", func(s *mcclient.ClientSession, args *SchedpoliciesCreateOptions) error {
  50. params := jsonutils.NewDict()
  51. params.Add(jsonutils.NewString(args.NAME), "name")
  52. params.Add(jsonutils.NewString(args.STRATEGY), "strategy")
  53. params.Add(jsonutils.NewString(args.CONDITION), "condition")
  54. params.Add(jsonutils.NewString(args.SCHEDTAG), "schedtag")
  55. if args.Enable {
  56. params.Add(jsonutils.JSONTrue, "enabled")
  57. } else if args.Disable {
  58. params.Add(jsonutils.JSONFalse, "disabled")
  59. }
  60. result, err := modules.Schedpolicies.Create(s, params)
  61. if err != nil {
  62. return err
  63. }
  64. printObject(result)
  65. return nil
  66. })
  67. type SchedpoliciesUpdateOptions struct {
  68. ID string `help:"ID or name of the sched policy"`
  69. Name string `help:"new name of sched policy"`
  70. Strategy string `help:"schedtag strategy" choices:"require|prefer|avoid|exclude"`
  71. SchedTag string `help:"ID or name of schedtag"`
  72. Condition string `help:"condition that assign schedtag to hosts"`
  73. Enable bool `help:"make the sched policy enabled"`
  74. Disable bool `help:"make the sched policy disabled"`
  75. }
  76. R(&SchedpoliciesUpdateOptions{}, "sched-policy-update", "update a sched policy", func(s *mcclient.ClientSession, args *SchedpoliciesUpdateOptions) error {
  77. params := jsonutils.NewDict()
  78. if len(args.Name) > 0 {
  79. params.Add(jsonutils.NewString(args.Name), "name")
  80. }
  81. if len(args.Strategy) > 0 {
  82. params.Add(jsonutils.NewString(args.Strategy), "strategy")
  83. }
  84. if len(args.Condition) > 0 {
  85. params.Add(jsonutils.NewString(args.Condition), "condition")
  86. }
  87. if len(args.SchedTag) > 0 {
  88. params.Add(jsonutils.NewString(args.SchedTag), "schedtag")
  89. }
  90. if args.Enable {
  91. params.Add(jsonutils.JSONTrue, "enabled")
  92. } else if args.Disable {
  93. params.Add(jsonutils.JSONFalse, "enabled")
  94. }
  95. if params.Size() == 0 {
  96. return InvalidUpdateError()
  97. }
  98. result, err := modules.Schedpolicies.Update(s, args.ID, params)
  99. if err != nil {
  100. return err
  101. }
  102. printObject(result)
  103. return nil
  104. })
  105. type SchedpoliciesDeleteOptions struct {
  106. ID string `help:"ID or name of the sched policy"`
  107. }
  108. R(&SchedpoliciesDeleteOptions{}, "sched-policy-delete", "delete a sched policy", func(s *mcclient.ClientSession, args *SchedpoliciesDeleteOptions) error {
  109. result, err := modules.Schedpolicies.Delete(s, args.ID, nil)
  110. if err != nil {
  111. return err
  112. }
  113. printObject(result)
  114. return nil
  115. })
  116. type SchedpoliciesShowOptions struct {
  117. ID string `help:"ID or name of the sched policy"`
  118. }
  119. R(&SchedpoliciesShowOptions{}, "sched-policy-show", "show details of a sched policy", func(s *mcclient.ClientSession, args *SchedpoliciesShowOptions) error {
  120. result, err := modules.Schedpolicies.Get(s, args.ID, nil)
  121. if err != nil {
  122. return err
  123. }
  124. printObject(result)
  125. return nil
  126. })
  127. type SchedpoliciesEvaluateOptions struct {
  128. ID string `help:"ID or name of the sched policy"`
  129. OBJECT string `help:"ID or name of the object"`
  130. ResourceType string `help:"Resource type of the object" default:"server" choices:"server|disk" short-token:"t"`
  131. }
  132. R(&SchedpoliciesEvaluateOptions{}, "sched-policy-evaluate", "Evaluate sched policy", func(s *mcclient.ClientSession, args *SchedpoliciesEvaluateOptions) error {
  133. params := jsonutils.NewDict()
  134. params.Add(jsonutils.NewString(args.OBJECT), "object")
  135. params.Add(jsonutils.NewString(args.ResourceType), "resource_type")
  136. result, err := modules.Schedpolicies.PerformAction(s, args.ID, "evaluate", params)
  137. if err != nil {
  138. return err
  139. }
  140. printObject(result)
  141. return nil
  142. })
  143. }