Commit 3866f609 by vikkyomkar Committed by Torkel Ödegaard

API: Added alert state validation before changing its state (#21375)

* added alert state validation before changing its state

* modified boolean condition

* converted most occurring string into const

* referred the const of alert models
parent fe61f6b7
...@@ -354,9 +354,10 @@ func NotificationTest(c *models.ReqContext, dto dtos.NotificationTestCommand) Re ...@@ -354,9 +354,10 @@ func NotificationTest(c *models.ReqContext, dto dtos.NotificationTestCommand) Re
//POST /api/alerts/:alertId/pause //POST /api/alerts/:alertId/pause
func PauseAlert(c *models.ReqContext, dto dtos.PauseAlertCommand) Response { func PauseAlert(c *models.ReqContext, dto dtos.PauseAlertCommand) Response {
alertID := c.ParamsInt64("alertId") alertID := c.ParamsInt64("alertId")
result := make(map[string]interface{})
result["alertId"] = alertID
query := models.GetAlertByIdQuery{Id: alertID} query := models.GetAlertByIdQuery{Id: alertID}
if err := bus.Dispatch(&query); err != nil { if err := bus.Dispatch(&query); err != nil {
return Error(500, "Get Alert failed", err) return Error(500, "Get Alert failed", err)
} }
...@@ -370,6 +371,17 @@ func PauseAlert(c *models.ReqContext, dto dtos.PauseAlertCommand) Response { ...@@ -370,6 +371,17 @@ func PauseAlert(c *models.ReqContext, dto dtos.PauseAlertCommand) Response {
return Error(403, "Access denied to this dashboard and alert", nil) return Error(403, "Access denied to this dashboard and alert", nil)
} }
// Alert state validation
if query.Result.State != models.AlertStatePaused && !dto.Paused {
result["state"] = "un-paused"
result["message"] = "Alert is already un-paused"
return JSON(200, result)
} else if query.Result.State == models.AlertStatePaused && dto.Paused {
result["state"] = models.AlertStatePaused
result["message"] = "Alert is already paused"
return JSON(200, result)
}
cmd := models.PauseAlertCommand{ cmd := models.PauseAlertCommand{
OrgId: c.OrgId, OrgId: c.OrgId,
AlertIds: []int64{alertID}, AlertIds: []int64{alertID},
...@@ -387,12 +399,8 @@ func PauseAlert(c *models.ReqContext, dto dtos.PauseAlertCommand) Response { ...@@ -387,12 +399,8 @@ func PauseAlert(c *models.ReqContext, dto dtos.PauseAlertCommand) Response {
pausedState = "paused" pausedState = "paused"
} }
result := map[string]interface{}{ result["state"] = response
"alertId": alertID, result["message"] = "Alert " + pausedState
"state": response,
"message": "Alert " + pausedState,
}
return JSON(200, result) return JSON(200, result)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment