Skip to content

Commit

Permalink
feat: Support querying sites by facility
Browse files Browse the repository at this point in the history
Add support for the facility field to the site datasource so that it can
be used to find sites by facility if it's unique, or retrieve the
facility for a site.
  • Loading branch information
Ikke authored and fbreckle committed Oct 10, 2023
1 parent 02542b6 commit 1f19bb5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions netbox/data_source_netbox_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func dataSourceNetboxSite() *schema.Resource {
Optional: true,
Computed: true,
},
"facility": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"asn_ids": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -84,6 +89,9 @@ func dataSourceNetboxSiteRead(d *schema.ResourceData, m interface{}) error {
if id, ok := d.Get("id").(string); ok && id != "0" {
params.SetID(&id)
}
if facility, ok := d.Get("facility").(string); ok && facility != "" {
params.SetFacility(&facility)
}

res, err := api.Dcim.DcimSitesList(params, nil)
if err != nil {
Expand All @@ -103,6 +111,7 @@ func dataSourceNetboxSiteRead(d *schema.ResourceData, m interface{}) error {
d.Set("site_id", site.ID)
d.Set("slug", site.Slug)
d.Set("time_zone", site.TimeZone)
d.Set("facility", site.Facility)

if site.Group != nil {
d.Set("group_id", site.Group.ID)
Expand Down
14 changes: 14 additions & 0 deletions netbox/data_source_netbox_site_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ resource "netbox_site" "test" {
region_id = netbox_region.test.id
tenant_id = netbox_tenant.test.id
timezone = "Europe/Berlin"
facility = "Facility"
}`, testName)
}

Expand Down Expand Up @@ -63,6 +64,13 @@ data "netbox_site" "test" {
}`
}

func testAccNetboxSiteByFacility() string {
return `
data "netbox_site" "test" {
facility = netbox_site.test.facility
}`
}

func TestAccNetboxSiteDataSource_basic(t *testing.T) {
testName := testAccGetTestName("site_ds_basic")
setUp := testAccNetboxSiteSetUp(testName)
Expand Down Expand Up @@ -104,6 +112,12 @@ func TestAccNetboxSiteDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair("data.netbox_site.test", "id", "netbox_site.test", "id"),
),
},
{
Config: setUp + testAccNetboxSiteByFacility(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.netbox_site.test", "id", "netbox_site.test", "id"),
),
},
},
})
}

0 comments on commit 1f19bb5

Please sign in to comment.