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: Initial ImportResourceState support #149

Merged
merged 6 commits into from
Sep 15, 2021
Merged

tfsdk: Initial ImportResourceState support #149

merged 6 commits into from
Sep 15, 2021

Conversation

bflad
Copy link
Contributor

@bflad bflad commented Sep 9, 2021

Closes #33

@bflad bflad added the enhancement New feature or request label Sep 9, 2021
@bflad bflad added this to the v0.4.0 milestone Sep 9, 2021
@bflad bflad requested a review from a team September 9, 2021 23:48
Copy link
Contributor

@paddycarver paddycarver left a comment

Choose a reason for hiding this comment

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

Looks great to me, some small quibbles.

tfsdk/request_import.go Outdated Show resolved Hide resolved
tfsdk/request_import.go Outdated Show resolved Hide resolved
tfsdk/serve_import_test.go Show resolved Hide resolved
Comment on lines 70 to 71
func (r testServeResourceConfigValidators) ImportState(ctx context.Context, req ImportResourceStateRequest, resp *ImportResourceStateResponse) {
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do all of the other test resource types get implementations that update importResourceStateCalledResourceType, but the validate ones don't?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, this is because when I implemented those two test resources I was trying to omit parts that would be uncalled so future travelers just focused on the importantly implemented methods. It should have been commented as such.

Do you have a preference between these options?

  1. Add a comment in these implementations, e.g. // Intentionally blank. Not expected to be called during testing.
  2. Copy the implementations of other test resources (recognizing they should all stay in sync going forward; not sure how to enforce that)
  3. Removing other unexpected test resource implementations, doing something like option 1 so it is more clear

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

I think my preference is 3, but replacing them with our not supported function and a comment saying that functionality is tested elsewhere. What do you think?

I mostly just am :/ about having code we don't need in the test resources if we're not testing it, because that breeds confusion about whether it's used and where and what can be safely changed and what can't be in the future, I think. 🤔

Copy link
Contributor Author

@bflad bflad Sep 14, 2021

Choose a reason for hiding this comment

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

Agreed! That will work for the new ImportState method implementations and I'll add the comment to the other Create/Read/Update/Delete test implementations that should be empty. Will update shortly after merging main to ensure this is ready to rock. 🎸

@bflad
Copy link
Contributor Author

bflad commented Sep 14, 2021

Everything should be ready for review again.

Something that gives me pause at the moment is how tooling will determine if a resource actually supports import and potentially how it can signal an example import identifier for documentation generation. I'm not sure if that should necessarily block an initial implementation, but it might be something worth iterating on in the future. Would love to hear thoughts and/or if I should create a followup issue.

@bflad
Copy link
Contributor Author

bflad commented Sep 14, 2021

Created #160 for potential followup.

Copy link
Contributor

@paddycarver paddycarver left a comment

Choose a reason for hiding this comment

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

Yeah, let's go ahead and do it. 👍 🚀 Nice work.

Copy link
Member

@kmoe kmoe left a comment

Choose a reason for hiding this comment

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

LGTM

return
}

emptyState := tftypes.NewValue(resourceSchema.TerraformType(ctx), nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

A thing worth thinking about here:

We need a resolution to #148 to make this work the way we want.

Another option is to have it do something like:

vals := map[string]tftypes.Value{}
typ := resourceSchema.TerraformType(ctx)
for name, t := range typ.(tftypes.Object).AttributeTypes {
  vals[name] = tftypes.NewValue(t, nil)
}
emptyState := tftypes.NewValue(typ, vals)

This matches the more-expected (imho) default state, which is an object in state with nothing filled in, instead of nothing in state.

However, this is weird during failure cases. I'm not clear on whether Terraform will persist the object to state or write nothing to state in the face of an error diagnostic. If the former, we should default to an empty state and just rely on #148. If the latter, we should confirm that will continue to be the case, and maybe defaulting to the empty object (instead of the null object) is more expected? I dunno. I don't know that it makes a huge difference, just wanted to call it out here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The current logic in Terraform CLI is to return early on ImportResourceState error diagnostics, before saving state information:

https://github.com/hashicorp/terraform/blob/2afa0a5e75d76de7e47157114c579b3b1bff994f/internal/terraform/transform_import_state.go#L141-L154

Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 how do we feel about just leaving it and waiting for feedback?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're going to merge this in as-is for now and iterate if necessary.

If we do wind up switching the implementation here in the future -- maybe creating the above code snippet as a method such as (Schema).newState() would be good to try and prevent "empty" object problems here and in any other potential places that might need it. That might be something we could export if/when multiple resource import support makes sense.

@bflad bflad merged commit 8de1d13 into main Sep 15, 2021
@bflad bflad deleted the bflad-f-import branch September 15, 2021 15:17
@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 Oct 16, 2021
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.

Support Managed Resource Import
3 participants