feat(alerting): make some settings properties required

This commit is contained in:
bergquist 2016-06-16 16:18:40 +02:00
parent f3009dc23b
commit 6705efef6f
2 changed files with 97 additions and 45 deletions

View File

@ -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" {
return &EmailNotifier{ to := settings.Get("to").MustString()
To: settings.Get("to").MustString(),
log: log.New("alerting.notification.email"), if to == "" {
return nil, fmt.Errorf("Could not find to propertie in settings")
} }
return &EmailNotifier{
To: to,
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
} }

View File

@ -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() { Convey("Parsing email", func() {
json := ` Convey("empty settings should return error", func() {
{ json := `{ }`
"to": "ops@grafana.org"
}`
settingsJSON, _ := simplejson.NewJson([]byte(json)) settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{ model := &m.AlertNotification{
Name: "ops", Name: "ops",
Type: "email", Type: "email",
Settings: settingsJSON, Settings: settingsJSON,
} }
not, err := NewNotificationFromDBModel(model) _, err := NewNotificationFromDBModel(model)
So(err, ShouldNotBeNil)
})
So(err, ShouldBeNil) Convey("from settings", func() {
So(not.Name, ShouldEqual, "ops") json := `
So(not.Type, ShouldEqual, "email") {
So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.EmailNotifier") "to": "ops@grafana.org"
}`
email := not.Notifierr.(*EmailNotifier) settingsJSON, _ := simplejson.NewJson([]byte(json))
So(email.To, ShouldEqual, "ops@grafana.org") 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() { Convey("Parsing webhook", func() {
json := ` Convey("empty settings should return error", func() {
{ json := `{ }`
"url": "http://localhost:3000",
"username": "username",
"password": "password"
}`
settingsJSON, _ := simplejson.NewJson([]byte(json)) settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{ model := &m.AlertNotification{
Name: "slack", Name: "ops",
Type: "webhook", Type: "webhook",
Settings: settingsJSON, Settings: settingsJSON,
} }
not, err := NewNotificationFromDBModel(model) _, err := NewNotificationFromDBModel(model)
So(err, ShouldNotBeNil)
})
So(err, ShouldBeNil) Convey("from settings", func() {
So(not.Name, ShouldEqual, "slack") json := `
So(not.Type, ShouldEqual, "webhook") {
So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.WebhookNotifier") "url": "http://localhost:3000",
"username": "username",
"password": "password"
}`
webhook := not.Notifierr.(*WebhookNotifier) settingsJSON, _ := simplejson.NewJson([]byte(json))
So(webhook.Url, ShouldEqual, "http://localhost:3000") 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")
})
}) })
}) })
} }