From 0791ed937c80b58c082bd49da26bfb6e67721ba7 Mon Sep 17 00:00:00 2001 From: tagur87 <43474056+tagur87@users.noreply.github.com> Date: Tue, 16 Jul 2024 11:24:41 -0400 Subject: [PATCH] Add site_id as a returned field to netbox_prefixes data source It can be helpful to know the ID of the site that the prefixes are assigned to. This allows us to reference it within in our TF code. --- netbox/data_source_netbox_prefixes.go | 7 +++++++ netbox/data_source_netbox_prefixes_test.go | 11 +++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/netbox/data_source_netbox_prefixes.go b/netbox/data_source_netbox_prefixes.go index 19932b65..ada84bcb 100644 --- a/netbox/data_source_netbox_prefixes.go +++ b/netbox/data_source_netbox_prefixes.go @@ -59,6 +59,10 @@ func dataSourceNetboxPrefixes() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "site_id": { + Type: schema.TypeInt, + Computed: true, + }, "vlan_vid": { Type: schema.TypeFloat, Computed: true, @@ -146,6 +150,9 @@ func dataSourceNetboxPrefixesRead(d *schema.ResourceData, m interface{}) error { if v.Vrf != nil { mapping["vrf_id"] = v.Vrf.ID } + if v.Site != nil { + mapping["site_id"] = v.Site.ID + } mapping["status"] = v.Status.Value mapping["tags"] = getTagListFromNestedTagList(v.Tags) diff --git a/netbox/data_source_netbox_prefixes_test.go b/netbox/data_source_netbox_prefixes_test.go index 745d832d..0656b9c0 100644 --- a/netbox/data_source_netbox_prefixes_test.go +++ b/netbox/data_source_netbox_prefixes_test.go @@ -49,9 +49,15 @@ resource "netbox_prefix" "with_site_id" { site_id = netbox_site.test.id } +resource "netbox_site" "test2" { + name = "site2-%[1]s" + timezone = "Europe/Berlin" +} + resource "netbox_prefix" "with_container" { - prefix = "%[8]s" - status = "container" + prefix = "%[8]s" + status = "container" + site_id = netbox_site.test2.id } resource "netbox_vrf" "test_vrf" { @@ -143,6 +149,7 @@ data "netbox_prefixes" "find_prefix_with_contains" { 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"), + resource.TestCheckResourceAttrSet("data.netbox_prefixes.find_prefix_with_contains", "prefixes.0.site_id"), ), }, },