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

[feature/netbox_device_custom_fields] added support custom_fields for… #242

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ENHANCEMENTS
* data-source/netbox_prefix: Add `tags` and tag filter attributes ([#284](https://github.com/e-breuninger/terraform-provider-netbox/pull/284) by [@kyle-burnett](https://github.com/kyle-burnett))

BUG FIXES
* data-source/netbox_prefixes: FIx kernel panic when finding prefixes without vlan or vrf
* data-source/netbox_prefixes: Fix kernel panic when finding prefixes without vlan or vrf

## 3.0.7 (November 3rd, 2022)

Expand Down
7 changes: 7 additions & 0 deletions docs/resources/device.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ 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

# custom fields - optional, not required
custom_fields = {
"test_field_1" = "test_field_value_1",
"test_field_2" = "test_field_value_2"
}
}
```

Expand All @@ -56,6 +62,7 @@ resource "netbox_device" "test" {

- `cluster_id` (Number)
- `comments` (String)
- `custom_fields` (Map of String)
- `location_id` (Number)
- `platform_id` (Number)
- `serial` (String)
Expand Down
6 changes: 6 additions & 0 deletions examples/resources/netbox_device/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ 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

# custom fields - optional, not required
custom_fields = {
"test_field_1" = "test_field_value_1",
"test_field_2" = "test_field_value_2"
}
}
3 changes: 2 additions & 1 deletion netbox/custom_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const customFieldsKey = "custom_fields"
var customFieldsSchema = &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Default: nil,
Default: "",
doncu marked this conversation as resolved.
Show resolved Hide resolved
Required: false,
Elem: &schema.Schema{
Type: schema.TypeString,
Default: nil,
Expand Down
17 changes: 17 additions & 0 deletions netbox/resource_netbox_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func resourceNetboxDevice() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"offline", "active", "planned", "staged", "failed", "inventory"}, false),
Default: "active",
},
customFieldsKey: customFieldsSchema,
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand Down Expand Up @@ -148,6 +149,11 @@ func resourceNetboxDeviceCreate(ctx context.Context, d *schema.ResourceData, m i

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))

ct, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = ct
}

params := dcim.NewDcimDevicesCreateParams().WithData(&data)

res, err := api.Dcim.DcimDevicesCreate(params, nil)
Expand Down Expand Up @@ -243,6 +249,12 @@ func resourceNetboxDeviceRead(ctx context.Context, d *schema.ResourceData, m int
d.Set("status", device.Status.Value)

d.Set(tagsKey, getTagListFromNestedTagList(device.Tags))

cf := getCustomFields(device.CustomFields)
if cf != nil {
d.Set(customFieldsKey, cf)
}

return diags
}

Expand Down Expand Up @@ -323,6 +335,11 @@ func resourceNetboxDeviceUpdate(ctx context.Context, d *schema.ResourceData, m i

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))

cf, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = cf
}

if d.HasChanges("comments") {
// check if comment is set
commentsValue, ok := d.GetOk("comments")
Expand Down
8 changes: 8 additions & 0 deletions netbox/resource_netbox_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,19 @@ func TestAccNetboxDevice_basic(t *testing.T) {

testSlug := "device_basic"
testName := testAccGetTestName(testSlug)
//testField := strings.ReplaceAll(testAccGetTestName(testSlug), "-", "-")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDeviceDestroy,
Steps: []resource.TestStep{
{
Config: testAccNetboxDeviceFullDependencies(testName) + fmt.Sprintf(`
resource "netbox_custom_field" "test" {
name = "custom_field"
type = "text"
content_types = ["dcim.device"]
}
resource "netbox_device" "test" {
name = "%[1]s"
comments = "thisisacomment"
Expand All @@ -90,6 +96,7 @@ resource "netbox_device" "test" {
location_id = netbox_location.test.id
status = "staged"
serial = "ABCDEF"
custom_fields = {"${netbox_custom_field.test.name}" = "13"}
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_device.test", "name", testName),
Expand All @@ -104,6 +111,7 @@ resource "netbox_device" "test" {
resource.TestCheckResourceAttr("netbox_device.test", "serial", "ABCDEF"),
resource.TestCheckResourceAttr("netbox_device.test", "tags.#", "1"),
resource.TestCheckResourceAttr("netbox_device.test", "tags.0", testName+"a"),
resource.TestCheckResourceAttr("netbox_device.test", "custom_fields.custom_field", "13"),
),
},
{
Expand Down