mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Also clear fields in model/store validation for recording rules (#93506)
* Fix model validation * Remove validation from provisioning service
This commit is contained in:
parent
9a40e8dca6
commit
f1ba7deff5
@ -610,9 +610,20 @@ func validateRecordingRuleFields(rule *AlertRule) error {
|
||||
if !prommodels.IsValidMetricName(metricName) {
|
||||
return fmt.Errorf("%w: %s", ErrAlertRuleFailedValidation, "metric name for recording rule must be a valid Prometheus metric name")
|
||||
}
|
||||
|
||||
clearRecordingRuleIgnoredFields(rule)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func clearRecordingRuleIgnoredFields(rule *AlertRule) {
|
||||
rule.NoDataState = ""
|
||||
rule.ExecErrState = ""
|
||||
rule.Condition = ""
|
||||
rule.For = 0
|
||||
rule.NotificationSettings = nil
|
||||
}
|
||||
|
||||
func (alertRule *AlertRule) ResourceType() string {
|
||||
return "alertRule"
|
||||
}
|
||||
|
@ -747,6 +747,24 @@ func TestIntegrationInsertAlertRules(t *testing.T) {
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("clears fields that should not exist on recording rules", func(t *testing.T) {
|
||||
rule := recordingRulesGen.Generate()
|
||||
rules, err := store.InsertAlertRules(context.Background(), []models.AlertRule{rule})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, rules, 1)
|
||||
ruleUID := rules[0].UID
|
||||
savedRule, err := store.GetAlertRuleByUID(context.Background(), &models.GetAlertRuleByUIDQuery{
|
||||
OrgID: orgID,
|
||||
UID: ruleUID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "", savedRule.Condition)
|
||||
require.Equal(t, models.NoDataState(""), savedRule.NoDataState)
|
||||
require.Equal(t, models.ExecutionErrorState(""), savedRule.ExecErrState)
|
||||
require.Zero(t, savedRule.For)
|
||||
require.Nil(t, savedRule.NotificationSettings)
|
||||
})
|
||||
|
||||
t.Run("fail to insert rules with same ID", func(t *testing.T) {
|
||||
_, err = store.InsertAlertRules(context.Background(), []models.AlertRule{rules[0]})
|
||||
require.ErrorIs(t, err, models.ErrAlertRuleConflictBase)
|
||||
|
Loading…
Reference in New Issue
Block a user