Skip to content

Commit

Permalink
Add site_id as a returned field to netbox_prefixes data source
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tagur87 committed Jul 16, 2024
1 parent ae36542 commit 0791ed9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions netbox/data_source_netbox_prefixes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
11 changes: 9 additions & 2 deletions netbox/data_source_netbox_prefixes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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"),
),
},
},
Expand Down

0 comments on commit 0791ed9

Please sign in to comment.