diff --git a/pkg/services/ngalert/api/api_ruler_validation.go b/pkg/services/ngalert/api/api_ruler_validation.go index 5b4e36c272d..395beaa0291 100644 --- a/pkg/services/ngalert/api/api_ruler_validation.go +++ b/pkg/services/ngalert/api/api_ruler_validation.go @@ -21,16 +21,9 @@ func validateRuleNode( namespace *models.Folder, conditionValidator func(ngmodels.Condition) error, cfg *setting.UnifiedAlertingSettings) (*ngmodels.AlertRule, error) { - intervalSeconds := int64(interval.Seconds()) - - baseIntervalSeconds := int64(cfg.BaseInterval.Seconds()) - - if interval <= 0 { - return nil, fmt.Errorf("rule evaluation interval must be positive duration that is multiple of the base interval %d seconds", baseIntervalSeconds) - } - - if intervalSeconds%baseIntervalSeconds != 0 { - return nil, fmt.Errorf("rule evaluation interval %d should be multiple of the base interval of %d seconds", int64(interval.Seconds()), baseIntervalSeconds) + intervalSeconds, err := validateInterval(cfg, interval) + if err != nil { + return nil, err } if ruleNode.GrafanaManagedAlert == nil { @@ -54,7 +47,6 @@ func validateRuleNode( } if ruleNode.GrafanaManagedAlert.NoDataState != "" { - var err error noDataState, err = ngmodels.NoDataStateFromString(string(ruleNode.GrafanaManagedAlert.NoDataState)) if err != nil { return nil, err @@ -68,7 +60,6 @@ func validateRuleNode( } if ruleNode.GrafanaManagedAlert.ExecErrState != "" { - var err error errorState, err = ngmodels.ErrStateFromString(string(ruleNode.GrafanaManagedAlert.ExecErrState)) if err != nil { return nil, err @@ -90,7 +81,7 @@ func validateRuleNode( Condition: ruleNode.GrafanaManagedAlert.Condition, Data: ruleNode.GrafanaManagedAlert.Data, } - if err := conditionValidator(cond); err != nil { + if err = conditionValidator(cond); err != nil { return nil, fmt.Errorf("failed to validate condition of alert rule %s: %w", ruleNode.GrafanaManagedAlert.Title, err) } } @@ -108,7 +99,6 @@ func validateRuleNode( ExecErrState: errorState, } - var err error newAlertRule.For, err = validateForInterval(ruleNode) if err != nil { return nil, err @@ -126,6 +116,22 @@ func validateRuleNode( return &newAlertRule, nil } +func validateInterval(cfg *setting.UnifiedAlertingSettings, interval time.Duration) (int64, error) { + intervalSeconds := int64(interval.Seconds()) + + baseIntervalSeconds := int64(cfg.BaseInterval.Seconds()) + + if interval <= 0 { + return 0, fmt.Errorf("rule evaluation interval must be positive duration that is multiple of the base interval %d seconds", baseIntervalSeconds) + } + + if intervalSeconds%baseIntervalSeconds != 0 { + return 0, fmt.Errorf("rule evaluation interval %d should be multiple of the base interval of %d seconds", int64(interval.Seconds()), baseIntervalSeconds) + } + + return intervalSeconds, nil +} + // validateForInterval validates ApiRuleNode.For and converts it to time.Duration. If the field is not specified returns 0 if GrafanaManagedAlert.UID is empty and -1 if it is not. func validateForInterval(ruleNode *apimodels.PostableExtendedRuleNode) (time.Duration, error) { if ruleNode.ApiRuleNode == nil || ruleNode.ApiRuleNode.For == nil {