Skip to content

Commit dd80d61

Browse files
(V2 Backport) Alerting Rule Group: Support colon in title
Backport of #1625
1 parent fd79c5e commit dd80d61

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

internal/common/resource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (r *Resource) ImportExample() string {
5353
fieldTemplates[i] = fmt.Sprintf("{{ %s }}", fields[i].Name)
5454
}
5555
return fmt.Sprintf(`terraform import %s.name %q
56-
`, r.Name, strings.Join(fieldTemplates, defaultSeparator))
56+
`, r.Name, strings.Join(fieldTemplates, ResourceIDSeparator))
5757
}
5858

5959
id := r.IDType

internal/common/resource_id.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
type ResourceIDFieldType string
1111

1212
const (
13-
defaultSeparator = ":"
13+
ResourceIDSeparator = ":"
1414
ResourceIDFieldTypeInt = ResourceIDFieldType("int")
1515
ResourceIDFieldTypeString = ResourceIDFieldType("string")
1616
)
@@ -71,14 +71,14 @@ func (id *ResourceID) RequiredFields() []ResourceIDField {
7171
}
7272

7373
func NewResourceID(expectedFields ...ResourceIDField) *ResourceID {
74-
return newResourceIDWithSeparators([]string{defaultSeparator}, expectedFields...)
74+
return newResourceIDWithSeparators([]string{ResourceIDSeparator}, expectedFields...)
7575
}
7676

7777
// Deprecated: Use NewResourceID instead
7878
// We should standardize on a single separator, so that function should only be used for old resources
7979
// On major versions, switch to NewResourceID and remove uses of this function
8080
func NewResourceIDWithLegacySeparator(legacySeparator string, expectedFields ...ResourceIDField) *ResourceID {
81-
return newResourceIDWithSeparators([]string{defaultSeparator, legacySeparator}, expectedFields...)
81+
return newResourceIDWithSeparators([]string{ResourceIDSeparator, legacySeparator}, expectedFields...)
8282
}
8383

8484
func newResourceIDWithSeparators(separators []string, expectedFields ...ResourceIDField) *ResourceID {
@@ -118,7 +118,7 @@ func (id *ResourceID) Make(parts ...any) string {
118118
}
119119
}
120120

121-
return strings.Join(stringParts, defaultSeparator)
121+
return strings.Join(stringParts, ResourceIDSeparator)
122122
}
123123

124124
// Single parses a resource ID into a single value
@@ -181,5 +181,5 @@ func split(resourceID string, expectedFields []ResourceIDField, separators []str
181181
for i, f := range expectedFields {
182182
expectedFieldNames[i] = f.Name
183183
}
184-
return nil, fmt.Errorf("id %q does not match expected format. Should be in the format: %s", resourceID, strings.Join(expectedFieldNames, defaultSeparator))
184+
return nil, fmt.Errorf("id %q does not match expected format. Should be in the format: %s", resourceID, strings.Join(expectedFieldNames, ResourceIDSeparator))
185185
}

internal/resources/grafana/resource_alerting_rule_group.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,14 @@ func listRuleGroups(ctx context.Context, client *goapi.GrafanaHTTPAPI, data *Lis
286286
func readAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
287287
client, orgID, idWithoutOrg := OAPIClientFromExistingOrgResource(meta, data.Id())
288288

289-
split, err := resourceRuleGroupID.Split(idWithoutOrg)
290-
if err != nil {
291-
return diag.FromErr(err)
289+
folderUID, title, found := strings.Cut(idWithoutOrg, common.ResourceIDSeparator)
290+
if !found {
291+
split, err := resourceRuleGroupID.Split(idWithoutOrg)
292+
if err != nil {
293+
return diag.FromErr(err)
294+
}
295+
folderUID, title = split[0].(string), split[1].(string)
292296
}
293-
folderUID, title := split[0].(string), split[1].(string)
294297

295298
resp, err := client.Provisioning.GetAlertRuleGroup(title, folderUID)
296299
if err, shouldReturn := common.CheckReadError("rule group", data, err); shouldReturn {
@@ -385,11 +388,15 @@ func putAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta inte
385388
func deleteAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
386389
client, _, idWithoutOrg := OAPIClientFromExistingOrgResource(meta, data.Id())
387390

388-
split, err := resourceRuleGroupID.Split(idWithoutOrg)
389-
if err != nil {
390-
return diag.FromErr(err)
391+
folderUID, title, found := strings.Cut(idWithoutOrg, common.ResourceIDSeparator)
392+
if !found {
393+
split, err := resourceRuleGroupID.Split(idWithoutOrg)
394+
if err != nil {
395+
return diag.FromErr(err)
396+
}
397+
folderUID, title = split[0].(string), split[1].(string)
391398
}
392-
folderUID, title := split[0].(string), split[1].(string)
399+
393400
// TODO use DeleteAlertRuleGroup method instead (available since Grafana 11)
394401
resp, err := client.Provisioning.GetAlertRuleGroup(title, folderUID)
395402
if err != nil {

internal/resources/grafana/resource_alerting_rule_group_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func TestAccAlertRule_inOrg(t *testing.T) {
271271

272272
var group models.AlertRuleGroup
273273
var org models.OrgDetailsDTO
274-
name := acctest.RandString(10)
274+
name := "test:" + acctest.RandString(10)
275275

276276
resource.ParallelTest(t, resource.TestCase{
277277
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,

0 commit comments

Comments
 (0)