diff --git a/conf/provisioning/alert_notifications/sample.yaml b/conf/provisioning/alert_notifications/sample.yaml index 92d03a81720..9af25277738 100644 --- a/conf/provisioning/alert_notifications/sample.yaml +++ b/conf/provisioning/alert_notifications/sample.yaml @@ -1,20 +1,23 @@ # # config file version apiVersion: 1 -# alert_notifications: -# - name: default-slack -# type: slack -# org_id: 1 -# is_default: true -# settings: -# recipient: "XXX" -# token: "xoxb" -# uploadImage: true -# - name: default-email -# type: email -# org_id: 1 -# is_default: false -# delete_alert_notifications: -# - name: default-slack -# org_id: 1 -# - name: default-email \ No newline at end of file +alert_notifications: + - name: default-slack + type: slack + org_id: 1 + is_default: true + settings: + recipient: "XXX" + token: "xoxb" + uploadImage: true + url: https://slack.com + - name: default-email + type: email + org_id: 1 + is_default: false + settings: + addresses: example@example.com +delete_alert_notifications: + - name: default-slack + org_id: 1 + - name: default-email \ No newline at end of file diff --git a/docs/sources/administration/provisioning.md b/docs/sources/administration/provisioning.md index a926f926ca9..065db762e52 100644 --- a/docs/sources/administration/provisioning.md +++ b/docs/sources/administration/provisioning.md @@ -278,6 +278,7 @@ alert_notifications: recipient: "XXX" token: "xoxb" uploadImage: true + url: https://slack.com delete_alert_notifications: - name: notification-channel-1 diff --git a/pkg/services/provisioning/alert_notifications/alert_notifications.go b/pkg/services/provisioning/alert_notifications/alert_notifications.go index 2fa899dcd20..53b28d93c52 100644 --- a/pkg/services/provisioning/alert_notifications/alert_notifications.go +++ b/pkg/services/provisioning/alert_notifications/alert_notifications.go @@ -4,7 +4,6 @@ import ( "errors" "github.com/grafana/grafana/pkg/bus" - "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/models" ) @@ -92,20 +91,13 @@ func (dc *NotificationProvisioner) mergeNotifications(notificationToMerge []*not return err } - settings := simplejson.New() - if len(notification.Settings) > 0 { - for k, v := range notification.Settings { - settings.Set(k, v) - } - } - if cmd.Result == nil { dc.log.Info("Inserting alert notification from configuration ", "name", notification.Name) insertCmd := &models.CreateAlertNotificationCommand{ Name: notification.Name, Type: notification.Type, IsDefault: notification.IsDefault, - Settings: settings, + Settings: notification.SettingsToJson(), OrgId: notification.OrgId, } if err := bus.Dispatch(insertCmd); err != nil { @@ -118,7 +110,7 @@ func (dc *NotificationProvisioner) mergeNotifications(notificationToMerge []*not Name: notification.Name, Type: notification.Type, IsDefault: notification.IsDefault, - Settings: settings, + Settings: notification.SettingsToJson(), OrgId: notification.OrgId, } if err := bus.Dispatch(updateCmd); err != nil { diff --git a/pkg/services/provisioning/alert_notifications/config_reader.go b/pkg/services/provisioning/alert_notifications/config_reader.go index 8d38bc76a1f..058159e49a4 100644 --- a/pkg/services/provisioning/alert_notifications/config_reader.go +++ b/pkg/services/provisioning/alert_notifications/config_reader.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/grafana/grafana/pkg/log" + m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" "gopkg.in/yaml.v2" ) @@ -109,10 +110,17 @@ func validateType(notifications []*notificationsAsConfig) error { for _, notifier := range notifierTypes { if notifier.Type == notification.Type { foundNotifier = true + _, notifierError := notifier.Factory(&m.AlertNotification{ + Name: notification.Name, + Settings: notification.SettingsToJson(), + Type: notification.Type, + }) + if notifierError != nil { + return notifierError + } break } } - if !foundNotifier { return ErrInvalidNotifierType } diff --git a/pkg/services/provisioning/alert_notifications/config_reader_test.go b/pkg/services/provisioning/alert_notifications/config_reader_test.go index 11dddd3e475..274335e82bb 100644 --- a/pkg/services/provisioning/alert_notifications/config_reader_test.go +++ b/pkg/services/provisioning/alert_notifications/config_reader_test.go @@ -7,6 +7,7 @@ import ( "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" + "github.com/grafana/grafana/pkg/services/alerting/notifiers" . "github.com/smartystreets/goconvey/convey" ) @@ -14,6 +15,7 @@ var ( logger = log.New("fake.log") correct_properties = "./test-configs/correct-properties" + incorrect_properties = "./test-configs/incorrect-properties" correct_properties_with_orgName = "./test-configs/correct-properties-with-orgName" brokenYaml = "./test-configs/broken-yaml" doubleNotificationsConfig = "./test-configs/double-default" @@ -36,12 +38,14 @@ func TestNotificationAsConfig(t *testing.T) { bus.AddHandler("test", mockGetOrg) alerting.RegisterNotifier(&alerting.NotifierPlugin{ - Type: "slack", - Name: "slack", + Type: "slack", + Name: "slack", + Factory: notifiers.NewSlackNotifier, }) alerting.RegisterNotifier(&alerting.NotifierPlugin{ - Type: "email", - Name: "email", + Type: "email", + Name: "email", + Factory: notifiers.NewEmailNotifier, }) Convey("Can read correct properties", func() { cfgProvifer := &configReader{log: log.New("test logger")} @@ -61,7 +65,7 @@ func TestNotificationAsConfig(t *testing.T) { So(nt.OrgId, ShouldEqual, 2) So(nt.IsDefault, ShouldBeTrue) So(nt.Settings, ShouldResemble, map[string]interface{}{ - "recipient": "XXX", "token": "xoxb", "uploadImage": true, + "recipient": "XXX", "token": "xoxb", "uploadImage": true, "url": "https://slack.com", }) nt = nts[1] @@ -218,6 +222,13 @@ func TestNotificationAsConfig(t *testing.T) { So(err, ShouldEqual, ErrInvalidNotifierType) }) + Convey("Read incorrect properties", func() { + cfgProvifer := &configReader{log: log.New("test logger")} + _, err := cfgProvifer.readConfig(incorrect_properties) + So(err, ShouldNotBeNil) + So(err.Error(), ShouldEqual, "Alert validation error: Could not find url property in settings") + }) + }) } diff --git a/pkg/services/provisioning/alert_notifications/test-configs/correct-properties-with-orgName/correct-properties-with-orgName.yaml b/pkg/services/provisioning/alert_notifications/test-configs/correct-properties-with-orgName/correct-properties-with-orgName.yaml index eafca5e77de..45631c38b9f 100644 --- a/pkg/services/provisioning/alert_notifications/test-configs/correct-properties-with-orgName/correct-properties-with-orgName.yaml +++ b/pkg/services/provisioning/alert_notifications/test-configs/correct-properties-with-orgName/correct-properties-with-orgName.yaml @@ -7,8 +7,11 @@ alert_notifications: recipient: "XXX" token: "xoxb" uploadImage: true + url: https://slack.com - name: another-not-default-notification type: email + settings: + addresses: example@example.com org_name: Main Org. 2 is_default: false delete_alert_notifications: diff --git a/pkg/services/provisioning/alert_notifications/test-configs/correct-properties/correct-properties.yaml b/pkg/services/provisioning/alert_notifications/test-configs/correct-properties/correct-properties.yaml index d18f538d4ee..c4b7d7cb224 100644 --- a/pkg/services/provisioning/alert_notifications/test-configs/correct-properties/correct-properties.yaml +++ b/pkg/services/provisioning/alert_notifications/test-configs/correct-properties/correct-properties.yaml @@ -7,16 +7,23 @@ alert_notifications: recipient: "XXX" token: "xoxb" uploadImage: true + url: https://slack.com - name: another-not-default-notification type: email + settings: + addresses: example@exmaple.com org_id: 3 is_default: false - name: check-unset-is_default-is-false type: slack org_id: 3 + settings: + url: https://slack.com - name: Added notification with whitespaces in name type: email org_id: 3 + settings: + addresses: example@exmaple.com delete_alert_notifications: - name: default-slack-notification org_id: 2 diff --git a/pkg/services/provisioning/alert_notifications/test-configs/double-default/default-1.yml b/pkg/services/provisioning/alert_notifications/test-configs/double-default/default-1.yml index aaa4541b993..ca6c347433f 100644 --- a/pkg/services/provisioning/alert_notifications/test-configs/double-default/default-1.yml +++ b/pkg/services/provisioning/alert_notifications/test-configs/double-default/default-1.yml @@ -1,4 +1,6 @@ alert_notifications: - name: first-default type: slack - is_default: true \ No newline at end of file + is_default: true + settings: + url: https://slack.com \ No newline at end of file diff --git a/pkg/services/provisioning/alert_notifications/test-configs/double-default/default-2.yaml b/pkg/services/provisioning/alert_notifications/test-configs/double-default/default-2.yaml index 0636316614a..8b07e4ca9c4 100644 --- a/pkg/services/provisioning/alert_notifications/test-configs/double-default/default-2.yaml +++ b/pkg/services/provisioning/alert_notifications/test-configs/double-default/default-2.yaml @@ -1,4 +1,6 @@ alert_notifications: - name: second-default type: email - is_default: true \ No newline at end of file + is_default: true + settings: + addresses: example@example.com \ No newline at end of file diff --git a/pkg/services/provisioning/alert_notifications/test-configs/incorrect-properties/incorrect-properties.yaml b/pkg/services/provisioning/alert_notifications/test-configs/incorrect-properties/incorrect-properties.yaml new file mode 100644 index 00000000000..f01234564e0 --- /dev/null +++ b/pkg/services/provisioning/alert_notifications/test-configs/incorrect-properties/incorrect-properties.yaml @@ -0,0 +1,9 @@ +alert_notifications: + - name: slack-notification-without-url-in-settings + type: slack + org_id: 2 + is_default: true + settings: + recipient: "XXX" + token: "xoxb" + uploadImage: true \ No newline at end of file diff --git a/pkg/services/provisioning/alert_notifications/test-configs/two-notifications/two-notifications.yaml b/pkg/services/provisioning/alert_notifications/test-configs/two-notifications/two-notifications.yaml index a5e34d92fd5..dd93c815b04 100644 --- a/pkg/services/provisioning/alert_notifications/test-configs/two-notifications/two-notifications.yaml +++ b/pkg/services/provisioning/alert_notifications/test-configs/two-notifications/two-notifications.yaml @@ -1,5 +1,9 @@ alert_notifications: - name: channel1 type: slack + settings: + url: http://slack.com - name: channel2 - type: email \ No newline at end of file + type: email + settings: + addresses: example@example.com \ No newline at end of file diff --git a/pkg/services/provisioning/alert_notifications/types.go b/pkg/services/provisioning/alert_notifications/types.go index 469ce1a3586..7ac2e6900e5 100644 --- a/pkg/services/provisioning/alert_notifications/types.go +++ b/pkg/services/provisioning/alert_notifications/types.go @@ -1,5 +1,7 @@ package alert_notifications +import "github.com/grafana/grafana/pkg/components/simplejson" + type notificationsAsConfig struct { Notifications []*notificationFromConfig `json:"alert_notifications" yaml:"alert_notifications"` DeleteNotifications []*deleteNotificationConfig `json:"delete_alert_notifications" yaml:"delete_alert_notifications"` @@ -19,3 +21,13 @@ type notificationFromConfig struct { IsDefault bool `json:"is_default" yaml:"is_default"` Settings map[string]interface{} `json:"settings" yaml:"settings"` } + +func (notification notificationFromConfig) SettingsToJson() *simplejson.Json { + settings := simplejson.New() + if len(notification.Settings) > 0 { + for k, v := range notification.Settings { + settings.Set(k, v) + } + } + return settings +}