terraform-provider-netbox icon indicating copy to clipboard operation
terraform-provider-netbox copied to clipboard

Unable to create or update VMs with Integer custom fields

Open brunoofe-te opened this issue 1 year ago • 2 comments

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:

  • netbox_virtual_machine It may affect all resources with custom fields

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

brunoofe-te avatar Mar 02 '23 11:03 brunoofe-te

@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:

  • If a field is set to Boolean, a boolean value must be supplied
  • If a field is set to Integer, an integer value must be supplied
  • If a field is set to Decimal, a float value will be inferred from a string, as JSON does not allow random . characters in fields that aren't quoted strings
  • If a field is set to JSON, a JSON object is expected. If you pass it a string, the JSON object becomes just a string. This is probably not expected.
  • If a field is set to Mutliple Object or Multiple Selection, a set of strings must be supplied
  • If a field is set to basically anything else, a simple string is fine. It won't have any problems being parsed properly, including Date, Date&Time, and URL.

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).

zeddD1abl0 avatar May 01 '23 11:05 zeddD1abl0