2016-04-26 10:36:50 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-10-07 09:33:50 -05:00
|
|
|
"context"
|
2020-10-20 06:53:48 -05:00
|
|
|
"errors"
|
2016-07-21 03:29:11 -05:00
|
|
|
"fmt"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2018-06-01 07:36:40 -05:00
|
|
|
"strconv"
|
2016-07-21 03:29:11 -05:00
|
|
|
|
2016-04-27 06:02:28 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2019-08-12 13:03:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-07-20 09:13:36 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
2018-01-30 07:41:25 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
2021-05-04 06:58:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/notifier"
|
2018-06-01 07:36:40 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/search"
|
2021-10-07 09:33:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2020-09-11 11:04:43 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2016-04-26 10:36:50 -05:00
|
|
|
)
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) ValidateOrgAlert(c *models.ReqContext) {
|
2022-01-14 10:55:57 -06:00
|
|
|
id, err := strconv.ParseInt(web.Params(c.Req)[":alertId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(http.StatusBadRequest, "alertId is invalid", nil)
|
|
|
|
return
|
|
|
|
}
|
2019-08-12 13:03:48 -05:00
|
|
|
query := models.GetAlertByIdQuery{Id: id}
|
2016-04-26 10:36:50 -05:00
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
if err := hs.SQLStore.GetAlertById(c.Req.Context(), &query); err != nil {
|
2016-04-26 10:36:50 -05:00
|
|
|
c.JsonApiErr(404, "Alert not found", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.OrgId != query.Result.OrgId {
|
|
|
|
c.JsonApiErr(403, "You are not allowed to edit/view alert", nil)
|
|
|
|
return
|
2017-10-23 02:56:52 -05:00
|
|
|
}
|
2016-04-26 10:36:50 -05:00
|
|
|
}
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) GetAlertStatesForDashboard(c *models.ReqContext) response.Response {
|
2018-03-22 06:37:35 -05:00
|
|
|
dashboardID := c.QueryInt64("dashboardId")
|
2016-09-30 10:37:47 -05:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
if dashboardID == 0 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Missing query parameter dashboardId", nil)
|
2016-09-30 10:37:47 -05:00
|
|
|
}
|
|
|
|
|
2019-08-12 13:03:48 -05:00
|
|
|
query := models.GetAlertStatesForDashboardQuery{
|
2016-09-30 10:37:47 -05:00
|
|
|
OrgId: c.OrgId,
|
|
|
|
DashboardId: c.QueryInt64("dashboardId"),
|
|
|
|
}
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
if err := hs.SQLStore.GetAlertStatesForDashboard(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to fetch alert states", err)
|
2016-09-30 10:37:47 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, query.Result)
|
2016-09-30 10:37:47 -05:00
|
|
|
}
|
|
|
|
|
2016-09-14 01:36:44 -05:00
|
|
|
// GET /api/alerts
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) GetAlerts(c *models.ReqContext) response.Response {
|
2018-06-01 07:36:40 -05:00
|
|
|
dashboardQuery := c.Query("dashboardQuery")
|
|
|
|
dashboardTags := c.QueryStrings("dashboardTag")
|
|
|
|
stringDashboardIDs := c.QueryStrings("dashboardId")
|
|
|
|
stringFolderIDs := c.QueryStrings("folderId")
|
|
|
|
|
|
|
|
dashboardIDs := make([]int64, 0)
|
|
|
|
for _, id := range stringDashboardIDs {
|
|
|
|
dashboardID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
dashboardIDs = append(dashboardIDs, dashboardID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dashboardQuery != "" || len(dashboardTags) > 0 || len(stringFolderIDs) > 0 {
|
|
|
|
folderIDs := make([]int64, 0)
|
|
|
|
for _, id := range stringFolderIDs {
|
|
|
|
folderID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
folderIDs = append(folderIDs, folderID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
searchQuery := search.Query{
|
|
|
|
Title: dashboardQuery,
|
|
|
|
Tags: dashboardTags,
|
|
|
|
SignedInUser: c.SignedInUser,
|
|
|
|
Limit: 1000,
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
DashboardIds: dashboardIDs,
|
2022-03-21 10:54:30 -05:00
|
|
|
Type: string(models.DashHitDB),
|
2018-06-01 07:36:40 -05:00
|
|
|
FolderIds: folderIDs,
|
2019-08-12 13:03:48 -05:00
|
|
|
Permission: models.PERMISSION_VIEW,
|
2018-06-01 07:36:40 -05:00
|
|
|
}
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
err := hs.SearchService.SearchHandler(c.Req.Context(), &searchQuery)
|
2018-06-01 07:36:40 -05:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "List alerts failed", err)
|
2018-06-01 07:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range searchQuery.Result {
|
2022-03-21 10:54:30 -05:00
|
|
|
if d.Type == models.DashHitDB && d.ID > 0 {
|
2021-02-11 01:49:16 -06:00
|
|
|
dashboardIDs = append(dashboardIDs, d.ID)
|
2018-06-01 07:36:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we didn't find any dashboards, return empty result
|
|
|
|
if len(dashboardIDs) == 0 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, []*models.AlertListItemDTO{})
|
2018-06-01 07:36:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:03:48 -05:00
|
|
|
query := models.GetAlertsQuery{
|
2018-06-01 07:36:40 -05:00
|
|
|
OrgId: c.OrgId,
|
|
|
|
DashboardIDs: dashboardIDs,
|
|
|
|
PanelId: c.QueryInt64("panelId"),
|
|
|
|
Limit: c.QueryInt64("limit"),
|
|
|
|
User: c.SignedInUser,
|
|
|
|
Query: c.Query("query"),
|
2016-04-26 10:36:50 -05:00
|
|
|
}
|
|
|
|
|
2016-09-14 07:12:19 -05:00
|
|
|
states := c.QueryStrings("state")
|
|
|
|
if len(states) > 0 {
|
|
|
|
query.State = states
|
|
|
|
}
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
if err := hs.SQLStore.HandleAlertsQuery(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "List alerts failed", err)
|
2016-04-26 10:36:50 -05:00
|
|
|
}
|
|
|
|
|
2018-02-16 06:56:04 -06:00
|
|
|
for _, alert := range query.Result {
|
2019-08-12 13:03:48 -05:00
|
|
|
alert.Url = models.GetDashboardUrl(alert.DashboardUid, alert.DashboardSlug)
|
2018-01-31 03:47:31 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, query.Result)
|
2016-04-26 10:36:50 -05:00
|
|
|
}
|
|
|
|
|
2016-07-20 09:13:36 -05:00
|
|
|
// POST /api/alerts/test
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) AlertTest(c *models.ReqContext) response.Response {
|
|
|
|
dto := dtos.AlertTestCommand{}
|
|
|
|
if err := web.Bind(c.Req, &dto); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2016-11-23 07:55:10 -06:00
|
|
|
if _, idErr := dto.Dashboard.Get("id").Int64(); idErr != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "The dashboard needs to be saved at least once before you can test an alert rule", nil)
|
2016-11-23 07:55:10 -06:00
|
|
|
}
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
res, err := hs.AlertEngine.AlertTest(c.OrgId, dto.Dashboard, dto.PanelId, c.SignedInUser)
|
|
|
|
if err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
var validationErr alerting.ValidationError
|
|
|
|
if errors.As(err, &validationErr) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(422, validationErr.Error(), nil)
|
2016-07-21 06:09:12 -05:00
|
|
|
}
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrDataSourceAccessDenied) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(403, "Access denied to datasource", err)
|
2018-11-05 07:25:19 -06:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to test rule", err)
|
2016-07-20 09:13:36 -05:00
|
|
|
}
|
|
|
|
|
2016-07-21 03:29:11 -05:00
|
|
|
dtoRes := &dtos.AlertTestResult{
|
2016-11-17 08:48:15 -06:00
|
|
|
Firing: res.Firing,
|
|
|
|
ConditionEvals: res.ConditionEvals,
|
2017-01-13 03:24:40 -06:00
|
|
|
State: res.Rule.State,
|
2016-07-21 03:29:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
dtoRes.Error = res.Error.Error()
|
|
|
|
}
|
|
|
|
|
2016-07-21 06:09:12 -05:00
|
|
|
for _, log := range res.Logs {
|
|
|
|
dtoRes.Logs = append(dtoRes.Logs, &dtos.AlertTestResultLog{Message: log.Message, Data: log.Data})
|
|
|
|
}
|
2016-08-18 04:22:24 -05:00
|
|
|
for _, match := range res.EvalMatches {
|
|
|
|
dtoRes.EvalMatches = append(dtoRes.EvalMatches, &dtos.EvalMatch{Metric: match.Metric, Value: match.Value})
|
|
|
|
}
|
2016-07-21 06:09:12 -05:00
|
|
|
|
2016-07-26 05:29:52 -05:00
|
|
|
dtoRes.TimeMs = fmt.Sprintf("%1.3fms", res.GetDurationMs())
|
2016-07-21 03:29:11 -05:00
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, dtoRes)
|
2016-07-20 09:13:36 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 01:23:50 -05:00
|
|
|
// GET /api/alerts/:id
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) GetAlert(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
id, err := strconv.ParseInt(web.Params(c.Req)[":alertId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "alertId is invalid", err)
|
|
|
|
}
|
2019-08-12 13:03:48 -05:00
|
|
|
query := models.GetAlertByIdQuery{Id: id}
|
2016-04-26 10:36:50 -05:00
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
if err := hs.SQLStore.GetAlertById(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "List alerts failed", err)
|
2016-04-26 10:36:50 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, &query.Result)
|
2016-04-26 10:36:50 -05:00
|
|
|
}
|
2016-04-28 01:23:50 -05:00
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) GetAlertNotifiers(ngalertEnabled bool) func(*models.ReqContext) response.Response {
|
2021-05-04 06:58:39 -05:00
|
|
|
return func(_ *models.ReqContext) response.Response {
|
|
|
|
if ngalertEnabled {
|
|
|
|
return response.JSON(200, notifier.GetAvailableNotifiers())
|
|
|
|
}
|
|
|
|
// TODO(codesome): This wont be required in 8.0 since ngalert
|
|
|
|
// will be enabled by default with no disabling. This is to be removed later.
|
|
|
|
return response.JSON(200, alerting.GetNotifiers())
|
|
|
|
}
|
2017-01-06 05:04:25 -06:00
|
|
|
}
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) GetAlertNotificationLookup(c *models.ReqContext) response.Response {
|
|
|
|
alertNotifications, err := hs.getAlertNotificationsInternal(c)
|
2019-08-12 13:03:48 -05:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get alert notifications", err)
|
2019-08-12 13:03:48 -05:00
|
|
|
}
|
2016-06-15 03:48:04 -05:00
|
|
|
|
2019-08-12 13:03:48 -05:00
|
|
|
result := make([]*dtos.AlertNotificationLookup, 0)
|
|
|
|
|
|
|
|
for _, notification := range alertNotifications {
|
|
|
|
result = append(result, dtos.NewAlertNotificationLookup(notification))
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, result)
|
2019-08-12 13:03:48 -05:00
|
|
|
}
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) GetAlertNotifications(c *models.ReqContext) response.Response {
|
|
|
|
alertNotifications, err := hs.getAlertNotificationsInternal(c)
|
2019-08-12 13:03:48 -05:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get alert notifications", err)
|
2016-06-15 03:48:04 -05:00
|
|
|
}
|
|
|
|
|
2016-11-06 08:40:02 -06:00
|
|
|
result := make([]*dtos.AlertNotification, 0)
|
2016-06-20 04:31:20 -05:00
|
|
|
|
2019-08-12 13:03:48 -05:00
|
|
|
for _, notification := range alertNotifications {
|
2018-06-04 15:19:27 -05:00
|
|
|
result = append(result, dtos.NewAlertNotification(notification))
|
2016-06-20 04:31:20 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, result)
|
2016-06-15 03:48:04 -05:00
|
|
|
}
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) getAlertNotificationsInternal(c *models.ReqContext) ([]*models.AlertNotification, error) {
|
2019-08-12 13:03:48 -05:00
|
|
|
query := &models.GetAllAlertNotificationsQuery{OrgId: c.OrgId}
|
|
|
|
|
2022-02-16 11:54:29 -06:00
|
|
|
if err := hs.AlertNotificationService.GetAllAlertNotifications(c.Req.Context(), query); err != nil {
|
2019-08-12 13:03:48 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return query.Result, nil
|
|
|
|
}
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) GetAlertNotificationByID(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
notificationId, err := strconv.ParseInt(web.Params(c.Req)[":notificationId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "notificationId is invalid", err)
|
|
|
|
}
|
2019-08-12 13:03:48 -05:00
|
|
|
query := &models.GetAlertNotificationsQuery{
|
2016-07-22 09:45:17 -05:00
|
|
|
OrgId: c.OrgId,
|
2022-01-14 10:55:57 -06:00
|
|
|
Id: notificationId,
|
2016-06-16 07:29:20 -05:00
|
|
|
}
|
|
|
|
|
2019-03-26 06:37:02 -05:00
|
|
|
if query.Id == 0 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Alert notification not found", nil)
|
2019-03-26 06:37:02 -05:00
|
|
|
}
|
|
|
|
|
2022-02-16 11:54:29 -06:00
|
|
|
if err := hs.AlertNotificationService.GetAlertNotifications(c.Req.Context(), query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get alert notifications", err)
|
2019-03-26 06:37:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if query.Result == nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Alert notification not found", nil)
|
2019-03-26 06:37:02 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, dtos.NewAlertNotification(query.Result))
|
2019-03-26 06:37:02 -05:00
|
|
|
}
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) GetAlertNotificationByUID(c *models.ReqContext) response.Response {
|
2019-08-12 13:03:48 -05:00
|
|
|
query := &models.GetAlertNotificationsWithUidQuery{
|
2019-03-26 06:37:02 -05:00
|
|
|
OrgId: c.OrgId,
|
2021-10-11 07:30:59 -05:00
|
|
|
Uid: web.Params(c.Req)[":uid"],
|
2019-03-26 06:37:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if query.Uid == "" {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Alert notification not found", nil)
|
2019-03-26 06:37:02 -05:00
|
|
|
}
|
|
|
|
|
2022-02-16 11:54:29 -06:00
|
|
|
if err := hs.AlertNotificationService.GetAlertNotificationsWithUid(c.Req.Context(), query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get alert notifications", err)
|
2016-06-16 07:29:20 -05:00
|
|
|
}
|
|
|
|
|
2019-01-13 13:30:20 -06:00
|
|
|
if query.Result == nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Alert notification not found", nil)
|
2019-01-13 13:30:20 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, dtos.NewAlertNotification(query.Result))
|
2016-06-16 07:29:20 -05:00
|
|
|
}
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) CreateAlertNotification(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
cmd := models.CreateAlertNotificationCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2016-07-22 09:45:17 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
2016-06-15 03:48:04 -05:00
|
|
|
|
2022-02-16 11:54:29 -06:00
|
|
|
if err := hs.AlertNotificationService.CreateAlertNotificationCommand(c.Req.Context(), &cmd); err != nil {
|
2020-10-20 06:53:48 -05:00
|
|
|
if errors.Is(err, models.ErrAlertNotificationWithSameNameExists) || errors.Is(err, models.ErrAlertNotificationWithSameUIDExists) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(409, "Failed to create alert notification", err)
|
2020-10-20 06:53:48 -05:00
|
|
|
}
|
2021-11-02 08:11:19 -05:00
|
|
|
var alertingErr alerting.ValidationError
|
|
|
|
if errors.As(err, &alertingErr) {
|
|
|
|
return response.Error(400, err.Error(), err)
|
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to create alert notification", err)
|
2016-06-15 03:48:04 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, dtos.NewAlertNotification(cmd.Result))
|
2016-06-15 03:48:04 -05:00
|
|
|
}
|
|
|
|
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) UpdateAlertNotification(c *models.ReqContext) response.Response {
|
|
|
|
cmd := models.UpdateAlertNotificationCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2016-07-22 09:45:17 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
2016-06-15 03:48:04 -05:00
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
err := hs.fillWithSecureSettingsData(c.Req.Context(), &cmd)
|
2020-07-08 03:17:05 -05:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update alert notification", err)
|
2020-07-08 03:17:05 -05:00
|
|
|
}
|
|
|
|
|
2022-02-16 11:54:29 -06:00
|
|
|
if err := hs.AlertNotificationService.UpdateAlertNotification(c.Req.Context(), &cmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, err.Error(), err)
|
2020-09-11 11:04:43 -05:00
|
|
|
}
|
2021-11-02 08:11:19 -05:00
|
|
|
var alertingErr alerting.ValidationError
|
|
|
|
if errors.As(err, &alertingErr) {
|
|
|
|
return response.Error(400, err.Error(), err)
|
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update alert notification", err)
|
2016-06-15 03:48:04 -05:00
|
|
|
}
|
|
|
|
|
2020-07-08 03:17:05 -05:00
|
|
|
query := models.GetAlertNotificationsQuery{
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
Id: cmd.Id,
|
|
|
|
}
|
|
|
|
|
2022-02-16 11:54:29 -06:00
|
|
|
if err := hs.AlertNotificationService.GetAlertNotifications(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get alert notification", err)
|
2020-07-08 03:17:05 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, dtos.NewAlertNotification(query.Result))
|
2016-06-15 03:48:04 -05:00
|
|
|
}
|
2016-06-16 08:21:44 -05:00
|
|
|
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) UpdateAlertNotificationByUID(c *models.ReqContext) response.Response {
|
|
|
|
cmd := models.UpdateAlertNotificationWithUidCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2019-03-26 06:37:02 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
2021-10-11 07:30:59 -05:00
|
|
|
cmd.Uid = web.Params(c.Req)[":uid"]
|
2019-03-26 06:37:02 -05:00
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
err := hs.fillWithSecureSettingsDataByUID(c.Req.Context(), &cmd)
|
2020-07-08 03:17:05 -05:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update alert notification", err)
|
2020-07-08 03:17:05 -05:00
|
|
|
}
|
|
|
|
|
2022-02-16 11:54:29 -06:00
|
|
|
if err := hs.AlertNotificationService.UpdateAlertNotificationWithUid(c.Req.Context(), &cmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, err.Error(), nil)
|
2020-09-11 11:04:43 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update alert notification", err)
|
2019-03-26 06:37:02 -05:00
|
|
|
}
|
|
|
|
|
2020-07-08 03:17:05 -05:00
|
|
|
query := models.GetAlertNotificationsWithUidQuery{
|
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
Uid: cmd.Uid,
|
|
|
|
}
|
|
|
|
|
2022-02-16 11:54:29 -06:00
|
|
|
if err := hs.AlertNotificationService.GetAlertNotificationsWithUid(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get alert notification", err)
|
2020-07-08 03:17:05 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, dtos.NewAlertNotification(query.Result))
|
2020-07-08 03:17:05 -05:00
|
|
|
}
|
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
func (hs *HTTPServer) fillWithSecureSettingsData(ctx context.Context, cmd *models.UpdateAlertNotificationCommand) error {
|
2020-07-08 03:17:05 -05:00
|
|
|
if len(cmd.SecureSettings) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
query := &models.GetAlertNotificationsQuery{
|
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
Id: cmd.Id,
|
|
|
|
}
|
|
|
|
|
2022-02-16 11:54:29 -06:00
|
|
|
if err := hs.AlertNotificationService.GetAlertNotifications(ctx, query); err != nil {
|
2021-10-07 09:33:50 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
secureSettings, err := hs.EncryptionService.DecryptJsonData(ctx, query.Result.SecureSettings, setting.SecretKey)
|
|
|
|
if err != nil {
|
2020-07-08 03:17:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range secureSettings {
|
|
|
|
if _, ok := cmd.SecureSettings[k]; !ok {
|
|
|
|
cmd.SecureSettings[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
func (hs *HTTPServer) fillWithSecureSettingsDataByUID(ctx context.Context, cmd *models.UpdateAlertNotificationWithUidCommand) error {
|
2020-07-08 03:17:05 -05:00
|
|
|
if len(cmd.SecureSettings) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
query := &models.GetAlertNotificationsWithUidQuery{
|
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
Uid: cmd.Uid,
|
|
|
|
}
|
|
|
|
|
2022-02-16 11:54:29 -06:00
|
|
|
if err := hs.AlertNotificationService.GetAlertNotificationsWithUid(ctx, query); err != nil {
|
2021-10-07 09:33:50 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
secureSettings, err := hs.EncryptionService.DecryptJsonData(ctx, query.Result.SecureSettings, setting.SecretKey)
|
|
|
|
if err != nil {
|
2020-07-08 03:17:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range secureSettings {
|
|
|
|
if _, ok := cmd.SecureSettings[k]; !ok {
|
|
|
|
cmd.SecureSettings[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-03-26 06:37:02 -05:00
|
|
|
}
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) DeleteAlertNotification(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
notificationId, err := strconv.ParseInt(web.Params(c.Req)[":notificationId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "notificationId is invalid", err)
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:03:48 -05:00
|
|
|
cmd := models.DeleteAlertNotificationCommand{
|
2016-06-16 08:21:44 -05:00
|
|
|
OrgId: c.OrgId,
|
2022-01-14 10:55:57 -06:00
|
|
|
Id: notificationId,
|
2016-06-16 08:21:44 -05:00
|
|
|
}
|
|
|
|
|
2022-02-16 11:54:29 -06:00
|
|
|
if err := hs.AlertNotificationService.DeleteAlertNotification(c.Req.Context(), &cmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, err.Error(), nil)
|
2020-09-11 11:04:43 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to delete alert notification", err)
|
2016-06-16 08:21:44 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Notification deleted")
|
2016-06-16 08:21:44 -05:00
|
|
|
}
|
2016-08-30 02:32:56 -05:00
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) DeleteAlertNotificationByUID(c *models.ReqContext) response.Response {
|
2019-08-12 13:03:48 -05:00
|
|
|
cmd := models.DeleteAlertNotificationWithUidCommand{
|
2019-03-26 06:37:02 -05:00
|
|
|
OrgId: c.OrgId,
|
2021-10-11 07:30:59 -05:00
|
|
|
Uid: web.Params(c.Req)[":uid"],
|
2019-03-26 06:37:02 -05:00
|
|
|
}
|
|
|
|
|
2022-02-16 11:54:29 -06:00
|
|
|
if err := hs.AlertNotificationService.DeleteAlertNotificationWithUid(c.Req.Context(), &cmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, err.Error(), nil)
|
2020-09-11 11:04:43 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to delete alert notification", err)
|
2019-03-26 06:37:02 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, util.DynMap{
|
2020-09-11 11:04:43 -05:00
|
|
|
"message": "Notification deleted",
|
|
|
|
"id": cmd.DeletedAlertNotificationId,
|
|
|
|
})
|
2019-03-26 06:37:02 -05:00
|
|
|
}
|
|
|
|
|
2020-09-22 09:22:19 -05:00
|
|
|
// POST /api/alert-notifications/test
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) NotificationTest(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
dto := dtos.NotificationTestCommand{}
|
|
|
|
if err := web.Bind(c.Req, &dto); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2016-09-05 07:43:53 -05:00
|
|
|
cmd := &alerting.NotificationTestCommand{
|
2020-07-08 03:17:05 -05:00
|
|
|
OrgID: c.OrgId,
|
|
|
|
ID: dto.ID,
|
|
|
|
Name: dto.Name,
|
|
|
|
Type: dto.Type,
|
|
|
|
Settings: dto.Settings,
|
|
|
|
SecureSettings: dto.SecureSettings,
|
2016-09-05 07:43:53 -05:00
|
|
|
}
|
|
|
|
|
2022-04-08 07:30:25 -05:00
|
|
|
if err := hs.AlertNotificationService.HandleNotificationTestCommand(c.Req.Context(), cmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrSmtpNotEnabled) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(412, err.Error(), err)
|
2017-04-25 06:16:37 -05:00
|
|
|
}
|
2021-04-22 09:00:21 -05:00
|
|
|
var alertingErr alerting.ValidationError
|
|
|
|
if errors.As(err, &alertingErr) {
|
|
|
|
return response.Error(400, err.Error(), err)
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to send alert notifications", err)
|
2016-09-05 07:43:53 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Test notification sent")
|
2016-09-05 07:43:53 -05:00
|
|
|
}
|
|
|
|
|
2020-09-22 09:22:19 -05:00
|
|
|
// POST /api/alerts/:alertId/pause
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) PauseAlert(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
dto := dtos.PauseAlertCommand{}
|
|
|
|
if err := web.Bind(c.Req, &dto); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-01-14 10:55:57 -06:00
|
|
|
alertID, err := strconv.ParseInt(web.Params(c.Req)[":alertId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "alertId is invalid", err)
|
|
|
|
}
|
2020-01-09 04:18:51 -06:00
|
|
|
result := make(map[string]interface{})
|
|
|
|
result["alertId"] = alertID
|
2018-01-30 07:41:25 -06:00
|
|
|
|
2019-08-12 13:03:48 -05:00
|
|
|
query := models.GetAlertByIdQuery{Id: alertID}
|
2022-02-04 06:41:15 -06:00
|
|
|
if err := hs.SQLStore.GetAlertById(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Get Alert failed", err)
|
2018-01-30 07:41:25 -06:00
|
|
|
}
|
|
|
|
|
2021-09-23 10:43:32 -05:00
|
|
|
guardian := guardian.New(c.Req.Context(), query.Result.DashboardId, c.OrgId, c.SignedInUser)
|
2018-01-30 07:41:25 -06:00
|
|
|
if canEdit, err := guardian.CanEdit(); err != nil || !canEdit {
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Error while checking permissions for Alert", err)
|
2018-01-30 07:41:25 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(403, "Access denied to this dashboard and alert", nil)
|
2018-01-30 07:41:25 -06:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:18:51 -06:00
|
|
|
// Alert state validation
|
|
|
|
if query.Result.State != models.AlertStatePaused && !dto.Paused {
|
|
|
|
result["state"] = "un-paused"
|
|
|
|
result["message"] = "Alert is already un-paused"
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, result)
|
2020-01-09 04:18:51 -06:00
|
|
|
} else if query.Result.State == models.AlertStatePaused && dto.Paused {
|
|
|
|
result["state"] = models.AlertStatePaused
|
|
|
|
result["message"] = "Alert is already paused"
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, result)
|
2020-01-09 04:18:51 -06:00
|
|
|
}
|
|
|
|
|
2019-08-12 13:03:48 -05:00
|
|
|
cmd := models.PauseAlertCommand{
|
2016-12-19 09:07:55 -06:00
|
|
|
OrgId: c.OrgId,
|
2018-03-22 16:13:46 -05:00
|
|
|
AlertIds: []int64{alertID},
|
2016-12-19 09:07:55 -06:00
|
|
|
Paused: dto.Paused,
|
2016-10-10 07:26:09 -05:00
|
|
|
}
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
if err := hs.SQLStore.PauseAlert(c.Req.Context(), &cmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "", err)
|
2016-10-10 07:26:09 -05:00
|
|
|
}
|
|
|
|
|
2022-04-12 07:15:16 -05:00
|
|
|
resp := models.AlertStateUnknown
|
2017-12-31 07:16:19 -06:00
|
|
|
pausedState := "un-paused"
|
2016-10-10 07:26:09 -05:00
|
|
|
if cmd.Paused {
|
2021-01-15 07:43:20 -06:00
|
|
|
resp = models.AlertStatePaused
|
2016-10-11 02:45:38 -05:00
|
|
|
pausedState = "paused"
|
2016-10-10 07:26:09 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
result["state"] = resp
|
2020-01-09 04:18:51 -06:00
|
|
|
result["message"] = "Alert " + pausedState
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, result)
|
2016-10-10 07:26:09 -05:00
|
|
|
}
|
2016-12-15 10:01:45 -06:00
|
|
|
|
2020-09-22 09:22:19 -05:00
|
|
|
// POST /api/admin/pause-all-alerts
|
2022-02-04 06:41:15 -06:00
|
|
|
func (hs *HTTPServer) PauseAllAlerts(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
dto := dtos.PauseAllAlertsCommand{}
|
|
|
|
if err := web.Bind(c.Req, &dto); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2019-08-12 13:03:48 -05:00
|
|
|
updateCmd := models.PauseAllAlertCommand{
|
2016-12-16 09:07:23 -06:00
|
|
|
Paused: dto.Paused,
|
2016-12-15 10:01:45 -06:00
|
|
|
}
|
|
|
|
|
2022-02-04 06:41:15 -06:00
|
|
|
if err := hs.SQLStore.PauseAllAlerts(c.Req.Context(), &updateCmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to pause alerts", err)
|
2016-12-15 10:01:45 -06:00
|
|
|
}
|
|
|
|
|
2022-04-12 07:15:16 -05:00
|
|
|
resp := models.AlertStatePending
|
2016-12-15 10:01:45 -06:00
|
|
|
pausedState := "un paused"
|
|
|
|
if updateCmd.Paused {
|
2021-01-15 07:43:20 -06:00
|
|
|
resp = models.AlertStatePaused
|
2016-12-15 10:01:45 -06:00
|
|
|
pausedState = "paused"
|
|
|
|
}
|
|
|
|
|
|
|
|
result := map[string]interface{}{
|
2021-01-15 07:43:20 -06:00
|
|
|
"state": resp,
|
2016-12-19 09:44:59 -06:00
|
|
|
"message": "alerts " + pausedState,
|
2016-12-15 10:01:45 -06:00
|
|
|
"alertsAffected": updateCmd.ResultCount,
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, result)
|
2016-12-15 10:01:45 -06:00
|
|
|
}
|