Alerting: validate condition before updating rulegroup (#33367)

* Alerting: validate condition before updating rulegroup

* Apply suggestions from code review
This commit is contained in:
Sofia Papagiannaki
2021-04-28 11:31:51 +03:00
committed by GitHub
parent 643e7af3e0
commit 7ccb022c03
5 changed files with 102 additions and 41 deletions
+16 -2
View File
@@ -2,9 +2,11 @@ package api
import (
"errors"
"fmt"
"net/http"
"time"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/ngalert/store"
coreapi "github.com/grafana/grafana/pkg/api"
@@ -18,8 +20,9 @@ import (
)
type RulerSrv struct {
store store.RuleStore
log log.Logger
store store.RuleStore
DatasourceCache datasources.CacheService
log log.Logger
}
func (srv RulerSrv) RouteDeleteNamespaceRulesConfig(c *models.ReqContext) response.Response {
@@ -196,6 +199,17 @@ func (srv RulerSrv) RoutePostNameRulesConfig(c *models.ReqContext, ruleGroupConf
return response.Error(http.StatusBadRequest, "rule group name is not valid", nil)
}
for _, r := range ruleGroupConfig.Rules {
cond := ngmodels.Condition{
Condition: r.GrafanaManagedAlert.Condition,
OrgID: c.SignedInUser.OrgId,
Data: r.GrafanaManagedAlert.Data,
}
if err := validateCondition(cond, c.SignedInUser, c.SkipCache, srv.DatasourceCache); err != nil {
return response.Error(http.StatusBadRequest, fmt.Sprintf("failed to validate alert rule %s", r.GrafanaManagedAlert.Title), err)
}
}
if err := srv.store.UpdateRuleGroup(store.UpdateRuleGroupCmd{
OrgID: c.SignedInUser.OrgId,
NamespaceUID: namespace.Uid,