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 Long Term Retention Policies details in table azure_sql_database closes #219 #255

Merged
merged 9 commits into from
Aug 12, 2021
Merged
4 changes: 1 addition & 3 deletions azure-test/tests/azure_sql_database/test-get-query.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
select name, id, server_name, status, type, containment_state, default_secondary_location, earliest_restore_date, edition, elastic_pool_name, location,max_size_bytes, zone_redundant, requested_service_objective_name, service_level_objective,transparent_data_encryption, title, tags, akas, region, resource_group, subscription_id
from
azure.azure_sql_database
where
name = '{{ resourceName }}'
and resource_group = '{{ resourceName }}';
where name = '{{ resourceName }}' and resource_group = '{{ resourceName }}';
76 changes: 72 additions & 4 deletions azure/table_azure_sql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"strings"

sqlV5 "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v5.0/sql"
"github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2017-03-01-preview/sql"
"github.com/turbot/steampipe-plugin-sdk/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/plugin/transform"
Expand Down Expand Up @@ -148,6 +149,33 @@ func tableAzureSqlDatabase(_ context.Context) *plugin.Table {
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromField("DatabaseProperties.RestorePointInTime").Transform(convertDateToTime),
},
{
Name: "requested_service_objective_name",
Description: "The name of the configured service level objective of the database.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DatabaseProperties.RequestedServiceObjectiveName"),
},
{
Name: "retention_policy_id",
Description: "Retention policy ID.",
Type: proto.ColumnType_STRING,
Hydrate: getSqlDatabaseLongTermRetentionPolicies,
Transform: transform.FromField("ID"),
},
{
Name: "retention_policy_name",
Description: "Retention policy Name.",
Type: proto.ColumnType_STRING,
Hydrate: getSqlDatabaseLongTermRetentionPolicies,
Transform: transform.FromField("Name"),
},
{
Name: "retention_policy_type",
Description: "Long term Retention policy Type.",
Type: proto.ColumnType_STRING,
Hydrate: getSqlDatabaseLongTermRetentionPolicies,
Transform: transform.FromField("Type"),
},
{
Name: "source_database_deletion_date",
Description: "Specifies the time that the database was deleted when createMode is Restore and sourceDatabaseId is the deleted database's original resource id.",
Expand Down Expand Up @@ -185,10 +213,11 @@ func tableAzureSqlDatabase(_ context.Context) *plugin.Table {
Transform: transform.FromField("DatabaseProperties.RecommendedIndex"),
},
{
Name: "requested_service_objective_name",
Description: "The name of the configured service level objective of the database.",
Name: "retention_policy_property",
Description: "Long term Retention policy Property.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("DatabaseProperties.RequestedServiceObjectiveName"),
Hydrate: getSqlDatabaseLongTermRetentionPolicies,
Transform: transform.FromField("BaseLongTermRetentionPolicyProperties"),
},
{
Name: "sample_name",
Expand Down Expand Up @@ -216,7 +245,7 @@ func tableAzureSqlDatabase(_ context.Context) *plugin.Table {
Transform: transform.FromField("TransparentDataEncryptionProperties"),
},

// Standard columns
// Azure standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Expand Down Expand Up @@ -362,6 +391,45 @@ func getSqlDatabaseTransparentDataEncryption(ctx context.Context, d *plugin.Quer
return nil, nil
}

func getSqlDatabaseLongTermRetentionPolicies(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
var serverName, databaseName, resourceGroupName string
plugin.Logger(ctx).Trace("Hydrate Database Propery", h.Item)
if h.Item != nil {
database := h.Item.(sql.Database)
serverName = strings.Split(*database.ID, "/")[8]
databaseName = *database.Name
resourceGroupName = strings.Split(string(*database.ID), "/")[4]
} else {
serverName = d.KeyColumnQuals["server_name"].GetStringValue()
databaseName = d.KeyColumnQuals["name"].GetStringValue()
resourceGroupName = d.KeyColumnQuals["resource_group"].GetStringValue()
}

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

client := sqlV5.NewLongTermRetentionPoliciesClient(subscriptionID)
client.Authorizer = session.Authorizer

op, err := client.ListByDatabase(ctx, resourceGroupName, serverName, databaseName)
if err != nil {
return nil, err
}

// We can add only one retention policy per SQL Database.
res := op.Values()

// For master database we are getting the response as empty array
if len(res) == 0 {
return nil, nil
}

return res[0], nil
}

//// TRANSFORM FUNCTION

func idToServerName(ctx context.Context, d *transform.TransformData) (interface{}, error) {
Expand Down