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

Persist existing ResourceDiff from plan during apply #760

Open
skeggse opened this issue May 7, 2021 · 1 comment
Open

Persist existing ResourceDiff from plan during apply #760

skeggse opened this issue May 7, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@skeggse
Copy link

skeggse commented May 7, 2021

SDK version

github.com/hashicorp/terraform-plugin-sdk/v2 v2.6.1

Use-cases

I'd like to make a provider that exposes a variant of the random_shuffle resource, except that it attempts to provide stability when the input field changes. I ran with hashicorp/terraform-provider-random#121, and mostly got an approach that uses CustomizeDiff working. However, I'm finding that I have no way to use the value created during the plan phase - this results in Terraform complaining about an inconsistent final plan:

Error: Provider produced inconsistent final plan

When expanding the plan for stable_subset.res to include new values learned so
far during apply, provider "registry.terraform.io/hashicorp/stable" produced
an invalid new value for .result: planned set element cty.StringVal("b") does
not correlate with any element in actual.

This is a bug in the provider, which should be reported in the provider's own
issue tracker.


Error: Provider produced inconsistent final plan

When expanding the plan for stable_subset.res to include new values learned so
far during apply, provider "registry.terraform.io/hashicorp/stable" produced
an invalid new value for .result: planned set element cty.StringVal("d") does
not correlate with any element in actual.

This is a bug in the provider, which should be reported in the provider's own
issue tracker.

Attempted Solutions

I've looked through all the interfaces I can find in the various helper packages, and got as far as discovering the PlannedPrivate field in the provider messaging implementation before discovering that I couldn't seem use that mechanism from a provider implementation either.

I suspect this is getting complicated by the CreateContext not getting called when the CustomizeDiff hook invokes SetNew; I wouldn't expect this to be the case, but perhaps if I was able to do both it would be possible to work around the problem?

Work-in-progress implementation is here for anyone interested.

Proposal

Prepopulate (opt-in, perhaps?) the schema.ResourceDiff value passed to CustomizeDiff and the Create/Read/Update/Delete family of functions in a schema.Resource based on a previously-generated plan - atop addition to anything already in the state file.

Example:

  1. terraform plan -out tfplan generates a tfplan file that includes the resource-level changes Terraform anticipates
  2. terraform apply tfplan takes the existing tfplan file and passes its content to the provider
  3. the provider exposes the existing diff information in resource-level ResourceDiff objects, so that the provider can avoid duplicate work and maintain stability for random operations.

References

@skeggse skeggse added the enhancement New feature or request label May 7, 2021
@skeggse
Copy link
Author

skeggse commented Jul 8, 2022

Oh, I suppose this isn't strictly necessary for my intended use-case, though it is still really nice to have: we can deterministically derive the result based on a seeded random, and this should produce the same value in plan as during apply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant