Terraform provider ignore order of TypeList

96 views Asked by At

I have a terraform resource with a TypeList field, which is modeled like this:

&schema.Resource{
    Schema: map[string]*schema.Schema{
        "spec": &schema.Schema{
            Description: "Specification of the resource",
            Elem: &schema.Resource{Schema: map[string]*schema.Schema{
                "custom_addons": &schema.Schema{
                    Description: "some desc",
                    Elem: &schema.Resource{Schema: map[string]*schema.Schema{
                        "depends_on": &schema.Schema{
                            Description: "some desc",
                            Elem:        &schema.Schema{Type: schema.TypeString},
                            Optional:    true,
                            Type:        schema.TypeList,
                        
                        },
                        "name": &schema.Schema{
                            Description: "some desc",
                            Optional:    true,
                            Type:        schema.TypeString,
                        }
                    }},
                    MaxItems: 0,
                    MinItems: 0,
                    Optional: true,
                    Type:     schema.TypeList,
                
                },
                -- other complex fields here --
                -- other complex fields here --
                -- other complex fields here --
            MaxItems: 1,
            MinItems: 1,
            Optional: true,
            Type:     schema.TypeList,
        },
    },
}

I want the "custom_addons" field to ignore any change in order of it's items. The API does not keep track of the order of items and may store the items internally in an order that is not same as the order specified in the terraform configuration file. In such cases, if we run terraform plan without changing anything, we see a diff that shows the order of items being changed.

I know this is the intended behaviour for TypeList and I should be using TypeSet if ordering is not important. This one field was just an example. Our terraform provider at the moment manages around ~40 resources and all of them combined we will have thousands of TypeList fields that should have actually been TypeSet. But making this change is simply not feasible for us at the moment as apart from simple DataType change, we will also need to change a lot of code to use correct type-casting (from data.([]interface{}) to data.(*schema.Set).

So I am looking for quick and generic ways to make it work with TypeList. What I have tried:
I have tried DiffSupressFunc as described here. This solution works fine if the only change to be applied is change in order as I can suppress it in that case. But in cases when there has been a change in order of items and a new item being added/edited, I cant simply suppress the diff (because a valid change has been made) and the diff shows the new valid change along with the custom_addon items order change.

So I explored further to see if I can do something with CustomizeDiff function, but looks like it only allows changing (using the setNew method) on computed fields.

How do I make this work? Is this even possible with sdkv2?

0

There are 0 answers