terraform-coop / terraform-provider-foreman

Terraform provider for Foreman
https://registry.terraform.io/providers/terraform-coop/foreman
Mozilla Public License 2.0
35 stars 32 forks source link

Compatibility issue makes plugin crash: "panic: interface conversion: interface {} is nil, not string" #160

Closed HectorIrr closed 1 month ago

HectorIrr commented 4 months ago

Hello,

I would like to create both foreman hosts and proxmox VMs simultaneously through terraform. In order to do that I use both terraform-provider-foreman and foreman-provider-proxmox (FOG).

Even though they work well separately (I can make a foreman host with terraform, and a proxmox VM directly with foreman), whenever I try to "merge" those two steps, terraform-provider-foreman crashes (it does create both the VM and the host but immediately crashes afterwards, thus preventing me to create multiple host/VM at once).

Here is the error message I get:

│ Error: Plugin did not respond
│ 
│   with foreman_host.TerraformTest[0],
│   on main.tf line 53, in resource "foreman_host" "TerraformTest":
│   53: resource "foreman_host" "TerraformTest" {
│ 
│ The plugin encountered an error, and failed to respond to the plugin.(*GRPCProvider).ApplyResourceChange call. The plugin logs may contain more details.
╵

Stack trace from the terraform-provider-foreman_v0.6.2 plugin:

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

goroutine 67 [running]:
github.com/terraform-coop/terraform-provider-foreman/foreman.setResourceDataFromForemanInterfacesAttributes(0xc000332e80?, 0xc0000b0240)
    github.com/terraform-coop/terraform-provider-foreman/foreman/resource_foreman_host.go:1126 +0xb2c
github.com/terraform-coop/terraform-provider-foreman/foreman.setResourceDataFromForemanHost(0xc000000420?, 0xc0000b0240)
    github.com/terraform-coop/terraform-provider-foreman/foreman/resource_foreman_host.go:1101 +0x766
github.com/terraform-coop/terraform-provider-foreman/foreman.resourceForemanHostCreate({0xe4b8c8, 0xc000000420}, 0xc000332e80, {0xcf65c0?, 0xc000538ea0})
    github.com/terraform-coop/terraform-provider-foreman/foreman/resource_foreman_host.go:1211 +0x233
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).create(0xc0003da540, {0xe4b900, 0xc00039f860}, 0xd?, {0xcf65c0, 0xc000538ea0})
    github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.0/helper/schema/resource.go:707 +0x12e
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).Apply(0xc0003da540, {0xe4b900, 0xc00039f860}, 0xc0004e0270, 0xc000332a80, {0xcf65c0, 0xc000538ea0})
    github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.0/helper/schema/resource.go:837 +0xa85
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ApplyResourceChange(0xc000464b70, {0xe4b900?, 0xc00039f740?}, 0xc000135a40)
    github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.0/helper/schema/grpc_provider.go:1021 +0xe8d
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ApplyResourceChange(0xc0001ec5a0, {0xe4b900?, 0xc00039ee70?}, 0xc0002de4d0)
    github.com/hashicorp/terraform-plugin-go@v0.14.0/tfprotov5/tf5server/server.go:818 +0x574
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0xcc8660?, 0xc0001ec5a0}, {0xe4b900, 0xc00039ee70}, 0xc0002de380, 0x0)
    github.com/hashicorp/terraform-plugin-go@v0.14.0/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:385 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc00047e000, {0xe4f3e0, 0xc000682340}, 0xc00054ea20, 0xc0004c2b70, 0x1330260, 0x0)
    google.golang.org/grpc@v1.48.0/server.go:1295 +0xb2b
google.golang.org/grpc.(*Server).handleStream(0xc00047e000, {0xe4f3e0, 0xc000682340}, 0xc00054ea20, 0x0)
    google.golang.org/grpc@v1.48.0/server.go:1636 +0xa2f
google.golang.org/grpc.(*Server).serveStreams.func1.2()
    google.golang.org/grpc@v1.48.0/server.go:932 +0x98
created by google.golang.org/grpc.(*Server).serveStreams.func1
    google.golang.org/grpc@v1.48.0/server.go:930 +0x28a

Error: The terraform-provider-foreman_v0.6.2 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.

In order to get this error message, you simply have to install the foreman-provider-proxmox plugin on your foreman, and use try to build a host with a compute profile so that FOG recognizes that you attempt to use it.

And here is a simplied version of the code I have been using to get it:

provider "foreman" {
  server_hostname = "example.server_hostname"
  server_protocol = "https"
  client_tls_insecure = true
  client_username = "Username"
  client_password = "P4ssw0rD"
  location_id     = 2
  organization_id = 3
}

terraform {
  required_providers {
    foreman = {
      source  = "terraform-coop/foreman"
      version = "0.6.2"
    }
  }
}

data "foreman_architecture" "OS_Architecture" {
  name = "any_archi"
}

data "foreman_operatingsystem" "OS" {
  title = "any_OS"
}

data "foreman_media" "media" {
  name = "any_media"
}

data "foreman_partitiontable" "ptable" {
  name = "any_ptable"
}

data "foreman_domain" "domain" {
  name = "any_domainname"
}

data "foreman_computeresource" "ComputeResource" {
  name = "any_computeresource"
}

data "foreman_computeprofile" "ComputeProfile" {
  name = "any_computeprofile"
}

resource "foreman_host" "TerraformTest" {
  shortname      = "Shortname"
  operatingsystem_id = data.foreman_operatingsystem.OS.id
  architecture_id    = data.foreman_architecture.OS_Architecture.id
  medium_id          = data.foreman_media.media.id
  ptable_id          = data.foreman_partitiontable.ptable.id
  root_password  = "R00tP4ssw0rD"
  domain_id      = data.foreman_domain.domain.id
  compute_resource_id = data.foreman_computeresource.ComputeResource.id
  compute_profile_id  = data.foreman_computeprofile.ComputeProfile.id
}

My guess is that it is a compatibility error between your plugin and the foreman-provider-proxmox, but I haven't been able to fix it by changing/adding parameters to my terraform code (I have been especially trying to change interfaces_attributes{} and compute_attributes{}).

It seems the FOG plugin adds a new field in interface (requiring a string), while your plugin has no way to fill it. It is just my guess but it would explain why the error is "interface {} is nil, not string" (it is possible to avoid filling this field by setting "virtual = true", but the error remains).

bitkeks commented 1 month ago

While we cannot currently test the scenario with the Proxmox provider, we can definitely patch the missing type conversion error handling that causes this issue! See PR #180 for details