mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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
This commit is contained in:
parent
fe61f6b7a7
commit
3866f609ce
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user