Skip to content

Commit

Permalink
feat: allow any custom field type in netbox_devices data source
Browse files Browse the repository at this point in the history
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
}
```
  • Loading branch information
tagur87 authored and fbreckle committed Aug 25, 2023
1 parent 2037d82 commit fa4e157
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions netbox/data_source_netbox_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 13 additions & 5 deletions netbox/data_source_netbox_devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ func TestAccNetboxDevicesDataSource_CustomFields(t *testing.T) {
data "netbox_devices" "test" {
depends_on = [
netbox_device.test,
netbox_custom_field.test,
]
filter {
Expand All @@ -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"
Expand All @@ -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(
Expand All @@ -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\"}"),
),
},
},
Expand Down

0 comments on commit fa4e157

Please sign in to comment.