diff --git a/README.md b/README.md index ea9c9088..9e2c4f70 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ make testacc ``` If you notice a failed test, it might be due to a stale netbox data volume. Before concluding there is a problem, -refresh the docker containers by running `docker-compose down --volumes` in the `docker` directory. Then run the tests again. +refresh the docker containers by running `docker compose down --volumes` in the `docker` directory. Then run the tests again. If you get `too many open files` errors when running the acceptance test suite locally on Linux, your user limit for open file descriptors might be too low. You can increase that limit with `ulimit -n 2048`. diff --git a/docs/resources/available_ip_address.md b/docs/resources/available_ip_address.md index e3705cc9..9e5c4a45 100644 --- a/docs/resources/available_ip_address.md +++ b/docs/resources/available_ip_address.md @@ -77,6 +77,21 @@ resource "netbox_available_ip_address" "myvm-ip" { } ``` +### Creating an IP with Custom Fields +```terraform +data "netbox_prefix" "test" { + cidr = "10.0.0.0/24" +} + +resource "netbox_available_ip_address" "test" { + prefix_id = data.netbox_prefix.test.id + + custom_fields = { + "test_field_1" = "test_field_value_1", + "test_field_2" = "test_field_value_2" + } +} +``` ## Schema @@ -95,7 +110,8 @@ resource "netbox_available_ip_address" "myvm-ip" { - `tenant_id` (Number) - `virtual_machine_interface_id` (Number) Conflicts with `interface_id` and `device_interface_id`. - `vrf_id` (Number) - +- `custom_fields` (Map of String) +- ### Read-Only - `id` (String) The ID of this resource. diff --git a/examples/resources/netbox_available_ip_address/prefix.tf b/examples/resources/netbox_available_ip_address/prefix.tf index c2582dcf..188a9b35 100644 --- a/examples/resources/netbox_available_ip_address/prefix.tf +++ b/examples/resources/netbox_available_ip_address/prefix.tf @@ -4,4 +4,8 @@ data "netbox_prefix" "test" { resource "netbox_available_ip_address" "test" { prefix_id = data.netbox_prefix.test.id + custom_fields = { + "test_field_1" = "test_field_value_1", + "test_field_2" = "test_field_value_2" + } } diff --git a/netbox/resource_netbox_available_ip_address.go b/netbox/resource_netbox_available_ip_address.go index 5e2a1869..49df91a7 100644 --- a/netbox/resource_netbox_available_ip_address.go +++ b/netbox/resource_netbox_available_ip_address.go @@ -91,7 +91,8 @@ This resource will retrieve the next available IP address from a given prefix or Type: schema.TypeString, Optional: true, }, - tagsKey: tagsSchema, + tagsKey: tagsSchema, + customFieldsKey: customFieldsSchema, "role": { Type: schema.TypeString, Optional: true, @@ -192,6 +193,10 @@ func resourceNetboxAvailableIPAddressRead(d *schema.ResourceData, m interface{}) d.Set("description", ipAddress.Description) d.Set("status", ipAddress.Status.Value) d.Set(tagsKey, getTagListFromNestedTagList(ipAddress.Tags)) + cf := getCustomFields(ipAddress.CustomFields) + if cf != nil { + d.Set(customFieldsKey, cf) + } return nil } @@ -209,13 +214,14 @@ func resourceNetboxAvailableIPAddressUpdate(d *schema.ResourceData, m interface{ data.DNSName = getOptionalStr(d, "dns_name", false) data.Vrf = getOptionalInt(d, "vrf_id") data.Tenant = getOptionalInt(d, "tenant_id") - if interfaceID, ok := d.GetOk("interface_id"); ok { // The other possible type is dcim.interface for devices data.AssignedObjectType = strToPtr("virtualization.vminterface") data.AssignedObjectID = int64ToPtr(int64(interfaceID.(int))) } - + if customFields, ok := d.GetOk(customFieldsKey); ok { + data.CustomFields = getCustomFields(customFields) + } vmInterfaceID := getOptionalInt(d, "virtual_machine_interface_id") deviceInterfaceID := getOptionalInt(d, "device_interface_id") interfaceID := getOptionalInt(d, "interface_id") diff --git a/netbox/resource_netbox_available_ip_address_test.go b/netbox/resource_netbox_available_ip_address_test.go index de5a9f7f..c0c6876a 100644 --- a/netbox/resource_netbox_available_ip_address_test.go +++ b/netbox/resource_netbox_available_ip_address_test.go @@ -284,6 +284,38 @@ resource "netbox_available_ip_address" "test" { }, }) } +func TestAccNetboxAvailableIPAddress_customField(t *testing.T) { + testPrefix := "1.1.8.0/24" + testIP := "1.1.8.1/24" + resource.ParallelTest(t, resource.TestCase{ + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(` +resource "netbox_prefix" "test" { + prefix = "%s" + status = "active" + is_pool = false +} +resource "netbox_custom_field" "test_ip_address" { + name = "custom_field_ip_address" + type = "text" + content_types = ["ipam.ipaddress"] +} +resource "netbox_available_ip_address" "test" { + prefix_id = netbox_prefix.test.id + status = "active" + custom_fields = {"${netbox_custom_field.test_ip_address.name}" = "HelloWorld"} +}`, testPrefix), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netbox_available_ip_address.test", "ip_address", testIP), + resource.TestCheckResourceAttr("netbox_available_ip_address.test", "status", "active"), + resource.TestCheckResourceAttr("netbox_available_ip_address.test", "custom_fields.custom_field_ip_address", "HelloWorld"), + ), + }, + }, + }) +} func init() { resource.AddTestSweepers("netbox_available_ip_address", &resource.Sweeper{