Skip to content

Commit

Permalink
Add column backup_policy to table azure_cosmosdb_account. Closes #…
Browse files Browse the repository at this point in the history
…584 (#585)

Co-authored-by: Ved misra <[email protected]>
  • Loading branch information
karanpopat and misraved authored Mar 24, 2023
1 parent 818c843 commit ff38265
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 11 deletions.
60 changes: 58 additions & 2 deletions azure/table_azure_cosmosdb_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"strings"

"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2020-04-01-preview/documentdb"
"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2021-04-01-preview/documentdb"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"

Expand Down Expand Up @@ -166,6 +166,12 @@ func tableAzureCosmosDBAccount(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DatabaseAccount.DatabaseAccountGetProperties.APIProperties.ServerVersion").Transform(transform.ToString),
},
{
Name: "backup_policy",
Description: "The object representing the policy for taking backups on an account.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("DatabaseAccount.DatabaseAccountGetProperties.BackupPolicy"),
},
{
Name: "capabilities",
Description: "A list of Cosmos DB capabilities for the account.",
Expand Down Expand Up @@ -200,7 +206,7 @@ func tableAzureCosmosDBAccount(_ context.Context) *plugin.Table {
Name: "private_endpoint_connections",
Description: "A list of Private Endpoint Connections configured for the Cosmos DB account.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("DatabaseAccount.DatabaseAccountGetProperties.PrivateEndpointConnections"),
Transform: transform.From(cosmosDBPrivateEndpointConnectionMap),
},
{
Name: "read_locations",
Expand Down Expand Up @@ -323,3 +329,53 @@ func extractCosmosDBVirtualNetworkRule(ctx context.Context, d *transform.Transfo
}
return nil, nil
}

// If we return the API response directly, the output will not give
// all the contents of PrivateEndpointConnection
func cosmosDBPrivateEndpointConnectionMap(ctx context.Context, d *transform.TransformData) (interface{}, error) {
info := d.HydrateItem.(databaseAccountInfo)
conns := info.DatabaseAccount.PrivateEndpointConnections

if len(*conns) == 0 {
return nil, nil
}

var privateEndpointConnections []PrivateConnectionInfo

for _, conn := range *conns {
var connection PrivateConnectionInfo
if conn.ID != nil {
connection.PrivateEndpointConnectionId = string(*conn.ID)
}
if conn.Name != nil {
connection.PrivateEndpointConnectionName = string(*conn.Name)
}
if conn.Type != nil {
connection.PrivateEndpointConnectionType = string(*conn.Type)
}
if conn.PrivateEndpointConnectionProperties != nil {
if conn.PrivateEndpoint != nil {
if conn.PrivateEndpoint.ID != nil {
connection.PrivateEndpointId = string(*conn.PrivateEndpoint.ID)
}
}
if conn.PrivateLinkServiceConnectionState != nil {
if conn.PrivateLinkServiceConnectionState.ActionsRequired != nil {
connection.PrivateLinkServiceConnectionStateActionsRequired = string(*conn.PrivateLinkServiceConnectionState.ActionsRequired)
}
if conn.PrivateLinkServiceConnectionState.Status != nil {
connection.PrivateLinkServiceConnectionStateStatus = string(*conn.PrivateLinkServiceConnectionState.Status)
}
if conn.PrivateLinkServiceConnectionState.Description != nil {
connection.PrivateLinkServiceConnectionStateDescription = string(*conn.PrivateLinkServiceConnectionState.Description)
}
}
if conn.ProvisioningState != nil {
connection.ProvisioningState = string(*conn.ProvisioningState)
}
}
privateEndpointConnections = append(privateEndpointConnections, connection)
}

return privateEndpointConnections, nil
}
48 changes: 39 additions & 9 deletions docs/tables/azure_cosmosdb_account.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,41 @@ Azure Cosmos DB is a fully managed NoSQL database service for modern app develop
```sql
select
name,
location,
region,
enable_automatic_failover,
resource_group
from
azure_cosmosdb_account;
azure_cosmosdb_account
where
not enable_automatic_failover;
```


### List of database accounts which allows traffic from all networks, including the public Internet.

```sql
select
name,
location,
region,
virtual_network_rules
from
azure_cosmosdb_account
where
virtual_network_rules = '[]';
```


### List of database accounts where multiple write location is not enabled

```sql
select
name,
location,
region,
enable_multiple_write_locations
from
azure_cosmosdb_account
where
not enable_multiple_write_locations;
```


### Failover policy info for the database accounts

```sql
Expand All @@ -57,7 +56,6 @@ from
cross join jsonb_array_elements(failover_policies) as fp;
```


### Consistency policy info for each account

```sql
Expand All @@ -69,4 +67,36 @@ select
default_consistency_level
from
azure_cosmosdb_account;
```
```

### Get backup policy for accounts having periodic backups enabled

```sql
select
name,
region,
backup_policy -> 'periodicModeProperties' ->> 'backupIntervalInMinutes' as backup_interval_mins,
backup_policy -> 'periodicModeProperties' ->> 'backupRetentionIntervalInHours' as backup_retention_interval_hrs,
backup_policy -> 'periodicModeProperties' ->> 'backupStorageRedundancy' as backup_storage_redundancy
from
azure_cosmosdb_account
where
backup_policy ->> 'type' = 'Periodic';
```

### Get private endpoint connection details for each account

```sql
select
c ->> 'PrivateEndpointConnectionName' as private_endpoint_connection_name,
c ->> 'PrivateEndpointConnectionType' as private_endpoint_connection_type,
c ->> 'PrivateEndpointId' as private_endpoint_id,
c ->> 'PrivateLinkServiceConnectionStateActionsRequired' as private_link_service_connection_state_actions_required,
c ->> 'PrivateLinkServiceConnectionStateDescription' as private_link_service_connection_state_description,
c ->> 'PrivateLinkServiceConnectionStateStatus' as private_link_service_connection_state_status,
c ->> 'ProvisioningState' as provisioning_state,
c ->> 'PrivateEndpointConnectionId' as private_endpoint_connection_id
from
azure_cosmosdb_account,
jsonb_array_elements(private_endpoint_connections) as c;
```

0 comments on commit ff38265

Please sign in to comment.