Skip to content

Commit 1b9da40

Browse files
Alert Rule: Fix 0s issue (#1299)
Turns out Terraform is giving us an empty string there. In that case, it should be zero, so it's parseable
1 parent 3659c5c commit 1b9da40

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

internal/resources/grafana/resource_alerting_rule_group.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,11 @@ func unpackAlertRule(raw interface{}, groupName string, folderUID string, orgID
334334
return nil, err
335335
}
336336

337-
forDuration, err := strfmt.ParseDuration(json["for"].(string))
337+
forStr := json["for"].(string)
338+
if forStr == "" {
339+
forStr = "0"
340+
}
341+
forDuration, err := strfmt.ParseDuration(forStr)
338342
if err != nil {
339343
return nil, err
340344
}

internal/resources/grafana/resource_alerting_rule_group_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,30 @@ func TestAccAlertRule_disableProvenance(t *testing.T) {
342342
})
343343
}
344344

345+
func TestAccAlertRule_zeroSeconds(t *testing.T) {
346+
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")
347+
348+
var group models.AlertRuleGroup
349+
var name = acctest.RandString(10)
350+
351+
resource.ParallelTest(t, resource.TestCase{
352+
ProviderFactories: testutils.ProviderFactories,
353+
CheckDestroy: alertingRuleGroupCheckExists.destroyed(&group, nil),
354+
Steps: []resource.TestStep{
355+
{
356+
Config: testAccAlertRuleZeroSeconds(name),
357+
Check: resource.ComposeTestCheckFunc(
358+
alertingRuleGroupCheckExists.exists("grafana_rule_group.my_rule_group", &group),
359+
resource.TestCheckResourceAttr("grafana_rule_group.my_rule_group", "name", name),
360+
resource.TestCheckResourceAttr("grafana_rule_group.my_rule_group", "rule.#", "1"),
361+
resource.TestCheckResourceAttr("grafana_rule_group.my_rule_group", "rule.0.name", "My Random Walk Alert"),
362+
resource.TestCheckResourceAttr("grafana_rule_group.my_rule_group", "rule.0.for", "0s"),
363+
),
364+
},
365+
},
366+
})
367+
}
368+
345369
func testAccAlertRuleGroupInOrgConfig(name string, interval int, disableProvenance bool) string {
346370
return fmt.Sprintf(`
347371
resource "grafana_organization" "test" {
@@ -385,3 +409,44 @@ resource "grafana_rule_group" "test" {
385409
}
386410
`, name, interval, disableProvenance)
387411
}
412+
413+
func testAccAlertRuleZeroSeconds(name string) string {
414+
return fmt.Sprintf(`
415+
resource "grafana_folder" "rule_folder" {
416+
title = "%[1]s"
417+
}
418+
419+
resource "grafana_data_source" "testdata_datasource" {
420+
name = "%[1]s"
421+
type = "grafana-testdata-datasource"
422+
url = "http://localhost:3333"
423+
}
424+
425+
resource "grafana_rule_group" "my_rule_group" {
426+
name = "%[1]s"
427+
folder_uid = grafana_folder.rule_folder.uid
428+
interval_seconds = 60
429+
org_id = 1
430+
431+
rule {
432+
name = "My Random Walk Alert"
433+
condition = "C"
434+
for = "0s"
435+
436+
// Query the datasource.
437+
data {
438+
ref_id = "A"
439+
relative_time_range {
440+
from = 600
441+
to = 0
442+
}
443+
datasource_uid = grafana_data_source.testdata_datasource.uid
444+
model = jsonencode({
445+
intervalMs = 1000
446+
maxDataPoints = 43200
447+
refId = "A"
448+
})
449+
}
450+
}
451+
}`, name)
452+
}

0 commit comments

Comments
 (0)