diff --git a/netbox/data_source_netbox_tenants.go b/netbox/data_source_netbox_tenants.go index 3f3d94d5..33be2c51 100644 --- a/netbox/data_source_netbox_tenants.go +++ b/netbox/data_source_netbox_tenants.go @@ -6,6 +6,7 @@ import ( "github.com/fbreckle/go-netbox/netbox/client" "github.com/fbreckle/go-netbox/netbox/client/tenancy" + "github.com/fbreckle/go-netbox/netbox/models" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -109,6 +110,30 @@ func dataSourceNetboxTenants() *schema.Resource { Type: schema.TypeInt, Computed: true, }, + "tenant_group": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeInt, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "slug": { + Type: schema.TypeString, + Computed: true, + }, + "tenant_count": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, }, }, }, @@ -173,6 +198,7 @@ func dataSourceNetboxTenantsRead(d *schema.ResourceData, m interface{}) error { mapping["circuit_count"] = v.CircuitCount mapping["cluster_count"] = v.ClusterCount + mapping["tenant_group"] = flattenTenantGroup(v.Group) s = append(s, mapping) } @@ -180,3 +206,16 @@ func dataSourceNetboxTenantsRead(d *schema.ResourceData, m interface{}) error { return d.Set("tenants", s) } + +func flattenTenantGroup(group *models.NestedTenantGroup) []map[string]interface{} { + var s []map[string]interface{} + if group != nil { + var mapping = make(map[string]interface{}) + mapping["id"] = group.ID + mapping["name"] = group.Name + mapping["slug"] = group.Slug + mapping["tenant_count"] = group.TenantCount + s = append(s, mapping) + } + return s +} diff --git a/netbox/data_source_netbox_tenants_test.go b/netbox/data_source_netbox_tenants_test.go index d33b6b01..9491ea8c 100644 --- a/netbox/data_source_netbox_tenants_test.go +++ b/netbox/data_source_netbox_tenants_test.go @@ -65,3 +65,39 @@ data "netbox_tenants" "test" { }, }) } + +func TestAccNetboxTenantsDataSource_tenantgroups(t *testing.T) { + + testSlug := "tnt_ds_tenant_group_filter" + testName := testAccGetTestName(testSlug) + resource.Test(t, resource.TestCase{ + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(` +resource "netbox_tenant_group" "group_0" { + name = "group_%[1]s_1" +} + +resource "netbox_tenant" "tenant_0" { + name = "tenant_%[1]s_0" + group_id = netbox_tenant_group.group_0.id +} + +data "netbox_tenants" "test" { + depends_on = [netbox_tenant.tenant_0, netbox_tenant_group.group_0] + + filter { + name = "name" + value = "tenant_%[1]s_0" + } +}`, testName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.netbox_tenants.test", "tenants.#", "1"), + resource.TestCheckResourceAttrPair("data.netbox_tenants.test", "tenants.0.tenant_group.0.name", "netbox_tenant_group.group_0", "name"), + resource.TestCheckResourceAttrPair("data.netbox_tenants.test", "tenants.0.tenant_group.0.slug", "netbox_tenant_group.group_0", "slug"), + ), + }, + }, + }) +}