Alerting: Add provenance field to /api/v1/provisioning/alert-rules (#76252)

This commit adds the missing Provenance field to responses for
/api/v1/provisioning/alert-rules.
This commit is contained in:
George Robinson
2023-10-11 14:51:20 +01:00
committed by GitHub
parent 6b52bb9c27
commit 05e12e787b
4 changed files with 55 additions and 12 deletions
+3 -3
View File
@@ -57,7 +57,7 @@ type MuteTimingService interface {
}
type AlertRuleService interface {
GetAlertRules(ctx context.Context, orgID int64) ([]*alerting_models.AlertRule, error)
GetAlertRules(ctx context.Context, orgID int64) ([]*alerting_models.AlertRule, map[string]alerting_models.Provenance, error)
GetAlertRule(ctx context.Context, orgID int64, ruleUID string) (alerting_models.AlertRule, alerting_models.Provenance, error)
CreateAlertRule(ctx context.Context, rule alerting_models.AlertRule, provenance alerting_models.Provenance, userID int64) (alerting_models.AlertRule, error)
UpdateAlertRule(ctx context.Context, rule alerting_models.AlertRule, provenance alerting_models.Provenance) (alerting_models.AlertRule, error)
@@ -300,11 +300,11 @@ func (srv *ProvisioningSrv) RouteDeleteMuteTiming(c *contextmodel.ReqContext, na
}
func (srv *ProvisioningSrv) RouteGetAlertRules(c *contextmodel.ReqContext) response.Response {
rules, err := srv.alertRules.GetAlertRules(c.Req.Context(), c.SignedInUser.GetOrgID())
rules, provenances, err := srv.alertRules.GetAlertRules(c.Req.Context(), c.SignedInUser.GetOrgID())
if err != nil {
return ErrResp(http.StatusInternalServerError, err, "")
}
return response.JSON(http.StatusOK, ProvisionedAlertRuleFromAlertRules(rules))
return response.JSON(http.StatusOK, ProvisionedAlertRuleFromAlertRules(rules, provenances))
}
func (srv *ProvisioningSrv) RouteRouteGetAlertRule(c *contextmodel.ReqContext, UID string) response.Response {
+2 -2
View File
@@ -55,10 +55,10 @@ func ProvisionedAlertRuleFromAlertRule(rule models.AlertRule, provenance models.
}
// ProvisionedAlertRuleFromAlertRules converts a collection of models.AlertRule to definitions.ProvisionedAlertRules with provenance status models.ProvenanceNone
func ProvisionedAlertRuleFromAlertRules(rules []*models.AlertRule) definitions.ProvisionedAlertRules {
func ProvisionedAlertRuleFromAlertRules(rules []*models.AlertRule, provenances map[string]models.Provenance) definitions.ProvisionedAlertRules {
result := make([]definitions.ProvisionedAlertRule, 0, len(rules))
for _, r := range rules {
result = append(result, ProvisionedAlertRuleFromAlertRule(*r, models.ProvenanceNone))
result = append(result, ProvisionedAlertRuleFromAlertRule(*r, provenances[r.UID]))
}
return result
}
@@ -45,16 +45,23 @@ func NewAlertRuleService(ruleStore RuleStore,
}
}
func (service *AlertRuleService) GetAlertRules(ctx context.Context, orgID int64) ([]*models.AlertRule, error) {
func (service *AlertRuleService) GetAlertRules(ctx context.Context, orgID int64) ([]*models.AlertRule, map[string]models.Provenance, error) {
q := models.ListAlertRulesQuery{
OrgID: orgID,
}
rules, err := service.ruleStore.ListAlertRules(ctx, &q)
if err != nil {
return nil, err
return nil, nil, err
}
// TODO: GET provenance
return rules, nil
provenances := make(map[string]models.Provenance)
if len(rules) > 0 {
resourceType := rules[0].ResourceType()
provenances, err = service.provenanceStore.GetProvenances(ctx, orgID, resourceType)
if err != nil {
return nil, nil, err
}
}
return rules, provenances, nil
}
func (service *AlertRuleService) GetAlertRule(ctx context.Context, orgID int64, ruleUID string) (models.AlertRule, models.Provenance, error) {
@@ -62,15 +69,15 @@ func (service *AlertRuleService) GetAlertRule(ctx context.Context, orgID int64,
OrgID: orgID,
UID: ruleUID,
}
rules, err := service.ruleStore.GetAlertRuleByUID(ctx, query)
rule, err := service.ruleStore.GetAlertRuleByUID(ctx, query)
if err != nil {
return models.AlertRule{}, models.ProvenanceNone, err
}
provenance, err := service.provenanceStore.GetProvenance(ctx, rules, orgID)
provenance, err := service.provenanceStore.GetProvenance(ctx, rule, orgID)
if err != nil {
return models.AlertRule{}, models.ProvenanceNone, err
}
return *rules, provenance, nil
return *rule, provenance, nil
}
type AlertRuleWithFolderTitle struct {