config_reader.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package alert_notifications
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/grafana/grafana/pkg/log"
  8. "github.com/grafana/grafana/pkg/services/alerting"
  9. "gopkg.in/yaml.v2"
  10. )
  11. type configReader struct {
  12. log log.Logger
  13. }
  14. func (cr *configReader) readConfig(path string) ([]*notificationsAsConfig, error) {
  15. var notifications []*notificationsAsConfig
  16. cr.log.Debug("Looking for alert notification provisioning files", "path", path)
  17. files, err := ioutil.ReadDir(path)
  18. if err != nil {
  19. cr.log.Error("Can't read alert notification provisioning files from directory", "path", path)
  20. return notifications, nil
  21. }
  22. for _, file := range files {
  23. if strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml") {
  24. cr.log.Debug("Parsing alert notifications provisioning file", "path", path, "file.Name", file.Name())
  25. notifs, err := cr.parseNotificationConfig(path, file)
  26. if err != nil {
  27. return nil, err
  28. }
  29. if notifs != nil {
  30. notifications = append(notifications, notifs)
  31. }
  32. }
  33. }
  34. cr.log.Debug("Validating alert notifications")
  35. err = validateDefaultUniqueness(notifications)
  36. if err != nil {
  37. return nil, err
  38. }
  39. err = validateType(notifications)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return notifications, nil
  44. }
  45. func (cr *configReader) parseNotificationConfig(path string, file os.FileInfo) (*notificationsAsConfig, error) {
  46. filename, _ := filepath.Abs(filepath.Join(path, file.Name()))
  47. yamlFile, err := ioutil.ReadFile(filename)
  48. if err != nil {
  49. return nil, err
  50. }
  51. var cfg *notificationsAsConfig
  52. err = yaml.Unmarshal(yamlFile, &cfg)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return cfg.mapToNotificationFromConfig(), nil
  57. }
  58. func validateDefaultUniqueness(notifications []*notificationsAsConfig) error {
  59. for i := range notifications {
  60. for _, notification := range notifications[i].Notifications {
  61. if notification.OrgId < 1 {
  62. if notification.OrgName == "" {
  63. notification.OrgId = 1
  64. } else {
  65. notification.OrgId = 0
  66. }
  67. }
  68. }
  69. for _, notification := range notifications[i].DeleteNotifications {
  70. if notification.OrgId < 1 {
  71. if notification.OrgName == "" {
  72. notification.OrgId = 1
  73. } else {
  74. notification.OrgId = 0
  75. }
  76. }
  77. }
  78. }
  79. return nil
  80. }
  81. func validateType(notifications []*notificationsAsConfig) error {
  82. notifierTypes := alerting.GetNotifiers()
  83. for i := range notifications {
  84. if notifications[i].Notifications == nil {
  85. continue
  86. }
  87. for _, notification := range notifications[i].Notifications {
  88. foundNotifier := false
  89. for _, notifier := range notifierTypes {
  90. if notifier.Type == notification.Type {
  91. foundNotifier = true
  92. break
  93. }
  94. }
  95. if !foundNotifier {
  96. return ErrInvalidNotifierType
  97. }
  98. }
  99. }
  100. return nil
  101. }