From fce56685e2352bf25c92590f4faf629ec19c738e Mon Sep 17 00:00:00 2001 From: Arnab <45350738+c0d3r-arnab@users.noreply.github.com> Date: Tue, 28 Sep 2021 18:28:38 +0530 Subject: [PATCH] Add private_endpoint_connections column in azure_servicebus_namespace table. Closes #295 (#334) --- azure/table_azure_servicebus_namespace.go | 75 +++++++++++++++++++++++ docs/tables/azure_servicebus_namespace.md | 17 +++++ 2 files changed, 92 insertions(+) diff --git a/azure/table_azure_servicebus_namespace.go b/azure/table_azure_servicebus_namespace.go index 3f4bcd94..0fa354d0 100644 --- a/azure/table_azure_servicebus_namespace.go +++ b/azure/table_azure_servicebus_namespace.go @@ -116,6 +116,13 @@ func tableAzureServiceBusNamespace(_ context.Context) *plugin.Table { Hydrate: getServiceBusNamespaceNetworkRuleSet, Transform: transform.FromValue(), }, + { + Name: "private_endpoint_connections", + Description: "The private endpoint connections of the namespace.", + Type: proto.ColumnType_JSON, + Hydrate: listServiceBusNamespacePrivateEndpointConnections, + Transform: transform.FromValue(), + }, // Steampipe standard columns { @@ -291,3 +298,71 @@ func listServiceBusNamespaceDiagnosticSettings(ctx context.Context, d *plugin.Qu } return diagnosticSettings, nil } + +func listServiceBusNamespacePrivateEndpointConnections(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + plugin.Logger(ctx).Trace("listServiceBusNamespacePrivateEndpointConnections") + + namespace := h.Item.(servicebus.SBNamespace) + resourceGroup := strings.Split(string(*namespace.ID), "/")[4] + namespaceName := *namespace.Name + + session, err := GetNewSession(ctx, d, "MANAGEMENT") + if err != nil { + return nil, err + } + subscriptionID := session.SubscriptionID + + client := servicebus.NewPrivateEndpointConnectionsClient(subscriptionID) + client.Authorizer = session.Authorizer + + op, err := client.List(ctx, resourceGroup, namespaceName) + if err != nil { + plugin.Logger(ctx).Error("listServiceBusNamespacePrivateEndpointConnections", "list", err) + return nil, err + } + + var serviceBusNamespacePrivateEndpointConnections []map[string]interface{} + + for _, i := range op.Values() { + serviceBusNamespacePrivateEndpointConnections = append(serviceBusNamespacePrivateEndpointConnections, extractServiceBusNamespacePrivateEndpointConnection(i)) + } + + for op.NotDone() { + err = op.NextWithContext(ctx) + if err != nil { + plugin.Logger(ctx).Error("listServiceBusNamespacePrivateEndpointConnections", "list_paging", err) + return nil, err + } + for _, i := range op.Values() { + serviceBusNamespacePrivateEndpointConnections = append(serviceBusNamespacePrivateEndpointConnections, extractServiceBusNamespacePrivateEndpointConnection(i)) + } + } + + return serviceBusNamespacePrivateEndpointConnections, nil +} + +// If we return the API response directly, the output will not provide the properties of PrivateEndpointConnections +func extractServiceBusNamespacePrivateEndpointConnection(i servicebus.PrivateEndpointConnection) map[string]interface{} { + serviceBusNamespacePrivateEndpointConnection := make(map[string]interface{}) + if i.ID != nil { + serviceBusNamespacePrivateEndpointConnection["id"] = *i.ID + } + if i.Name != nil { + serviceBusNamespacePrivateEndpointConnection["name"] = *i.Name + } + if i.Type != nil { + serviceBusNamespacePrivateEndpointConnection["type"] = *i.Type + } + if i.PrivateEndpointConnectionProperties != nil { + if len(i.PrivateEndpointConnectionProperties.ProvisioningState) > 0 { + serviceBusNamespacePrivateEndpointConnection["provisioningState"] = i.PrivateEndpointConnectionProperties.ProvisioningState + } + if i.PrivateEndpointConnectionProperties.PrivateLinkServiceConnectionState != nil { + serviceBusNamespacePrivateEndpointConnection["privateLinkServiceConnectionState"] = i.PrivateEndpointConnectionProperties.PrivateLinkServiceConnectionState + } + if i.PrivateEndpointConnectionProperties.PrivateEndpoint != nil && i.PrivateEndpointConnectionProperties.PrivateEndpoint.ID != nil { + serviceBusNamespacePrivateEndpointConnection["privateEndpointPropertyID"] = i.PrivateEndpointConnectionProperties.PrivateEndpoint.ID + } + } + return serviceBusNamespacePrivateEndpointConnection +} diff --git a/docs/tables/azure_servicebus_namespace.md b/docs/tables/azure_servicebus_namespace.md index b0d980bb..8847dbb5 100644 --- a/docs/tables/azure_servicebus_namespace.md +++ b/docs/tables/azure_servicebus_namespace.md @@ -67,3 +67,20 @@ where ) ); ``` + +### List private endpoint connection details + +```sql +select + name, + id, + connections ->> 'id' as connection_id, + connections ->> 'name' as connection_name, + connections ->> 'privateEndpointPropertyID' as property_private_endpoint_id, + connections ->> 'provisioningState' as property_provisioning_state, + jsonb_pretty(connections -> 'privateLinkServiceConnectionState') as property_private_link_service_connection_state, + connections ->> 'type' as connection_type +from + azure_servicebus_namespace, + jsonb_array_elements(private_endpoint_connections) as connections; +```