diff --git a/azure/table_azure_mysql_server.go b/azure/table_azure_mysql_server.go index 231400e1..9aa6b88e 100644 --- a/azure/table_azure_mysql_server.go +++ b/azure/table_azure_mysql_server.go @@ -2,6 +2,7 @@ package azure import ( "context" + "strings" "github.com/turbot/steampipe-plugin-sdk/grpc/proto" "github.com/turbot/steampipe-plugin-sdk/plugin" @@ -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 { @@ -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") @@ -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 @@ -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 +} diff --git a/docs/tables/azure_mysql_server.md b/docs/tables/azure_mysql_server.md index 8018f14d..83f8b788 100644 --- a/docs/tables/azure_mysql_server.md +++ b/docs/tables/azure_mysql_server.md @@ -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; +```