terraform-provider-azapi icon indicating copy to clipboard operation
terraform-provider-azapi copied to clipboard

Ignore the order of list properties with unpredictable ordering

Open cwe1ss opened this issue 1 year ago • 0 comments
trafficstars

The "subnets"-property of Microsoft.Network/virtualNetworks is a list of objects with unreliable ordering behavior. Whenever someone modifies a subnet outside of Terraform (e.g via the portal), the order of elements changes. This inconsistent ordering results in permanent Terraform drift, because Terraform now shows plan changes (it wants to reorder the subnets), but the actual apply does not do anything.

Another case where the order of a list is not important is the "serviceEndpoints"-property within a subnet. Azure seems to add new service endpoints to the end of the list, so if I have a dynamic way to add service endpoints via Terraform, this can easily result in drift.

Ideally this would be solved by the upstream resource provider (e.g. by always returning ordered elements), but I'm wondering if this could/should also be solved in azapi in a generic way?

There will definitely be many cases where the order of lists is important so it would not be good to always ignore the order, but maybe you could apply this to relevant properties based on API specs, a convention, or explicit configuration?

For an explicit configuration, there could maybe be a property similar to "ignore_body_changes", called e.g. "ignore_body_order", so we could do something like this:

resource "azapi_resource" "vnet" {
  type = "Microsoft.Network/virtualNetworks@2023-04-01"
  name = "vnet-foo"
  body = jsonencode({
    properties = {
      subnets = local.subnets
    }
  })

  ignore_body_order = [
    "properties.subnets",
    "properties.subnets.serviceEndpoints",  
  ]
}

cwe1ss avatar Apr 03 '24 11:04 cwe1ss