Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add contains filter to data_source_netbox_prefixes
Browse files Browse the repository at this point in the history
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.
tagur87 committed Jul 16, 2024
1 parent 3810615 commit ae36542
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion netbox/data_source_netbox_prefixes.go
Original file line number Diff line number Diff line change
@@ -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":
20 changes: 18 additions & 2 deletions netbox/data_source_netbox_prefixes_test.go
Original file line number Diff line number Diff line change
@@ -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"),
),
},
},

0 comments on commit ae36542

Please sign in to comment.