mirror of
https://github.com/grafana/grafana.git
synced 2025-01-21 22:13:38 -06:00
Added orgName parameter for alert_notifications
This commit is contained in:
parent
6e3e9a337d
commit
4fa45253cf
@ -47,6 +47,15 @@ func (dc *NotificationProvisioner) deleteNotifications(notificationToDelete []*d
|
||||
for _, notification := range notificationToDelete {
|
||||
dc.log.Info("Deleting alert notification", "name", notification.Name)
|
||||
|
||||
if notification.OrgId == 0 && notification.OrgName != "" {
|
||||
getOrg := &models.GetOrgByNameQuery{Name: notification.OrgName}
|
||||
if err := bus.Dispatch(getOrg); err != nil {
|
||||
return err
|
||||
}
|
||||
notification.OrgId = getOrg.Result.Id
|
||||
} else if notification.OrgId < 0 {
|
||||
notification.OrgId = 1
|
||||
}
|
||||
getNotification := &models.GetAlertNotificationsQuery{Name: notification.Name, OrgId: notification.OrgId}
|
||||
|
||||
if err := bus.Dispatch(getNotification); err != nil {
|
||||
@ -66,6 +75,17 @@ func (dc *NotificationProvisioner) deleteNotifications(notificationToDelete []*d
|
||||
|
||||
func (dc *NotificationProvisioner) mergeNotifications(notificationToMerge []*notificationFromConfig) error {
|
||||
for _, notification := range notificationToMerge {
|
||||
|
||||
if notification.OrgId == 0 && notification.OrgName != "" {
|
||||
getOrg := &models.GetOrgByNameQuery{Name: notification.OrgName}
|
||||
if err := bus.Dispatch(getOrg); err != nil {
|
||||
return err
|
||||
}
|
||||
notification.OrgId = getOrg.Result.Id
|
||||
} else if notification.OrgId < 0 {
|
||||
notification.OrgId = 1
|
||||
}
|
||||
|
||||
cmd := &models.GetAlertNotificationsQuery{OrgId: notification.OrgId, Name: notification.Name}
|
||||
err := bus.Dispatch(cmd)
|
||||
if err != nil {
|
||||
@ -109,6 +129,7 @@ func (dc *NotificationProvisioner) mergeNotifications(notificationToMerge []*not
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *notificationsAsConfig) mapToNotificationFromConfig() *notificationsAsConfig {
|
||||
r := ¬ificationsAsConfig{}
|
||||
if cfg == nil {
|
||||
@ -118,6 +139,7 @@ func (cfg *notificationsAsConfig) mapToNotificationFromConfig() *notificationsAs
|
||||
for _, notification := range cfg.Notifications {
|
||||
r.Notifications = append(r.Notifications, ¬ificationFromConfig{
|
||||
OrgId: notification.OrgId,
|
||||
OrgName: notification.OrgName,
|
||||
Name: notification.Name,
|
||||
Type: notification.Type,
|
||||
IsDefault: notification.IsDefault,
|
||||
@ -127,8 +149,9 @@ func (cfg *notificationsAsConfig) mapToNotificationFromConfig() *notificationsAs
|
||||
|
||||
for _, notification := range cfg.DeleteNotifications {
|
||||
r.DeleteNotifications = append(r.DeleteNotifications, &deleteNotificationConfig{
|
||||
OrgId: notification.OrgId,
|
||||
Name: notification.Name,
|
||||
OrgId: notification.OrgId,
|
||||
OrgName: notification.OrgName,
|
||||
Name: notification.Name,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -73,13 +73,21 @@ func validateDefaultUniqueness(notifications []*notificationsAsConfig) error {
|
||||
for i := range notifications {
|
||||
for _, notification := range notifications[i].Notifications {
|
||||
if notification.OrgId < 1 {
|
||||
notification.OrgId = 1
|
||||
if notification.OrgName == "" {
|
||||
notification.OrgId = 1
|
||||
} else {
|
||||
notification.OrgId = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, notification := range notifications[i].DeleteNotifications {
|
||||
if notification.OrgId < 1 {
|
||||
notification.OrgId = 1
|
||||
if notification.OrgName == "" {
|
||||
notification.OrgId = 1
|
||||
} else {
|
||||
notification.OrgId = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,13 +13,14 @@ import (
|
||||
var (
|
||||
logger = log.New("fake.log")
|
||||
|
||||
correct_properties = "./test-configs/correct-properties"
|
||||
brokenYaml = "./test-configs/broken-yaml"
|
||||
doubleNotificationsConfig = "./test-configs/double-default"
|
||||
emptyFolder = "./test-configs/empty_folder"
|
||||
emptyFile = "./test-configs/empty"
|
||||
twoNotificationsConfig = "./test-configs/two-notifications"
|
||||
unknownNotifier = "./test-configs/unknown-notifier"
|
||||
correct_properties = "./test-configs/correct-properties"
|
||||
correct_properties_with_orgName = "./test-configs/correct-properties-with-orgName"
|
||||
brokenYaml = "./test-configs/broken-yaml"
|
||||
doubleNotificationsConfig = "./test-configs/double-default"
|
||||
emptyFolder = "./test-configs/empty_folder"
|
||||
emptyFile = "./test-configs/empty"
|
||||
twoNotificationsConfig = "./test-configs/two-notifications"
|
||||
unknownNotifier = "./test-configs/unknown-notifier"
|
||||
|
||||
fakeRepo *fakeRepository
|
||||
)
|
||||
@ -32,6 +33,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
bus.AddHandler("test", mockInsert)
|
||||
bus.AddHandler("test", mockUpdate)
|
||||
bus.AddHandler("test", mockGet)
|
||||
bus.AddHandler("test", mockGetOrg)
|
||||
|
||||
alerting.RegisterNotifier(&alerting.NotifierPlugin{
|
||||
Type: "slack",
|
||||
@ -155,6 +157,36 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Can read correct properties with orgName instead of orgId", func() {
|
||||
fakeRepo.loadAllOrg = []*models.Org{
|
||||
{Name: "Main Org. 1", Id: 1},
|
||||
{Name: "Main Org. 2", Id: 2},
|
||||
}
|
||||
|
||||
fakeRepo.loadAll = []*models.AlertNotification{
|
||||
{Name: "default-slack-notification", OrgId: 1, Id: 1},
|
||||
{Name: "another-not-default-notification", OrgId: 2, Id: 2},
|
||||
}
|
||||
dc := newNotificationProvisioner(logger)
|
||||
err := dc.applyChanges(correct_properties_with_orgName)
|
||||
if err != nil {
|
||||
t.Fatalf("applyChanges return an error %v", err)
|
||||
}
|
||||
So(len(fakeRepo.deleted), ShouldEqual, 2)
|
||||
So(len(fakeRepo.inserted), ShouldEqual, 0)
|
||||
So(len(fakeRepo.updated), ShouldEqual, 2)
|
||||
updated := fakeRepo.updated
|
||||
nt := updated[0]
|
||||
So(nt.Name, ShouldEqual, "default-slack-notification")
|
||||
So(nt.OrgId, ShouldEqual, 1)
|
||||
|
||||
nt = updated[1]
|
||||
So(nt.Name, ShouldEqual, "another-not-default-notification")
|
||||
So(nt.OrgId, ShouldEqual, 2)
|
||||
|
||||
})
|
||||
|
||||
Convey("Empty yaml file", func() {
|
||||
Convey("should have not changed repo", func() {
|
||||
dc := newNotificationProvisioner(logger)
|
||||
@ -190,10 +222,11 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
type fakeRepository struct {
|
||||
inserted []*models.CreateAlertNotificationCommand
|
||||
deleted []*models.DeleteAlertNotificationCommand
|
||||
updated []*models.UpdateAlertNotificationCommand
|
||||
loadAll []*models.AlertNotification
|
||||
inserted []*models.CreateAlertNotificationCommand
|
||||
deleted []*models.DeleteAlertNotificationCommand
|
||||
updated []*models.UpdateAlertNotificationCommand
|
||||
loadAll []*models.AlertNotification
|
||||
loadAllOrg []*models.Org
|
||||
}
|
||||
|
||||
func mockDelete(cmd *models.DeleteAlertNotificationCommand) error {
|
||||
@ -217,3 +250,12 @@ func mockGet(cmd *models.GetAlertNotificationsQuery) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func mockGetOrg(cmd *models.GetOrgByNameQuery) error {
|
||||
for _, v := range fakeRepo.loadAllOrg {
|
||||
if cmd.Name == v.Name {
|
||||
cmd.Result = v
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
alert_notifications:
|
||||
- name: default-slack-notification
|
||||
type: slack
|
||||
org_name: Main Org. 1
|
||||
is_default: true
|
||||
settings:
|
||||
recipient: "XXX"
|
||||
token: "xoxb"
|
||||
uploadImage: true
|
||||
- name: another-not-default-notification
|
||||
type: email
|
||||
org_name: Main Org. 2
|
||||
is_default: false
|
||||
delete_alert_notifications:
|
||||
- name: default-slack-notification
|
||||
org_name: Main Org. 1
|
||||
- name: another-not-default-notification
|
||||
org_name: Main Org. 2
|
@ -6,12 +6,14 @@ type notificationsAsConfig struct {
|
||||
}
|
||||
|
||||
type deleteNotificationConfig struct {
|
||||
Name string `json:"name" yaml:"name"`
|
||||
OrgId int64 `json:"org_id" yaml:"org_id"`
|
||||
Name string `json:"name" yaml:"name"`
|
||||
OrgId int64 `json:"org_id" yaml:"org_id"`
|
||||
OrgName string `json:"org_name" yaml:"org_name"`
|
||||
}
|
||||
|
||||
type notificationFromConfig struct {
|
||||
OrgId int64 `json:"org_id" yaml:"org_id"`
|
||||
OrgName string `json:"org_name" yaml:"org_name"`
|
||||
Name string `json:"name" yaml:"name"`
|
||||
Type string `json:"type" yaml:"type"`
|
||||
IsDefault bool `json:"is_default" yaml:"is_default"`
|
||||
|
Loading…
Reference in New Issue
Block a user