Skip to content

Commit

Permalink
feat: add filter by tag on ip_addresses data source
Browse files Browse the repository at this point in the history
  • Loading branch information
sjurtf authored and fbreckle committed Dec 4, 2023
1 parent 146c37f commit afdb826
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions netbox/data_source_netbox_ip_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func dataSourceNetboxIPAddressesRead(d *schema.ResourceData, m interface{}) erro

if filter, ok := d.GetOk("filter"); ok {
var filterParams = filter.(*schema.Set)
var tags []string
for _, f := range filterParams.List() {
k := f.(map[string]interface{})["name"]
v := f.(map[string]interface{})["value"]
Expand All @@ -172,6 +173,9 @@ func dataSourceNetboxIPAddressesRead(d *schema.ResourceData, m interface{}) erro
params.Tenant = &vString
case "parent_prefix":
params.Parent = &vString
case "tag":
tags = append(tags, vString)
params.Tag = tags
default:
return fmt.Errorf("'%s' is not a supported filter parameter", k)
}
Expand Down
50 changes: 50 additions & 0 deletions netbox/data_source_netbox_ip_addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,53 @@ func TestAccNetboxIpAddressessDataSource_many(t *testing.T) {
},
})
}

func TestAccNetboxIpAddressesDataSource_filter_tags(t *testing.T) {
testSlug := "ipam_ipaddrs_ds_filter_tags"
testTag := "default-gw"
testName := testAccGetTestName(testSlug)
testIP0 := "203.0.113.1/24"
testIP1 := "203.0.113.2/24"
testIP2 := "203.0.113.3/24"
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNetboxIPAddressFullDependencies(testName) + fmt.Sprintf(`
resource "netbox_tag" "gw_tag" {
name = "%s"
}
resource "netbox_ip_address" "test_list_0" {
ip_address = "%s"
virtual_machine_interface_id = netbox_interface.test.id
status = "active"
tags = [netbox_tag.test.name]
}
resource "netbox_ip_address" "test_list_1" {
ip_address = "%s"
virtual_machine_interface_id = netbox_interface.test.id
status = "active"
tags = [netbox_tag.test.name, netbox_tag.gw_tag.name]
}
resource "netbox_ip_address" "test_list_2" {
ip_address = "%s"
virtual_machine_interface_id = netbox_interface.test.id
status = "active"
tags = [netbox_tag.test.name]
}
data "netbox_ip_addresses" "test_list" {
depends_on = [netbox_ip_address.test_list_0, netbox_ip_address.test_list_1, netbox_ip_address.test_list_2]
filter {
name = "tag"
value = "%s"
}
}`, testTag, testIP0, testIP1, testIP2, testTag),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_ip_addresses.test_list", "ip_addresses.#", "1"),
resource.TestCheckResourceAttrPair("data.netbox_ip_addresses.test_list", "ip_addresses.0.ip_address", "netbox_ip_address.test_list_1", "ip_address"),
),
},
},
})
}

0 comments on commit afdb826

Please sign in to comment.