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

Type defined in schema.Schema not being honored/accepted after Getting from state #211

Closed
asciifaceman opened this issue Oct 23, 2019 · 4 comments

Comments

@asciifaceman
Copy link

asciifaceman commented Oct 23, 2019

I am experiencing an issue in terraform core while developing a provider. I am defining several portions of schema.Schema with their types, but receiving unexpected outputs.

Terraform Version

Terraform v0.12.8-dev

Terraform Configuration Files

Schema Code
		Type:        schema.TypeMap,
		Optional:    true,
		Description: "Concerning GZIP, level of compression, and mimetypes",
		Elem: &schema.Resource{
			Schema: map[string]*schema.Schema{
				"enabled": {
					Type:     schema.TypeBool,
					Default:  false,
					Optional: true,
				},
				"level": {
					Type:        schema.TypeInt,
					Optional:    true,
					Default:     1,
					Description: "The level of compression used for gzip",
				},
			},
		},
Implementation Code
mapContainingCompression := d.Get("compression").(map[string]interface{})
test := mapContainingCompression["enabled"].(bool)
test2 := mapContainingCompression["level"].(int)
HCL
compression = {
    enabled = true
    level = 2
}

Debug Output

Custom debug log line log.Println(spew.Sprint(stateMap))

		2019-10-23T14:02:32.108-0700 [DEBUG]
		plugin.terraform-provider-striketracker:
		2019/10/23 14:02:32
		map[
			enabled:true
			level:2
		]

Crash Output

panic: interface conversion: interface {} is string, not bool

&

panic: interface conversion: interface {} is string, not int

Expected Behavior

The bool and int should have casted as expected (and as is done properly by other similar fields in the provider's schema).

Actual Behavior

Bool and Int fields were strings

Additional Context

This same process is working fine with other schema fields, however. Dumping the data via a log shows they are what I would expect

	map[enabled:true level:2]

( https://play.golang.org/p/37UeYsyGr-W )

Additionally, I've found nulls where defaults are set in other schemas.

@hashibot hashibot transferred this issue from hashicorp/terraform Oct 24, 2019
@asciifaceman
Copy link
Author

I should clarify that this schema is a subschema of a TypeList

...
		Type:        schema.TypeList,
		MaxItems:    1,
		Elem: &schema.Resource{
			Schema: map[string]*schema.Schema{
				"compression": compressionSchema,
                         },
                 },
...

@asciifaceman
Copy link
Author

		log.Println(spew.Sprint(stateMap["enabled"]))
		c.Enabled = stateMap["enabled"].(bool)

=>

2019-10-30T17:26:07.177-0700 [DEBUG] plugin.terraform-provider-striketracker: 2019/10/30 17:26:07 true
2019-10-30T17:26:07.180-0700 [DEBUG] plugin.terraform-provider-striketracker: panic: interface conversion: interface {} is string, not bool

This has no problems in other schemas

@paultyng
Copy link
Contributor

TypeMap does not support non-primitive types (ie. resources) yet. Terraform core supports this but the SDK has not surfaced this ability due to some backwards compatibility constraints. Going to merge this to #62

@ghost
Copy link

ghost commented Mar 14, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Mar 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants