Skip to content

Commit e1e0462

Browse files
Alerting Rule Group: Support colon in title
Will backport this one to v2 once this is merged
1 parent 34b6381 commit e1e0462

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

internal/common/resource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (r *Resource) ImportExample() string {
106106
fieldTemplates[i] = fmt.Sprintf("{{ %s }}", fields[i].Name)
107107
}
108108
return fmt.Sprintf(`terraform import %s.name %q
109-
`, r.Name, strings.Join(fieldTemplates, defaultSeparator))
109+
`, r.Name, strings.Join(fieldTemplates, ResourceIDSeparator))
110110
}
111111

112112
id := r.IDType

internal/common/resource_id.go

+4-4
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
)
@@ -104,7 +104,7 @@ func (id *ResourceID) Make(parts ...any) string {
104104
}
105105
}
106106

107-
return strings.Join(stringParts, defaultSeparator)
107+
return strings.Join(stringParts, ResourceIDSeparator)
108108
}
109109

110110
// Single parses a resource ID into a single value
@@ -141,7 +141,7 @@ func (id *ResourceID) Split(resourceID string) ([]any, error) {
141141
// Split parses a resource ID into its parts
142142
// The parts will be cast to the expected types
143143
func split(resourceID string, expectedFields []ResourceIDField) ([]any, error) {
144-
parts := strings.Split(resourceID, defaultSeparator)
144+
parts := strings.Split(resourceID, ResourceIDSeparator)
145145
if len(parts) == len(expectedFields) {
146146
partsAsAny := make([]any, len(parts))
147147
for i, part := range parts {
@@ -165,5 +165,5 @@ func split(resourceID string, expectedFields []ResourceIDField) ([]any, error) {
165165
for i, f := range expectedFields {
166166
expectedFieldNames[i] = f.Name
167167
}
168-
return nil, fmt.Errorf("id %q does not match expected format. Should be in the format: %s", resourceID, strings.Join(expectedFieldNames, defaultSeparator))
168+
return nil, fmt.Errorf("id %q does not match expected format. Should be in the format: %s", resourceID, strings.Join(expectedFieldNames, ResourceIDSeparator))
169169
}

internal/resources/grafana/resource_alerting_rule_group.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,10 @@ func listRuleGroups(ctx context.Context, client *goapi.GrafanaHTTPAPI, data *Lis
298298
func readAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
299299
client, orgID, idWithoutOrg := OAPIClientFromExistingOrgResource(meta, data.Id())
300300

301-
split, err := resourceRuleGroupID.Split(idWithoutOrg)
302-
if err != nil {
303-
return diag.FromErr(err)
301+
folderUID, title, found := strings.Cut(idWithoutOrg, common.ResourceIDSeparator)
302+
if !found {
303+
return diag.Errorf("invalid ID %q", idWithoutOrg)
304304
}
305-
folderUID, title := split[0].(string), split[1].(string)
306305

307306
resp, err := client.Provisioning.GetAlertRuleGroup(title, folderUID)
308307
if err, shouldReturn := common.CheckReadError("rule group", data, err); shouldReturn {
@@ -413,11 +412,11 @@ func putAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta inte
413412
func deleteAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
414413
client, _, idWithoutOrg := OAPIClientFromExistingOrgResource(meta, data.Id())
415414

416-
split, err := resourceRuleGroupID.Split(idWithoutOrg)
417-
if err != nil {
418-
return diag.FromErr(err)
415+
folderUID, title, found := strings.Cut(idWithoutOrg, common.ResourceIDSeparator)
416+
if !found {
417+
return diag.Errorf("invalid ID %q", idWithoutOrg)
419418
}
420-
folderUID, title := split[0].(string), split[1].(string)
419+
421420
// TODO use DeleteAlertRuleGroup method instead (available since Grafana 11)
422421
resp, err := client.Provisioning.GetAlertRuleGroup(title, folderUID)
423422
if err != nil {

internal/resources/grafana/resource_alerting_rule_group_test.go

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

244244
var group models.AlertRuleGroup
245245
var org models.OrgDetailsDTO
246-
name := acctest.RandString(10)
246+
name := "test:" + acctest.RandString(10)
247247

248248
resource.ParallelTest(t, resource.TestCase{
249249
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,

0 commit comments

Comments
 (0)