mobile.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/onecloud/pkg/apis/notify"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/notify/models"
  22. )
  23. type SMobileSender struct {
  24. config map[string]api.SNotifyConfigContent
  25. }
  26. func (smsSender *SMobileSender) GetSenderType() string {
  27. return api.MOBILE
  28. }
  29. func (smsSender *SMobileSender) Send(ctx context.Context, args api.SendParams) error {
  30. smsSendParams := api.SSMSSendParams{
  31. TemplateParas: args.Message,
  32. To: args.Receivers.Contact,
  33. RemoteTemplate: args.RemoteTemplate,
  34. RemoteTemplateParam: args.RemoteTemplateParam,
  35. }
  36. config, ok := models.ConfigMap[api.MOBILE]
  37. if !ok {
  38. return errors.Wrapf(errors.ErrNotFound, "no set %s config", api.MOBILE)
  39. }
  40. if config.Content == nil {
  41. return errors.Wrapf(errors.ErrNotFound, "no %s content found", api.MOBILE)
  42. }
  43. smsdriver := models.GetSMSDriver(config.Content.SmsDriver)
  44. return smsdriver.Send(smsSendParams, false, &api.NotifyConfig{
  45. SNotifyConfigContent: *config.Content,
  46. Attribution: config.Attribution,
  47. DomainId: config.DomainId,
  48. })
  49. }
  50. func (smsSender *SMobileSender) ValidateConfig(ctx context.Context, config api.NotifyConfig) (string, error) {
  51. driver := models.GetSMSDriver(config.SmsDriver)
  52. if driver == nil {
  53. return "", errors.Wrap(errors.ErrNotFound, "driver disabled")
  54. }
  55. return "", driver.Verify(&config)
  56. }
  57. func (smsSender *SMobileSender) UpdateConfig(config api.NotifyConfig) error {
  58. q := models.ConfigManager.Query()
  59. q = q.Equals("type", api.MOBILE)
  60. confs := []models.SConfig{}
  61. db.FetchModelObjects(models.ConfigManager, q, &confs)
  62. if len(confs) == 0 {
  63. return errors.Wrapf(errors.ErrNotFound, "config type:%s", api.MOBILE)
  64. }
  65. _, err := db.Update(&confs[0], func() error {
  66. confs[0].Content = &config.SNotifyConfigContent
  67. return nil
  68. })
  69. if err != nil {
  70. return errors.Wrap(err, "update config")
  71. }
  72. models.ConfigMap[api.MOBILE] = models.SConfig{
  73. Content: &config.SNotifyConfigContent,
  74. }
  75. return nil
  76. }
  77. func (smsSender *SMobileSender) AddConfig(config api.NotifyConfig) error {
  78. return cloudprovider.ErrNotImplemented
  79. }
  80. func (smsSender *SMobileSender) DeleteConfig(config api.NotifyConfig) error {
  81. return cloudprovider.ErrNotImplemented
  82. }
  83. func (smsSender *SMobileSender) ContactByMobile(ctx context.Context, mobile, domainId string) (string, error) {
  84. return "", nil
  85. }
  86. func (smsSender *SMobileSender) IsPersonal() bool {
  87. return true
  88. }
  89. func (smsSender *SMobileSender) IsRobot() bool {
  90. return false
  91. }
  92. func (smsSender *SMobileSender) IsValid() bool {
  93. return len(smsSender.config) > 0
  94. }
  95. func (smsSender *SMobileSender) IsPullType() bool {
  96. return false
  97. }
  98. func (smsSender *SMobileSender) IsSystemConfigContactType() bool {
  99. return true
  100. }
  101. func (smsSender *SMobileSender) GetAccessToken(ctx context.Context, key string) error {
  102. return nil
  103. }
  104. func (smsSender *SMobileSender) RegisterConfig(config models.SConfig) {
  105. models.ConfigMap[config.Type] = config
  106. }
  107. func init() {
  108. models.Register(&SMobileSender{
  109. config: map[string]api.SNotifyConfigContent{},
  110. })
  111. }