Skip to content

Commit

Permalink
(V2 Backport) Alerting Rule Group: Support colon in title (#1643)
Browse files Browse the repository at this point in the history
Backport of #1625
  • Loading branch information
julienduchesne authored Jun 14, 2024
1 parent fd79c5e commit c4449a2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion internal/common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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
10 changes: 5 additions & 5 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 @@ -71,14 +71,14 @@ func (id *ResourceID) RequiredFields() []ResourceIDField {
}

func NewResourceID(expectedFields ...ResourceIDField) *ResourceID {
return newResourceIDWithSeparators([]string{defaultSeparator}, expectedFields...)
return newResourceIDWithSeparators([]string{ResourceIDSeparator}, expectedFields...)
}

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

func newResourceIDWithSeparators(separators []string, expectedFields ...ResourceIDField) *ResourceID {
Expand Down Expand Up @@ -118,7 +118,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 @@ -181,5 +181,5 @@ func split(resourceID string, expectedFields []ResourceIDField, separators []str
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))
}
23 changes: 15 additions & 8 deletions internal/resources/grafana/resource_alerting_rule_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,14 @@ 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 {
split, err := resourceRuleGroupID.Split(idWithoutOrg)
if err != nil {
return diag.FromErr(err)
}
folderUID, title = split[0].(string), split[1].(string)
}
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 @@ -385,11 +388,15 @@ 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 {
split, err := resourceRuleGroupID.Split(idWithoutOrg)
if err != nil {
return diag.FromErr(err)
}
folderUID, title = split[0].(string), split[1].(string)
}
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 @@ -271,7 +271,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 c4449a2

Please sign in to comment.