Skip to content

Commit

Permalink
Add custom fields support in IPAM IP address.
Browse files Browse the repository at this point in the history
  • Loading branch information
greatman authored and coveowlebel committed Sep 13, 2024
1 parent 659d65b commit 42ed820
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/resources/ip_address.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions netbox/resource_netbox_ip_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func resourceNetboxIPAddress() *schema.Resource {
},
},
},
customFieldsKey: customFieldsSchema,
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions netbox/resource_netbox_ip_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
})
}
Expand Down

0 comments on commit 42ed820

Please sign in to comment.