Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename TFID -> ResourceID #1394

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions internal/common/id.go → internal/common/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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])
Expand All @@ -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
}
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/cloud/resource_cloud_access_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/cloud/resource_cloud_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/cloud/resource_cloud_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading