alertnotification.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/pkg/util/printutils"
  17. monitorapi "yunion.io/x/onecloud/pkg/apis/monitor"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. "yunion.io/x/onecloud/pkg/mcclient/modules/monitor"
  20. options "yunion.io/x/onecloud/pkg/mcclient/options/monitor"
  21. )
  22. func init() {
  23. initAlertNotification()
  24. }
  25. func initAlertNotification() {
  26. aN := cmdN("alert-notification")
  27. R(&options.AlertNotificationListOptions{}, aN("list"), "List alert notification pairs",
  28. func(s *mcclient.ClientSession, args *options.AlertNotificationListOptions) error {
  29. params, err := args.Params()
  30. if err != nil {
  31. return err
  32. }
  33. var result *printutils.ListResult
  34. if len(args.Notification) > 0 {
  35. result, err = monitor.Alertnotification.ListDescendent2(s, args.Notification, params)
  36. } else {
  37. result, err = monitor.Alertnotification.List(s, params)
  38. }
  39. if err != nil {
  40. return err
  41. }
  42. printList(result, monitor.Alertnotification.GetColumns(s))
  43. return nil
  44. })
  45. R(&options.AlertNotificationAttachOptions{}, aN("attach"), "Attach a notification to a alert",
  46. func(s *mcclient.ClientSession, args *options.AlertNotificationAttachOptions) error {
  47. input := &monitorapi.AlertnotificationCreateInput{
  48. UsedBy: args.UsedBy,
  49. }
  50. ret, err := monitor.Alertnotification.Attach(s, args.ALERT, args.NOTIFICATION, input.JSON(input))
  51. if err != nil {
  52. return err
  53. }
  54. printObject(ret)
  55. return nil
  56. })
  57. R(&options.AlertNotificationAttachOptions{}, aN("detach"), "Detach a notification to a alert",
  58. func(s *mcclient.ClientSession, args *options.AlertNotificationAttachOptions) error {
  59. ret, err := monitor.Alertnotification.Detach(s, args.ALERT, args.NOTIFICATION, nil)
  60. if err != nil {
  61. return err
  62. }
  63. printObject(ret)
  64. return nil
  65. })
  66. }