Hi all,
While developing a Terraform Provider for our API, we stumbled upon the problem that our API has several attributes where the order is not defined. The API can return items in an order that differs from what is in the configuration. Our initial implementation used ListNestedAttribute, which obviously does not work when order changes. We then decided to switch to SetNestedAttribute, but we did not realize that this actually makes the problem worse. The list could result in mismatches due to changes in order. The set however gets confused all over the place, because our elements also contain Computed attributes, which can change at every request. This causes the set to completely loose track of its elements, resulting in plans which essentially try to recreate the contents of every set every time.
I’ve done some searching on this forum and found several other users with the same issue, but none with a solution:
SetNestedAttribute with nested computed attributes
Explicit hash function for SetNestedAttribute
Framework v1.1.1 - Strategies for diagnosing resource state churn
Somewhere, I found a suggestion to use a map. This might work for nested elements that define a single unique key that a user knows upfront and that needs to be put in the config anyway, but many of our nested elements only define an implicit identifier that is generated when the element is created. This rules out a map for our case.
With list, set and map all eliminated, I’m out of options. So my question is: how do you deal with unordered lists? At Custom sort for ListNestedAttributes the suggestion was made to sort the elements. I can see how I might sort the elements in Read, Create and Update methods to match the state, but I don’t understand the part about the PlanModifier. Why is that needed? And how would you only discard changes that result from differences in ordering, without discarding any other potential changes? Maybe somebody already implemented this approach somewhere and could point to an example?
Btw, the provider I’m working on is here: GitHub - topicuskeyhub/terraform-provider-keyhub . However, the code is difficult to read, because it is entirely generated from an OpenAPI specification and testing it requires access to a working Topicus KeyHub deployment. The generated documentation does however describe the structure of some of these troublesome attributes, such as the account and client_permissions attributes for the keyhub_group resource: Terraform Registry
Best regards,
Emond