-
Notifications
You must be signed in to change notification settings - Fork 93
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
Conversation
…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.
There was a problem hiding this 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.
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👨🍳 💋
// 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 { |
There was a problem hiding this comment.
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{}
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. |
Reference: #160
Due to provider developer feedback, it has been suggested to make the current
Resource
interfaceImportState
method optional. This change accomplishes that by moving the existing method signature to a newResourceWithImportState
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 theImportState
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.