websocket.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 sender
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/onecloud/pkg/ansibleserver/options"
  22. api "yunion.io/x/onecloud/pkg/apis/notify"
  23. "yunion.io/x/onecloud/pkg/mcclient/auth"
  24. modules "yunion.io/x/onecloud/pkg/mcclient/modules/websocket"
  25. "yunion.io/x/onecloud/pkg/notify/models"
  26. )
  27. type SWebsocketSender struct {
  28. config map[string]api.SNotifyConfigContent
  29. }
  30. func (websocket *SWebsocketSender) GetSenderType() string {
  31. return api.WEBSOCKET
  32. }
  33. func (websocket *SWebsocketSender) Send(ctx context.Context, params api.SendParams) error {
  34. return websocket.send(ctx, params)
  35. }
  36. func (websocket *SWebsocketSender) send(ctx context.Context, args api.SendParams) error {
  37. params := jsonutils.NewDict()
  38. params.Add(jsonutils.NewString("notify"), "obj_type")
  39. params.Add(jsonutils.NewString(""), "obj_id")
  40. params.Add(jsonutils.NewString(""), "obj_name")
  41. params.Add(jsonutils.JSONTrue, "success")
  42. // component request body
  43. body := jsonutils.DeepCopy(params).(*jsonutils.JSONDict)
  44. body.Add(jsonutils.NewString(args.Title), "action")
  45. body.Add(jsonutils.NewString(fmt.Sprintf("priority=%s; content=%s", args.Priority, args.Message)), "notes")
  46. body.Add(jsonutils.NewString(args.Receivers.Contact), "user_id")
  47. body.Add(jsonutils.NewString(args.Receivers.Contact), "user")
  48. if len(args.Receivers.Contact) == 0 {
  49. body.Add(jsonutils.JSONTrue, "broadcast")
  50. }
  51. if websocket.isFailed(args.Title, args.Message) {
  52. body.Add(jsonutils.JSONFalse, "success")
  53. }
  54. session := auth.GetAdminSession(context.Background(), options.Options.Region)
  55. _, err := modules.Websockets.Create(session, body)
  56. if err != nil {
  57. // failed
  58. _, err = modules.Websockets.Create(session, body)
  59. return err
  60. }
  61. return nil
  62. }
  63. func (websocket *SWebsocketSender) isFailed(title, message string) bool {
  64. for _, c := range []string{title, message} {
  65. for _, k := range FAIL_KEY {
  66. if strings.Contains(c, k) {
  67. return true
  68. }
  69. }
  70. }
  71. return false
  72. }
  73. func (websocket *SWebsocketSender) IsPersonal() bool {
  74. return true
  75. }
  76. func (websocket *SWebsocketSender) IsRobot() bool {
  77. return false
  78. }
  79. func (websocket *SWebsocketSender) IsValid() bool {
  80. return true
  81. }
  82. func (websocket *SWebsocketSender) IsPullType() bool {
  83. return true
  84. }
  85. func (websocket *SWebsocketSender) IsSystemConfigContactType() bool {
  86. return true
  87. }
  88. func (websocket *SWebsocketSender) ContactByMobile(ctx context.Context, mobile, domainId string) (string, error) {
  89. return "", nil
  90. }
  91. func (websocket *SWebsocketSender) GetAccessToken(ctx context.Context, key string) error {
  92. return nil
  93. }
  94. func (websocket *SWebsocketSender) ValidateConfig(ctx context.Context, config api.NotifyConfig) (string, error) {
  95. return "", cloudprovider.ErrNotImplemented
  96. }
  97. func (websocket *SWebsocketSender) RegisterConfig(config models.SConfig) {
  98. }
  99. func init() {
  100. models.Register(&SWebsocketSender{
  101. config: map[string]api.SNotifyConfigContent{},
  102. })
  103. }