terraform-plugin-sdk icon indicating copy to clipboard operation
terraform-plugin-sdk copied to clipboard

StateFunc cannot be used within a TypeSet in 0.12

Open jbardin opened this issue 5 years ago • 4 comments

If a provider is using Schema.StateFunc on an attribute of a set value, the resource will see extra entries in the set obtained from d.Get during Apply.

This is because the SDK is getting different values for the set between PlanResourceChange and ApplyResourceChange, and generating different hash values for the flatmap representation. This prevents the new protocol shims from being able to correlate the planned set values with the original config values, and they appear as separate set elements.

The recommended option here is to convert the TypeSet to a TypeList in the schema, which should behave the same in most respects, other than the inherent deduplication of identical config blocks which isn't usually a concern.

Migrating the state from the hashed value to the config value is a harder prospect, and will probably require manual intervention by the user, i.e. applying once without changing the config to save the new values.

This situation is not likely to have a remedy when using the legacy SDK with 0.12 or later, but I'm leaving the issue open here for provider developers that may encounter it during the upgrade process.

jbardin avatar Jun 06 '19 21:06 jbardin

Run into this issue today, I made a repo to reproduce https://github.com/jerome-quere/terraform-provider-test

jerome-quere avatar Jun 28 '19 17:06 jerome-quere

I hit this issue today. StateFunc is being used to json-normalize one of the fields before being stored in the set as a string value. If the normalization returns a string that differs by even one character then I get a defunct entry in the schema set with the original json in it and seemingly no other fields. If the normalization returns the exact same string then the bug does not occur.

In my case one of the other entries in the map that comprises the set entry is a required string so I can just check that it's not "" and ignore the set entry if it is. The state file stored on disk seems to have the correct entries so I don't get unexpected plan vs. infra changes.

andysworkshop avatar Aug 20 '19 09:08 andysworkshop

Is this gonna be handled by hashicorp/terraform-plugin-framework#11?

magodo avatar May 14 '21 08:05 magodo

It seems like StateFunc is ignored for TypeList as well.

Hiieu avatar Apr 29 '22 10:04 Hiieu