Skip to content

Commit 804625f

Browse files
Alerting Rule Group: Allow days in validation (#959)
Bug introduced in #941 Closes #955 Turns out the duration lib used in alerting supports days and weeks. Let's use the same lib here
1 parent 45a4dcb commit 804625f

File tree

7 files changed

+22
-8
lines changed

7 files changed

+22
-8
lines changed

examples/resources/grafana_rule_group/_acc_multi_rule_group.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ EOT
8585

8686
rule {
8787
name = "My Alert Rule 2"
88-
for = "4m"
88+
for = "6d"
8989
condition = "B"
9090
no_data_state = "NoData"
9191
exec_err_state = "Alerting"

examples/resources/grafana_rule_group/_acc_multi_rule_group_added.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ EOT
8585

8686
rule {
8787
name = "My Alert Rule 2"
88-
for = "4m"
88+
for = "144h" // 6 days
8989
condition = "B"
9090
no_data_state = "NoData"
9191
exec_err_state = "Alerting"

examples/resources/grafana_rule_group/_acc_multi_rule_group_subtracted.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ EOT
8585

8686
rule {
8787
name = "My Alert Rule 2"
88-
for = "4m"
88+
for = "8640m" // 6 days
8989
condition = "B"
9090
no_data_state = "NoData"
9191
exec_err_state = "Alerting"

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
1414
github.com/hashicorp/terraform-plugin-docs v0.15.0
1515
github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1
16+
github.com/prometheus/common v0.44.0
1617
golang.org/x/text v0.10.0
1718
)
1819

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
171171
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
172172
github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXqo=
173173
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
174-
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
174+
github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdOOfY=
175+
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
176+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
175177
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
176178
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
177179
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
@@ -280,8 +282,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
280282
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
281283
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
282284
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
283-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
284285
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
286+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
285287
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
286288
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
287289
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

internal/common/schema.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/go-cty/cty"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
promModel "github.com/prometheus/common/model"
1213
)
1314

1415
// SchemaDiffFloat32 is a SchemaDiffSuppressFunc for diffing float32 values.
@@ -62,3 +63,12 @@ func ValidateDuration(i interface{}, p cty.Path) diag.Diagnostics {
6263
}
6364
return nil
6465
}
66+
67+
func ValidateDurationWithDays(i interface{}, p cty.Path) diag.Diagnostics {
68+
v := i.(string)
69+
_, err := promModel.ParseDuration(v)
70+
if err != nil {
71+
return diag.Errorf("%q is not a valid duration: %s", v, err)
72+
}
73+
return nil
74+
}

internal/resources/grafana/resource_alerting_rule_group.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1515
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1616
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
17+
promModel "github.com/prometheus/common/model"
1718
)
1819

1920
func ResourceRuleGroup() *schema.Resource {
@@ -81,10 +82,10 @@ This resource requires Grafana 9.1.0 or later.
8182
Optional: true,
8283
Default: 0,
8384
Description: "The amount of time for which the rule must be breached for the rule to be considered to be Firing. Before this time has elapsed, the rule is only considered to be Pending.",
84-
ValidateDiagFunc: common.ValidateDuration,
85+
ValidateDiagFunc: common.ValidateDurationWithDays,
8586
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
86-
oldDuration, _ := time.ParseDuration(oldValue)
87-
newDuration, _ := time.ParseDuration(newValue)
87+
oldDuration, _ := promModel.ParseDuration(oldValue)
88+
newDuration, _ := promModel.ParseDuration(newValue)
8889
return oldDuration == newDuration
8990
},
9091
},

0 commit comments

Comments
 (0)