Handling of nil and empty schema.TypeList

Hello,

I’m writing a Terraform provider and wondering about how the default and empty representations of Lists should be handled.

I have the following type in my resource schema:

"items": &schema.Schema{
    Type:     schema.TypeList,
    Optional: true,
    Elem:     &schema.Schema{Type: schema.TypeString},
},

In my Read function I set items in *schema.ResourceData to nil, when items is empty in the response that I retrieved via the API.
When I run terraform apply I create the resource without specifying the items field.
This causes that when I run terraform plan after creating the resource, Terraform reports that items changed to an empy list ([]).

I also get the warning:

- .items: was null, but now cty.ListValEmpty(cty.String)

I get the same warning when I set items to an empty slice ([]string{}) instead of nil.
Terraform seems to recognize both values as the same empty list value. The default value for an optional TypeList field seems to be recognized as a different value though.

How do I prevent that a difference is reported for the default unset value of a TypeList field and it’s Go empty representation (nil or []string{})?

  • Can I initialize TypeList fields with a cty.ListValEmpty(cty.String) value somehow? (did not work via a DefaultFunc that returns an []string{})
  • Can I set a TypeList field to an empty value that is equal to the default unset value somehow?
  • Should I only set TypeList values when either their current value or the new value is not empty?

I’m using github.com/hashicorp/terraform-plugin-sdk/v2 v2.8.0.

2 Likes

I found the following related posts: