From f5a8ddf0e2e6791399e5f80c077b62d24f0cda18 Mon Sep 17 00:00:00 2001 From: William Lebel Date: Fri, 13 Sep 2024 14:20:55 -0400 Subject: [PATCH] Add custom fields support in IPAM IP address. --- docs/resources/ip_address.md | 1 + netbox/resource_netbox_ip_address.go | 14 +++++++++++++ netbox/resource_netbox_ip_address_test.go | 24 +++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/docs/resources/ip_address.md b/docs/resources/ip_address.md index 1feeaa0b..46359dc7 100644 --- a/docs/resources/ip_address.md +++ b/docs/resources/ip_address.md @@ -112,6 +112,7 @@ resource "netbox_ip_address" "this" { ### Optional +- `custom_fields` (Map of String) - `description` (String) - `device_interface_id` (Number) Conflicts with `interface_id` and `virtual_machine_interface_id`. - `dns_name` (String) diff --git a/netbox/resource_netbox_ip_address.go b/netbox/resource_netbox_ip_address.go index bd8d39db..b36df779 100644 --- a/netbox/resource_netbox_ip_address.go +++ b/netbox/resource_netbox_ip_address.go @@ -108,6 +108,7 @@ func resourceNetboxIPAddress() *schema.Resource { }, }, }, + customFieldsKey: customFieldsSchema, }, Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, @@ -153,6 +154,11 @@ func resourceNetboxIPAddressCreate(d *schema.ResourceData, m interface{}) error data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey)) + cf, ok := d.GetOk(customFieldsKey) + if ok { + data.CustomFields = cf + } + params := ipam.NewIpamIPAddressesCreateParams().WithData(&data) res, err := api.Ipam.IpamIPAddressesCreate(params, nil) @@ -255,6 +261,10 @@ func resourceNetboxIPAddressRead(d *schema.ResourceData, m interface{}) error { d.Set("description", ipAddress.Description) d.Set("status", ipAddress.Status.Value) d.Set(tagsKey, getTagListFromNestedTagList(ipAddress.Tags)) + cf := getCustomFields(res.GetPayload().CustomFields) + if cf != nil { + d.Set(customFieldsKey, cf) + } return nil } @@ -297,6 +307,10 @@ func resourceNetboxIPAddressUpdate(d *schema.ResourceData, m interface{}) error data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey)) + if cf, ok := d.GetOk(customFieldsKey); ok { + data.CustomFields = cf + } + params := ipam.NewIpamIPAddressesUpdateParams().WithID(id).WithData(&data) _, err := api.Ipam.IpamIPAddressesUpdate(params, nil) diff --git a/netbox/resource_netbox_ip_address_test.go b/netbox/resource_netbox_ip_address_test.go index e6c55721..7601aebb 100644 --- a/netbox/resource_netbox_ip_address_test.go +++ b/netbox/resource_netbox_ip_address_test.go @@ -204,6 +204,30 @@ resource "netbox_ip_address" "test" { ImportStateVerify: true, ImportStateVerifyIgnore: []string{"interface_id", "object_type"}, }, + { + Config: testAccNetboxIPAddressFullDependencies(testName) + fmt.Sprintf(` +resource "netbox_custom_field" "test" { + name = "test" + type = "text" + weight = 100 + content_types = ["ipam.ipaddress"] +} +resource "netbox_ip_address" "test_customfield" { + ip_address = "%s" + status = "active" + custom_fields = { + "${netbox_custom_field.test.name}" = "test-field" + } +}`, testIP), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netbox_ip_address.test_customfield", "custom_fields.test", "test-field"), + ), + }, + { + ResourceName: "netbox_ip_address.test_customfield", + ImportState: true, + ImportStateVerify: true, + }, }, }) }