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

Added column environment_name in All tables. #424

Merged
merged 5 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
72 changes: 72 additions & 0 deletions azure/common_columns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package azure

import (
"context"

"github.com/turbot/steampipe-plugin-sdk/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/plugin"
"github.com/turbot/steampipe-plugin-sdk/plugin/transform"
)

// column definitions for the common columns
func commonColumns() []*plugin.Column {
return []*plugin.Column{
{
Name: "environment_name",
Type: proto.ColumnType_STRING,
Hydrate: getEnvironmentName,
Description: ColumnDescriptionEnvironmentName,
Transform: transform.FromValue(),
},
{
Name: "subscription_id",
Type: proto.ColumnType_STRING,
Hydrate: getSubscriptionID,
Description: ColumnDescriptionSubscription,
Transform: transform.FromValue(),
},
}
}

// append the common azure columns onto the column list
func azureColumns(columns []*plugin.Column) []*plugin.Column {
return append(columns, commonColumns()...)
}

func getSubscriptionID(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getSubscriptionID")
cacheKey := "getSubscriptionID"

if cachedData, ok := d.ConnectionManager.Cache.Get(cacheKey); ok {
return cachedData.(string), nil
}

session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}

// cache subscription id for the session
d.ConnectionManager.Cache.Set(cacheKey, session.SubscriptionID)

return session.SubscriptionID, nil
}

func getEnvironmentName(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getEnvironmentName")
cacheKey := "getEnvironmentName"

if cachedData, ok := d.ConnectionManager.Cache.Get(cacheKey); ok {
return cachedData.(string), nil
}

session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}

// cache environment name for the session
d.ConnectionManager.Cache.Set(cacheKey, session.EnvironmentName)

return session.EnvironmentName, nil
}
10 changes: 9 additions & 1 deletion azure/monitoring_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ func commonMonitoringMetricColumns() []*plugin.Column {
Description: "The units in which the metric value is reported.",
Type: proto.ColumnType_STRING,
},
{
Name: "environment_name",
Description: ColumnDescriptionEnvironmentName,
Type: proto.ColumnType_STRING,
Hydrate: plugin.HydrateFunc(getEnvironmentName).WithCache(),
Transform: transform.FromValue(),
},
{
Name: "resource_group",
Description: ColumnDescriptionResourceGroup,
Expand All @@ -88,7 +95,8 @@ func commonMonitoringMetricColumns() []*plugin.Column {
Name: "subscription_id",
Description: ColumnDescriptionSubscription,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DimensionValue").Transform(idToSubscriptionID),
Hydrate: plugin.HydrateFunc(getSubscriptionID).WithCache(),
Transform: transform.FromValue(),
},
}
}
Expand Down
35 changes: 9 additions & 26 deletions azure/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import (

// Session info
type Session struct {
SubscriptionID string
TenantID string
Authorizer autorest.Authorizer
EnvironmentName string
Expires *time.Time
GraphEndpoint string
ResourceManagerEndpoint string
StorageEndpointSuffix string
GraphEndpoint string
SubscriptionID string
TenantID string
}

/* GetNewSession creates an session configured from (~/.steampipe/config, environment variables and CLI) in the order:
Expand Down Expand Up @@ -150,7 +151,7 @@ func GetNewSession(ctx context.Context, d *plugin.QueryData, tokenAudience strin
return nil, err
}

// Get the subscription ID and tenant ID for "GRAPH" token audience
// Get the subscription ID and tenant ID for "GRAPH" token audience
case "CLI":
authorizer, err = auth.NewAuthorizerFromCLIWithResource(resource)
if err != nil {
Expand Down Expand Up @@ -200,13 +201,14 @@ func GetNewSession(ctx context.Context, d *plugin.QueryData, tokenAudience strin
}

sess := &Session{
SubscriptionID: subscriptionID,
Authorizer: authorizer,
TenantID: tenantID,
EnvironmentName: settings.Environment.Name,
Expires: &expiresOn,
GraphEndpoint: settings.Environment.GraphEndpoint,
ResourceManagerEndpoint: settings.Environment.ResourceManagerEndpoint,
StorageEndpointSuffix: settings.Environment.StorageEndpointSuffix,
GraphEndpoint: settings.Environment.GraphEndpoint,
SubscriptionID: subscriptionID,
TenantID: tenantID,
}

if sess.Expires != nil {
Expand Down Expand Up @@ -335,22 +337,3 @@ func getSubscriptionFromCLI(resource string) (*subscription, error) {
func WillExpireIn(t time.Time, d time.Duration) bool {
return !t.After(time.Now().Add(d))
}

func getSubscriptionID(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getSubscriptionID")
cacheKey := "getSubscriptionID"

if cachedData, ok := d.ConnectionManager.Cache.Get(cacheKey); ok {
return cachedData.(string), nil
}

session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}

// cache tenant id for the session
d.ConnectionManager.Cache.Set(cacheKey, session.SubscriptionID)

return session.SubscriptionID, nil
}
2 changes: 1 addition & 1 deletion azure/table_azure_ad_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func tableAzureAdGroup(_ context.Context) *plugin.Table {
Type: proto.ColumnType_JSON,
},

// Standard columns
// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Expand Down
2 changes: 1 addition & 1 deletion azure/table_azure_ad_service_principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func tableAzureAdServicePrincipal(_ context.Context) *plugin.Table {
Type: proto.ColumnType_JSON,
},

// Standard columns
// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Expand Down
2 changes: 1 addition & 1 deletion azure/table_azure_ad_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func tableAzureAdUser(_ context.Context) *plugin.Table {
Type: proto.ColumnType_JSON,
},

// Standard columns
// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Expand Down
10 changes: 2 additions & 8 deletions azure/table_azure_api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func tableAzureAPIManagement(_ context.Context) *plugin.Table {
List: &plugin.ListConfig{
Hydrate: listAPIManagements,
},
Columns: []*plugin.Column{
Columns: azureColumns([]*plugin.Column{
{
Name: "name",
Type: proto.ColumnType_STRING,
Expand Down Expand Up @@ -287,13 +287,7 @@ func tableAzureAPIManagement(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(extractResourceGroupFromID),
},
{
Name: "subscription_id",
Description: ColumnDescriptionSubscription,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(idToSubscriptionID),
},
},
}),
}
}

Expand Down
10 changes: 2 additions & 8 deletions azure/table_azure_app_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func tableAzureAppConfiguration(_ context.Context) *plugin.Table {
List: &plugin.ListConfig{
Hydrate: listAppConfigurations,
},
Columns: []*plugin.Column{
Columns: azureColumns([]*plugin.Column{
{
Name: "name",
Description: "The name of the resource.",
Expand Down Expand Up @@ -131,13 +131,7 @@ func tableAzureAppConfiguration(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(extractResourceGroupFromID),
},
{
Name: "subscription_id",
Description: ColumnDescriptionSubscription,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(idToSubscriptionID),
},
},
}),
}
}

Expand Down
14 changes: 5 additions & 9 deletions azure/table_azure_app_service_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func tableAzureAppServiceEnvironment(_ context.Context) *plugin.Table {
List: &plugin.ListConfig{
Hydrate: listAppServiceEnvironments,
},
Columns: []*plugin.Column{
Columns: azureColumns([]*plugin.Column{
{
Name: "name",
Type: proto.ColumnType_STRING,
Expand Down Expand Up @@ -129,7 +129,7 @@ func tableAzureAppServiceEnvironment(_ context.Context) *plugin.Table {
Transform: transform.FromField("AppServiceEnvironment.ClusterSettings"),
},

// Standard columns
// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Expand All @@ -147,6 +147,8 @@ func tableAzureAppServiceEnvironment(_ context.Context) *plugin.Table {
Type: proto.ColumnType_JSON,
Transform: transform.FromField("ID").Transform(idToAkas),
},

// Azure standard columns
{
Name: "region",
Description: ColumnDescriptionRegion,
Expand All @@ -159,13 +161,7 @@ func tableAzureAppServiceEnvironment(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("AppServiceEnvironment.ResourceGroup").Transform(toLower),
},
{
Name: "subscription_id",
Description: ColumnDescriptionSubscription,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("AppServiceEnvironment.SubscriptionID"),
},
},
}),
}
}

Expand Down
10 changes: 2 additions & 8 deletions azure/table_azure_app_service_function_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func tableAzureAppServiceFunctionApp(_ context.Context) *plugin.Table {
List: &plugin.ListConfig{
Hydrate: listAppServiceFunctionApps,
},
Columns: []*plugin.Column{
Columns: azureColumns([]*plugin.Column{
{
Name: "name",
Description: "The friendly name that identifies the app service function app.",
Expand Down Expand Up @@ -166,13 +166,7 @@ func tableAzureAppServiceFunctionApp(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("SiteProperties.ResourceGroup").Transform(toLower),
},
{
Name: "subscription_id",
Description: ColumnDescriptionSubscription,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(idToSubscriptionID),
},
},
}),
}
}

Expand Down
14 changes: 5 additions & 9 deletions azure/table_azure_app_service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func tableAzureAppServicePlan(_ context.Context) *plugin.Table {
List: &plugin.ListConfig{
Hydrate: listAppServicePlans,
},
Columns: []*plugin.Column{
Columns: azureColumns([]*plugin.Column{
{
Name: "name",
Type: proto.ColumnType_STRING,
Expand Down Expand Up @@ -137,7 +137,7 @@ func tableAzureAppServicePlan(_ context.Context) *plugin.Table {
Transform: transform.FromField("AppServicePlanProperties.Status").Transform(transform.ToString),
},

// Standard columns
// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Expand All @@ -155,6 +155,8 @@ func tableAzureAppServicePlan(_ context.Context) *plugin.Table {
Type: proto.ColumnType_JSON,
Transform: transform.FromField("ID").Transform(idToAkas),
},

// Azure standard columns
{
Name: "region",
Description: ColumnDescriptionRegion,
Expand All @@ -167,13 +169,7 @@ func tableAzureAppServicePlan(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("AppServicePlanProperties.ResourceGroup").Transform(toLower),
},
{
Name: "subscription_id",
Description: ColumnDescriptionSubscription,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("AppServicePlanProperties.Subscription"),
},
},
}),
}
}

Expand Down
10 changes: 2 additions & 8 deletions azure/table_azure_app_service_web_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func tableAzureAppServiceWebApp(_ context.Context) *plugin.Table {
Depends: []plugin.HydrateFunc{getAppServiceWebAppSiteConfiguration},
},
},
Columns: []*plugin.Column{
Columns: azureColumns([]*plugin.Column{
{
Name: "name",
Description: "The friendly name that identifies the app service web app.",
Expand Down Expand Up @@ -185,13 +185,7 @@ func tableAzureAppServiceWebApp(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("SiteProperties.ResourceGroup").Transform(toLower),
},
{
Name: "subscription_id",
Description: ColumnDescriptionSubscription,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(idToSubscriptionID),
},
},
}),
}
}

Expand Down
10 changes: 2 additions & 8 deletions azure/table_azure_application_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func tableAzureApplicationGateway(_ context.Context) *plugin.Table {
List: &plugin.ListConfig{
Hydrate: listApplicationGateways,
},
Columns: []*plugin.Column{
Columns: azureColumns([]*plugin.Column{
{
Name: "name",
Description: "The resource name.",
Expand Down Expand Up @@ -285,13 +285,7 @@ func tableAzureApplicationGateway(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(extractResourceGroupFromID),
},
{
Name: "subscription_id",
Description: ColumnDescriptionSubscription,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(idToSubscriptionID),
},
},
}),
}
}

Expand Down
Loading