Alerting: Default pending period (for) to 0s when the rule is created using file provisioning (#94646)

Alerting: default for to 0s when the rule is created using file provisioning
pull/94797/head
Alexander Akhmetov 1 year ago committed by GitHub
parent 384562e1ad
commit bfbbdf5efb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 12
      pkg/services/provisioning/alerting/rules_types.go
  2. 7
      pkg/services/provisioning/alerting/rules_types_test.go

@ -97,11 +97,17 @@ func (rule *AlertRuleV1) mapToModel(orgID int64) (models.AlertRule, error) {
return models.AlertRule{}, fmt.Errorf("rule '%s' failed to parse: no UID set", alertRule.Title)
}
alertRule.OrgID = orgID
duration, err := model.ParseDuration(rule.For.Value())
if err != nil {
return models.AlertRule{}, fmt.Errorf("rule '%s' failed to parse: %w", alertRule.Title, err)
duration := model.Duration(0)
if rule.For.Value() != "" {
var err error
duration, err = model.ParseDuration(rule.For.Value())
if err != nil {
return models.AlertRule{}, fmt.Errorf("rule '%s' failed to parse 'for' field: %w", alertRule.Title, err)
}
}
alertRule.For = time.Duration(duration)
dasboardUID := rule.DasboardUID.Value()
dashboardUID := rule.DashboardUID.Value()
alertRule.DashboardUID = withFallback(dashboardUID, dasboardUID) // Use correct spelling over supported typo.

@ -102,11 +102,12 @@ func TestRules(t *testing.T) {
_, err := rule.mapToModel(1)
require.Error(t, err)
})
t.Run("a rule with out a for duration should error", func(t *testing.T) {
t.Run("a rule without a for duration should default to 0s", func(t *testing.T) {
rule := validRuleV1(t)
rule.For = values.StringValue{}
_, err := rule.mapToModel(1)
require.Error(t, err)
ruleMapped, err := rule.mapToModel(1)
require.NoError(t, err)
require.Equal(t, time.Duration(0), ruleMapped.For)
})
t.Run("a rule with an invalid for duration should error", func(t *testing.T) {
rule := validRuleV1(t)

Loading…
Cancel
Save