Skip to content

Commit

Permalink
Fix make alert rule uid settable to avoid accidental overwriting of e…
Browse files Browse the repository at this point in the history
…xisting rule history

Unfortunately this is the only viable workaround. It will require users to manually set the uid for alert rules similar to how it is done in file provisioning.

Addresses #1928
  • Loading branch information
moustafab committed Dec 24, 2024
1 parent 1aa1b32 commit 431ad6c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
3 changes: 0 additions & 3 deletions docs/resources/rule_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,6 @@ Optional:
- `no_data_state` (String) Describes what state to enter when the rule's query returns No Data. Options are OK, NoData, KeepLast, and Alerting. Defaults to `NoData`.
- `notification_settings` (Block List, Max: 1) Notification settings for the rule. If specified, it overrides the notification policies. Available since Grafana 10.4, requires feature flag 'alertingSimplifiedRouting' to be enabled. (see [below for nested schema](#nestedblock--rule--notification_settings))
- `record` (Block List, Max: 1) Settings for a recording rule. Available since Grafana 11.2, requires feature flag 'grafanaManagedRecordingRules' to be enabled. (see [below for nested schema](#nestedblock--rule--record))

Read-Only:

- `uid` (String) The unique identifier of the alert rule.

<a id="nestedblock--rule--data"></a>
Expand Down
6 changes: 5 additions & 1 deletion internal/resources/grafana/resource_alerting_rule_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ This resource requires Grafana 9.1.0 or later.
Schema: map[string]*schema.Schema{
"uid": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The unique identifier of the alert rule.",
},
Expand Down Expand Up @@ -383,11 +384,14 @@ func putAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta inte
return retry.NonRetryableError(err)
}

// Check if a rule with the same name already exists within the same rule group
// Check if a rule with the same name or uid already exists within the same rule group
for _, r := range rules {
if *r.Title == *ruleToApply.Title {
return retry.NonRetryableError(fmt.Errorf("rule with name %q is defined more than once", *ruleToApply.Title))
}
if ruleToApply.UID != "" && r.UID == ruleToApply.UID {
return retry.NonRetryableError(fmt.Errorf("rule with UID %q is defined more than once. Rules with name %q and %q have the same uid", ruleToApply.UID, *r.Title, *ruleToApply.Title))
}
}

// Check if a rule with the same name already exists within the same folder (changing the ordering is allowed within the same rule group)
Expand Down
81 changes: 81 additions & 0 deletions internal/resources/grafana/resource_alerting_rule_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,87 @@ resource "grafana_rule_group" "first" {
})
}

func TestAccAlertRule_ruleUIDConflict(t *testing.T) {
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

name := acctest.RandString(10)
uid := acctest.RandString(10)

resource.ParallelTest(t, resource.TestCase{
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "grafana_organization" "test" {
name = "%[1]s"
}
resource "grafana_folder" "first" {
org_id = grafana_organization.test.id
title = "%[1]s-first"
}
resource "grafana_rule_group" "first" {
org_id = grafana_organization.test.id
name = "alert rule group"
folder_uid = grafana_folder.first.uid
interval_seconds = 60
rule {
name = "%[1]s"
uid = "%[2]s"
for = "2m"
condition = "B"
no_data_state = "NoData"
exec_err_state = "Alerting"
is_paused = false
data {
ref_id = "A"
query_type = ""
relative_time_range {
from = 600
to = 0
}
datasource_uid = "PD8C576611E62080A"
model = jsonencode({
hide = false
intervalMs = 1000
maxDataPoints = 43200
refId = "A"
})
}
}
rule {
name = "%[1]s 2"
uid = "%[2]s"
for = "2m"
condition = "B"
no_data_state = "NoData"
exec_err_state = "Alerting"
is_paused = false
data {
ref_id = "A"
query_type = ""
relative_time_range {
from = 600
to = 0
}
datasource_uid = "PD8C576611E62080A"
model = jsonencode({
hide = false
intervalMs = 1000
maxDataPoints = 43200
refId = "A"
})
}
}
}
`, name, uid),
ExpectError: regexp.MustCompile(`rule with UID "` + uid + `" is defined more than once. Rules with name "` + name + `" and "` + name + ` 2" have the same uid`),
},
},
})
}

func TestAccAlertRule_moveRules(t *testing.T) {
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

Expand Down
1 change: 1 addition & 0 deletions pkg/generate/testdata/generate/alerting-in-org.tf
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ resource "grafana_rule_group" "my_alert_rule" {
folder_uid = grafana_folder.rule_folder.uid
interval_seconds = 240
rule {
uid = "myalertrule1"
name = "My Alert Rule 1"
for = "2m"
condition = "B"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ resource "grafana_rule_group" "_2_alert-rule-folder_My_Rule_Group" {
}
name = "My Alert Rule 1"
no_data_state = "NoData"
uid = "myalertrule1"
data {
datasource_uid = "PD8C576611E62080A"
model = jsonencode({
Expand Down

0 comments on commit 431ad6c

Please sign in to comment.