mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: make sure that rules in rule group are nil if not provided (#55301)
This commit is contained in:
parent
bf5b21563c
commit
4dc0d49025
@ -191,20 +191,19 @@ type AlertRuleGroup struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *AlertRuleGroup) ToModel() (models.AlertRuleGroup, error) {
|
func (a *AlertRuleGroup) ToModel() (models.AlertRuleGroup, error) {
|
||||||
rules := make([]models.AlertRule, 0, len(a.Rules))
|
ruleGroup := models.AlertRuleGroup{
|
||||||
|
Title: a.Title,
|
||||||
|
FolderUID: a.FolderUID,
|
||||||
|
Interval: a.Interval,
|
||||||
|
}
|
||||||
for i := range a.Rules {
|
for i := range a.Rules {
|
||||||
converted, err := a.Rules[i].UpstreamModel()
|
converted, err := a.Rules[i].UpstreamModel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return models.AlertRuleGroup{}, err
|
return models.AlertRuleGroup{}, err
|
||||||
}
|
}
|
||||||
rules = append(rules, converted)
|
ruleGroup.Rules = append(ruleGroup.Rules, converted)
|
||||||
}
|
}
|
||||||
return models.AlertRuleGroup{
|
return ruleGroup, nil
|
||||||
Title: a.Title,
|
|
||||||
FolderUID: a.FolderUID,
|
|
||||||
Interval: a.Interval,
|
|
||||||
Rules: rules,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAlertRuleGroupFromModel(d models.AlertRuleGroup) AlertRuleGroup {
|
func NewAlertRuleGroupFromModel(d models.AlertRuleGroup) AlertRuleGroup {
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
package definitions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestToModel(t *testing.T) {
|
||||||
|
t.Run("if no rules are provided the rule field should be nil", func(t *testing.T) {
|
||||||
|
ruleGroup := AlertRuleGroup{
|
||||||
|
Title: "123",
|
||||||
|
FolderUID: "123",
|
||||||
|
Interval: 10,
|
||||||
|
}
|
||||||
|
tm, err := ruleGroup.ToModel()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Nil(t, tm.Rules)
|
||||||
|
})
|
||||||
|
t.Run("if rules are provided the rule field should be not nil", func(t *testing.T) {
|
||||||
|
ruleGroup := AlertRuleGroup{
|
||||||
|
Title: "123",
|
||||||
|
FolderUID: "123",
|
||||||
|
Interval: 10,
|
||||||
|
Rules: []ProvisionedAlertRule{
|
||||||
|
{
|
||||||
|
UID: "1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
tm, err := ruleGroup.ToModel()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, tm.Rules, 1)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user