From 4fa45253cf30eb8af96c9c36b7f7fc80705704cb Mon Sep 17 00:00:00 2001 From: Pavel Bakulev Date: Thu, 29 Nov 2018 10:22:56 +0200 Subject: [PATCH] Added orgName parameter for alert_notifications --- .../alert_notifications.go | 27 +++++++- .../alert_notifications/config_reader.go | 12 +++- .../alert_notifications/config_reader_test.go | 64 +++++++++++++++---- .../correct-properties-with-orgName.yaml | 18 ++++++ .../provisioning/alert_notifications/types.go | 6 +- 5 files changed, 110 insertions(+), 17 deletions(-) create mode 100644 pkg/services/provisioning/alert_notifications/test-configs/correct-properties-with-orgName/correct-properties-with-orgName.yaml diff --git a/pkg/services/provisioning/alert_notifications/alert_notifications.go b/pkg/services/provisioning/alert_notifications/alert_notifications.go index eae5b4bb44e..2fa899dcd20 100644 --- a/pkg/services/provisioning/alert_notifications/alert_notifications.go +++ b/pkg/services/provisioning/alert_notifications/alert_notifications.go @@ -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, }) } diff --git a/pkg/services/provisioning/alert_notifications/config_reader.go b/pkg/services/provisioning/alert_notifications/config_reader.go index b69f9bf15b1..8d38bc76a1f 100644 --- a/pkg/services/provisioning/alert_notifications/config_reader.go +++ b/pkg/services/provisioning/alert_notifications/config_reader.go @@ -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 + } } } } diff --git a/pkg/services/provisioning/alert_notifications/config_reader_test.go b/pkg/services/provisioning/alert_notifications/config_reader_test.go index e028f5bac40..11dddd3e475 100644 --- a/pkg/services/provisioning/alert_notifications/config_reader_test.go +++ b/pkg/services/provisioning/alert_notifications/config_reader_test.go @@ -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 +} diff --git a/pkg/services/provisioning/alert_notifications/test-configs/correct-properties-with-orgName/correct-properties-with-orgName.yaml b/pkg/services/provisioning/alert_notifications/test-configs/correct-properties-with-orgName/correct-properties-with-orgName.yaml new file mode 100644 index 00000000000..eafca5e77de --- /dev/null +++ b/pkg/services/provisioning/alert_notifications/test-configs/correct-properties-with-orgName/correct-properties-with-orgName.yaml @@ -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 \ No newline at end of file diff --git a/pkg/services/provisioning/alert_notifications/types.go b/pkg/services/provisioning/alert_notifications/types.go index 7718605cd67..469ce1a3586 100644 --- a/pkg/services/provisioning/alert_notifications/types.go +++ b/pkg/services/provisioning/alert_notifications/types.go @@ -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"`