From b01a56c2b7c3df61cefac9ad642b0c7f2557f3f1 Mon Sep 17 00:00:00 2001 From: idafurjes <36131195+idafurjes@users.noreply.github.com> Date: Wed, 16 Feb 2022 18:54:29 +0100 Subject: [PATCH] Fix alerting methods using AlertNotificationService (#45477) --- pkg/api/alerting.go | 24 ++++++++++++------------ pkg/api/http_server.go | 8 ++++++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index 23efe48f686..15414b6269d 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -239,7 +239,7 @@ func (hs *HTTPServer) GetAlertNotifications(c *models.ReqContext) response.Respo func (hs *HTTPServer) getAlertNotificationsInternal(c *models.ReqContext) ([]*models.AlertNotification, error) { query := &models.GetAllAlertNotificationsQuery{OrgId: c.OrgId} - if err := hs.SQLStore.GetAllAlertNotifications(c.Req.Context(), query); err != nil { + if err := hs.AlertNotificationService.GetAllAlertNotifications(c.Req.Context(), query); err != nil { return nil, err } @@ -260,7 +260,7 @@ func (hs *HTTPServer) GetAlertNotificationByID(c *models.ReqContext) response.Re return response.Error(404, "Alert notification not found", nil) } - if err := hs.SQLStore.GetAlertNotifications(c.Req.Context(), query); err != nil { + if err := hs.AlertNotificationService.GetAlertNotifications(c.Req.Context(), query); err != nil { return response.Error(500, "Failed to get alert notifications", err) } @@ -281,7 +281,7 @@ func (hs *HTTPServer) GetAlertNotificationByUID(c *models.ReqContext) response.R return response.Error(404, "Alert notification not found", nil) } - if err := hs.SQLStore.GetAlertNotificationsWithUid(c.Req.Context(), query); err != nil { + if err := hs.AlertNotificationService.GetAlertNotificationsWithUid(c.Req.Context(), query); err != nil { return response.Error(500, "Failed to get alert notifications", err) } @@ -299,7 +299,7 @@ func (hs *HTTPServer) CreateAlertNotification(c *models.ReqContext) response.Res } cmd.OrgId = c.OrgId - if err := hs.SQLStore.CreateAlertNotificationCommand(c.Req.Context(), &cmd); err != nil { + if err := hs.AlertNotificationService.CreateAlertNotificationCommand(c.Req.Context(), &cmd); err != nil { if errors.Is(err, models.ErrAlertNotificationWithSameNameExists) || errors.Is(err, models.ErrAlertNotificationWithSameUIDExists) { return response.Error(409, "Failed to create alert notification", err) } @@ -325,7 +325,7 @@ func (hs *HTTPServer) UpdateAlertNotification(c *models.ReqContext) response.Res return response.Error(500, "Failed to update alert notification", err) } - if err := hs.SQLStore.UpdateAlertNotification(c.Req.Context(), &cmd); err != nil { + if err := hs.AlertNotificationService.UpdateAlertNotification(c.Req.Context(), &cmd); err != nil { if errors.Is(err, models.ErrAlertNotificationNotFound) { return response.Error(404, err.Error(), err) } @@ -341,7 +341,7 @@ func (hs *HTTPServer) UpdateAlertNotification(c *models.ReqContext) response.Res Id: cmd.Id, } - if err := hs.SQLStore.GetAlertNotifications(c.Req.Context(), &query); err != nil { + if err := hs.AlertNotificationService.GetAlertNotifications(c.Req.Context(), &query); err != nil { return response.Error(500, "Failed to get alert notification", err) } @@ -361,7 +361,7 @@ func (hs *HTTPServer) UpdateAlertNotificationByUID(c *models.ReqContext) respons return response.Error(500, "Failed to update alert notification", err) } - if err := hs.SQLStore.UpdateAlertNotificationWithUid(c.Req.Context(), &cmd); err != nil { + if err := hs.AlertNotificationService.UpdateAlertNotificationWithUid(c.Req.Context(), &cmd); err != nil { if errors.Is(err, models.ErrAlertNotificationNotFound) { return response.Error(404, err.Error(), nil) } @@ -373,7 +373,7 @@ func (hs *HTTPServer) UpdateAlertNotificationByUID(c *models.ReqContext) respons Uid: cmd.Uid, } - if err := hs.SQLStore.GetAlertNotificationsWithUid(c.Req.Context(), &query); err != nil { + if err := hs.AlertNotificationService.GetAlertNotificationsWithUid(c.Req.Context(), &query); err != nil { return response.Error(500, "Failed to get alert notification", err) } @@ -390,7 +390,7 @@ func (hs *HTTPServer) fillWithSecureSettingsData(ctx context.Context, cmd *model Id: cmd.Id, } - if err := hs.SQLStore.GetAlertNotifications(ctx, query); err != nil { + if err := hs.AlertNotificationService.GetAlertNotifications(ctx, query); err != nil { return err } @@ -418,7 +418,7 @@ func (hs *HTTPServer) fillWithSecureSettingsDataByUID(ctx context.Context, cmd * Uid: cmd.Uid, } - if err := hs.SQLStore.GetAlertNotificationsWithUid(ctx, query); err != nil { + if err := hs.AlertNotificationService.GetAlertNotificationsWithUid(ctx, query); err != nil { return err } @@ -447,7 +447,7 @@ func (hs *HTTPServer) DeleteAlertNotification(c *models.ReqContext) response.Res Id: notificationId, } - if err := hs.SQLStore.DeleteAlertNotification(c.Req.Context(), &cmd); err != nil { + if err := hs.AlertNotificationService.DeleteAlertNotification(c.Req.Context(), &cmd); err != nil { if errors.Is(err, models.ErrAlertNotificationNotFound) { return response.Error(404, err.Error(), nil) } @@ -463,7 +463,7 @@ func (hs *HTTPServer) DeleteAlertNotificationByUID(c *models.ReqContext) respons Uid: web.Params(c.Req)[":uid"], } - if err := hs.SQLStore.DeleteAlertNotificationWithUid(c.Req.Context(), &cmd); err != nil { + if err := hs.AlertNotificationService.DeleteAlertNotificationWithUid(c.Req.Context(), &cmd); err != nil { if errors.Is(err, models.ErrAlertNotificationNotFound) { return response.Error(404, err.Error(), nil) } diff --git a/pkg/api/http_server.go b/pkg/api/http_server.go index 15fa35dd508..8fdd76cb7d7 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -135,6 +135,7 @@ type HTTPServer struct { dashboardProvisioningService dashboards.DashboardProvisioningService folderService dashboards.FolderService DatasourcePermissionsService DatasourcePermissionsService + AlertNotificationService *alerting.AlertNotificationService } type ServerOptions struct { @@ -162,8 +163,10 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi dataSourcesService datasources.DataSourceService, secretsService secrets.Service, queryDataService *query.Service, ldapGroups ldap.Groups, teamGuardian teamguardian.TeamGuardian, serviceaccountsService serviceaccounts.Service, authInfoService login.AuthInfoService, resourcePermissionServices *resourceservices.ResourceServices, - notificationService *notifications.NotificationService, dashboardService dashboards.DashboardService, dashboardProvisioningService dashboards.DashboardProvisioningService, - folderService dashboards.FolderService, datasourcePermissionsService DatasourcePermissionsService) (*HTTPServer, error) { + notificationService *notifications.NotificationService, dashboardService dashboards.DashboardService, + dashboardProvisioningService dashboards.DashboardProvisioningService, folderService dashboards.FolderService, + datasourcePermissionsService DatasourcePermissionsService, alertNotificationService *alerting.AlertNotificationService, +) (*HTTPServer, error) { web.Env = cfg.Env m := web.New() @@ -228,6 +231,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi dashboardProvisioningService: dashboardProvisioningService, folderService: folderService, DatasourcePermissionsService: datasourcePermissionsService, + AlertNotificationService: alertNotificationService, } if hs.Listener != nil { hs.log.Debug("Using provided listener")