mirror of
https://github.com/grafana/grafana.git
synced 2024-12-01 21:19:28 -06:00
53cd39fde5
* dashboards: new command for validating dashboard before update Removes validation logic from saveDashboard and later on use the new command for validating dashboard before saving a dashboard. This due to the fact that we need to validate permissions for overwriting other dashboards by uid and title. * dashboards: use the new command for validating dashboard before saving Had to refactor dashboard provisioning a bit to be able to sidetrack the permission validation in a somewhat reasonable way. Adds some initial tests of the dashboard repository, but needs to be extended later. At least now you can mock the dashboard guardian * dashboards: removes validation logic in the save dashboard api layer Use the dashboard repository solely for create/update dashboards and let it do all the validation. One exception regarding quota validation which still is in api layer since that logic is in a macaron middleware. Need to move out-commented api tests later. * dashboards: fix database tests for validate and saving dashboards * dashboards: rename dashboard repository to dashboard service Split the old dashboard repository interface in two new interfaces, IDashboardService and IDashboardProvisioningService. Makes it more explicit when using it from the provisioning package and there's no possibility of calling an incorrect method for saving a dashboard. * database: make the InitTestDB function available to use from other packages * dashboards: rename ValidateDashboardForUpdateCommand and some refactoring * dashboards: integration tests of dashboard service * dashboard: fix sqlstore test due to folder exist validation * dashboards: move dashboard service integration tests to sqlstore package Had to move it to the sqlstore package due to concurrency problems when running against mysql and postgres. Using InitTestDB from two packages added conflicts when clearing and running migrations on the test database * dashboards: refactor how to find id to be used for save permission check * dashboards: remove duplicated dashboard tests * dashboards: cleanup dashboard service integration tests * dashboards: handle save dashboard errors and return correct http status * fix: remove log statement * dashboards: import dashboard should use dashboard service Had to move alerting commands to models package due to problems with import cycles of packages. * dashboards: cleanup dashboard api tests and add some tests for post dashboard * dashboards: rename dashboard service interfaces * dashboards: rename dashboard guardian interface
289 lines
7.3 KiB
Go
289 lines
7.3 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
|
)
|
|
|
|
func ValidateOrgAlert(c *middleware.Context) {
|
|
id := c.ParamsInt64(":alertId")
|
|
query := models.GetAlertByIdQuery{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
|
|
}
|
|
}
|
|
|
|
func GetAlertStatesForDashboard(c *middleware.Context) Response {
|
|
dashboardId := c.QueryInt64("dashboardId")
|
|
|
|
if dashboardId == 0 {
|
|
return ApiError(400, "Missing query parameter dashboardId", nil)
|
|
}
|
|
|
|
query := models.GetAlertStatesForDashboardQuery{
|
|
OrgId: c.OrgId,
|
|
DashboardId: c.QueryInt64("dashboardId"),
|
|
}
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
return ApiError(500, "Failed to fetch alert states", err)
|
|
}
|
|
|
|
return Json(200, query.Result)
|
|
}
|
|
|
|
// GET /api/alerts
|
|
func GetAlerts(c *middleware.Context) Response {
|
|
query := models.GetAlertsQuery{
|
|
OrgId: c.OrgId,
|
|
DashboardId: c.QueryInt64("dashboardId"),
|
|
PanelId: c.QueryInt64("panelId"),
|
|
Limit: c.QueryInt64("limit"),
|
|
User: c.SignedInUser,
|
|
}
|
|
|
|
states := c.QueryStrings("state")
|
|
if len(states) > 0 {
|
|
query.State = states
|
|
}
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
return ApiError(500, "List alerts failed", err)
|
|
}
|
|
|
|
for _, alert := range query.Result {
|
|
alert.Url = models.GetDashboardUrl(alert.DashboardUid, alert.DashboardSlug)
|
|
}
|
|
|
|
return Json(200, query.Result)
|
|
}
|
|
|
|
// POST /api/alerts/test
|
|
func AlertTest(c *middleware.Context, dto dtos.AlertTestCommand) Response {
|
|
if _, idErr := dto.Dashboard.Get("id").Int64(); idErr != nil {
|
|
return ApiError(400, "The dashboard needs to be saved at least once before you can test an alert rule", nil)
|
|
}
|
|
|
|
backendCmd := alerting.AlertTestCommand{
|
|
OrgId: c.OrgId,
|
|
Dashboard: dto.Dashboard,
|
|
PanelId: dto.PanelId,
|
|
}
|
|
|
|
if err := bus.Dispatch(&backendCmd); err != nil {
|
|
if validationErr, ok := err.(alerting.ValidationError); ok {
|
|
return ApiError(422, validationErr.Error(), nil)
|
|
}
|
|
return ApiError(500, "Failed to test rule", err)
|
|
}
|
|
|
|
res := backendCmd.Result
|
|
dtoRes := &dtos.AlertTestResult{
|
|
Firing: res.Firing,
|
|
ConditionEvals: res.ConditionEvals,
|
|
State: res.Rule.State,
|
|
}
|
|
|
|
if res.Error != nil {
|
|
dtoRes.Error = res.Error.Error()
|
|
}
|
|
|
|
for _, log := range res.Logs {
|
|
dtoRes.Logs = append(dtoRes.Logs, &dtos.AlertTestResultLog{Message: log.Message, Data: log.Data})
|
|
}
|
|
for _, match := range res.EvalMatches {
|
|
dtoRes.EvalMatches = append(dtoRes.EvalMatches, &dtos.EvalMatch{Metric: match.Metric, Value: match.Value})
|
|
}
|
|
|
|
dtoRes.TimeMs = fmt.Sprintf("%1.3fms", res.GetDurationMs())
|
|
|
|
return Json(200, dtoRes)
|
|
}
|
|
|
|
// GET /api/alerts/:id
|
|
func GetAlert(c *middleware.Context) Response {
|
|
id := c.ParamsInt64(":alertId")
|
|
query := models.GetAlertByIdQuery{Id: id}
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
return ApiError(500, "List alerts failed", err)
|
|
}
|
|
|
|
return Json(200, &query.Result)
|
|
}
|
|
|
|
func GetAlertNotifiers(c *middleware.Context) Response {
|
|
return Json(200, alerting.GetNotifiers())
|
|
}
|
|
|
|
func GetAlertNotifications(c *middleware.Context) Response {
|
|
query := &models.GetAllAlertNotificationsQuery{OrgId: c.OrgId}
|
|
|
|
if err := bus.Dispatch(query); err != nil {
|
|
return ApiError(500, "Failed to get alert notifications", err)
|
|
}
|
|
|
|
result := make([]*dtos.AlertNotification, 0)
|
|
|
|
for _, notification := range query.Result {
|
|
result = append(result, &dtos.AlertNotification{
|
|
Id: notification.Id,
|
|
Name: notification.Name,
|
|
Type: notification.Type,
|
|
IsDefault: notification.IsDefault,
|
|
Created: notification.Created,
|
|
Updated: notification.Updated,
|
|
})
|
|
}
|
|
|
|
return Json(200, result)
|
|
}
|
|
|
|
func GetAlertNotificationById(c *middleware.Context) Response {
|
|
query := &models.GetAlertNotificationsQuery{
|
|
OrgId: c.OrgId,
|
|
Id: c.ParamsInt64("notificationId"),
|
|
}
|
|
|
|
if err := bus.Dispatch(query); err != nil {
|
|
return ApiError(500, "Failed to get alert notifications", err)
|
|
}
|
|
|
|
return Json(200, query.Result)
|
|
}
|
|
|
|
func CreateAlertNotification(c *middleware.Context, cmd models.CreateAlertNotificationCommand) Response {
|
|
cmd.OrgId = c.OrgId
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
return ApiError(500, "Failed to create alert notification", err)
|
|
}
|
|
|
|
return Json(200, cmd.Result)
|
|
}
|
|
|
|
func UpdateAlertNotification(c *middleware.Context, cmd models.UpdateAlertNotificationCommand) Response {
|
|
cmd.OrgId = c.OrgId
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
return ApiError(500, "Failed to update alert notification", err)
|
|
}
|
|
|
|
return Json(200, cmd.Result)
|
|
}
|
|
|
|
func DeleteAlertNotification(c *middleware.Context) Response {
|
|
cmd := models.DeleteAlertNotificationCommand{
|
|
OrgId: c.OrgId,
|
|
Id: c.ParamsInt64("notificationId"),
|
|
}
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
return ApiError(500, "Failed to delete alert notification", err)
|
|
}
|
|
|
|
return ApiSuccess("Notification deleted")
|
|
}
|
|
|
|
//POST /api/alert-notifications/test
|
|
func NotificationTest(c *middleware.Context, dto dtos.NotificationTestCommand) Response {
|
|
cmd := &alerting.NotificationTestCommand{
|
|
Name: dto.Name,
|
|
Type: dto.Type,
|
|
Settings: dto.Settings,
|
|
}
|
|
|
|
if err := bus.Dispatch(cmd); err != nil {
|
|
if err == models.ErrSmtpNotEnabled {
|
|
return ApiError(412, err.Error(), err)
|
|
}
|
|
return ApiError(500, "Failed to send alert notifications", err)
|
|
}
|
|
|
|
return ApiSuccess("Test notification sent")
|
|
}
|
|
|
|
//POST /api/alerts/:alertId/pause
|
|
func PauseAlert(c *middleware.Context, dto dtos.PauseAlertCommand) Response {
|
|
alertId := c.ParamsInt64("alertId")
|
|
|
|
query := models.GetAlertByIdQuery{Id: alertId}
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
return ApiError(500, "Get Alert failed", err)
|
|
}
|
|
|
|
guardian := guardian.New(query.Result.DashboardId, c.OrgId, c.SignedInUser)
|
|
if canEdit, err := guardian.CanEdit(); err != nil || !canEdit {
|
|
if err != nil {
|
|
return ApiError(500, "Error while checking permissions for Alert", err)
|
|
}
|
|
|
|
return ApiError(403, "Access denied to this dashboard and alert", nil)
|
|
}
|
|
|
|
cmd := models.PauseAlertCommand{
|
|
OrgId: c.OrgId,
|
|
AlertIds: []int64{alertId},
|
|
Paused: dto.Paused,
|
|
}
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
return ApiError(500, "", err)
|
|
}
|
|
|
|
var response models.AlertStateType = models.AlertStatePending
|
|
pausedState := "un-paused"
|
|
if cmd.Paused {
|
|
response = models.AlertStatePaused
|
|
pausedState = "paused"
|
|
}
|
|
|
|
result := map[string]interface{}{
|
|
"alertId": alertId,
|
|
"state": response,
|
|
"message": "Alert " + pausedState,
|
|
}
|
|
|
|
return Json(200, result)
|
|
}
|
|
|
|
//POST /api/admin/pause-all-alerts
|
|
func PauseAllAlerts(c *middleware.Context, dto dtos.PauseAllAlertsCommand) Response {
|
|
updateCmd := models.PauseAllAlertCommand{
|
|
Paused: dto.Paused,
|
|
}
|
|
|
|
if err := bus.Dispatch(&updateCmd); err != nil {
|
|
return ApiError(500, "Failed to pause alerts", err)
|
|
}
|
|
|
|
var response models.AlertStateType = models.AlertStatePending
|
|
pausedState := "un paused"
|
|
if updateCmd.Paused {
|
|
response = models.AlertStatePaused
|
|
pausedState = "paused"
|
|
}
|
|
|
|
result := map[string]interface{}{
|
|
"state": response,
|
|
"message": "alerts " + pausedState,
|
|
"alertsAffected": updateCmd.ResultCount,
|
|
}
|
|
|
|
return Json(200, result)
|
|
}
|