Skip to content

Commit

Permalink
Alerting Rule Group: Suppress duration equivalent diffs (#941)
Browse files Browse the repository at this point in the history
Closes #861
When setting 60m, the API returns 1h. Also added a duration validation
  • Loading branch information
julienduchesne authored Jun 14, 2023
1 parent cefd806 commit 9e6d1f5
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ resource "grafana_rule_group" "my_multi_alert_group" {
org_id = 1
rule {
name = "My Alert Rule 1"
for = "2m"
for = "60m"
condition = "B"
no_data_state = "NoData"
exec_err_state = "Alerting"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ resource "grafana_rule_group" "my_multi_alert_group" {
org_id = 1
rule {
name = "My Alert Rule 1"
for = "2m"
for = "1h"
condition = "B"
no_data_state = "NoData"
exec_err_state = "Alerting"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ resource "grafana_rule_group" "my_multi_alert_group" {
org_id = 1
rule {
name = "My Alert Rule 1"
for = "2m"
for = "1h"
condition = "B"
no_data_state = "NoData"
exec_err_state = "Alerting"
Expand Down
12 changes: 12 additions & 0 deletions internal/common/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"fmt"
"strconv"
"strings"
"time"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -50,3 +53,12 @@ func CloneResourceSchemaForDatasource(r *schema.Resource, updates map[string]*sc
func AllowedValuesDescription(description string, allowedValues []string) string {
return fmt.Sprintf("%s. Allowed values: `%s`.", description, strings.Join(allowedValues, "`, `"))
}

func ValidateDuration(i interface{}, p cty.Path) diag.Diagnostics {
v := i.(string)
_, err := time.ParseDuration(v)
if err != nil {
return diag.Errorf("%q is not a valid duration: %s", v, err)
}
return nil
}
16 changes: 4 additions & 12 deletions internal/resources/cloud/resource_cloud_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -86,17 +85,10 @@ available at “https://<stack_slug>.grafana.net".`,
DiffSuppressFunc: func(_, _, newValue string, _ *schema.ResourceData) bool { return newValue == "false" },
},
"wait_for_readiness_timeout": {
Type: schema.TypeString,
Optional: true,
Default: defaultReadinessTimeout.String(),
ValidateDiagFunc: func(i interface{}, p cty.Path) diag.Diagnostics {
v := i.(string)
_, err := time.ParseDuration(v)
if err != nil {
return diag.Errorf("%q is not a valid duration: %s", v, err)
}
return nil
},
Type: schema.TypeString,
Optional: true,
Default: defaultReadinessTimeout.String(),
ValidateDiagFunc: common.ValidateDuration,
// Only used when wait_for_readiness is true
DiffSuppressFunc: func(_, _, newValue string, d *schema.ResourceData) bool {
return newValue == defaultReadinessTimeout.String()
Expand Down
14 changes: 10 additions & 4 deletions internal/resources/grafana/resource_alerting_rule_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@ This resource requires Grafana 9.1.0 or later.
Description: "The name of the alert rule.",
},
"for": {
Type: schema.TypeString,
Optional: true,
Default: 0,
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.",
Type: schema.TypeString,
Optional: true,
Default: 0,
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.",
ValidateDiagFunc: common.ValidateDuration,
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
oldDuration, _ := time.ParseDuration(oldValue)
newDuration, _ := time.ParseDuration(newValue)
return oldDuration == newDuration
},
},
"no_data_state": {
Type: schema.TypeString,
Expand Down

0 comments on commit 9e6d1f5

Please sign in to comment.