plugindriver.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 models
  15. import (
  16. "context"
  17. api "yunion.io/x/onecloud/pkg/apis/notify"
  18. )
  19. type ISenderDriver interface {
  20. GetSenderType() string
  21. Send(ctx context.Context, args api.SendParams) error
  22. ValidateConfig(ctx context.Context, args api.NotifyConfig) (string, error)
  23. ContactByMobile(ctx context.Context, mobile, domainId string) (string, error)
  24. IsRobot() bool
  25. IsPersonal() bool
  26. IsSystemConfigContactType() bool
  27. IsValid() bool
  28. IsPullType() bool
  29. GetAccessToken(ctx context.Context, domainId string) error
  30. RegisterConfig(config SConfig)
  31. }
  32. var (
  33. driverTable = make(map[string]ISenderDriver)
  34. )
  35. func Register(driver ISenderDriver) {
  36. driverTable[driver.GetSenderType()] = driver
  37. }
  38. func GetSenderTypes() []string {
  39. ret := []string{}
  40. for k := range driverTable {
  41. ret = append(ret, k)
  42. }
  43. return ret
  44. }
  45. func GetRobotTypes() []string {
  46. ret := []string{}
  47. for k := range driverTable {
  48. if driverTable[k].IsRobot() {
  49. ret = append(ret, k)
  50. }
  51. }
  52. return ret
  53. }
  54. func GetValidPersonalSenderTypes() []string {
  55. ret := []string{}
  56. for k := range driverTable {
  57. if driverTable[k].IsValid() && driverTable[k].IsPersonal() {
  58. ret = append(ret, k)
  59. }
  60. }
  61. return ret
  62. }
  63. func GetDriver(sendType string) ISenderDriver {
  64. driver := driverTable[sendType]
  65. return driver
  66. }