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 server keys details in table azure_mysql_server. Closes #305 #337

Merged
merged 4 commits into from
Sep 28, 2021
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
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;
```