mod_notify.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/util/httputils"
  20. notify_apis "yunion.io/x/onecloud/pkg/apis/notify"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  23. "yunion.io/x/onecloud/pkg/mcclient/modules"
  24. )
  25. type ReceiverManager struct {
  26. modulebase.ResourceManager
  27. }
  28. type ConfigsManager struct {
  29. modulebase.ResourceManager
  30. }
  31. var (
  32. NotifyReceiver ReceiverManager
  33. NotifyConfig modulebase.ResourceManager
  34. NotifyRobot modulebase.ResourceManager
  35. Notification modulebase.ResourceManager
  36. NotifyTemplate modulebase.ResourceManager
  37. NotifyTopic modulebase.ResourceManager
  38. NotifySubscriber modulebase.ResourceManager
  39. Configs ConfigsManager
  40. )
  41. func (rm *ReceiverManager) SyncUserContact(s *mcclient.ClientSession, userId string, mobile string, email string) error {
  42. recv, err := rm.GetById(s, userId, nil)
  43. if err != nil {
  44. if httputils.ErrorCode(err) == 404 {
  45. // not created yet, do create
  46. newRecv := notify_apis.ReceiverCreateInput{
  47. UID: userId,
  48. }
  49. if len(mobile) > 0 {
  50. newRecv.InternationalMobile.Mobile = mobile
  51. newRecv.EnabledContactTypes = append(newRecv.EnabledContactTypes, "mobile")
  52. }
  53. if len(email) > 0 {
  54. newRecv.Email = email
  55. newRecv.EnabledContactTypes = append(newRecv.EnabledContactTypes, "email")
  56. }
  57. _, err := rm.Create(s, jsonutils.Marshal(newRecv))
  58. if err != nil {
  59. // create failed
  60. return errors.Wrap(err, "create receiver")
  61. }
  62. // success
  63. } else {
  64. return errors.Wrap(err, "GetById")
  65. }
  66. } else {
  67. // receiver exists
  68. fmt.Println("receiver exists", recv.String())
  69. recvObj := notify_apis.ReceiverDetails{}
  70. err := recv.Unmarshal(&recvObj)
  71. if err != nil {
  72. return errors.Wrap(err, "Unmarshal ReceiverDetails")
  73. }
  74. updateRecv := notify_apis.ReceiverUpdateInput{}
  75. changeMobile := false
  76. changeEmail := false
  77. if mobile != "" && recvObj.InternationalMobile.Mobile != mobile {
  78. updateRecv.InternationalMobile.Mobile = mobile
  79. updateRecv.EnabledContactTypes = append(updateRecv.EnabledContactTypes, "mobile")
  80. changeMobile = true
  81. }
  82. if email != "" && recvObj.Email != email {
  83. updateRecv.Email = email
  84. updateRecv.EnabledContactTypes = append(updateRecv.EnabledContactTypes, "email")
  85. changeEmail = true
  86. }
  87. if changeMobile || changeEmail {
  88. _, err = rm.Update(s, userId, jsonutils.Marshal(updateRecv))
  89. if err != nil {
  90. return errors.Wrap(err, "update receiver")
  91. }
  92. }
  93. }
  94. return nil
  95. }
  96. func init() {
  97. NotifyReceiver = ReceiverManager{
  98. ResourceManager: modules.NewNotifyv2Manager(
  99. "receiver",
  100. "receivers",
  101. []string{"ID", "Name", "Domain_Id", "Project_Domain", "Email", "International_Mobile", "Enabled_Contact_Types", "Verified_Infos"},
  102. []string{},
  103. ),
  104. }
  105. modules.Register(&NotifyReceiver)
  106. NotifyConfig = modules.NewNotifyv2Manager(
  107. "notifyconfig",
  108. "notifyconfigs",
  109. []string{"Name", "Type", "Content", "Attribution", "Project_Domain"},
  110. []string{},
  111. )
  112. modules.Register(&NotifyConfig)
  113. NotifyRobot = modules.NewNotifyv2Manager(
  114. "robot",
  115. "robots",
  116. []string{"ID", "Name", "Type", "Address", "Lang"},
  117. []string{},
  118. )
  119. modules.Register(&NotifyRobot)
  120. Notification = modules.NewNotifyv2Manager(
  121. "notification",
  122. "notifications",
  123. []string{"ID", "Name", "Contact_Type", "Title", "Content", "Priority", "Status", "Received_At", "Receiver_Type"},
  124. []string{},
  125. )
  126. modules.Register(&Notification)
  127. NotifyTemplate = modules.NewNotifyv2Manager(
  128. "notifytemplate",
  129. "notifytemplates",
  130. []string{"ID", "Name", "Contact_Type", "Topic", "Template_Type", "Content", "Example"},
  131. []string{},
  132. )
  133. modules.Register(&NotifyTemplate)
  134. NotifyTopic = modules.NewNotifyv2Manager(
  135. "topic",
  136. "topics",
  137. []string{"ID", "Name", "Type", "Enabled", "Resources"},
  138. []string{},
  139. )
  140. modules.Register(&NotifyTopic)
  141. NotifySubscriber = modules.NewNotifyv2Manager(
  142. "subscriber",
  143. "subscribers",
  144. []string{"ID", "Name", "Topic_Id", "Type", "Resource_Scope", "Role_Scope", "Receivers", "Role", "Robot"},
  145. []string{},
  146. )
  147. modules.Register(&NotifySubscriber)
  148. // important: Notifications' init must be behind Notication's init
  149. Notifications = NotificationManager{
  150. Notification,
  151. }
  152. }