Skip to content

Commit

Permalink
Add server_keys column in table azure_mysql_server. Closes #305 (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
c0d3r-arnab authored Sep 28, 2021
1 parent cbafd08 commit cc017c1
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
81 changes: 80 additions & 1 deletion azure/table_azure_mysql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azure

import (
"context"
"strings"

"github.com/turbot/steampipe-plugin-sdk/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/plugin"
Expand Down Expand Up @@ -190,6 +191,13 @@ func tableAzureMySQLServer(_ context.Context) *plugin.Table {
Type: proto.ColumnType_JSON,
Transform: transform.From(extractMySQLServerPrivateEndpointConnections),
},
{
Name: "server_keys",
Description: "The server keys of the server.",
Type: proto.ColumnType_JSON,
Hydrate: listMySQLServersServerKeys,
Transform: transform.FromValue(),
},

// Steampipe standard columns
{
Expand Down Expand Up @@ -259,7 +267,7 @@ func listMySQLServers(ctx context.Context, d *plugin.QueryData, _ *plugin.Hydrat
return nil, err
}

//// HYDRATE FUNCTION
//// HYDRATE FUNCTIONS

func getMySQLServer(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getMySQLServer")
Expand Down Expand Up @@ -297,6 +305,48 @@ func getMySQLServer(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateD
return nil, nil
}

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

namespace := h.Item.(mysql.Server)
resourceGroup := strings.Split(string(*namespace.ID), "/")[4]
serverName := *namespace.Name

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

client := mysql.NewServerKeysClient(subscriptionID)
client.Authorizer = session.Authorizer

op, err := client.List(ctx, resourceGroup, serverName)
if err != nil {
plugin.Logger(ctx).Error("listMySQLServersServerKeys", "list", err)
return nil, err
}

var mySQLServersServerKeys []map[string]interface{}

for _, i := range op.Values() {
mySQLServersServerKeys = append(mySQLServersServerKeys, extractMySQLServersServerKey(i))
}

for op.NotDone() {
err = op.NextWithContext(ctx)
if err != nil {
plugin.Logger(ctx).Error("listMySQLServersServerKeys", "list_paging", err)
return nil, err
}
for _, i := range op.Values() {
mySQLServersServerKeys = append(mySQLServersServerKeys, extractMySQLServersServerKey(i))
}
}

return mySQLServersServerKeys, nil
}

//// TRANSFORM FUNCTION

// If we return the API response directly, the output will not provide the properties of PrivateEndpointConnections
Expand Down Expand Up @@ -335,3 +385,32 @@ func extractMySQLServerPrivateEndpointConnections(ctx context.Context, d *transf

return properties, nil
}

// If we return the API response directly, the output will not provide the properties of ServerKeys
func extractMySQLServersServerKey(i mysql.ServerKey) map[string]interface{} {
mySQLServersServerKey := make(map[string]interface{})
if i.ID != nil {
mySQLServersServerKey["id"] = *i.ID
}
if i.Name != nil {
mySQLServersServerKey["name"] = *i.Name
}
if i.Type != nil {
mySQLServersServerKey["type"] = *i.Type
}
if i.Type != nil {
mySQLServersServerKey["kind"] = *i.Kind
}
if i.ServerKeyProperties != nil {
if i.ServerKeyProperties.ServerKeyType != nil {
mySQLServersServerKey["serverKeyType"] = i.ServerKeyProperties.ServerKeyType
}
if i.ServerKeyProperties.URI != nil {
mySQLServersServerKey["uri"] = i.ServerKeyProperties.URI
}
if i.ServerKeyProperties.CreationDate != nil {
mySQLServersServerKey["creationDate"] = i.ServerKeyProperties.CreationDate
}
}
return mySQLServersServerKey
}
18 changes: 18 additions & 0 deletions docs/tables/azure_mysql_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,21 @@ from
azure_mysql_server,
jsonb_array_elements(private_endpoint_connections) as connections;
```

### List server keys

```sql
select
name as server_name,
id as server_id,
keys ->> 'creationDate' as keys_creation_date,
keys ->> 'id' as keys_id,
keys ->> 'kind' as keys_kind,
keys ->> 'name' as keys_name,
keys ->> 'serverKeyType' as keys_server_key_type,
keys ->> 'type' as keys_type,
keys ->> 'uri' as keys_uri
from
azure_mysql_server,
jsonb_array_elements(server_keys) as keys;
```

0 comments on commit cc017c1

Please sign in to comment.