From ae3654259181e2cf86238f86c3053322ec21dc84 Mon Sep 17 00:00:00 2001 From: tagur87 <43474056+tagur87@users.noreply.github.com> Date: Tue, 16 Jul 2024 10:36:21 -0400 Subject: [PATCH] Add contains filter to data_source_netbox_prefixes It is helpful to be able to look up the prefix by an IP address within the prefix. The contains filter can be used for this. If you specify an IP within a prefix, it will return that prefix. --- netbox/data_source_netbox_prefixes.go | 4 +++- netbox/data_source_netbox_prefixes_test.go | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/netbox/data_source_netbox_prefixes.go b/netbox/data_source_netbox_prefixes.go index 59326f6e..19932b65 100644 --- a/netbox/data_source_netbox_prefixes.go +++ b/netbox/data_source_netbox_prefixes.go @@ -25,7 +25,7 @@ func dataSourceNetboxPrefixes() *schema.Resource { "name": { Type: schema.TypeString, Required: true, - Description: "The name of the field to filter on. Supported fields are: `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `status`, `site_id`, & `tag`.", + Description: "The name of the field to filter on. Supported fields are: `prefix`, `contains`, `vlan_vid`, `vrf_id`, `vlan_id`, `status`, `site_id`, & `tag`.", }, "value": { Type: schema.TypeString, @@ -107,6 +107,8 @@ func dataSourceNetboxPrefixesRead(d *schema.ResourceData, m interface{}) error { return err } params.VlanVid = &float + case "contains": + params.Contains = &vString case "vrf_id": params.VrfID = &vString case "vlan_id": diff --git a/netbox/data_source_netbox_prefixes_test.go b/netbox/data_source_netbox_prefixes_test.go index 2e5a194f..745d832d 100644 --- a/netbox/data_source_netbox_prefixes_test.go +++ b/netbox/data_source_netbox_prefixes_test.go @@ -8,7 +8,7 @@ import ( ) func TestAccNetboxPrefixesDataSource_basic(t *testing.T) { - testPrefixes := []string{"10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24", "10.0.7.0/24"} + testPrefixes := []string{"10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24", "10.0.7.0/24", "10.0.8.0/24"} testSlug := "prefixes_ds_basic" testVlanVids := []int{4093, 4094} testName := testAccGetTestName(testSlug) @@ -49,6 +49,11 @@ resource "netbox_prefix" "with_site_id" { site_id = netbox_site.test.id } +resource "netbox_prefix" "with_container" { + prefix = "%[8]s" + status = "container" +} + resource "netbox_vrf" "test_vrf" { name = "%[1]s_test_vrf" } @@ -117,7 +122,16 @@ data "netbox_prefixes" "find_prefix_with_site_id" { value = netbox_site.test.id } } -`, testName, testPrefixes[0], testPrefixes[1], testPrefixes[2], testPrefixes[3], testVlanVids[0], testVlanVids[1]), + +data "netbox_prefixes" "find_prefix_with_contains" { + depends_on = [netbox_prefix.with_container] + filter { + name = "contains" + value = "10.0.8.50" + } +} + +`, testName, testPrefixes[0], testPrefixes[1], testPrefixes[2], testPrefixes[3], testVlanVids[0], testVlanVids[1], testPrefixes[4]), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("data.netbox_prefixes.by_vrf", "prefixes.#", "2"), resource.TestCheckResourceAttrPair("data.netbox_prefixes.by_vrf", "prefixes.1.vlan_vid", "netbox_vlan.test_vlan2", "vid"), @@ -127,6 +141,8 @@ data "netbox_prefixes" "find_prefix_with_site_id" { resource.TestCheckResourceAttr("data.netbox_prefixes.no_results", "prefixes.#", "0"), resource.TestCheckResourceAttr("data.netbox_prefixes.find_prefix_with_site_id", "prefixes.#", "1"), resource.TestCheckResourceAttr("data.netbox_prefixes.find_prefix_with_site_id", "prefixes.0.prefix", "10.0.7.0/24"), + resource.TestCheckResourceAttr("data.netbox_prefixes.find_prefix_with_contains", "prefixes.#", "1"), + resource.TestCheckResourceAttr("data.netbox_prefixes.find_prefix_with_contains", "prefixes.0.prefix", "10.0.8.0/24"), ), }, },