mirror of
https://github.com/grafana/grafana.git
synced 2025-07-29 19:22:57 +08:00
feat(alerting): add api endpoints for listing alerts
This commit is contained in:
47
pkg/api/alerting.go
Normal file
47
pkg/api/alerting.go
Normal file
@ -0,0 +1,47 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
func ValidateOrgAlert(c *middleware.Context) {
|
||||
id := c.ParamsInt64(":id")
|
||||
query := models.GetAlertById{Id: id}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// GET /api/alert_rule
|
||||
func GetAlerts(c *middleware.Context) Response {
|
||||
query := models.GetAlertsQuery{
|
||||
OrgId: c.OrgId,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "List alerts failed", err)
|
||||
}
|
||||
|
||||
return Json(200, query.Result)
|
||||
}
|
||||
|
||||
// GET /api/alert_rule/:id
|
||||
func GetAlert(c *middleware.Context) Response {
|
||||
id := c.ParamsInt64(":id")
|
||||
query := models.GetAlertById{Id: id}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "List alerts failed", err)
|
||||
}
|
||||
|
||||
return Json(200, &query.Result)
|
||||
}
|
Reference in New Issue
Block a user