meteralert.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 monitor
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/onecloud/pkg/mcclient"
  18. modules "yunion.io/x/onecloud/pkg/mcclient/modules/monitor"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. func init() {
  22. /**
  23. * 创建一条报警规则
  24. */
  25. type MeterAlertCreateOptions struct {
  26. Type string `help:"Alert rule type" required:"true" choices:"balance|resFee|monthFee"`
  27. Threshold float64 `help:"Threshold value of the metric" required:"true"`
  28. Comparator string `help:"Comparison operator for join expressions" required:"true" choices:">|<|>=|<=|=|!="`
  29. Recipients string `help:"Comma separated recipient ID" required:"true"`
  30. Level string `help:"Alert level" required:"true" choices:"normal|important|fatal"`
  31. Channel string `help:"Ways to send an alarm" required:"true" choices:"email|mobile"`
  32. Provider string `help:"Name of the cloud platform"`
  33. AccountId string `help:"ID of the cloud platform"`
  34. ProjectId string `help:"ID of the project" required:"true"`
  35. }
  36. R(&MeterAlertCreateOptions{}, "meteralert-create", "Create a meter alert rule", func(s *mcclient.ClientSession, args *MeterAlertCreateOptions) error {
  37. params, err := options.StructToParams(args)
  38. if err != nil {
  39. return err
  40. }
  41. rst, err := modules.MeterAlert.Create(s, params)
  42. if err != nil {
  43. return err
  44. }
  45. printObject(rst)
  46. return nil
  47. })
  48. /**
  49. * 删除指定ID的报警规则
  50. */
  51. type MeterAlertDeleteOptions struct {
  52. ID string `help:"ID of alarm"`
  53. }
  54. R(&MeterAlertDeleteOptions{}, "meteralert-delete", "Delete a meter alert", func(s *mcclient.ClientSession, args *MeterAlertDeleteOptions) error {
  55. alarm, e := modules.MeterAlert.Delete(s, args.ID, nil)
  56. if e != nil {
  57. return e
  58. }
  59. printObject(alarm)
  60. return nil
  61. })
  62. /**
  63. * 修改指定ID的报警规则状态
  64. */
  65. type MeterAlertUpdateOptions struct {
  66. ID string `help:"ID of the meter alert"`
  67. STATUS string `help:"Name of the new alarm" choices:"Enabled|Disabled"`
  68. }
  69. R(&MeterAlertUpdateOptions{}, "meteralert-change-status", "Change status of a meter alert", func(s *mcclient.ClientSession, args *MeterAlertUpdateOptions) error {
  70. params := jsonutils.NewDict()
  71. params.Add(jsonutils.NewString(args.STATUS), "status")
  72. alarm, err := modules.MeterAlert.Patch(s, args.ID, params)
  73. if err != nil {
  74. return err
  75. }
  76. printObject(alarm)
  77. return nil
  78. })
  79. /**
  80. * 列出报警规则
  81. */
  82. type MeterAlertListOptions struct {
  83. Type string `help:"Alarm rule type" choices:"balance|resFee|monthFee"`
  84. CloudProvider string `help:"Name of cloud provider, case sensitive"`
  85. AccountId string `help:"Cloud account ID"`
  86. ProjectId string `help:"ID of the project" required:"true"`
  87. options.BaseListOptions
  88. }
  89. R(&MeterAlertListOptions{}, "meteralert-list", "List meter alert", func(s *mcclient.ClientSession, args *MeterAlertListOptions) error {
  90. params, err := options.ListStructToParams(args)
  91. if err != nil {
  92. return err
  93. }
  94. result, err := modules.MeterAlert.List(s, params)
  95. if err != nil {
  96. return err
  97. }
  98. printList(result, modules.MeterAlert.GetColumns(s))
  99. return nil
  100. })
  101. }