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

tfsdk: Move Resource ImportState method to optional ResourceWithImportState interface #297

Merged
merged 4 commits into from
Apr 25, 2022

Conversation

bflad
Copy link
Contributor

@bflad bflad commented Apr 21, 2022

Reference: #160

Due to provider developer feedback, it has been suggested to make the current Resource interface ImportState method optional. This change accomplishes that by moving the existing method signature to a new ResourceWithImportState interface. This has the add-on effect of allowing future documentation generation easily determine whether resource import support is implemented, by type checking against the new interface.

This also deprecates the ResourceImportStateNotImplemented() function. Providers can now signal that a resource does not support import by omitting the ImportState method and the framework will automatically return an error diagnostic. From a quick GitHub search, it appeared there was only one usage of a custom error message outside the framework testing. However, it was only being used to include the resource type in the message and was of no real utility over the generic messaging. A code comment is left with a suggested implementation, should there be a feature request for customized messaging.

…tState interface

Reference: #160

Due to provider developer feedback, it has been suggested to make the current `Resource` interface `ImportState` method optional. This change accomplishes that by moving the existing method signature to a new `ResourceWithImportState` interface.

This also deprecates the `ResourceImportStateNotImplemented()` function. Providers can now signal that a resource does not support import by omitting the `ImportState` method and the framework will automatically return an error diagnostic. From a quick GitHub search, it appeared there was only one usage of a custom error message outside the framework testing. However, it was only being used to include the resource type in the message and was of no real utility over the generic messaging. A code comment is left with a suggested implementation, should there be a feature request for customized messaging.
@bflad bflad added the enhancement New feature or request label Apr 21, 2022
@bflad bflad added this to the v0.7.0 milestone Apr 21, 2022
@bflad bflad requested a review from a team as a code owner April 21, 2022 20:48
Copy link
Contributor

@detro detro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change makes for a neat cleanup.

Comment on lines +89 to +107
resourceWithImportState, ok := resource.(ResourceWithImportState)

if !ok {
// If there is a feature request for customizing this messaging,
// provider developers can implement a ImportState method that
// immediately returns a custom error diagnostic.
//
// However, implementing the ImportState method could cause issues
// with automated documentation generation, which likely would check
// if the resource implements the ResourceWithImportState interface.
// Instead, a separate "ResourceWithoutImportState" interface could be
// created with a method such as:
// ImportNotImplementedMessage(context.Context) string.
resp.Diagnostics.AddError(
"Resource Import Not Implemented",
"This resource does not support import. Please contact the provider developer for additional information.",
)
return
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👨‍🍳 💋

@bflad bflad merged commit 07be7c7 into main Apr 25, 2022
@bflad bflad deleted the bflad-ResourceWithImportState branch April 25, 2022 19:27
// the ImportResourceState RPC. This RPC is called by Terraform when the
// `terraform import` command is executed. Afterwards, the ReadResource RPC
// is executed to allow providers to fully populate the resource state.
type ResourceWithImportState interface {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps nit-picking, but shouldn't this interface contain Resource, which would allow for validation that an implementation is fully implementing both using a single line? e.g.

var _ ResourceWithImportState = MyResource{}
# which also implies
# var _ Resource = MyResource{}

@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants