Alerting: Add method to provisioning API for obtaining a group and its rules (#51398)

* Generate shell for new route

* Propagate path parameters

* Implement route logic

* Add a couple simple tests

* Use NotFound error for not found, avoid returning pointer

* Regenerate
This commit is contained in:
Alexander Weaver
2022-07-05 11:53:50 -05:00
committed by GitHub
parent e64cde8727
commit b9c7eb1380
10 changed files with 220 additions and 228 deletions
@@ -7,6 +7,7 @@ import (
"time"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/ngalert/store"
"github.com/grafana/grafana/pkg/util"
@@ -89,6 +90,32 @@ func (service *AlertRuleService) CreateAlertRule(ctx context.Context, rule model
return rule, nil
}
func (service *AlertRuleService) GetRuleGroup(ctx context.Context, orgID int64, folder, group string) (definitions.AlertRuleGroup, error) {
q := models.ListAlertRulesQuery{
OrgID: orgID,
NamespaceUIDs: []string{folder},
RuleGroup: group,
}
if err := service.ruleStore.ListAlertRules(ctx, &q); err != nil {
return definitions.AlertRuleGroup{}, err
}
if len(q.Result) == 0 {
return definitions.AlertRuleGroup{}, store.ErrAlertRuleGroupNotFound
}
res := definitions.AlertRuleGroup{
Title: q.Result[0].RuleGroup,
FolderUID: q.Result[0].NamespaceUID,
Interval: q.Result[0].IntervalSeconds,
Rules: []models.AlertRule{},
}
for _, r := range q.Result {
if r != nil {
res.Rules = append(res.Rules, *r)
}
}
return res, nil
}
// UpdateRuleGroup will update the interval for all rules in the group.
func (service *AlertRuleService) UpdateRuleGroup(ctx context.Context, orgID int64, namespaceUID string, ruleGroup string, interval int64) error {
if err := models.ValidateRuleGroupInterval(interval, service.baseIntervalSeconds); err != nil {