Alerting: Fix rule API to accept 0 duration of field For (#50992)

* make 'for' pointer to distinguish between missing field and 0
* set 'for' to -1 if the value is missing but not allow negative in the request + path -1 with the value from original rule
* update store validation to not allow negative 'for'
* update usages to use pointer
This commit is contained in:
Yuriy Tseretyan
2022-06-30 11:46:26 -04:00
committed by GitHub
parent 0e7a495829
commit 8b3b667a47
13 changed files with 104 additions and 35 deletions
@@ -257,7 +257,7 @@ type ApiRuleNode struct {
Record string `yaml:"record,omitempty" json:"record,omitempty"`
Alert string `yaml:"alert,omitempty" json:"alert,omitempty"`
Expr string `yaml:"expr" json:"expr"`
For model.Duration `yaml:"for,omitempty" json:"for,omitempty"`
For *model.Duration `yaml:"for,omitempty" json:"for,omitempty"`
Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"`
Annotations map[string]string `yaml:"annotations,omitempty" json:"annotations,omitempty"`
}
@@ -50,7 +50,7 @@ func Test_Rule_Marshaling(t *testing.T) {
desc: "grafana with for, annotation and label properties",
input: PostableExtendedRuleNode{
ApiRuleNode: &ApiRuleNode{
For: dur,
For: &dur,
Annotations: map[string]string{"foo": "bar"},
Labels: map[string]string{"label1": "val1"}},
GrafanaManagedAlert: &PostableGrafanaRule{},
@@ -136,7 +136,7 @@ func Test_Rule_Group_Marshaling(t *testing.T) {
Rules: []PostableExtendedRuleNode{
{
ApiRuleNode: &ApiRuleNode{
For: dur,
For: &dur,
Annotations: map[string]string{"foo": "bar"},
Labels: map[string]string{"label1": "val1"},
},