notification.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 notify
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. api "yunion.io/x/onecloud/pkg/apis/notify"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. modules "yunion.io/x/onecloud/pkg/mcclient/modules/notify"
  21. "yunion.io/x/onecloud/pkg/mcclient/options"
  22. )
  23. func init() {
  24. type NotificationCreateInput struct {
  25. Receivers []string `help:"ID or Name of Receiver"`
  26. Robots []string `help:"ID or Name of Robot"`
  27. ContactType string `help:"Contact type of receiver"`
  28. TOPIC string `help:"Topic"`
  29. Priority string `help:"Priority"`
  30. MESSAGE string `help:"Message"`
  31. Oldsdk bool `help:"Old sdk"`
  32. }
  33. R(&NotificationCreateInput{}, "notify-send", "Send a notify message", func(s *mcclient.ClientSession, args *NotificationCreateInput) error {
  34. var (
  35. ret jsonutils.JSONObject
  36. err error
  37. )
  38. if args.Oldsdk {
  39. msg := modules.SNotifyMessage{
  40. Uid: args.Receivers,
  41. Robots: args.Robots,
  42. ContactType: modules.TNotifyChannel(args.ContactType),
  43. Topic: args.TOPIC,
  44. Priority: modules.TNotifyPriority(args.Priority),
  45. Msg: args.MESSAGE,
  46. Remark: "",
  47. Broadcast: false,
  48. }
  49. err = modules.Notifications.Send(s, msg)
  50. if err != nil {
  51. return err
  52. }
  53. } else {
  54. input := api.NotificationCreateInput{
  55. Receivers: args.Receivers,
  56. Robots: args.Robots,
  57. ContactType: args.ContactType,
  58. Topic: args.TOPIC,
  59. Priority: args.Priority,
  60. Message: args.MESSAGE,
  61. }
  62. ret, err = modules.Notification.Create(s, jsonutils.Marshal(input))
  63. if err != nil {
  64. return err
  65. }
  66. printObject(ret)
  67. }
  68. return nil
  69. })
  70. type NotificationInput struct {
  71. ID string `help:"Id of notification"`
  72. }
  73. R(&NotificationInput{}, "notify-show", "Show a notify message", func(s *mcclient.ClientSession, args *NotificationInput) error {
  74. ret, err := modules.Notification.Get(s, args.ID, nil)
  75. if err != nil {
  76. return nil
  77. }
  78. printObject(ret)
  79. return nil
  80. })
  81. type NotificationListInput struct {
  82. options.BaseListOptions
  83. ContactType string `help:"contact_type"`
  84. ReceiverId string `help:"receiver_id"`
  85. TopicType string `help:"topic type"`
  86. }
  87. R(&NotificationListInput{}, "notify-list", "List notify message", func(s *mcclient.ClientSession, args *NotificationListInput) error {
  88. params, err := options.ListStructToParams(args)
  89. if err != nil {
  90. return err
  91. }
  92. ret, err := modules.Notification.List(s, params)
  93. if err != nil {
  94. return err
  95. }
  96. printList(ret, modules.Notification.GetColumns(s))
  97. return nil
  98. })
  99. type NotificationEventInput struct {
  100. AdvanceDays int
  101. Event string
  102. Priority string
  103. MsgBody string
  104. ResourceType string
  105. Action string
  106. Contacts string
  107. IsFailed string
  108. }
  109. R(&NotificationEventInput{}, "notify-event-send", "Send notify event message", func(s *mcclient.ClientSession, args *NotificationEventInput) error {
  110. body, err := jsonutils.ParseString(args.MsgBody)
  111. if err != nil {
  112. return err
  113. }
  114. dict, ok := body.(*jsonutils.JSONDict)
  115. if !ok {
  116. return fmt.Errorf("msg_body should be a json string, like '{'name': 'hello'}'")
  117. }
  118. params := api.NotificationManagerEventNotifyInput{
  119. AdvanceDays: args.AdvanceDays,
  120. ReceiverIds: []string{},
  121. ResourceDetails: dict,
  122. Event: args.Event,
  123. Priority: args.Priority,
  124. ResourceType: args.ResourceType,
  125. Action: api.SAction(args.Action),
  126. IsFailed: api.SResult(args.IsFailed),
  127. }
  128. _, err = modules.Notification.PerformClassAction(s, "event-notify", jsonutils.Marshal(params))
  129. if err != nil {
  130. return fmt.Errorf("unable to EventNotify: %s", err)
  131. }
  132. return nil
  133. })
  134. type NotificationContactInput struct {
  135. Subject string
  136. Body string
  137. ContactType []string
  138. ReceiverIds []string
  139. RobotIds []string
  140. RoleIds []string
  141. }
  142. R(&NotificationContactInput{}, "notify-contact-send", "Send notify event message", func(s *mcclient.ClientSession, args *NotificationContactInput) error {
  143. params := api.NotificationManagerContactNotifyInput{
  144. Subject: args.Subject,
  145. Body: args.Body,
  146. ReceiverIds: args.ReceiverIds,
  147. ContactTypes: args.ContactType,
  148. RobotIds: args.RobotIds,
  149. RoleIds: args.RoleIds,
  150. }
  151. _, err := modules.Notification.PerformClassAction(s, "contact-notify", jsonutils.Marshal(params))
  152. if err != nil {
  153. return fmt.Errorf("unable to ContactNotify: %s", err)
  154. }
  155. return nil
  156. })
  157. }