I am using the Provider Framework and have a Computed attribute which changes based on another attribute. I will call the attribute in the config A and the dependent attribute B.
The value of B is calculated when the values in A are processed. This works fine for Create, but Update gives the “produced an unexpected new value” error if a value is added to or removed from A.
To me, it seems logical, that a Computed value can change and that’s ok. I saw another post about a custom plan modifier, but the values in B must be calculated from the values in A and doing this changes the values permanently in the backing API … in this case, the value of B is not known until the apply processing completes.
Here is the schema snippet
attrs[“A”] = schema.SetAttribute{
Description: “A”,
ElementType: types.Int64Type,
Optional: true,
}
What is the purpose of this? It’s storing another ID which gets created after processing A and makes it easier during Updates because all of the data is in state instead of figuring it out each time.
How can I fix this so that updated the computed value B doesn’t cause an error?
As @hQnVyLRx mentions, UseStateForUnknown is telling the framework to plan on always preserving the prior state value when it sounds like what really should be happening is that it is conditional when the prior state is preserved. To implement this behavior today in the framework would require some custom plan modification logic to encode the expectations.
PlanModifiers: []planmodifier.Map{
mapplanmodifier.UseStateForUnknown(), // always preserve prior state
mapplanmodifier.UnknownIfChanged(path.Root("a")), // except this case :)
},
If that sounds like something you might be interested in, please feel free to and/or comment about your use case there (even if its just a link to this topic) to help the maintainers prioritize that enhancement.