-
Notifications
You must be signed in to change notification settings - Fork 16
helper/resource: plannable import re-work to support resources with dependencies #476
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
Conversation
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.
notes to self
helper/resource/importstate/import_block_for_resource_with_a_dependency_test.go
Show resolved
Hide resolved
b02facc
to
f97b206
Compare
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.
Looks great! I left a couple nits/thoughts 🚀
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 is less of a nit and more of a discussion our team should probably have at some point.
I'm not really sure how to do the changelogs for alpha/beta releases. I've currently been doing just one or two NOTE
changelogs mentioning the general features and not specific deltas between releases, but I can see why you'd want to be more explicit in-case anyone was trying the features out 🤔
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.
Also, I failed to call out the import test changes in the first changelog for alpha, so maybe a general note would be useful.
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 is less of a nit and more of a discussion our team should probably have at some point.
💯
I can see why you'd want to be more explicit in-case anyone was trying the features out 🤔
Right, I'm thinking to bias toward clarity here. Pre-releases will have partial implementations, so a little expectation-setting can go a long way.
helper/resource/importstate/import_block_for_resource_with_a_dependency_test.go
Show resolved
Hide resolved
if !step.ImportStatePersist { | ||
if kind.plannable() { | ||
if stepNumber > 1 { | ||
err = importWd.CopyState(ctx, wd.StateFilePath()) |
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 shouldn't block the PR merging, just some thinking out loud
I'm trying to figure out if this is all we need to do 🤔. I think in most cases this should suffice, but wondering if we have other external providers included in the first step... are we going to need to copy more (lockfile, downloaded providers)? Maybe the entire directory?
Would the follow-up init handle this seamlessly anyways? I think the state rm
command should be safe since it doesn't need to initialize anything.
Maybe an eventual new test case could prove this theory out using a utility provider.
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.
+1 for a test case with external providers. I can add this in a follow-up PR.
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.
Would the follow-up init handle this seamlessly anyways?
I ran a quick test of this. Observations:
- Contents of a work directory
$ find step_1/work1565457440 -type f
step_1/work1565457440/testdata/1/examplecloud_container.tf
step_1/work1565457440/testdata/2/examplecloud_container_import.tf
step_1/work1565457440/terraform.tfstate
step_1/work1565457440/tfplan
step_1/work1565457440/.terraform/providers/registry.terraform.io/hashicorp/random/3.7.1/darwin_arm64/terraform-provider-random_v3.7.1_x5
step_1/work1565457440/.terraform/providers/registry.terraform.io/hashicorp/random/3.7.1/darwin_arm64/LICENSE.txt
step_1/work1565457440/terraform_plugin_test.tf
step_1/work1565457440/.terraform.lock.hcl
- Yes, the follow-up init handles this seamlessly
- The follow-up external provider download is an optimization opportunity -- why download twice? 😃
- What if
ImportState
used Terraform workspaces?
var resourceChangeUnderTest *tfjson.ResourceChange | ||
|
||
if len(plan.ResourceChanges) == 0 { | ||
return fmt.Errorf("importing resource %s: expected a resource change, got no changes", resourceName) | ||
} |
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.
It just now struck me reading this code that a resource change will appear even if it's a no-op 😆
nit - Maybe this error messaging could reflect what we actually mean, i.e, the resource was not found in the plan at all (change != planned to be updated)
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.
👍 to a more specific error message. Technically, this is simply checking safety of traversing plan.ResourceChanges
, so it's the means to checking what we want. So we can make the test feedback more about the "end" than the "means."
This change re-works plannable import testing to support a scenario uncovered in alpha releases: the resource-to-be-imported requires replacement ... because it depends on a resource that does not exist in state before the plan is generated.
The new approach: a plannable import
TestStep
constructs this state:[copy of state from previous config mode TestStep] minus [the "resource under test," indicated by TestStep.ResourceName]
.terraform-plugin-testing/helper/resource/testing_new_import_state.go
Lines 155 to 169 in 88a9bc9
The plan is generated against this prior state; therefore, only the the "resource under test" should have planned changes.
terraform-plugin-testing/helper/resource/testing_new_import_state.go
Lines 205 to 229 in 88a9bc9
This PR also makes a couple changes to turn off anything that expects plannable import to produce a new state. A "plan-only" plannable import has sufficed so far.
ImportStatePersist
with plannable importterraform-plugin-testing/helper/resource/testing_new_import_state.go
Lines 430 to 431 in 88a9bc9
ImportStateVerify
with plannable importImportStateVerify
was error-free for plannable import tests, despite the plan-only mechanics of these tests (no apply, no new state). I found that these tests trivially satisfiedImportStateVerify
-- a loop over an emptynewResources
slice does nothing 😃terraform-plugin-testing/helper/resource/testing_new_import_state.go
Lines 433 to 434 in 88a9bc9
cc: @austinvalle who paired with me on 💭 and design
cc: @gdavison who raised the zone-domain example