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 table azure_tenant closes #140 #142

Merged
merged 3 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Empty file.
7 changes: 7 additions & 0 deletions azure-test/tests/azure_tenant/test-list-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"display_name": null,
"name": "{{ output.current_tenant_display_name.value }}",
"tenant_id": "{{ output.tenant_id.value }}"
}
]
3 changes: 3 additions & 0 deletions azure-test/tests/azure_tenant/test-list-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, display_name, tenant_id
from azure.azure_tenant
where tenant_id = '{{ output.tenant_id.value }}'
1 change: 1 addition & 0 deletions azure-test/tests/azure_tenant/test-notfound-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
3 changes: 3 additions & 0 deletions azure-test/tests/azure_tenant/test-notfound-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select display_name, title
from azure.azure_tenant
where display_name = 'dummy-{{ resourceName }}';
6 changes: 6 additions & 0 deletions azure-test/tests/azure_tenant/test-turbot-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"name": "{{ output.current_tenant_display_name.value }}",
"title": "{{ output.current_tenant_display_name.value }}"
}
]
3 changes: 3 additions & 0 deletions azure-test/tests/azure_tenant/test-turbot-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, title
from azure.azure_tenant
where tenant_id = '{{ output.tenant_id.value }}';
46 changes: 46 additions & 0 deletions azure-test/tests/azure_tenant/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
variable "resource_name" {
type = string
default = "turbot-test-20200125-create-update"
description = "Name of the resource used throughout the test."
}

variable "azure_environment" {
type = string
default = "public"
description = "Azure environment used for the test."
}

variable "azure_subscription" {
type = string
default = "3510ae4d-530b-497d-8f30-53b9616fc6c1"
description = "Azure environment used for the test."
}

variable "azure_tenant" {
type = string
default = "cdffd708-7da0-4cea-abeb-0a4c334d7f64"
description = "Azure environment used for the test."
}

provider "azuread" {
# Cannot be passed as a variable
# version = "=0.10.0"
environment = var.azure_environment
subscription_id = var.azure_subscription
tenant_id = var.azure_tenant
}

data "azurerm_client_config" "current" {
}

provider "azurerm" {
features {}
}

output "current_tenant_display_name" {
value = data.azurerm_client_config.current.tenant_id
}

output "tenant_id" {
value = data.azurerm_client_config.current.tenant_id
}
1 change: 1 addition & 0 deletions azure/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"azure_storage_table_service": tableAzureStorageTableService(ctx),
"azure_subnet": tableAzureSubnet(ctx),
"azure_subscription": tableAzureSubscription(ctx),
"azure_tenant": tableAzureTenant(ctx),
"azure_virtual_network": tableAzureVirtualNetwork(ctx),
// "azure_storage_blob": tableAzureStorageBlob(ctx),
// "azure_storage_table": tableAzureStorageTable(ctx),
Expand Down
115 changes: 115 additions & 0 deletions azure/table_azure_tenant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package azure

import (
"context"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-06-01/subscriptions"
"github.com/turbot/steampipe-plugin-sdk/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/plugin"
"github.com/turbot/steampipe-plugin-sdk/plugin/transform"
)

//// TABLE DEFINITION

func tableAzureTenant(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "azure_tenant",
Description: "Azure Tenant",
List: &plugin.ListConfig{
Hydrate: listTenants,
},
Columns: []*plugin.Column{
{
Name: "name",
Type: proto.ColumnType_STRING,
Description: "The display name of the tenant.",
Transform: transform.From(getNameOrID),
},
{
Name: "id",
Type: proto.ColumnType_STRING,
Description: "The fully qualified ID of the tenant. For example, /tenants/00000000-0000-0000-0000-000000000000.",
Transform: transform.FromGo(),
},
{
Name: "tenant_id",
Type: proto.ColumnType_STRING,
Description: "The tenant ID. For example, 00000000-0000-0000-0000-000000000000.",
Transform: transform.FromField("TenantID"),
},
{
Name: "tenant_category",
Type: proto.ColumnType_STRING,
Description: "The tenant category. Possible values include: 'Home', 'ProjectedBy', 'ManagedBy'",
Transform: transform.FromField("TenantCategory").Transform(transform.ToString),
},
{
Name: "country",
Type: proto.ColumnType_STRING,
Description: "Country/region name of the address for the tenant.",
},
{
Name: "country_code",
Type: proto.ColumnType_STRING,
Description: "Country/region abbreviation for the tenant.",
},
{
Name: "display_name",
Type: proto.ColumnType_STRING,
Description: "The list of domains for the tenant.",
},
{
Name: "domains",
Type: proto.ColumnType_JSON,
Description: "The list of domains for the tenant.",
},

// Standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.From(getNameOrID),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.FromField("ID").Transform(idToAkas),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 ["azure:///tenants/cdffd708-7da0-4cea-abeb-0a4c334d7f64","azure:///tenants/cdffd708-7da0-4cea-abeb-0a4c334d7f64"]

both values are same - can we keep only one of it for this table

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
},
}
}

//// LIST FUNCTION

func listTenants(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}

client := subscriptions.NewTenantsClient()
client.Authorizer = session.Authorizer

op, err := client.List(ctx)
if err != nil {
return nil, err
}

for _, resp := range op.Values() {
d.StreamListItem(ctx, resp)
}

return nil, nil
}

//// TRANSFORM FUNCTION

func getNameOrID(ctx context.Context, d *transform.TransformData) (interface{}, error) {
data := d.HydrateItem.(subscriptions.TenantIDDescription)
if data.DisplayName != nil {
return data.DisplayName, nil
}
return data.TenantID, nil
}
9 changes: 9 additions & 0 deletions azure/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ func idToSubscriptionID(ctx context.Context, d *transform.TransformData) (interf
func idToAkas(ctx context.Context, d *transform.TransformData) (interface{}, error) {
id := types.SafeString(d.Value)
akas := []string{"azure://" + id, "azure://" + strings.ToLower(id)}
occured := map[string]bool{}
result := []string{}
for i := range akas {
if !occured[akas[i]] {
occured[akas[i]] = true
result = append(result, akas[i])
}
}
akas = result
return akas, nil
}

Expand Down
21 changes: 21 additions & 0 deletions docs/tables/azure_tenant.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Table: azure_tenant

A dedicated and trusted instance of Azure AD that's automatically created when your organization signs up for a Microsoft cloud service subscription, such as Microsoft Azure, Microsoft Intune, or Microsoft 365. An Azure tenant represents a single organization.

## Examples

### Basic info

```sql
select
name,
id,
tenant_id,
tenant_category,
country,
country_code,
display_name,
domains
from
azure_tenant;
```