From fa4e1579c5282af355823bec327c155c108abc0e Mon Sep 17 00:00:00 2001 From: tagur87 <43474056+tagur87@users.noreply.github.com> Date: Tue, 1 Aug 2023 12:19:31 -0400 Subject: [PATCH] feat: allow any custom field type in netbox_devices data source Update the data source `netbox_devices` to use the string based schema for the `custom_fields` attribute. This allows for any underlying type to be accessed by using the `jsondecode()` terraform function. For example, to access custom fields, the following code can be used. ```terraform data "netbox_devices" "test" { filter { name = "name" value = "device1" } } output "device_field" { value = jsondecode(data.netbox_devices.test[0].custom_fields).field1 } ``` --- docs/data-sources/devices.md | 1 + netbox/data_source_netbox_devices.go | 10 +++++++--- netbox/data_source_netbox_devices_test.go | 18 +++++++++++++----- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/docs/data-sources/devices.md b/docs/data-sources/devices.md index b3a05cd6..802ac229 100644 --- a/docs/data-sources/devices.md +++ b/docs/data-sources/devices.md @@ -45,6 +45,7 @@ Read-Only: - `comments` (String) - `custom_fields` (Map of String) - `description` (String) +- `custom_fields` (String) - `device_id` (Number) - `device_type_id` (Number) - `location_id` (Number) diff --git a/netbox/data_source_netbox_devices.go b/netbox/data_source_netbox_devices.go index fabcae8a..84fc1f63 100644 --- a/netbox/data_source_netbox_devices.go +++ b/netbox/data_source_netbox_devices.go @@ -63,8 +63,10 @@ func dataSourceNetboxDevices() *schema.Resource { Computed: true, }, "custom_fields": { - Type: schema.TypeMap, + Type: schema.TypeString, Computed: true, + Description: "A JSON string that defines the custom fields as defined under the `custom_fields` key in the object's api." + + "The data can be accessed by using the `jsondecode()` function around the `custom_fields` attribute.", }, "description": { Type: schema.TypeString, @@ -247,9 +249,11 @@ func dataSourceNetboxDevicesRead(d *schema.ResourceData, m interface{}) error { if device.Status != nil { mapping["status"] = *device.Status.Value } - if device.CustomFields != nil { - mapping["custom_fields"] = device.CustomFields + cf, err := handleCustomFieldRead(device.CustomFields) + if err != nil { + return err } + mapping["custom_fields"] = cf if device.Rack != nil { mapping["rack_id"] = device.Rack.ID } diff --git a/netbox/data_source_netbox_devices_test.go b/netbox/data_source_netbox_devices_test.go index 206f1d00..71c0f105 100644 --- a/netbox/data_source_netbox_devices_test.go +++ b/netbox/data_source_netbox_devices_test.go @@ -190,7 +190,6 @@ func TestAccNetboxDevicesDataSource_CustomFields(t *testing.T) { data "netbox_devices" "test" { depends_on = [ netbox_device.test, - netbox_custom_field.test, ] filter { @@ -199,12 +198,18 @@ data "netbox_devices" "test" { } } -resource "netbox_custom_field" "test" { - name = "%[1]s" +resource "netbox_custom_field" "text" { + name = "%[1]s_text" type = "text" content_types = ["dcim.device"] } +resource "netbox_custom_field" "boolean" { + name = "%[1]s_boolean" + type = "boolean" + content_types = ["dcim.device"] +} + resource "netbox_device" "test" { name = "%[2]s" comments = "thisisacomment" @@ -219,7 +224,10 @@ resource "netbox_device" "test" { location_id = netbox_location.test.id status = "staged" serial = "ABCDEF" - custom_fields = {"${netbox_custom_field.test.name}" = "81"} + custom_fields = jsonencode({ + "${netbox_custom_field.text.name}" = "81" + "${netbox_custom_field.boolean.name}" = true + }) } `, testField, testName), Check: resource.ComposeTestCheckFunc( @@ -235,7 +243,7 @@ resource "netbox_device" "test" { resource.TestCheckResourceAttrPair("data.netbox_devices.test", "devices.0.location_id", "netbox_location.test", "id"), resource.TestCheckResourceAttr("data.netbox_devices.test", "devices.0.serial", "ABCDEF"), resource.TestCheckResourceAttr("data.netbox_devices.test", "devices.0.status", "staged"), - resource.TestCheckResourceAttr("data.netbox_devices.test", "devices.0.custom_fields."+testField, "81"), + resource.TestCheckResourceAttr("data.netbox_devices.test", "devices.0.custom_fields", "{\""+testField+"_boolean\":true,\""+testField+"_text\":\"81\"}"), ), }, },