mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Instantiating notifiers from config before using
This commit is contained in:
parent
e1b87fc597
commit
5c10a897f8
@ -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
|
@ -278,6 +278,7 @@ alert_notifications:
|
||||
recipient: "XXX"
|
||||
token: "xoxb"
|
||||
uploadImage: true
|
||||
url: https://slack.com
|
||||
|
||||
delete_alert_notifications:
|
||||
- name: notification-channel-1
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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"
|
||||
@ -38,10 +40,12 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
alerting.RegisterNotifier(&alerting.NotifierPlugin{
|
||||
Type: "slack",
|
||||
Name: "slack",
|
||||
Factory: notifiers.NewSlackNotifier,
|
||||
})
|
||||
alerting.RegisterNotifier(&alerting.NotifierPlugin{
|
||||
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")
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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,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
|
||||
|
@ -2,3 +2,5 @@ alert_notifications:
|
||||
- name: first-default
|
||||
type: slack
|
||||
is_default: true
|
||||
settings:
|
||||
url: https://slack.com
|
@ -2,3 +2,5 @@ alert_notifications:
|
||||
- name: second-default
|
||||
type: email
|
||||
is_default: true
|
||||
settings:
|
||||
addresses: example@example.com
|
@ -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
|
@ -1,5 +1,9 @@
|
||||
alert_notifications:
|
||||
- name: channel1
|
||||
type: slack
|
||||
settings:
|
||||
url: http://slack.com
|
||||
- name: channel2
|
||||
type: email
|
||||
settings:
|
||||
addresses: example@example.com
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user