From 50225dc4a52483644b2804b23ab5d002774018eb Mon Sep 17 00:00:00 2001 From: nothinux Date: Tue, 20 Feb 2024 15:47:49 +0700 Subject: [PATCH 1/2] add custom field support in ipam prefix --- netbox/resource_netbox_prefix.go | 17 +++++++++++++++- netbox/resource_netbox_prefix_test.go | 29 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/netbox/resource_netbox_prefix.go b/netbox/resource_netbox_prefix.go index 7de34261..b7621f30 100644 --- a/netbox/resource_netbox_prefix.go +++ b/netbox/resource_netbox_prefix.go @@ -69,7 +69,8 @@ func resourceNetboxPrefix() *schema.Resource { Type: schema.TypeInt, Optional: true, }, - tagsKey: tagsSchema, + customFieldsKey: customFieldsSchema, + tagsKey: tagsSchema, }, Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, @@ -114,6 +115,11 @@ func resourceNetboxPrefixCreate(d *schema.ResourceData, m interface{}) error { data.Role = int64ToPtr(int64(roleID.(int))) } + cf, ok := d.GetOk(customFieldsKey) + if ok { + data.CustomFields = cf + } + data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey)) params := ipam.NewIpamPrefixesCreateParams().WithData(&data) @@ -184,6 +190,11 @@ func resourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error { d.Set("role_id", nil) } + cf := getCustomFields(res.GetPayload().CustomFields) + if cf != nil { + d.Set(customFieldsKey, cf) + } + d.Set(tagsKey, getTagListFromNestedTagList(res.GetPayload().Tags)) // FIGURE OUT NESTED VRF AND NESTED VLAN (from maybe interfaces?) @@ -231,6 +242,10 @@ func resourceNetboxPrefixUpdate(d *schema.ResourceData, m interface{}) error { data.Role = int64ToPtr(int64(roleID.(int))) } + if cf, ok := d.GetOk(customFieldsKey); ok { + data.CustomFields = cf + } + data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey)) params := ipam.NewIpamPrefixesUpdateParams().WithID(id).WithData(&data) diff --git a/netbox/resource_netbox_prefix_test.go b/netbox/resource_netbox_prefix_test.go index 07c87043..da97d483 100644 --- a/netbox/resource_netbox_prefix_test.go +++ b/netbox/resource_netbox_prefix_test.go @@ -230,6 +230,35 @@ resource "netbox_prefix" "test" { ImportState: true, ImportStateVerify: true, }, + { + Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(` +resource "netbox_custom_field" "test" { + name = "test" + type = "text" + weight = 100 + content_types = ["ipam.prefix"] +} + +resource "netbox_prefix" "test_customfield" { + prefix = "%s" + description = "%s 2" + status = "active" + tags = [netbox_tag.test.name] + mark_utilized = true + + custom_fields = { + "${netbox_custom_field.test.name}" = "test-field" + } +}`, testPrefix, testDesc), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netbox_prefix.test_customfield", "custom_fields.test", "test-field"), + ), + }, + { + ResourceName: "netbox_prefix.test_customfield", + ImportState: true, + ImportStateVerify: true, + }, }, }) } From 40ce5e7d30ded53eda3b9adc6cf940e1dbe12ebe Mon Sep 17 00:00:00 2001 From: nothinux Date: Tue, 20 Feb 2024 17:47:38 +0700 Subject: [PATCH 2/2] update docs --- docs/resources/prefix.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/resources/prefix.md b/docs/resources/prefix.md index 13642267..4877b3f3 100644 --- a/docs/resources/prefix.md +++ b/docs/resources/prefix.md @@ -36,6 +36,7 @@ resource "netbox_prefix" "my_prefix" { ### Optional +- `custom_fields` (Map of String) - `description` (String) - `is_pool` (Boolean) - `mark_utilized` (Boolean)