From f42a8b6b484c14a3eeb360077ec58f1232f87323 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Fri, 1 Mar 2024 23:07:22 -0500 Subject: [PATCH] Rename `TFID` -> `ResourceID` Reducing diff from https://github.com/grafana/terraform-provider-grafana/pull/1391 --- internal/common/{id.go => resource_id.go} | 26 +++++++++---------- .../cloud/resource_cloud_access_policy.go | 2 +- .../resource_cloud_access_policy_token.go | 2 +- .../resources/cloud/resource_cloud_api_key.go | 2 +- .../resources/cloud/resource_cloud_plugin.go | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) rename internal/common/{id.go => resource_id.go} (67%) diff --git a/internal/common/id.go b/internal/common/resource_id.go similarity index 67% rename from internal/common/id.go rename to internal/common/resource_id.go index 1e4ba7a54..c463e6431 100644 --- a/internal/common/id.go +++ b/internal/common/resource_id.go @@ -10,28 +10,28 @@ import ( var ( defaultSeparator = ":" - allIDs = []*TFID{} + allIDs = []*ResourceID{} ) -type TFID struct { +type ResourceID struct { resourceName string separators []string expectedFields []string } -func NewTFID(resourceName string, expectedFields ...string) *TFID { - return newTFIDWithSeparators(resourceName, []string{defaultSeparator}, expectedFields...) +func NewResourceID(resourceName string, expectedFields ...string) *ResourceID { + return newResourceIDWithSeparators(resourceName, []string{defaultSeparator}, expectedFields...) } -// Deprecated: Use NewTFID instead +// 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 NewTFID and remove uses of this function -func NewTFIDWithLegacySeparator(resourceName, legacySeparator string, expectedFields ...string) *TFID { - return newTFIDWithSeparators(resourceName, []string{defaultSeparator, legacySeparator}, expectedFields...) +// On major versions, switch to NewResourceID and remove uses of this function +func NewResourceIDWithLegacySeparator(resourceName, legacySeparator string, expectedFields ...string) *ResourceID { + return newResourceIDWithSeparators(resourceName, []string{defaultSeparator, legacySeparator}, expectedFields...) } -func newTFIDWithSeparators(resourceName string, separators []string, expectedFields ...string) *TFID { - tfID := &TFID{ +func newResourceIDWithSeparators(resourceName string, separators []string, expectedFields ...string) *ResourceID { + tfID := &ResourceID{ resourceName: resourceName, separators: separators, expectedFields: expectedFields, @@ -40,7 +40,7 @@ func newTFIDWithSeparators(resourceName string, separators []string, expectedFie return tfID } -func (id *TFID) Example() string { +func (id *ResourceID) Example() string { fields := make([]string, len(id.expectedFields)) for i := range fields { fields[i] = fmt.Sprintf("{{ %s }}", id.expectedFields[i]) @@ -49,7 +49,7 @@ func (id *TFID) Example() string { `, id.resourceName, strings.Join(fields, defaultSeparator)) } -func (id *TFID) Make(parts ...any) string { +func (id *ResourceID) Make(parts ...any) string { if len(parts) != len(id.expectedFields) { panic(fmt.Sprintf("expected %d fields, got %d", len(id.expectedFields), len(parts))) // This is a coding error, so panic is appropriate } @@ -60,7 +60,7 @@ func (id *TFID) Make(parts ...any) string { return strings.Join(stringParts, defaultSeparator) } -func (id *TFID) Split(resourceID string) ([]string, error) { +func (id *ResourceID) Split(resourceID string) ([]string, error) { for _, sep := range id.separators { parts := strings.Split(resourceID, sep) if len(parts) == len(id.expectedFields) { diff --git a/internal/resources/cloud/resource_cloud_access_policy.go b/internal/resources/cloud/resource_cloud_access_policy.go index 0ca470a68..3100b4da7 100644 --- a/internal/resources/cloud/resource_cloud_access_policy.go +++ b/internal/resources/cloud/resource_cloud_access_policy.go @@ -13,7 +13,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) -var ResourceAccessPolicyID = common.NewTFIDWithLegacySeparator("grafana_cloud_access_policy", "/", "region", "policyId") //nolint:staticcheck +var ResourceAccessPolicyID = common.NewResourceIDWithLegacySeparator("grafana_cloud_access_policy", "/", "region", "policyId") //nolint:staticcheck func ResourceAccessPolicy() *schema.Resource { return &schema.Resource{ diff --git a/internal/resources/cloud/resource_cloud_access_policy_token.go b/internal/resources/cloud/resource_cloud_access_policy_token.go index e68811686..b98c21ff6 100644 --- a/internal/resources/cloud/resource_cloud_access_policy_token.go +++ b/internal/resources/cloud/resource_cloud_access_policy_token.go @@ -11,7 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) -var ResourceAccessPolicyTokenID = common.NewTFIDWithLegacySeparator("grafana_cloud_access_policy_token", "/", "region", "tokenId") //nolint:staticcheck +var ResourceAccessPolicyTokenID = common.NewResourceIDWithLegacySeparator("grafana_cloud_access_policy_token", "/", "region", "tokenId") //nolint:staticcheck func ResourceAccessPolicyToken() *schema.Resource { return &schema.Resource{ diff --git a/internal/resources/cloud/resource_cloud_api_key.go b/internal/resources/cloud/resource_cloud_api_key.go index b4471ae3b..566e27eef 100644 --- a/internal/resources/cloud/resource_cloud_api_key.go +++ b/internal/resources/cloud/resource_cloud_api_key.go @@ -11,7 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) -var ResourceAPIKeyID = common.NewTFIDWithLegacySeparator("grafana_cloud_api_key", "-", "orgSlug", "apiKeyName") //nolint:staticcheck +var ResourceAPIKeyID = common.NewResourceIDWithLegacySeparator("grafana_cloud_api_key", "-", "orgSlug", "apiKeyName") //nolint:staticcheck var cloudAPIKeyRoles = []string{"Viewer", "Editor", "Admin", "MetricsPublisher", "PluginPublisher"} func ResourceAPIKey() *schema.Resource { diff --git a/internal/resources/cloud/resource_cloud_plugin.go b/internal/resources/cloud/resource_cloud_plugin.go index 07ff467db..be5b3fdf9 100644 --- a/internal/resources/cloud/resource_cloud_plugin.go +++ b/internal/resources/cloud/resource_cloud_plugin.go @@ -9,7 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -var ResourcePluginInstallationID = common.NewTFIDWithLegacySeparator("grafana_cloud_plugin_installation", "_", "stackSlug", "pluginSlug") //nolint:staticcheck +var ResourcePluginInstallationID = common.NewResourceIDWithLegacySeparator("grafana_cloud_plugin_installation", "_", "stackSlug", "pluginSlug") //nolint:staticcheck func ResourcePluginInstallation() *schema.Resource { return &schema.Resource{