Chore: Remove bus from Alerting API (#44894)

* assign handlers to httpserver

* turn sqlstore mock in to a pointer

* add search service interface

* fix tests for alerting and other apis

* once again, make linter happy
This commit is contained in:
Serge Zaitsev
2022-02-04 13:41:15 +01:00
committed by GitHub
parent b38d3f339a
commit 058e3ffc21
9 changed files with 274 additions and 290 deletions
+38 -38
View File
@@ -20,7 +20,7 @@ import (
"github.com/grafana/grafana/pkg/web"
)
func ValidateOrgAlert(c *models.ReqContext) {
func (hs *HTTPServer) ValidateOrgAlert(c *models.ReqContext) {
id, err := strconv.ParseInt(web.Params(c.Req)[":alertId"], 10, 64)
if err != nil {
c.JsonApiErr(http.StatusBadRequest, "alertId is invalid", nil)
@@ -28,7 +28,7 @@ func ValidateOrgAlert(c *models.ReqContext) {
}
query := models.GetAlertByIdQuery{Id: id}
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
if err := hs.SQLStore.GetAlertById(c.Req.Context(), &query); err != nil {
c.JsonApiErr(404, "Alert not found", nil)
return
}
@@ -39,7 +39,7 @@ func ValidateOrgAlert(c *models.ReqContext) {
}
}
func GetAlertStatesForDashboard(c *models.ReqContext) response.Response {
func (hs *HTTPServer) GetAlertStatesForDashboard(c *models.ReqContext) response.Response {
dashboardID := c.QueryInt64("dashboardId")
if dashboardID == 0 {
@@ -51,7 +51,7 @@ func GetAlertStatesForDashboard(c *models.ReqContext) response.Response {
DashboardId: c.QueryInt64("dashboardId"),
}
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
if err := hs.SQLStore.GetAlertStatesForDashboard(c.Req.Context(), &query); err != nil {
return response.Error(500, "Failed to fetch alert states", err)
}
@@ -59,7 +59,7 @@ func GetAlertStatesForDashboard(c *models.ReqContext) response.Response {
}
// GET /api/alerts
func GetAlerts(c *models.ReqContext) response.Response {
func (hs *HTTPServer) GetAlerts(c *models.ReqContext) response.Response {
dashboardQuery := c.Query("dashboardQuery")
dashboardTags := c.QueryStrings("dashboardTag")
stringDashboardIDs := c.QueryStrings("dashboardId")
@@ -94,7 +94,7 @@ func GetAlerts(c *models.ReqContext) response.Response {
Permission: models.PERMISSION_VIEW,
}
err := bus.Dispatch(c.Req.Context(), &searchQuery)
err := hs.SearchService.SearchHandler(c.Req.Context(), &searchQuery)
if err != nil {
return response.Error(500, "List alerts failed", err)
}
@@ -125,7 +125,7 @@ func GetAlerts(c *models.ReqContext) response.Response {
query.State = states
}
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
if err := hs.SQLStore.HandleAlertsQuery(c.Req.Context(), &query); err != nil {
return response.Error(500, "List alerts failed", err)
}
@@ -181,21 +181,21 @@ func (hs *HTTPServer) AlertTest(c *models.ReqContext) response.Response {
}
// GET /api/alerts/:id
func GetAlert(c *models.ReqContext) response.Response {
func (hs *HTTPServer) GetAlert(c *models.ReqContext) response.Response {
id, err := strconv.ParseInt(web.Params(c.Req)[":alertId"], 10, 64)
if err != nil {
return response.Error(http.StatusBadRequest, "alertId is invalid", err)
}
query := models.GetAlertByIdQuery{Id: id}
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
if err := hs.SQLStore.GetAlertById(c.Req.Context(), &query); err != nil {
return response.Error(500, "List alerts failed", err)
}
return response.JSON(200, &query.Result)
}
func GetAlertNotifiers(ngalertEnabled bool) func(*models.ReqContext) response.Response {
func (hs *HTTPServer) GetAlertNotifiers(ngalertEnabled bool) func(*models.ReqContext) response.Response {
return func(_ *models.ReqContext) response.Response {
if ngalertEnabled {
return response.JSON(200, notifier.GetAvailableNotifiers())
@@ -206,8 +206,8 @@ func GetAlertNotifiers(ngalertEnabled bool) func(*models.ReqContext) response.Re
}
}
func GetAlertNotificationLookup(c *models.ReqContext) response.Response {
alertNotifications, err := getAlertNotificationsInternal(c)
func (hs *HTTPServer) GetAlertNotificationLookup(c *models.ReqContext) response.Response {
alertNotifications, err := hs.getAlertNotificationsInternal(c)
if err != nil {
return response.Error(500, "Failed to get alert notifications", err)
}
@@ -221,8 +221,8 @@ func GetAlertNotificationLookup(c *models.ReqContext) response.Response {
return response.JSON(200, result)
}
func GetAlertNotifications(c *models.ReqContext) response.Response {
alertNotifications, err := getAlertNotificationsInternal(c)
func (hs *HTTPServer) GetAlertNotifications(c *models.ReqContext) response.Response {
alertNotifications, err := hs.getAlertNotificationsInternal(c)
if err != nil {
return response.Error(500, "Failed to get alert notifications", err)
}
@@ -236,17 +236,17 @@ func GetAlertNotifications(c *models.ReqContext) response.Response {
return response.JSON(200, result)
}
func getAlertNotificationsInternal(c *models.ReqContext) ([]*models.AlertNotification, error) {
func (hs *HTTPServer) getAlertNotificationsInternal(c *models.ReqContext) ([]*models.AlertNotification, error) {
query := &models.GetAllAlertNotificationsQuery{OrgId: c.OrgId}
if err := bus.Dispatch(c.Req.Context(), query); err != nil {
if err := hs.SQLStore.GetAllAlertNotifications(c.Req.Context(), query); err != nil {
return nil, err
}
return query.Result, nil
}
func GetAlertNotificationByID(c *models.ReqContext) response.Response {
func (hs *HTTPServer) GetAlertNotificationByID(c *models.ReqContext) response.Response {
notificationId, err := strconv.ParseInt(web.Params(c.Req)[":notificationId"], 10, 64)
if err != nil {
return response.Error(http.StatusBadRequest, "notificationId is invalid", err)
@@ -260,7 +260,7 @@ func GetAlertNotificationByID(c *models.ReqContext) response.Response {
return response.Error(404, "Alert notification not found", nil)
}
if err := bus.Dispatch(c.Req.Context(), query); err != nil {
if err := hs.SQLStore.GetAlertNotifications(c.Req.Context(), query); err != nil {
return response.Error(500, "Failed to get alert notifications", err)
}
@@ -271,7 +271,7 @@ func GetAlertNotificationByID(c *models.ReqContext) response.Response {
return response.JSON(200, dtos.NewAlertNotification(query.Result))
}
func GetAlertNotificationByUID(c *models.ReqContext) response.Response {
func (hs *HTTPServer) GetAlertNotificationByUID(c *models.ReqContext) response.Response {
query := &models.GetAlertNotificationsWithUidQuery{
OrgId: c.OrgId,
Uid: web.Params(c.Req)[":uid"],
@@ -281,7 +281,7 @@ func GetAlertNotificationByUID(c *models.ReqContext) response.Response {
return response.Error(404, "Alert notification not found", nil)
}
if err := bus.Dispatch(c.Req.Context(), query); err != nil {
if err := hs.SQLStore.GetAlertNotificationsWithUid(c.Req.Context(), query); err != nil {
return response.Error(500, "Failed to get alert notifications", err)
}
@@ -292,14 +292,14 @@ func GetAlertNotificationByUID(c *models.ReqContext) response.Response {
return response.JSON(200, dtos.NewAlertNotification(query.Result))
}
func CreateAlertNotification(c *models.ReqContext) response.Response {
func (hs *HTTPServer) CreateAlertNotification(c *models.ReqContext) response.Response {
cmd := models.CreateAlertNotificationCommand{}
if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
cmd.OrgId = c.OrgId
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
if err := hs.SQLStore.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 := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
if err := hs.SQLStore.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 := bus.Dispatch(c.Req.Context(), &query); err != nil {
if err := hs.SQLStore.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 := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
if err := hs.SQLStore.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 := bus.Dispatch(c.Req.Context(), &query); err != nil {
if err := hs.SQLStore.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 := bus.Dispatch(ctx, query); err != nil {
if err := hs.SQLStore.GetAlertNotifications(ctx, query); err != nil {
return err
}
@@ -418,7 +418,7 @@ func (hs *HTTPServer) fillWithSecureSettingsDataByUID(ctx context.Context, cmd *
Uid: cmd.Uid,
}
if err := bus.Dispatch(ctx, query); err != nil {
if err := hs.SQLStore.GetAlertNotificationsWithUid(ctx, query); err != nil {
return err
}
@@ -436,7 +436,7 @@ func (hs *HTTPServer) fillWithSecureSettingsDataByUID(ctx context.Context, cmd *
return nil
}
func DeleteAlertNotification(c *models.ReqContext) response.Response {
func (hs *HTTPServer) DeleteAlertNotification(c *models.ReqContext) response.Response {
notificationId, err := strconv.ParseInt(web.Params(c.Req)[":notificationId"], 10, 64)
if err != nil {
return response.Error(http.StatusBadRequest, "notificationId is invalid", err)
@@ -447,7 +447,7 @@ func DeleteAlertNotification(c *models.ReqContext) response.Response {
Id: notificationId,
}
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
if err := hs.SQLStore.DeleteAlertNotification(c.Req.Context(), &cmd); err != nil {
if errors.Is(err, models.ErrAlertNotificationNotFound) {
return response.Error(404, err.Error(), nil)
}
@@ -457,13 +457,13 @@ func DeleteAlertNotification(c *models.ReqContext) response.Response {
return response.Success("Notification deleted")
}
func DeleteAlertNotificationByUID(c *models.ReqContext) response.Response {
func (hs *HTTPServer) DeleteAlertNotificationByUID(c *models.ReqContext) response.Response {
cmd := models.DeleteAlertNotificationWithUidCommand{
OrgId: c.OrgId,
Uid: web.Params(c.Req)[":uid"],
}
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
if err := hs.SQLStore.DeleteAlertNotificationWithUid(c.Req.Context(), &cmd); err != nil {
if errors.Is(err, models.ErrAlertNotificationNotFound) {
return response.Error(404, err.Error(), nil)
}
@@ -477,7 +477,7 @@ func DeleteAlertNotificationByUID(c *models.ReqContext) response.Response {
}
// POST /api/alert-notifications/test
func NotificationTest(c *models.ReqContext) response.Response {
func (hs *HTTPServer) NotificationTest(c *models.ReqContext) response.Response {
dto := dtos.NotificationTestCommand{}
if err := web.Bind(c.Req, &dto); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
@@ -507,7 +507,7 @@ func NotificationTest(c *models.ReqContext) response.Response {
}
// POST /api/alerts/:alertId/pause
func PauseAlert(c *models.ReqContext) response.Response {
func (hs *HTTPServer) PauseAlert(c *models.ReqContext) response.Response {
dto := dtos.PauseAlertCommand{}
if err := web.Bind(c.Req, &dto); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
@@ -520,7 +520,7 @@ func PauseAlert(c *models.ReqContext) response.Response {
result["alertId"] = alertID
query := models.GetAlertByIdQuery{Id: alertID}
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
if err := hs.SQLStore.GetAlertById(c.Req.Context(), &query); err != nil {
return response.Error(500, "Get Alert failed", err)
}
@@ -550,7 +550,7 @@ func PauseAlert(c *models.ReqContext) response.Response {
Paused: dto.Paused,
}
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
if err := hs.SQLStore.PauseAlert(c.Req.Context(), &cmd); err != nil {
return response.Error(500, "", err)
}
@@ -567,7 +567,7 @@ func PauseAlert(c *models.ReqContext) response.Response {
}
// POST /api/admin/pause-all-alerts
func PauseAllAlerts(c *models.ReqContext) response.Response {
func (hs *HTTPServer) PauseAllAlerts(c *models.ReqContext) response.Response {
dto := dtos.PauseAllAlertsCommand{}
if err := web.Bind(c.Req, &dto); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
@@ -576,7 +576,7 @@ func PauseAllAlerts(c *models.ReqContext) response.Response {
Paused: dto.Paused,
}
if err := bus.Dispatch(c.Req.Context(), &updateCmd); err != nil {
if err := hs.SQLStore.PauseAllAlerts(c.Req.Context(), &updateCmd); err != nil {
return response.Error(500, "Failed to pause alerts", err)
}