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

View File

@@ -624,7 +624,7 @@ func TestRulerAccess(t *testing.T) {
Rules: []apimodels.PostableExtendedRuleNode{
{
ApiRuleNode: &apimodels.ApiRuleNode{
For: interval,
For: &interval,
Labels: map[string]string{"label1": "val1"},
Annotations: map[string]string{"annotation1": "val1"},
},
@@ -865,7 +865,7 @@ func TestAlertRuleCRUD(t *testing.T) {
rulegroup: "arulegroup",
rule: apimodels.PostableExtendedRuleNode{
ApiRuleNode: &apimodels.ApiRuleNode{
For: interval,
For: &interval,
Labels: map[string]string{"label1": "val1"},
Annotations: map[string]string{"annotation1": "val1"},
},
@@ -881,7 +881,7 @@ func TestAlertRuleCRUD(t *testing.T) {
rulegroup: "arulegroup",
rule: apimodels.PostableExtendedRuleNode{
ApiRuleNode: &apimodels.ApiRuleNode{
For: interval,
For: &interval,
Labels: map[string]string{"label1": "val1"},
Annotations: map[string]string{"annotation1": "val1"},
},
@@ -911,7 +911,7 @@ func TestAlertRuleCRUD(t *testing.T) {
rulegroup: "arulegroup",
rule: apimodels.PostableExtendedRuleNode{
ApiRuleNode: &apimodels.ApiRuleNode{
For: interval,
For: &interval,
Labels: map[string]string{"label1": "val1"},
Annotations: map[string]string{"annotation1": "val1"},
},
@@ -941,7 +941,7 @@ func TestAlertRuleCRUD(t *testing.T) {
rulegroup: getLongString(t, ngstore.AlertRuleMaxTitleLength+1),
rule: apimodels.PostableExtendedRuleNode{
ApiRuleNode: &apimodels.ApiRuleNode{
For: interval,
For: &interval,
Labels: map[string]string{"label1": "val1"},
Annotations: map[string]string{"annotation1": "val1"},
},
@@ -972,7 +972,7 @@ func TestAlertRuleCRUD(t *testing.T) {
interval: invalidInterval,
rule: apimodels.PostableExtendedRuleNode{
ApiRuleNode: &apimodels.ApiRuleNode{
For: interval,
For: &interval,
Labels: map[string]string{"label1": "val1"},
Annotations: map[string]string{"annotation1": "val1"},
},
@@ -1002,7 +1002,7 @@ func TestAlertRuleCRUD(t *testing.T) {
rulegroup: "arulegroup",
rule: apimodels.PostableExtendedRuleNode{
ApiRuleNode: &apimodels.ApiRuleNode{
For: interval,
For: &interval,
Labels: map[string]string{"label1": "val1"},
Annotations: map[string]string{"annotation1": "val1"},
},
@@ -1032,7 +1032,7 @@ func TestAlertRuleCRUD(t *testing.T) {
rulegroup: "arulegroup",
rule: apimodels.PostableExtendedRuleNode{
ApiRuleNode: &apimodels.ApiRuleNode{
For: interval,
For: &interval,
Labels: map[string]string{"label1": "val1"},
Annotations: map[string]string{"annotation1": "val1"},
},
@@ -1090,7 +1090,7 @@ func TestAlertRuleCRUD(t *testing.T) {
Rules: []apimodels.PostableExtendedRuleNode{
{
ApiRuleNode: &apimodels.ApiRuleNode{
For: interval,
For: &interval,
Labels: map[string]string{"label1": "val1"},
Annotations: map[string]string{"annotation1": "val1"},
},
@@ -1219,6 +1219,7 @@ func TestAlertRuleCRUD(t *testing.T) {
},
{
"expr":"",
"for": "0s",
"grafana_alert":{
"id":2,
"orgId":1,
@@ -1269,7 +1270,7 @@ func TestAlertRuleCRUD(t *testing.T) {
Rules: []apimodels.PostableExtendedRuleNode{
{
ApiRuleNode: &apimodels.ApiRuleNode{
For: interval,
For: &interval,
Labels: map[string]string{
"label1": "val42",
"foo": "bar",
@@ -1342,7 +1343,7 @@ func TestAlertRuleCRUD(t *testing.T) {
Rules: []apimodels.PostableExtendedRuleNode{
{
ApiRuleNode: &apimodels.ApiRuleNode{
For: interval,
For: &interval,
Labels: map[string]string{
"label1": "val42",
"foo": "bar",
@@ -1376,7 +1377,7 @@ func TestAlertRuleCRUD(t *testing.T) {
},
{
ApiRuleNode: &apimodels.ApiRuleNode{
For: interval,
For: &interval,
Labels: map[string]string{
"label1": "val42",
"foo": "bar",
@@ -1448,7 +1449,7 @@ func TestAlertRuleCRUD(t *testing.T) {
Rules: []apimodels.PostableExtendedRuleNode{
{
ApiRuleNode: &apimodels.ApiRuleNode{
For: forValue,
For: &forValue,
Labels: map[string]string{
// delete foo label
"label1": "val1", // update label value
@@ -1575,7 +1576,7 @@ func TestAlertRuleCRUD(t *testing.T) {
Rules: []apimodels.PostableExtendedRuleNode{
{
ApiRuleNode: &apimodels.ApiRuleNode{
For: forValue,
For: &forValue,
},
GrafanaManagedAlert: &apimodels.PostableGrafanaRule{
UID: ruleUID, // Including the UID in the payload makes the endpoint update the existing rule.