TypeList and terraform is crashing

Hello,

Currently I am using the old schema for provider development so I don’t know if this is the good place to ask my question.

I have defined a parameter for my resource:

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

Then in my add function I put this:

if hostTemplates, ok := d.GetOk("templates"); ok && len(hostTemplates.([]interface{})) > 0 { 
    for _, template := range hostTemplates.([]string) {                            
      if err := client.Hosts().AddTemplate(hostName, template); err != nil {       
        return err                                                                 
      }                                                                            
    }                                                                              
    d.SetPartial("templates")                                                      
  }

When I execute terraform, it is crashing and I don’t understand why ?

Regards,

Hi @smutel

Could you post the crash panic.log here or to a gist?

Thanks,
Matt

Hello,

Thanks.

From the crash.log:

2019-10-18T20:56:56.773+0200 [DEBUG] plugin.terraform-provider-centreon: panic: interface conversion: interface {} is []interface {}, not []string
2019-10-18T20:56:56.773+0200 [DEBUG] plugin.terraform-provider-centreon: 
2019-10-18T20:56:56.773+0200 [DEBUG] plugin.terraform-provider-centreon: goroutine 57 [running]:
2019-10-18T20:56:56.773+0200 [DEBUG] plugin.terraform-provider-centreon: github.com/smutel/terraform-provider-centreon/centreon.resourceCentreonHostCreate(0xc420130690, 0xee1da0, 0xc42047e6c0, 0x2, 0x1793cc0)
2019-10-18T20:56:56.773+0200 [DEBUG] plugin.terraform-provider-centreon: 	/home/samuel/go/src/github.com/smutel/terraform-provider-centreon/centreon/resource_centreon_host.go:274 +0xab6

So it looks like the problem is with this line of your code:

    for _, template := range hostTemplates.([]string) {

hostTemplates is not a []string type, but a []interface{} instead.

Try change the above code to:

    for _, template := range hostTemplates.([]interface{}) {
      if err := client.Hosts().AddTemplate(hostName, template.(string)); err != nil {

It works better. Thanks.

Le dim. 20 oct. 2019 à 22:42, Matt Morrison via HashiCorp Discuss hashicorp@discoursemail.com a écrit :