From ef0dc9e7769966200747028b1f40f00512de80c7 Mon Sep 17 00:00:00 2001 From: Arnab Ghosh Date: Fri, 24 Sep 2021 13:18:40 +0530 Subject: [PATCH 1/2] Fix private_endpoint_connections column for 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 | 30 +++++++++++++++++-- docs/tables/azure_mysql_server.md | 14 +++++++++ 4 files changed, 57 insertions(+), 15 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..fc167ab2 100644 --- a/azure/table_azure_mysql_server.go +++ b/azure/table_azure_mysql_server.go @@ -188,7 +188,7 @@ func tableAzureMySQLServer(_ context.Context) *plugin.Table { Name: "private_endpoint_connections", Description: "A list of private endpoint connections on a server.", Type: proto.ColumnType_JSON, - Transform: transform.FromField("ServerProperties.PrivateEndpointConnections"), + Transform: transform.From(extractMySQLServerPrivateEndpointConnections), }, // Steampipe standard columns @@ -247,6 +247,7 @@ func listMySQLServers(ctx context.Context, d *plugin.QueryData, _ *plugin.Hydrat result, err := client.List(ctx) if err != nil { + plugin.Logger(ctx).Error("listMySQLServers", "list", err) return nil, err } @@ -258,7 +259,7 @@ func listMySQLServers(ctx context.Context, d *plugin.QueryData, _ *plugin.Hydrat return nil, err } -//// HYDRATE FUNCTIONS +//// HYDRATE FUNCTION func getMySQLServer(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { plugin.Logger(ctx).Trace("getMySQLServer") @@ -283,6 +284,7 @@ func getMySQLServer(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateD op, err := client.Get(ctx, resourceGroup, name) if err != nil { + plugin.Logger(ctx).Error("getMySQLServer", "get", err) return nil, err } @@ -294,3 +296,27 @@ func getMySQLServer(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateD return nil, nil } + +//// TRANSFORM FUNCTION + +// If we return the API response directly, the output will not provide the properties of PrivateEndpointConnections +func extractMySQLServerPrivateEndpointConnections(ctx context.Context, d *transform.TransformData) (interface{}, error) { + server := d.HydrateItem.(mysql.Server) + var properties []map[string]interface{} + + if server.ServerProperties.PrivateEndpointConnections != nil { + for _, i := range *server.ServerProperties.PrivateEndpointConnections { + objectMap := make(map[string]interface{}) + if i.ID != nil { + objectMap["id"] = i.ID + } + if i.Properties != nil { + objectMap["properties"] = i.Properties + objectMap["provisioningState"] = i.Properties.ProvisioningState + } + properties = append(properties, objectMap) + } + } + + return properties, nil +} diff --git a/docs/tables/azure_mysql_server.md b/docs/tables/azure_mysql_server.md index af58bedd..7dfec4c3 100644 --- a/docs/tables/azure_mysql_server.md +++ b/docs/tables/azure_mysql_server.md @@ -83,3 +83,17 @@ where minimal_tls_version = 'TLS1_0' or minimal_tls_version = 'TLS1_1'; ``` + +### List private endpoint connection details + +```sql +select + name as server_name, + id as server_id, + connections ->> 'id' as connection_id, + jsonb_pretty(connections -> 'properties') as connection_property, + connections ->> 'provisioningState' as connection_provisioning_state +from + azure_mysql_server, + jsonb_array_elements(private_endpoint_connections) as connections; +``` From 01fcc5594a6f65cd849fd66bf0433492ee4dea29 Mon Sep 17 00:00:00 2001 From: Arnab Ghosh Date: Fri, 24 Sep 2021 14:25:13 +0530 Subject: [PATCH 2/2] refactor code --- azure/table_azure_mysql_server.go | 21 ++++++++++++++++++--- docs/tables/azure_mysql_server.md | 5 ++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/azure/table_azure_mysql_server.go b/azure/table_azure_mysql_server.go index fc167ab2..231400e1 100644 --- a/azure/table_azure_mysql_server.go +++ b/azure/table_azure_mysql_server.go @@ -311,12 +311,27 @@ func extractMySQLServerPrivateEndpointConnections(ctx context.Context, d *transf objectMap["id"] = i.ID } if i.Properties != nil { - objectMap["properties"] = i.Properties - objectMap["provisioningState"] = i.Properties.ProvisioningState + if i.Properties.PrivateEndpoint != nil { + objectMap["privateEndpointPropertyId"] = i.Properties.PrivateEndpoint.ID + } + if i.Properties.PrivateLinkServiceConnectionState != nil { + if len(i.Properties.PrivateLinkServiceConnectionState.ActionsRequired) > 0 { + objectMap["privateLinkServiceConnectionStateActionsRequired"] = i.Properties.PrivateLinkServiceConnectionState.ActionsRequired + } + if len(i.Properties.PrivateLinkServiceConnectionState.Status) > 0 { + objectMap["privateLinkServiceConnectionStateStatus"] = i.Properties.PrivateLinkServiceConnectionState.Status + } + if i.Properties.PrivateLinkServiceConnectionState.Description != nil { + objectMap["privateLinkServiceConnectionStateDescription"] = i.Properties.PrivateLinkServiceConnectionState.Description + } + } + if len(i.Properties.ProvisioningState) > 0 { + objectMap["provisioningState"] = i.Properties.ProvisioningState + } } properties = append(properties, objectMap) } } - + return properties, nil } diff --git a/docs/tables/azure_mysql_server.md b/docs/tables/azure_mysql_server.md index 7dfec4c3..8018f14d 100644 --- a/docs/tables/azure_mysql_server.md +++ b/docs/tables/azure_mysql_server.md @@ -91,7 +91,10 @@ select name as server_name, id as server_id, connections ->> 'id' as connection_id, - jsonb_pretty(connections -> 'properties') as connection_property, + connections ->> 'privateEndpointPropertyId' as connection_private_endpoint_property_id, + connections ->> 'privateLinkServiceConnectionStateActionsRequired' as connection_actions_required, + connections ->> 'privateLinkServiceConnectionStateDescription' as connection_description, + connections ->> 'privateLinkServiceConnectionStateStatus' as connection_status, connections ->> 'provisioningState' as connection_provisioning_state from azure_mysql_server,