From 7f2bacaf5b8d7ae321315ca735c90ead1590f118 Mon Sep 17 00:00:00 2001 From: Arnab Ghosh Date: Thu, 23 Sep 2021 20:51:37 +0530 Subject: [PATCH 1/3] Add server keys details in table azure_mysql_server --- .../azure_mysql_server/test-get-expected.json | 6 +- .../tests/azure_mysql_server/variables.tf | 22 ++-- azure/table_azure_mysql_server.go | 101 ++++++++++++++++++ docs/tables/azure_mysql_server.md | 18 ++++ 4 files changed, 134 insertions(+), 13 deletions(-) diff --git a/azure-test/tests/azure_mysql_server/test-get-expected.json b/azure-test/tests/azure_mysql_server/test-get-expected.json index d6ad306e..611380a9 100644 --- a/azure-test/tests/azure_mysql_server/test-get-expected.json +++ b/azure-test/tests/azure_mysql_server/test-get-expected.json @@ -11,15 +11,15 @@ "public_network_access": "Enabled", "region": "eastus", "resource_group": "{{ resourceName }}", - "sku_capacity": 1, + "sku_capacity": 2, "sku_family": "Gen5", - "sku_name": "B_Gen5_1", + "sku_name": "B_Gen5_2", "sku_tier": "Basic", "ssl_enforcement": "Enabled", "storage_auto_grow": "Disabled", "storage_mb": 5120, "subscription_id": "{{ output.subscription_id.value }}", "type": "Microsoft.DBforMySQL/servers", - "version": "5.6" + "version": "5.7" } ] diff --git a/azure-test/tests/azure_mysql_server/variables.tf b/azure-test/tests/azure_mysql_server/variables.tf index f48cad33..6b897b99 100644 --- a/azure-test/tests/azure_mysql_server/variables.tf +++ b/azure-test/tests/azure_mysql_server/variables.tf @@ -17,20 +17,22 @@ variable "azure_subscription" { description = "Azure subscription used for the test." } +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "=2.73.0" + } + } +} + +# Configure the Microsoft Azure Provider provider "azurerm" { features {} environment = var.azure_environment subscription_id = var.azure_subscription } -data "azurerm_client_config" "current" {} - -data "null_data_source" "resource" { - inputs = { - scope = "azure:///subscriptions/${data.azurerm_client_config.current.subscription_id}" - } -} - resource "azurerm_resource_group" "named_test_resource" { name = var.resource_name location = "East US" @@ -44,9 +46,9 @@ resource "azurerm_mysql_server" "named_test_resource" { administrator_login = "mradministrator" administrator_login_password = "H@Sh1CoR3!" - sku_name = "B_Gen5_1" + sku_name = "B_Gen5_2" storage_mb = 5120 - version = "5.6" + version = "5.7" auto_grow_enabled = false backup_retention_days = 7 diff --git a/azure/table_azure_mysql_server.go b/azure/table_azure_mysql_server.go index 68cf68fe..c792f5fa 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.FromField("ServerProperties.PrivateEndpointConnections"), }, + { + Name: "server_keys", + Description: "The server keys of the server.", + Type: proto.ColumnType_JSON, + Hydrate: listMySQLServersServerKeys, + Transform: transform.FromValue(), + }, // Steampipe standard columns { @@ -294,3 +302,96 @@ func getMySQLServer(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateD return nil, nil } + +// If we return the API response directly, the output will not provide the properties of ServerKeys +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() { + 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 + } + } + + mySQLServersServerKeys = append(mySQLServersServerKeys, mySQLServersServerKey) + } + + 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() { + 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 + } + } + + mySQLServersServerKeys = append(mySQLServersServerKeys, mySQLServersServerKey) + } + } + + return mySQLServersServerKeys, nil +} diff --git a/docs/tables/azure_mysql_server.md b/docs/tables/azure_mysql_server.md index af58bedd..74bbadca 100644 --- a/docs/tables/azure_mysql_server.md +++ b/docs/tables/azure_mysql_server.md @@ -83,3 +83,21 @@ where minimal_tls_version = 'TLS1_0' or minimal_tls_version = 'TLS1_1'; ``` + +### 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; +``` From 9d63f1a2944aa3e3dc05a43f9ab437be4beae0eb Mon Sep 17 00:00:00 2001 From: Arnab Ghosh Date: Tue, 28 Sep 2021 15:39:57 +0530 Subject: [PATCH 2/3] refactor server key get code --- azure/table_azure_mysql_server.go | 84 ++++++++++++------------------- 1 file changed, 31 insertions(+), 53 deletions(-) diff --git a/azure/table_azure_mysql_server.go b/azure/table_azure_mysql_server.go index df8dead3..9bbcb9ae 100644 --- a/azure/table_azure_mysql_server.go +++ b/azure/table_azure_mysql_server.go @@ -305,7 +305,6 @@ func getMySQLServer(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateD return nil, nil } -// If we return the API response directly, the output will not provide the properties of ServerKeys func listMySQLServersServerKeys(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { plugin.Logger(ctx).Trace("listMySQLServersServerKeys") @@ -331,32 +330,7 @@ func listMySQLServersServerKeys(ctx context.Context, d *plugin.QueryData, h *plu var mySQLServersServerKeys []map[string]interface{} for _, i := range op.Values() { - 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 - } - } - - mySQLServersServerKeys = append(mySQLServersServerKeys, mySQLServersServerKey) + mySQLServersServerKeys = append(mySQLServersServerKeys, extractMySQLServersServerKeys(i)) } for op.NotDone() { @@ -366,32 +340,7 @@ func listMySQLServersServerKeys(ctx context.Context, d *plugin.QueryData, h *plu return nil, err } for _, i := range op.Values() { - 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 - } - } - - mySQLServersServerKeys = append(mySQLServersServerKeys, mySQLServersServerKey) + mySQLServersServerKeys = append(mySQLServersServerKeys, extractMySQLServersServerKeys(i)) } } @@ -436,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 extractMySQLServersServerKeys(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 +} From ccbe9e220b74cfde438b47ea858f26ab374900bb Mon Sep 17 00:00:00 2001 From: Arnab Ghosh Date: Tue, 28 Sep 2021 15:52:41 +0530 Subject: [PATCH 3/3] fix method name --- azure/table_azure_mysql_server.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/azure/table_azure_mysql_server.go b/azure/table_azure_mysql_server.go index 9bbcb9ae..9aa6b88e 100644 --- a/azure/table_azure_mysql_server.go +++ b/azure/table_azure_mysql_server.go @@ -330,7 +330,7 @@ func listMySQLServersServerKeys(ctx context.Context, d *plugin.QueryData, h *plu var mySQLServersServerKeys []map[string]interface{} for _, i := range op.Values() { - mySQLServersServerKeys = append(mySQLServersServerKeys, extractMySQLServersServerKeys(i)) + mySQLServersServerKeys = append(mySQLServersServerKeys, extractMySQLServersServerKey(i)) } for op.NotDone() { @@ -340,7 +340,7 @@ func listMySQLServersServerKeys(ctx context.Context, d *plugin.QueryData, h *plu return nil, err } for _, i := range op.Values() { - mySQLServersServerKeys = append(mySQLServersServerKeys, extractMySQLServersServerKeys(i)) + mySQLServersServerKeys = append(mySQLServersServerKeys, extractMySQLServersServerKey(i)) } } @@ -387,7 +387,7 @@ func extractMySQLServerPrivateEndpointConnections(ctx context.Context, d *transf } // If we return the API response directly, the output will not provide the properties of ServerKeys -func extractMySQLServersServerKeys(i mysql.ServerKey) map[string]interface{} { +func extractMySQLServersServerKey(i mysql.ServerKey) map[string]interface{} { mySQLServersServerKey := make(map[string]interface{}) if i.ID != nil { mySQLServersServerKey["id"] = *i.ID