mod_notification.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/pkg/util/sets"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  21. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  22. )
  23. var (
  24. Notifications NotificationManager
  25. )
  26. type SNotifyMessage struct {
  27. Uid []string `json:"uid,omitempty"`
  28. Gid []string `json:"gid,omitempty"`
  29. Robots []string `json:"robots,omitempty"`
  30. ContactType TNotifyChannel `json:"contact_type,omitempty"`
  31. Contacts []string `json:"contracts"`
  32. Topic string `json:"topic,omitempty"`
  33. Priority TNotifyPriority `json:"priority,omitempty"`
  34. Msg string `json:"msg,omitempty"`
  35. Remark string `json:"remark,omitempty"`
  36. Broadcast bool `json:"broadcast,omitempty"`
  37. Tag string `json:"tag"`
  38. Metadata map[string]interface{} `json:"metadata"`
  39. IgnoreNonexistentReceiver bool `json:"ignore_nonexistent_receiver"`
  40. }
  41. type SNotifyV2Message struct {
  42. Receivers []string `json:"receivers"`
  43. Contacts []string `json:"contacts"`
  44. Robots []string `json:"robots"`
  45. ContactType string `json:"contact_type"`
  46. Topic string `json:"topic"`
  47. Priority string `json:"priority"`
  48. Message string `json:"message"`
  49. Tag string `json:"tag"`
  50. Metadata map[string]interface{} `json:"metadata"`
  51. IgnoreNonexistentReceiver bool `json:"ignore_nonexistent_receiver"`
  52. }
  53. type NotificationManager struct {
  54. modulebase.ResourceManager
  55. }
  56. func (manager *NotificationManager) Send(s *mcclient.ClientSession, msg SNotifyMessage) error {
  57. receiverIds := make([]string, 0, len(msg.Uid))
  58. if len(msg.Gid) > 0 {
  59. // fetch uid
  60. uidSet := sets.NewString()
  61. for _, gid := range msg.Gid {
  62. users, err := identity.Groups.GetUsers(s, gid, nil)
  63. if err != nil {
  64. return errors.Wrapf(err, "Groups.GetUsers for group %q", gid)
  65. }
  66. for i := range users.Data {
  67. id, _ := users.Data[i].GetString("id")
  68. uidSet.Insert(id)
  69. }
  70. }
  71. receiverIds = append(receiverIds, uidSet.UnsortedList()...)
  72. }
  73. receiverIds = append(receiverIds, msg.Uid...)
  74. v2msg := SNotifyV2Message{
  75. Receivers: receiverIds,
  76. Contacts: msg.Contacts,
  77. Robots: msg.Robots,
  78. ContactType: string(msg.ContactType),
  79. Topic: msg.Topic,
  80. Priority: string(msg.Priority),
  81. Message: msg.Msg,
  82. Tag: msg.Tag,
  83. Metadata: msg.Metadata,
  84. IgnoreNonexistentReceiver: msg.IgnoreNonexistentReceiver,
  85. }
  86. params := jsonutils.Marshal(&v2msg)
  87. _, err := manager.Create(s, params)
  88. return err
  89. }