Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul Custom Fields for Devices to support any underlying type #443

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions docs/resources/device.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ resource "netbox_device" "test" {
- `custom_fields` (Map of String)
- `description` (String)
- `local_context_data` (String) This is best managed through the use of `jsonencode` and a map of settings.
- `custom_fields` (String) A JSON string that defines the custom fields as defined under the `custom_fields` key in the object's api.This is best managed with the `jsonencode()` & `jsondecode()` functions.
- `location_id` (Number)
- `platform_id` (Number)
- `rack_face` (String) Valid values are `front` and `rear`. Required when `rack_position` is set.
Expand Down
83 changes: 83 additions & 0 deletions netbox/custom_fields.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package netbox

import (
"encoding/json"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

const customFieldsKey = "custom_fields"
Expand All @@ -23,3 +27,82 @@ func getCustomFields(cf interface{}) map[string]interface{} {
}
return cfm
}

// customFieldsSchemaFunc is a function that returns the schema for all custom
// fields.
func customFieldsSchemaFunc() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "A JSON string that defines the custom fields as defined under the `custom_fields` key in the object's api." +
"This is best managed with the `jsonencode()` & `jsondecode()` functions.",
ValidateFunc: validation.StringIsJSON,
}
}

// handleCustomFieldUpdate is a function that takes in the old and new values returned
// from a terraform d.GetChange() function and returns a map with the values to be sent
// in the custom_fields field on an update to the netbox api.
// This function handles setting the custom_field fields to null when needed. It does
// this by comparing the custom fields that were previously in terraform state, and
// compares them with then new state. It then sets any fields that were previously
// set to nil, if the new state does not include them.
func handleCustomFieldUpdate(old, new interface{}) (map[string]interface{}, error) {
ret := make(map[string]interface{})
var newData map[string]interface{}

if new.(string) != "" {
err := json.Unmarshal([]byte(new.(string)), &newData)
if err != nil {
return nil, fmt.Errorf("err1: %w", err)
}
for k, v := range newData {
ret[k] = v
}
}
var oldData map[string]interface{}
if old.(string) != "" {
err := json.Unmarshal([]byte(old.(string)), &oldData)
if err != nil {
return nil, fmt.Errorf("err2: %w", err)
}
for k := range oldData {
if val, ok := ret[k]; !ok {
ret[k] = nil
} else {
ret[k] = val
}
}
}
return ret, nil
}

// handleCustomFieldRead is a function that take an input of the interface
// from the CustomField struct field, and returns a string with the value
// to set for terraform and an error.
// This function checks the number of keys in the map, and if the number of
// nil fields equal the number of fields, it returns an empty string. Since
// this means the custom fields are not managed. Otherwise, it will return
// the result of unmarshalling the field to a json string.
// This allows the custom_field field to be set to empty and have an empty plan
// even though the api still has the custom fields with null values.
func handleCustomFieldRead(cf interface{}) (string, error) {
cfMap, ok := cf.(map[string]interface{})
if !ok {
return "", fmt.Errorf("cannot cast %v to map[string]interface{}", cf)
}
numNull := 0
for k := range cfMap {
if cfMap[k] == nil {
numNull += 1
}
}
if len(cfMap) == 0 || numNull == len(cfMap) {
return "", nil
}
b, err := json.Marshal(cf)
if err != nil {
return "", err
}
return string(b), nil
}
118 changes: 118 additions & 0 deletions netbox/custom_fields_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package netbox

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_handleCustomFieldUpdate(t *testing.T) {
type args struct {
old interface{}
new interface{}
}
tests := []struct {
name string
args args
want map[string]interface{}
wantErr bool
}{
{
name: "create new custom field",
args: args{
old: "",
new: "{\"a\": \"b\", \"c\": true, \"d\": {\"e\": \"f\"}}",
},
want: map[string]interface{}{
"a": "b",
"c": true,
"d": map[string]interface{}{
"e": "f",
},
},
wantErr: false,
},
{
name: "update custom fields",
args: args{
old: "{\"a\": \"b\", \"c\": true, \"d\": {\"e\": \"f\"}}",
new: "{\"a\": \"q\", \"c\": false}",
},
want: map[string]interface{}{
"a": "q",
"c": false,
"d": nil,
},
wantErr: false,
},
{
name: "remove custom fields",
args: args{
old: "{\"a\": \"b\", \"c\": true, \"d\": {\"e\": \"f\"}}",
new: "",
},
want: map[string]interface{}{
"a": nil,
"c": nil,
"d": nil,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := handleCustomFieldUpdate(tt.args.old, tt.args.new)
if (err != nil) != tt.wantErr {
t.Errorf("handleCustomFieldUpdate() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(t, tt.want, got)
})
}
}

func Test_handleCustomFieldRead(t *testing.T) {
tests := []struct {
name string
cf interface{}
want string
wantErr bool
}{
{
name: "all fields are nil",
cf: map[string]interface{}{
"a": nil,
"c": nil,
"d": nil,
},
want: "",
wantErr: false,
},
{
name: "one field is valid",
cf: map[string]interface{}{
"a": nil,
"c": true,
"d": nil,
},
want: "{\"a\":null,\"c\":true,\"d\":null}",
wantErr: false,
},
{
name: "cannot marshal nil",
cf: nil,
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := handleCustomFieldRead(tt.cf)
if (err != nil) != tt.wantErr {
t.Errorf("handleCustomFieldRead() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(t, tt.want, got)
})
}
}
10 changes: 7 additions & 3 deletions netbox/data_source_netbox_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,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 @@ -268,9 +270,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 @@ -245,7 +245,6 @@ func TestAccNetboxDevicesDataSource_CustomFields(t *testing.T) {
data "netbox_devices" "test" {
depends_on = [
netbox_device.test,
netbox_custom_field.test,
]

filter {
Expand All @@ -254,12 +253,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 @@ -274,7 +279,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 @@ -290,7 +298,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
25 changes: 17 additions & 8 deletions netbox/resource_netbox_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func resourceNetboxDevice() *schema.Resource {
Optional: true,
Description: "This is best managed through the use of `jsonencode` and a map of settings.",
},
customFieldsKey: customFieldsSchema,
customFieldsKey: customFieldsSchemaFunc(),
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand Down Expand Up @@ -203,9 +203,14 @@ func resourceNetboxDeviceCreate(ctx context.Context, d *schema.ResourceData, m i
}
}

ct, ok := d.GetOk(customFieldsKey)
cf, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = ct
var cfMap map[string]interface{}
err := json.Unmarshal([]byte(cf.(string)), &cfMap)
if err != nil {
return diag.Errorf("error in resourceNetboxDeviceCreate[CustomFields]: %v", err)
}
data.CustomFields = cfMap
}

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))
Expand Down Expand Up @@ -300,10 +305,11 @@ func resourceNetboxDeviceRead(ctx context.Context, d *schema.ResourceData, m int
d.Set("site_id", nil)
}

cf := getCustomFields(res.GetPayload().CustomFields)
if cf != nil {
d.Set(customFieldsKey, cf)
cf, err := handleCustomFieldRead(device.CustomFields)
if err != nil {
return diag.FromErr(err)
}
d.Set(customFieldsKey, cf)

d.Set("asset_tag", device.AssetTag)

Expand Down Expand Up @@ -420,8 +426,11 @@ func resourceNetboxDeviceUpdate(ctx context.Context, d *schema.ResourceData, m i
}
}

cf, ok := d.GetOk(customFieldsKey)
if ok {
if d.HasChange(customFieldsKey) {
cf, err := handleCustomFieldUpdate(d.GetChange(customFieldsKey))
if err != nil {
return diag.Errorf("error in resourceNetboxDeviceUpdate[CustomFields]: %v", err)
}
data.CustomFields = cf
}

Expand Down
Loading
Loading