Repository: grafana/terraform-provider-grafana

Issue:
Rules with the same name are permitted by the API, but not the terraform provider
### Terraform Version

1.9

### Terraform Grafana Provider Version

dev

### Grafana Version

Cloud

### Affected Resource(s)

* grafana_rule_group

Hi,

We are building out our observability with Grafana, and have run into a discrepancy between the API/GUI and the Terraform Provider in regards to Alert Names.

As a long-time Prometheus user I went to setup my Alerts the same as I would under Prometheus:

```
- alert: HighErrorRate
  expr: rate(http_requests_total{status=~"5.."}[5m]) > 0.1
  labels:
    severity: warning
    team: backend

- alert: HighErrorRate
  expr: rate(http_requests_total{status=~"5.."}[5m]) > 0.5
  labels:
    severity: critical
    team: backend
```

Testing this in the GUI works fine, and is a pattern I've long used (in conjunction with Inhibition Rules) to provide varying thresholds for different severities. After confirming this works I wrote a terraform module but kept getting a similar error to this on deployment:

```│ Error: rule with name "ECSMemoryUtilization" already exists in the folder```

A quick look at the source code led me to [this block](https://github.com/grafana/terraform-provider-grafana/blob/main/internal/resources/grafana/resource_alerting_rule_group.go#L422-L424) and [this block](https://github.com/grafana/terraform-provider-grafana/blob/main/internal/resources/grafana/resource_alerting_rule_group.go#L430-L440) as the source of this. Removing these lines, rebuilding the provider and using a local development override then let my terraform module apply correctly.

Does Grafana support having Alert Names with the same Title in this manner? i.e. is this a bug with the provider or platform? 

### Terraform Configuration Files

_No response_

### Expected Behavior

_No response_

### Actual Behavior

_No response_

### Steps to Reproduce

_No response_

### Important Factoids

_No response_

### References

_No response_

Hints from issue comments:
@santihernandezc: Hi @bbetter173, thanks for your bug report!
I just merged a PR to fix it -> https://github.com/grafana/terraform-provider-grafana/pull/2323

Solution patch:
diff --git a/internal/resources/grafana/resource_alerting_rule_group.go b/internal/resources/grafana/resource_alerting_rule_group.go
index f4cc4c68..b6ad3455 100644
--- a/internal/resources/grafana/resource_alerting_rule_group.go
+++ b/internal/resources/grafana/resource_alerting_rule_group.go
@@ -427,26 +427,11 @@ func putAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta inte
 
 			// 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)
-			for _, existingRule := range respAlertRules.Payload {
-				if *existingRule.Title == *ruleToApply.Title && *existingRule.FolderUID == *ruleToApply.FolderUID {
-					if *ruleToApply.RuleGroup == *existingRule.RuleGroup {
-						break
-					}
-
-					// Retry so that if the user is moving a rule from one group to another, it will pass on the next iteration.
-					return retry.RetryableError(fmt.Errorf("rule with name %q already exists in the folder", *ruleToApply.Title))
-				}
-			}
-
 			rules = append(rules, ruleToApply)
 		}
 



Test patch:
diff --git a/internal/resources/grafana/resource_alerting_rule_group_test.go b/internal/resources/grafana/resource_alerting_rule_group_test.go
index eecc7889..37a6cd95 100644
--- a/internal/resources/grafana/resource_alerting_rule_group_test.go
+++ b/internal/resources/grafana/resource_alerting_rule_group_test.go
@@ -381,7 +381,7 @@ resource "grafana_rule_group" "second" {
 }
 
 func TestAccAlertRule_ruleNameConflict(t *testing.T) {
-	testutils.CheckOSSTestsEnabled(t, ">=9.1.0")
+	testutils.CheckOSSTestsEnabled(t, ">=11.6.0")
 
 	name := acctest.RandString(10)
 
@@ -452,7 +452,11 @@ resource "grafana_rule_group" "first" {
 	}
 }
 				`, name),
-				ExpectError: regexp.MustCompile(`rule with name "My Alert Rule" is defined more than once`),
+				Check: resource.ComposeTestCheckFunc(
+					resource.TestCheckResourceAttr("grafana_rule_group.first", "rule.#", "2"),
+					resource.TestCheckResourceAttr("grafana_rule_group.first", "rule.0.name", "My Alert Rule"),
+					resource.TestCheckResourceAttr("grafana_rule_group.first", "rule.1.name", "My Alert Rule"),
+				),
 			},
 		},
 	})