fix(diff): switch from SetNestedAttribute
to MapNestedAttribute
to resolve diff issues
#7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The original design for 'domains' used a nested attribute of a Set type (similar to the original Terraform provider) and this causes Terraform to generate inaccurate diffs.
An example of this can be seen in the below screenshot. I had two domains already in my state, the first domain was updated to have a comment, and the second domain had its name changed slightly. The generated diff correctly shows the second domain as needing to be deleted and a new domain with
-updated
in its name needing to be created BUT the first domain is incorrectly showing as needing to be deleted and recreated (which isn't what is actually happening as internally we make a single 'update' API call to add the comment to the existing domain resource):Solution
I'm switching to a nested map attribute type, and although more verbose (for the user) in Terraform config syntax, the change actually helps simplify a bunch of domain logic (such as not having to manually identify a 'key' to be calculated into a hash, which in the case of the domain resource was its name field, and now that 'key' is automatically handled simply by the nature of the design of a map type).
Here is my initial plan:
Here is the plan after making modifications:
Notice how the generated diff is more accurate. It can recognise that a single field (comment in this case) is being modified 'in-place' and so we're not showing the first domain as needing to be deleted and recreated but simply needing to be modified.
Extra
This change also makes abstracting the algorithm used for calculating changes easier. Meaning, we should be able to make a function that can abstract this logic so it can be reused across different resource types (e.g. backends).
Interface Changes
We're moving from config that looks like this (
SetNestedAttribute
):To config that looks like this (
MapNestedAttribute
):