Jelajahi Sumber

Instantiating notifiers from config before using

Pavel Bakulev 7 tahun lalu
induk
melakukan
5c10a897f8

+ 20 - 17
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
+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

+ 1 - 0
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

+ 2 - 10
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 {

+ 9 - 1
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
 			}

+ 16 - 5
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")
+		})
+
 	})
 }
 

+ 3 - 0
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:

+ 7 - 0
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

+ 3 - 1
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
+    is_default: true
+    settings:
+      url: https://slack.com

+ 3 - 1
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
+    is_default: true
+    settings:
+      addresses: example@example.com

+ 9 - 0
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

+ 5 - 1
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
+    type: email
+    settings:
+      addresses: example@example.com

+ 12 - 0
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
+}