e-breuninger / terraform-provider-netbox

Terraform provider to interact with Netbox
https://registry.terraform.io/providers/e-breuninger/netbox/latest/docs
Mozilla Public License 2.0
175 stars 127 forks source link

Unable to create or update VMs with Integer custom fields #349

Open brunoofe-te opened 1 year ago

brunoofe-te commented 1 year ago

Hi,

When I try to create or update a netbox_virtual_machine with an Integer type custom field, I get this error: Error: [POST /virtualization/virtual-machines/][400] virtualization_virtual-machines_create default map[__all__:[Invalid value for custom field 'max_num_agents': Value must be an integer.]]

Terraform Version

Terraform v1.3.6
on linux_amd64
+ provider registry.terraform.io/e-breuninger/netbox v3.1.0
+ provider registry.terraform.io/hashicorp/google v4.55.0
+ provider registry.terraform.io/vultr/vultr v2.12.1

Affected Resource(s)

At least:

Terraform Configuration Files

Relevant part

resource "netbox_virtual_machine" "nb_vm" {
  name         = var.vm-name
  cluster_id   = var.cluster-id
  site_id      = var.site.id
  tenant_id    = var.tenant.id
  platform_id  = data.netbox_platform.nb_platform.id
  role_id      = data.netbox_device_role.nb_role.id
  vcpus        = var.vcpus
  memory_mb    = var.mem
  disk_size_gb = var.disk
  custom_fields = {
    "max_num_agents"                 = 1
  }
}

image

Debug Output

https://gist.github.com/brunoofe-te/849b7084499fdd7cc88af19514eb1745

Panic Output

N/A

Expected Behavior

The Virtualization > virtual machine object should be created in NetBox with the Integer type custom field correctly set.

Actual Behavior

Error: [POST /virtualization/virtual-machines/][400] virtualization_virtual-machines_create default map[all:[Invalid value for custom field 'max_num_agents': Value must be an integer.]]

Steps to Reproduce

  1. terraform apply

Important Factoids

N/A

zeddD1abl0 commented 1 year ago

@fbreckle I think this is an issue with the definition of custom_fields and its custom_schema. I don't think it needs a custom definition of a type here, I think it should be a TypeMap, as TypeMap is a Golang map[string]interface which would be exactly what is expected. I should be able to hand a set of keys and whatever values to the custom fields. The fact values are forced to be strings means that values other than strings, which is possible with custom_fields, won't be available to the end user. Defining custom_fields as a TypeMap shouldn't be excessively difficult to do.

Edit: I went digging, and the data source is set as a TypeMap already, so why is the resource a custom value? Testing with the API on v3.5.0 (sorry, I upgraded already), the following appears to be true:

There is a tiny issue with the NetBox API, in that it will allow you to specify a URL that is non-functional (URLs require http:// or https:// to be valid if you edit from the WebUI, but the api just does it)

While it won't be possible to restrict the values that people include in the custom_fields and it won't be something users can be assisted with (in completions, etc), that's really their problem. If they create a custom_field it should be up to them to get the type of value correct.

I'm unsure if we should expect NetBox to infer value based on a string->type conversion, and therefore register this as an issue with the NetBox API, or whether we should take it upon this project to redo all the custom_fields definitions to schema.TypeMap and risk breaking people's previous implementations (though, it shouldn't I believe).

mraerino commented 2 weeks ago

i've also encountered this and it looks like the terraform-plugin-sdk used in this provider does not allow managing attributes with a map that has mixed types. this means terraform will always coalesce the values in the map to strings.

One could consider different ways to approach this: