Skip to content

Commit fd7d124

Browse files
grafana_rule_group: Detect name conflicts (#1550)
Closes #1534 Currently, it either crashes or overwrites the existing rule
1 parent 8020b13 commit fd7d124

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

internal/resources/grafana/resource_alerting_rule_group.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,23 @@ func readAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta int
329329
func putAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
330330
client, orgID := OAPIClientFromNewOrgResource(meta, data)
331331

332+
if data.IsNewResource() {
333+
// Check if a rule group with the same name already exists. The API either:
334+
// - returns a 500 error if it exists in a different folder, which is not very helpful.
335+
// - overwrites the existing rule group if it exists in the same folder, which is not expected of a TF provider.
336+
resp, err := client.Provisioning.GetAlertRules()
337+
if err != nil {
338+
return diag.FromErr(err)
339+
}
340+
341+
for _, rule := range resp.Payload {
342+
name := data.Get("name").(string)
343+
if *rule.RuleGroup == name {
344+
return diag.Errorf("rule group with name %q already exists", name)
345+
}
346+
}
347+
}
348+
332349
group := data.Get("name").(string)
333350
folder := data.Get("folder_uid").(string)
334351
interval := data.Get("interval_seconds").(int)

internal/resources/grafana/resource_alerting_rule_group_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package grafana_test
33
import (
44
"encoding/json"
55
"fmt"
6+
"regexp"
67
"testing"
78

89
"github.com/grafana/grafana-openapi-client-go/models"
@@ -320,6 +321,97 @@ func TestAccAlertRule_inOrg(t *testing.T) {
320321
})
321322
}
322323

324+
func TestAccAlertRule_nameConflict(t *testing.T) {
325+
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")
326+
327+
name := acctest.RandString(10)
328+
329+
resource.ParallelTest(t, resource.TestCase{
330+
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
331+
Steps: []resource.TestStep{
332+
{
333+
Config: fmt.Sprintf(`
334+
resource "grafana_organization" "test" {
335+
name = "%[1]s"
336+
}
337+
338+
resource "grafana_folder" "first" {
339+
org_id = grafana_organization.test.id
340+
title = "%[1]s-first"
341+
}
342+
343+
resource "grafana_rule_group" "first" {
344+
org_id = grafana_organization.test.id
345+
name = "%[1]s"
346+
folder_uid = grafana_folder.first.uid
347+
interval_seconds = 60
348+
rule {
349+
name = "My Alert Rule first"
350+
for = "2m"
351+
condition = "B"
352+
no_data_state = "NoData"
353+
exec_err_state = "Alerting"
354+
is_paused = false
355+
data {
356+
ref_id = "A"
357+
query_type = ""
358+
relative_time_range {
359+
from = 600
360+
to = 0
361+
}
362+
datasource_uid = "PD8C576611E62080A"
363+
model = jsonencode({
364+
hide = false
365+
intervalMs = 1000
366+
maxDataPoints = 43200
367+
refId = "A"
368+
})
369+
}
370+
}
371+
}
372+
373+
resource "grafana_folder" "second" {
374+
depends_on = [grafana_rule_group.first]
375+
org_id = grafana_organization.test.id
376+
title = "%[1]s-second"
377+
}
378+
379+
resource "grafana_rule_group" "second" {
380+
org_id = grafana_organization.test.id
381+
name = "%[1]s"
382+
folder_uid = grafana_folder.second.uid
383+
interval_seconds = 60
384+
rule {
385+
name = "My Alert Rule second"
386+
for = "2m"
387+
condition = "B"
388+
no_data_state = "NoData"
389+
exec_err_state = "Alerting"
390+
is_paused = false
391+
data {
392+
ref_id = "A"
393+
query_type = ""
394+
relative_time_range {
395+
from = 600
396+
to = 0
397+
}
398+
datasource_uid = "PD8C576611E62080A"
399+
model = jsonencode({
400+
hide = false
401+
intervalMs = 1000
402+
maxDataPoints = 43200
403+
refId = "A"
404+
})
405+
}
406+
}
407+
}
408+
`, name),
409+
ExpectError: regexp.MustCompile(`rule group with name "` + name + `" already exists`),
410+
},
411+
},
412+
})
413+
}
414+
323415
func TestAccAlertRule_disableProvenance(t *testing.T) {
324416
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")
325417

0 commit comments

Comments
 (0)