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
//POST /api/alerts/:alertId/pause
func PauseAlert(c *models.ReqContext, dto dtos.PauseAlertCommand) Response {
alertID := c.ParamsInt64("alertId")
result := make(map[string]interface{})
result["alertId"] = alertID
query := models.GetAlertByIdQuery{Id: alertID}
if err := bus.Dispatch(&query); err != nil {
return Error(500, "Get Alert failed", err)
}
......@@ -370,6 +371,17 @@ func PauseAlert(c *models.ReqContext, dto dtos.PauseAlertCommand) Response {
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{
OrgId: c.OrgId,
AlertIds: []int64{alertID},
......@@ -387,12 +399,8 @@ func PauseAlert(c *models.ReqContext, dto dtos.PauseAlertCommand) Response {
pausedState = "paused"
}
result := map[string]interface{}{
"alertId": alertID,
"state": response,
"message": "Alert " + pausedState,
}
result["state"] = response
result["message"] = "Alert " + pausedState
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