Skip to content

Commit 9e6d1f5

Browse files
Alerting Rule Group: Suppress duration equivalent diffs (#941)
Closes #861 When setting 60m, the API returns 1h. Also added a duration validation
1 parent cefd806 commit 9e6d1f5

File tree

6 files changed

+29
-19
lines changed

6 files changed

+29
-19
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
@@ -9,7 +9,7 @@ resource "grafana_rule_group" "my_multi_alert_group" {
99
org_id = 1
1010
rule {
1111
name = "My Alert Rule 1"
12-
for = "2m"
12+
for = "60m"
1313
condition = "B"
1414
no_data_state = "NoData"
1515
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
@@ -9,7 +9,7 @@ resource "grafana_rule_group" "my_multi_alert_group" {
99
org_id = 1
1010
rule {
1111
name = "My Alert Rule 1"
12-
for = "2m"
12+
for = "1h"
1313
condition = "B"
1414
no_data_state = "NoData"
1515
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
@@ -9,7 +9,7 @@ resource "grafana_rule_group" "my_multi_alert_group" {
99
org_id = 1
1010
rule {
1111
name = "My Alert Rule 1"
12-
for = "2m"
12+
for = "1h"
1313
condition = "B"
1414
no_data_state = "NoData"
1515
exec_err_state = "Alerting"

internal/common/schema.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"fmt"
55
"strconv"
66
"strings"
7+
"time"
78

9+
"github.com/hashicorp/go-cty/cty"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
811
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
912
)
1013

@@ -50,3 +53,12 @@ func CloneResourceSchemaForDatasource(r *schema.Resource, updates map[string]*sc
5053
func AllowedValuesDescription(description string, allowedValues []string) string {
5154
return fmt.Sprintf("%s. Allowed values: `%s`.", description, strings.Join(allowedValues, "`, `"))
5255
}
56+
57+
func ValidateDuration(i interface{}, p cty.Path) diag.Diagnostics {
58+
v := i.(string)
59+
_, err := time.ParseDuration(v)
60+
if err != nil {
61+
return diag.Errorf("%q is not a valid duration: %s", v, err)
62+
}
63+
return nil
64+
}

internal/resources/cloud/resource_cloud_stack.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
gapi "github.com/grafana/grafana-api-golang-client"
1616
"github.com/grafana/terraform-provider-grafana/internal/common"
17-
"github.com/hashicorp/go-cty/cty"
1817
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1918
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
2019
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -86,17 +85,10 @@ available at “https://<stack_slug>.grafana.net".`,
8685
DiffSuppressFunc: func(_, _, newValue string, _ *schema.ResourceData) bool { return newValue == "false" },
8786
},
8887
"wait_for_readiness_timeout": {
89-
Type: schema.TypeString,
90-
Optional: true,
91-
Default: defaultReadinessTimeout.String(),
92-
ValidateDiagFunc: func(i interface{}, p cty.Path) diag.Diagnostics {
93-
v := i.(string)
94-
_, err := time.ParseDuration(v)
95-
if err != nil {
96-
return diag.Errorf("%q is not a valid duration: %s", v, err)
97-
}
98-
return nil
99-
},
88+
Type: schema.TypeString,
89+
Optional: true,
90+
Default: defaultReadinessTimeout.String(),
91+
ValidateDiagFunc: common.ValidateDuration,
10092
// Only used when wait_for_readiness is true
10193
DiffSuppressFunc: func(_, _, newValue string, d *schema.ResourceData) bool {
10294
return newValue == defaultReadinessTimeout.String()

internal/resources/grafana/resource_alerting_rule_group.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@ This resource requires Grafana 9.1.0 or later.
7676
Description: "The name of the alert rule.",
7777
},
7878
"for": {
79-
Type: schema.TypeString,
80-
Optional: true,
81-
Default: 0,
82-
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.",
79+
Type: schema.TypeString,
80+
Optional: true,
81+
Default: 0,
82+
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.",
83+
ValidateDiagFunc: common.ValidateDuration,
84+
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
85+
oldDuration, _ := time.ParseDuration(oldValue)
86+
newDuration, _ := time.ParseDuration(newValue)
87+
return oldDuration == newDuration
88+
},
8389
},
8490
"no_data_state": {
8591
Type: schema.TypeString,

0 commit comments

Comments
 (0)