Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default TypeList / TypeSet #142

Open
iwarapter opened this issue Mar 15, 2019 · 5 comments
Open

Default TypeList / TypeSet #142

iwarapter opened this issue Mar 15, 2019 · 5 comments
Labels
enhancement New feature or request subsystem/types Issues and feature requests related to the type system of Terraform and our shims around it. terraform-plugin-framework Resolved in terraform-plugin-framework

Comments

@iwarapter
Copy link

iwarapter commented Mar 15, 2019

Terraform provides a test example for using a default for TypeList:
https://github.com/hashicorp/terraform/blob/master/helper/schema/schema_test.go#L3624

However if I define one in a provider I get:
foo: Default is not valid for lists or sets

I have also tried using a DefaultFunc:

"foo": &schema.Schema{
      Type:     schema.TypeSet,
      Optional: true,
      DefaultFunc: func() (interface{}, error) {
        return []interface{}{"bar", "thing"}, nil
      },
      Elem: &schema.Schema{
        Type: schema.TypeString,
      },
    },

Which compiles but doesn't provide a default, how would you go about achieving this?

Thanks!

@hashibot hashibot transferred this issue from hashicorp/terraform Sep 26, 2019
@radeksimko radeksimko added the enhancement New feature or request label Oct 31, 2019
@chkp-idoma
Copy link

Hi,
I ran into this problem also.

Do someone have some workaround to this problem?

Thanks.

@Tensho
Copy link

Tensho commented Mar 13, 2020

Hi, I'm still confused about Default option behavior for Set type. I'd expect if I specify Default option in schema it means the value should be returned by Get()/GetOk() call no matter is it specified in configuration or not.

resource example_box robin {
  bundle = "b-x"
  // "size" attribute of Set type is not specified here
}
func resourceExampleBox() *schema.Resource {
	return &schema.Resource{
		Create: resourceExampleBoxCreate,
...
	"size": {
		Type:     schema.TypeSet,
		Optional: true,
		Elem: &schema.Resource{
			Schema: map[string]*schema.Schema{
				"width": {
					Type:     schema.TypeInt,
					Optional: true,
					Default:  100,
				},
				"height": {
					Type:     schema.TypeInt,
					Optional: true,
					Default:  50,
				},
			},
		},
	},
...
func resourceExampleBoxCreate(d *schema.ResourceData, m interface{}) error {
...
    size := d.Get("size").(*schema.Set).List() // len: 0 here !!! why??? is it a bug or feature?

Here is my discussion with @slapula regarding the subject, but it didn't help me to understand is it a bug or feature. I've also created an example provider to play around and check the unexpected behavior of the defaults.

@EliiseS
Copy link

EliiseS commented Jun 17, 2020

I'd also love some clarity on this issue

@stikkireddy
Copy link

stikkireddy commented Jun 19, 2020

From my understanding the way that defaults applied to "blocks" such as lists and sets require the block to be declared for it to work.

As you can see in the below example the fields have default env variables and the field is a type list with a max item of one. For the default values to be propagated to the values in the Set/List field it requires you to declare that block.

provider databricks {
host =....
basic_auth {}
}

Without declaring the empty block terraform does not seem to populate default values.

AFAIK you cannot set defaults on the set/list object itself but only the values it contains.

What I cannot confirm is if this is the intended behavior of the sdk/terraform.

Concrete example below:

Provider field definition:
https://github.com/databrickslabs/terraform-provider-databricks/blob/c343db6a0c03b98d0f038b8a6c628d3ce2962db9/databricks/provider.go#L69-L89

Usage:
https://github.com/databrickslabs/terraform-provider-databricks/blob/c343db6a0c03b98d0f038b8a6c628d3ce2962db9/databricks/resource_databricks_mws_credentials_mws_test.go#L126-L138

@G-Rath
Copy link

G-Rath commented Oct 5, 2021

This also seems to be contrary to what the current documentation for Default says:

Value: any value of an elements Type for primitive types, or the type defined by Elem for complex types.
Restrictions:

  • Cannot be used if Required is true
  • Cannot be used with DefaultFunc

If Default is specified, that value that is used when this item is not set in the configuration.

Currently based on this the documentation is just plain wrong 😕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request subsystem/types Issues and feature requests related to the type system of Terraform and our shims around it. terraform-plugin-framework Resolved in terraform-plugin-framework
Projects
None yet
Development

No branches or pull requests

9 participants