Procházet zdrojové kódy

feat(alerting): make some settings properties required

bergquist před 9 roky
rodič
revize
6705efef6f

+ 25 - 6
pkg/services/alerting/notifier.go

@@ -1,6 +1,8 @@
 package alerting
 package alerting
 
 
 import (
 import (
+	"fmt"
+
 	"github.com/grafana/grafana/pkg/bus"
 	"github.com/grafana/grafana/pkg/bus"
 	"github.com/grafana/grafana/pkg/components/simplejson"
 	"github.com/grafana/grafana/pkg/components/simplejson"
 	"github.com/grafana/grafana/pkg/log"
 	"github.com/grafana/grafana/pkg/log"
@@ -108,27 +110,44 @@ func (n *NotifierImpl) getNotifiers(orgId int64, notificationGroups []int64) []*
 }
 }
 
 
 func NewNotificationFromDBModel(model *m.AlertNotification) (*Notification, error) {
 func NewNotificationFromDBModel(model *m.AlertNotification) (*Notification, error) {
+	notifier, err := createNotifier(model.Type, model.Settings)
+
+	if err != nil {
+		return nil, err
+	}
+
 	return &Notification{
 	return &Notification{
 		Name:         model.Name,
 		Name:         model.Name,
 		Type:         model.Type,
 		Type:         model.Type,
-		Notifierr:    createNotifier(model.Type, model.Settings),
+		Notifierr:    notifier,
 		SendCritical: !model.Settings.Get("ignoreCrit").MustBool(),
 		SendCritical: !model.Settings.Get("ignoreCrit").MustBool(),
 		SendWarning:  !model.Settings.Get("ignoreWarn").MustBool(),
 		SendWarning:  !model.Settings.Get("ignoreWarn").MustBool(),
 	}, nil
 	}, nil
 }
 }
 
 
-var createNotifier = func(notificationType string, settings *simplejson.Json) NotificationDispatcher {
+var createNotifier = func(notificationType string, settings *simplejson.Json) (NotificationDispatcher, error) {
 	if notificationType == "email" {
 	if notificationType == "email" {
+		to := settings.Get("to").MustString()
+
+		if to == "" {
+			return nil, fmt.Errorf("Could not find to propertie in settings")
+		}
+
 		return &EmailNotifier{
 		return &EmailNotifier{
-			To:  settings.Get("to").MustString(),
+			To:  to,
 			log: log.New("alerting.notification.email"),
 			log: log.New("alerting.notification.email"),
-		}
+		}, nil
+	}
+
+	url := settings.Get("url").MustString()
+	if url == "" {
+		return nil, fmt.Errorf("Could not find url propertie in settings")
 	}
 	}
 
 
 	return &WebhookNotifier{
 	return &WebhookNotifier{
-		Url:      settings.Get("url").MustString(),
+		Url:      url,
 		User:     settings.Get("user").MustString(),
 		User:     settings.Get("user").MustString(),
 		Password: settings.Get("password").MustString(),
 		Password: settings.Get("password").MustString(),
 		log:      log.New("alerting.notification.webhook"),
 		log:      log.New("alerting.notification.webhook"),
-	}
+	}, nil
 }
 }

+ 79 - 46
pkg/services/alerting/notifier_test.go

@@ -13,54 +13,87 @@ import (
 func TestAlertNotificationExtraction(t *testing.T) {
 func TestAlertNotificationExtraction(t *testing.T) {
 
 
 	Convey("Parsing alert notification from settings", t, func() {
 	Convey("Parsing alert notification from settings", t, func() {
-		Convey("Parsing email notification from settings", func() {
-			json := `
-            {
-                "to": "ops@grafana.org"
-            }`
-
-			settingsJSON, _ := simplejson.NewJson([]byte(json))
-			model := &m.AlertNotification{
-				Name:     "ops",
-				Type:     "email",
-				Settings: settingsJSON,
-			}
-
-			not, err := NewNotificationFromDBModel(model)
-
-			So(err, ShouldBeNil)
-			So(not.Name, ShouldEqual, "ops")
-			So(not.Type, ShouldEqual, "email")
-			So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.EmailNotifier")
-
-			email := not.Notifierr.(*EmailNotifier)
-			So(email.To, ShouldEqual, "ops@grafana.org")
+		Convey("Parsing email", func() {
+			Convey("empty settings should return error", func() {
+				json := `{ }`
+
+				settingsJSON, _ := simplejson.NewJson([]byte(json))
+				model := &m.AlertNotification{
+					Name:     "ops",
+					Type:     "email",
+					Settings: settingsJSON,
+				}
+
+				_, err := NewNotificationFromDBModel(model)
+				So(err, ShouldNotBeNil)
+			})
+
+			Convey("from settings", func() {
+				json := `
+				{
+					"to": "ops@grafana.org"
+				}`
+
+				settingsJSON, _ := simplejson.NewJson([]byte(json))
+				model := &m.AlertNotification{
+					Name:     "ops",
+					Type:     "email",
+					Settings: settingsJSON,
+				}
+
+				not, err := NewNotificationFromDBModel(model)
+
+				So(err, ShouldBeNil)
+				So(not.Name, ShouldEqual, "ops")
+				So(not.Type, ShouldEqual, "email")
+				So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.EmailNotifier")
+
+				email := not.Notifierr.(*EmailNotifier)
+				So(email.To, ShouldEqual, "ops@grafana.org")
+			})
 		})
 		})
 
 
-		Convey("Parsing webhook notification from settings", func() {
-			json := `
-            {
-                "url": "http://localhost:3000",
-                "username": "username",
-                "password": "password"
-            }`
-
-			settingsJSON, _ := simplejson.NewJson([]byte(json))
-			model := &m.AlertNotification{
-				Name:     "slack",
-				Type:     "webhook",
-				Settings: settingsJSON,
-			}
-
-			not, err := NewNotificationFromDBModel(model)
-
-			So(err, ShouldBeNil)
-			So(not.Name, ShouldEqual, "slack")
-			So(not.Type, ShouldEqual, "webhook")
-			So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.WebhookNotifier")
-
-			webhook := not.Notifierr.(*WebhookNotifier)
-			So(webhook.Url, ShouldEqual, "http://localhost:3000")
+		Convey("Parsing webhook", func() {
+			Convey("empty settings should return error", func() {
+				json := `{ }`
+
+				settingsJSON, _ := simplejson.NewJson([]byte(json))
+				model := &m.AlertNotification{
+					Name:     "ops",
+					Type:     "webhook",
+					Settings: settingsJSON,
+				}
+
+				_, err := NewNotificationFromDBModel(model)
+				So(err, ShouldNotBeNil)
+			})
+
+			Convey("from settings", func() {
+				json := `
+				{
+					"url": "http://localhost:3000",
+					"username": "username",
+					"password": "password"
+				}`
+
+				settingsJSON, _ := simplejson.NewJson([]byte(json))
+				model := &m.AlertNotification{
+					Name:     "slack",
+					Type:     "webhook",
+					Settings: settingsJSON,
+				}
+
+				not, err := NewNotificationFromDBModel(model)
+
+				So(err, ShouldBeNil)
+				So(not.Name, ShouldEqual, "slack")
+				So(not.Type, ShouldEqual, "webhook")
+				So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.WebhookNotifier")
+
+				webhook := not.Notifierr.(*WebhookNotifier)
+				So(webhook.Url, ShouldEqual, "http://localhost:3000")
+			})
 		})
 		})
+
 	})
 	})
 }
 }