Skip to content

Commit

Permalink
Alerting Rule Group: Support colon in title (#1625)
Browse files Browse the repository at this point in the history
Will backport this one to v2 once this is merged
  • Loading branch information
julienduchesne authored Jun 14, 2024
1 parent 1a64984 commit d2c8144
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion internal/common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (r *Resource) ImportExample() string {
fieldTemplates[i] = fmt.Sprintf("{{ %s }}", fields[i].Name)
}
return fmt.Sprintf(`terraform import %s.name %q
`, r.Name, strings.Join(fieldTemplates, defaultSeparator))
`, r.Name, strings.Join(fieldTemplates, ResourceIDSeparator))
}

id := r.IDType
Expand Down
8 changes: 4 additions & 4 deletions internal/common/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type ResourceIDFieldType string

const (
defaultSeparator = ":"
ResourceIDSeparator = ":"
ResourceIDFieldTypeInt = ResourceIDFieldType("int")
ResourceIDFieldTypeString = ResourceIDFieldType("string")
)
Expand Down Expand Up @@ -104,7 +104,7 @@ func (id *ResourceID) Make(parts ...any) string {
}
}

return strings.Join(stringParts, defaultSeparator)
return strings.Join(stringParts, ResourceIDSeparator)
}

// Single parses a resource ID into a single value
Expand Down Expand Up @@ -141,7 +141,7 @@ func (id *ResourceID) Split(resourceID string) ([]any, error) {
// Split parses a resource ID into its parts
// The parts will be cast to the expected types
func split(resourceID string, expectedFields []ResourceIDField) ([]any, error) {
parts := strings.Split(resourceID, defaultSeparator)
parts := strings.Split(resourceID, ResourceIDSeparator)
if len(parts) == len(expectedFields) {
partsAsAny := make([]any, len(parts))
for i, part := range parts {
Expand All @@ -165,5 +165,5 @@ func split(resourceID string, expectedFields []ResourceIDField) ([]any, error) {
for i, f := range expectedFields {
expectedFieldNames[i] = f.Name
}
return nil, fmt.Errorf("id %q does not match expected format. Should be in the format: %s", resourceID, strings.Join(expectedFieldNames, defaultSeparator))
return nil, fmt.Errorf("id %q does not match expected format. Should be in the format: %s", resourceID, strings.Join(expectedFieldNames, ResourceIDSeparator))
}
15 changes: 7 additions & 8 deletions internal/resources/grafana/resource_alerting_rule_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,10 @@ func listRuleGroups(ctx context.Context, client *goapi.GrafanaHTTPAPI, data *Lis
func readAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, orgID, idWithoutOrg := OAPIClientFromExistingOrgResource(meta, data.Id())

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

resp, err := client.Provisioning.GetAlertRuleGroup(title, folderUID)
if err, shouldReturn := common.CheckReadError("rule group", data, err); shouldReturn {
Expand Down Expand Up @@ -427,11 +426,11 @@ func putAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta inte
func deleteAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, _, idWithoutOrg := OAPIClientFromExistingOrgResource(meta, data.Id())

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

// TODO use DeleteAlertRuleGroup method instead (available since Grafana 11)
resp, err := client.Provisioning.GetAlertRuleGroup(title, folderUID)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func TestAccAlertRule_inOrg(t *testing.T) {

var group models.AlertRuleGroup
var org models.OrgDetailsDTO
name := acctest.RandString(10)
name := "test:" + acctest.RandString(10)

resource.ParallelTest(t, resource.TestCase{
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
Expand Down

0 comments on commit d2c8144

Please sign in to comment.