Instantiating notifiers from config before using

This commit is contained in:
Pavel Bakulev 2018-12-05 17:42:53 +02:00
parent e1b87fc597
commit 5c10a897f8
12 changed files with 90 additions and 36 deletions

View File

@ -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

View File

@ -278,6 +278,7 @@ alert_notifications:
recipient: "XXX"
token: "xoxb"
uploadImage: true
url: https://slack.com
delete_alert_notifications:
- name: notification-channel-1

View File

@ -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 {

View File

@ -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
}

View File

@ -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")
})
})
}

View File

@ -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:

View File

@ -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

View File

@ -1,4 +1,6 @@
alert_notifications:
- name: first-default
type: slack
is_default: true
is_default: true
settings:
url: https://slack.com

View File

@ -1,4 +1,6 @@
alert_notifications:
- name: second-default
type: email
is_default: true
is_default: true
settings:
addresses: example@example.com

View File

@ -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

View File

@ -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

View File

@ -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
}