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,7 +13,22 @@ 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() {
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 := ` json := `
{ {
"to": "ops@grafana.org" "to": "ops@grafana.org"
@ -36,8 +51,24 @@ func TestAlertNotificationExtraction(t *testing.T) {
email := not.Notifierr.(*EmailNotifier) email := not.Notifierr.(*EmailNotifier)
So(email.To, ShouldEqual, "ops@grafana.org") So(email.To, ShouldEqual, "ops@grafana.org")
}) })
})
Convey("Parsing webhook notification from settings", func() { 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 := ` json := `
{ {
"url": "http://localhost:3000", "url": "http://localhost:3000",
@ -63,4 +94,6 @@ func TestAlertNotificationExtraction(t *testing.T) {
So(webhook.Url, ShouldEqual, "http://localhost:3000") So(webhook.Url, ShouldEqual, "http://localhost:3000")
}) })
}) })
})
} }