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
189 stars 133 forks source link

DataSource netbox_devices cannot handle custom_fields of type bool #416

Open wuetz opened 1 year ago

wuetz commented 1 year ago

Issue Reporting Guide

Terraform Version

Terraform v1.3.4
on linux_amd64
+ provider registry.terraform.io/e-breuninger/netbox v3.3.2

Affected DataSource(s)

Terraform Configuration Files

data "netbox_devices" "test" {
    name_regex = "TEST"
}

output "test-dbs" {
    value = data.netbox_devices.test
}

Output

╷
│ Error: devices.0.custom_fields.isNAD: '' expected type 'string', got unconvertible type 'bool', value: 'false'
│
│   with data.netbox_devices.test,
│   on test.tf line 1, in data "netbox_devices" "test":
│    1: data "netbox_devices" "test" {
│
╵

Debug Output

ool', value: 'false': timestamp=2023-06-12T15:35:38.508+0200
2023-06-12T15:35:38.508+0200 [ERROR] provider.terraform-provider-netbox_v3.3.2: Response contains error diagnostic: diagnostic_severity=ERROR diagnostic_summary="devices.0.custom_fields.isNAD: '' expected t
ype 'string', got unconvertible type 'bool', value: 'false'" tf_data_source_type=netbox_devices tf_req_id=f34f193e-62ee-5526-4b63-72ff2acc70cd tf_rpc=ReadDataSource @module=sdk.proto tf_proto_version=5.3 tf
_provider_addr=registry.terraform.io/e-breuninger/netbox @caller=github.com/hashicorp/terraform-plugin-go@v0.14.3/tfprotov5/internal/diag/diagnostics.go:55 diagnostic_detail= timestamp=2023-06-12T15:35:38.5
08+0200
2023-06-12T15:35:38.510+0200 [ERROR] vertex "data.netbox_devices.test" error: devices.0.custom_fields.isNAD: '' expected type 'string', got unconvertible type 'bool', value: 'false'
2023-06-12T15:35:38.510+0200 [ERROR] vertex "data.netbox_devices.test (expand)" error: devices.0.custom_fields.isNAD: '' expected type 'string', got unconvertible type 'bool', value: 'false'

Expected Behavior

Read the device data

Actual Behavior

╷
│ Error: devices.0.custom_fields.isNAD: '' expected type 'string', got unconvertible type 'bool', value: 'false'
│
│   with data.netbox_devices.test,
│   on test.tf line 1, in data "netbox_devices" "test":
│    1: data "netbox_devices" "test" {
│
╵

Steps to Reproduce

Create a device with a custom field of type boolean. Then run the above TF config which tries to read the device from netbox

Additional Information

If i do a GET request on this device on the netbox API itself, I do get a valid response, where custom fields look like:

      "custom_fields": {
        "isNAD": false
      },
tagur87 commented 1 year ago

I beleive I found a way around this issue. The type for custom_fields has been set to TypeMap this is forcing all the attributes under it to be a string type. Since custom fields can be all sorts of types, we need to keep it as TypeString and us a Json Marshal to convert it to a json type.

Then we can use something like this to access the fields.

data "netbox_virtual_machines" "test" {
  name_regex = "test"
}

output "test-dbs" {
  value = jsondecode(data.netbox_virtual_machines.test.vms[0].custom_fields).deployed
}

@fbreckle - if you agree with this approach, I can get something started.

LukoJy3D commented 2 weeks ago

+1 on this Error: devices.0.custom_fields.redfish_run: '' expected type 'string', got unconvertible type 'bool', value: 'true'