Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔨 Add tenant_group to netbox_tenants datasource #129

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions netbox/data_source_netbox_tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
},
"tentant_count": {
twink0r marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -173,10 +198,24 @@ func dataSourceNetboxTenantsRead(d *schema.ResourceData, m interface{}) error {
mapping["circuit_count"] = v.CircuitCount
mapping["cluster_count"] = v.ClusterCount

mapping["tenant_group"] = flattenTentantGroup(v.Group)
twink0r marked this conversation as resolved.
Show resolved Hide resolved
s = append(s, mapping)
}

d.SetId(resource.UniqueId())
return d.Set("tenants", s)

}

func flattenTentantGroup(group *models.NestedTenantGroup) []map[string]interface{} {
twink0r marked this conversation as resolved.
Show resolved Hide resolved
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["tentant_count"] = group.TenantCount
twink0r marked this conversation as resolved.
Show resolved Hide resolved
s = append(s, mapping)
}
return s
}