Skip to content

Commit

Permalink
add local_context_data to device resource
Browse files Browse the repository at this point in the history
resolves #490
references #421
  • Loading branch information
RickyRajinder committed Oct 25, 2023
1 parent 80ff64b commit bf94f48
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/resources/device.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ resource "netbox_device" "test" {
- `status` (String) Valid values are `offline`, `active`, `planned`, `staged`, `failed` and `inventory`. Defaults to `active`.
- `tags` (Set of String)
- `tenant_id` (Number)
- `local_context_data` (String) This is best managed through the use of `jsonencode` and a map of settings.

### Read-Only

Expand Down
4 changes: 4 additions & 0 deletions examples/resources/netbox_device/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ resource "netbox_device" "test" {
device_type_id = netbox_device_type.test.id
role_id = netbox_device_role.test.id
site_id = netbox_site.test.id
local_context_data = jsonencode({
"setting_a" = "Some Setting"
"setting_b" = 42
})
}
31 changes: 31 additions & 0 deletions netbox/resource_netbox_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package netbox

import (
"context"
"encoding/json"
"strconv"

"github.com/fbreckle/go-netbox/netbox/client"
Expand Down Expand Up @@ -106,6 +107,10 @@ func resourceNetboxDevice() *schema.Resource {
Type: schema.TypeFloat,
Optional: true,
},
"local_context_data": {
Type: schema.TypeString,
Optional: true,
},
customFieldsKey: customFieldsSchema,
},
Importer: &schema.ResourceImporter{
Expand Down Expand Up @@ -188,6 +193,15 @@ func resourceNetboxDeviceCreate(ctx context.Context, d *schema.ResourceData, m i
data.Position = nil
}

localContextValue, ok := d.GetOk("local_context_data")
if ok {
var jsonObj any
localContextBA := []byte(localContextValue.(string))
if err := json.Unmarshal(localContextBA, &jsonObj); err == nil {
data.LocalContextData = jsonObj
}
}

ct, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = ct
Expand Down Expand Up @@ -314,6 +328,14 @@ func resourceNetboxDeviceRead(ctx context.Context, d *schema.ResourceData, m int

d.Set("rack_position", device.Position)

if device.LocalContextData != nil {
if jsonArr, err := json.Marshal(device.LocalContextData); err == nil {
d.Set("local_context_data", string(jsonArr))
}
} else {
d.Set("local_context_data", nil)
}

d.Set(tagsKey, getTagListFromNestedTagList(device.Tags))
return diags
}
Expand Down Expand Up @@ -388,6 +410,15 @@ func resourceNetboxDeviceUpdate(ctx context.Context, d *schema.ResourceData, m i
data.Face = getOptionalStr(d, "rack_face", false)
data.Position = getOptionalFloat(d, "rack_position")

localContextValue, ok := d.GetOk("local_context_data")
if ok {
var jsonObj any
localContextBA := []byte(localContextValue.(string))
if err := json.Unmarshal(localContextBA, &jsonObj); err == nil {
data.LocalContextData = jsonObj
}
}

cf, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = cf
Expand Down
26 changes: 26 additions & 0 deletions netbox/resource_netbox_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ resource "netbox_device" "test" {
rack_id = netbox_rack.test.id
rack_face = "front"
rack_position = 10
local_context_data = jsonencode({"context_string"="context_value"})
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_device.test", "name", testName),
Expand All @@ -128,6 +129,7 @@ resource "netbox_device" "test" {
resource.TestCheckResourceAttr("netbox_device.test", "tags.0", testName+"a"),
resource.TestCheckResourceAttr("netbox_device.test", "rack_face", "front"),
resource.TestCheckResourceAttr("netbox_device.test", "rack_position", "10"),
resource.TestCheckResourceAttr("netbox_device.test", "local_context_data", "{\"context_string\":\"context_value\"}"),
),
},
{
Expand All @@ -148,6 +150,7 @@ resource "netbox_device" "test" {
rack_id = netbox_rack.test.id
status = "staged"
serial = "ABCDEF"
local_context_data = jsonencode({"context_string"="context_value2"})
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_device.test", "name", testName),
Expand All @@ -167,6 +170,7 @@ resource "netbox_device" "test" {
resource.TestCheckResourceAttr("netbox_device.test", "tags.0", testName+"a"),
resource.TestCheckResourceAttr("netbox_device.test", "rack_face", ""),
resource.TestCheckResourceAttr("netbox_device.test", "rack_position", "0"),
resource.TestCheckResourceAttr("netbox_device.test", "local_context_data", "{\"context_string\":\"context_value2\"}"),
),
},
{
Expand All @@ -186,6 +190,7 @@ resource "netbox_device" "test" {
location_id = netbox_location.test.id
status = "staged"
serial = "ABCDEF"
local_context_data = jsonencode({"context_string"="context_value"})
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_device.test", "name", testName),
Expand All @@ -205,6 +210,27 @@ resource "netbox_device" "test" {
resource.TestCheckResourceAttr("netbox_device.test", "rack_id", "0"),
resource.TestCheckResourceAttr("netbox_device.test", "rack_face", ""),
resource.TestCheckResourceAttr("netbox_device.test", "rack_position", "0"),
resource.TestCheckResourceAttr("netbox_device.test", "local_context_data", "{\"context_string\":\"context_value\"}"),
),
},
{
Config: testAccNetboxDeviceFullDependencies(testName) + fmt.Sprintf(`
resource "netbox_device" "test" {
name = "%[1]s"
role_id = netbox_device_role.test.id
device_type_id = netbox_device_type.test.id
site_id = netbox_site.test.id
cluster_id = netbox_cluster.test.id
platform_id = netbox_platform.test.id
local_context_data = jsonencode({"context_string"="context_value"})
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_device.test", "name", testName),
resource.TestCheckResourceAttrPair("netbox_device.test", "role_id", "netbox_device_role.test", "id"),
resource.TestCheckResourceAttrPair("netbox_device.test", "platform_id", "netbox_platform.test", "id"),
resource.TestCheckResourceAttrPair("netbox_device.test", "cluster_id", "netbox_cluster.test", "id"),
resource.TestCheckResourceAttrPair("netbox_device.test", "site_id", "netbox_site.test", "id"),
resource.TestCheckResourceAttr("netbox_device.test", "local_context_data", "{\"context_string\":\"context_value\"}"),
),
},
{
Expand Down

0 comments on commit bf94f48

Please sign in to comment.