From e83a054296abda1dbfe9bc5273765fb9bf189e66 Mon Sep 17 00:00:00 2001 From: ParthaI Date: Wed, 29 Sep 2021 16:55:18 +0530 Subject: [PATCH 1/3] Add Guest Configuration Assignments details in table azure_compute_virtual_machine closes #324 --- azure/table_azure_compute_virtual_machine.go | 84 ++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/azure/table_azure_compute_virtual_machine.go b/azure/table_azure_compute_virtual_machine.go index 7f4011df..fe94bf7e 100644 --- a/azure/table_azure_compute_virtual_machine.go +++ b/azure/table_azure_compute_virtual_machine.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" + "github.com/Azure/azure-sdk-for-go/profiles/latest/guestconfiguration/mgmt/guestconfiguration" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute" "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-05-01/network" @@ -307,6 +308,13 @@ func tableAzureComputeVirtualMachine(_ context.Context) *plugin.Table { Hydrate: getAzureComputeVirtualMachineExtensions, Transform: transform.FromValue(), }, + { + Name: "guest_configuration_assignment", + Description: "Guest configuration assignments for a virtual machine.", + Type: proto.ColumnType_JSON, + Hydrate: getComputeVirtualMachineGuestConfigurationAssignment, + Transform: transform.FromValue(), + }, { Name: "win_rm", Description: "Specifies the windows remote management listeners. This enables remote windows powershell.", @@ -566,6 +574,82 @@ func getAzureComputeVirtualMachineExtensions(ctx context.Context, d *plugin.Quer return extensions, nil } +func getComputeVirtualMachineGuestConfigurationAssignment(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + plugin.Logger(ctx).Trace("getComputeVirtualMachineGuestConfigurationAssignment") + + virtualMachine := h.Item.(compute.VirtualMachine) + resourceGroupName := strings.Split(string(*virtualMachine.ID), "/")[4] + + session, err := GetNewSession(ctx, d, "MANAGEMENT") + if err != nil { + return nil, err + } + subscriptionID := session.SubscriptionID + client := guestconfiguration.NewAssignmentsClient(subscriptionID) + client.Authorizer = session.Authorizer + + op, err := client.List(ctx, resourceGroupName, *virtualMachine.Name) + if err != nil { + // API throws 404 error if vm does not have any guest configuration assignments + if strings.Contains(err.Error(), "404") { + return nil, nil + } + plugin.Logger(ctx).Error("getComputeVirtualMachineGuestConfigurationAssignment", "get", err) + return nil, err + } + + var assignments []map[string]interface{} + + // SDK does not support pagination yet + for _, configAssignment := range *op.Value { + objectMap := make(map[string]interface{}) + if configAssignment.ID != nil { + objectMap["id"] = configAssignment.ID + } + if configAssignment.Name != nil { + objectMap["name"] = configAssignment.Name + } + if configAssignment.Location != nil { + objectMap["location"] = configAssignment.Location + } + if configAssignment.Type != nil { + objectMap["type"] = configAssignment.Type + } + if configAssignment.Properties != nil { + if configAssignment.Properties.TargetResourceID != nil { + objectMap["targetResourceID"] = configAssignment.Properties.TargetResourceID + } + if configAssignment.Properties.TargetResourceID != nil { + objectMap["lastComplianceStatusChecked"] = configAssignment.Properties.LastComplianceStatusChecked + } + if configAssignment.Properties.ComplianceStatus != "" { + objectMap["complianceStatus"] = configAssignment.Properties.ComplianceStatus + } + if configAssignment.Properties.LatestReportID != nil { + objectMap["latestReportID"] = configAssignment.Properties.LatestReportID + } + if configAssignment.Properties.Context != nil { + objectMap["context"] = configAssignment.Properties.Context + } + if configAssignment.Properties.AssignmentHash != nil { + objectMap["assignmentHash"] = configAssignment.Properties.AssignmentHash + } + if configAssignment.Properties.ProvisioningState != "" { + objectMap["provisioningState"] = configAssignment.Properties.ProvisioningState + } + if configAssignment.Properties.GuestConfiguration != nil { + objectMap["guestConfiguration"] = configAssignment.Properties.GuestConfiguration + } + if configAssignment.Properties.LatestAssignmentReport != nil { + objectMap["latestAssignmentReport"] = configAssignment.Properties.LatestAssignmentReport + } + } + assignments = append(assignments, objectMap) + } + + return assignments, nil +} + // TRANSFORM FUNCTIONS func getPowerState(ctx context.Context, d *transform.TransformData) (interface{}, error) { From da0c476441fbad1b0a036ceda1f2b929e7d6ba6e Mon Sep 17 00:00:00 2001 From: ParthaI Date: Thu, 30 Sep 2021 13:43:26 +0530 Subject: [PATCH 2/3] made changes as per review comments --- azure/table_azure_compute_virtual_machine.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/azure/table_azure_compute_virtual_machine.go b/azure/table_azure_compute_virtual_machine.go index fe94bf7e..1cf7f0f3 100644 --- a/azure/table_azure_compute_virtual_machine.go +++ b/azure/table_azure_compute_virtual_machine.go @@ -309,10 +309,10 @@ func tableAzureComputeVirtualMachine(_ context.Context) *plugin.Table { Transform: transform.FromValue(), }, { - Name: "guest_configuration_assignment", + Name: "guest_configuration_assignments", Description: "Guest configuration assignments for a virtual machine.", Type: proto.ColumnType_JSON, - Hydrate: getComputeVirtualMachineGuestConfigurationAssignment, + Hydrate: listComputeVirtualMachineGuestConfigurationAssignment, Transform: transform.FromValue(), }, { @@ -574,8 +574,8 @@ func getAzureComputeVirtualMachineExtensions(ctx context.Context, d *plugin.Quer return extensions, nil } -func getComputeVirtualMachineGuestConfigurationAssignment(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { - plugin.Logger(ctx).Trace("getComputeVirtualMachineGuestConfigurationAssignment") +func listComputeVirtualMachineGuestConfigurationAssignment(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + plugin.Logger(ctx).Trace("listComputeVirtualMachineGuestConfigurationAssignment") virtualMachine := h.Item.(compute.VirtualMachine) resourceGroupName := strings.Split(string(*virtualMachine.ID), "/")[4] @@ -588,19 +588,20 @@ func getComputeVirtualMachineGuestConfigurationAssignment(ctx context.Context, d client := guestconfiguration.NewAssignmentsClient(subscriptionID) client.Authorizer = session.Authorizer + // SDK does not support pagination yet op, err := client.List(ctx, resourceGroupName, *virtualMachine.Name) if err != nil { // API throws 404 error if vm does not have any guest configuration assignments if strings.Contains(err.Error(), "404") { return nil, nil } - plugin.Logger(ctx).Error("getComputeVirtualMachineGuestConfigurationAssignment", "get", err) + plugin.Logger(ctx).Error("listComputeVirtualMachineGuestConfigurationAssignment", "get", err) return nil, err } var assignments []map[string]interface{} - // SDK does not support pagination yet + // If we return the API response directly, the output will not provide all the data for Guest Configuration Assignment for _, configAssignment := range *op.Value { objectMap := make(map[string]interface{}) if configAssignment.ID != nil { From 2454113f5a66e6a1b5d901e7976d2e98ef74bcb5 Mon Sep 17 00:00:00 2001 From: sourav Date: Thu, 30 Sep 2021 13:47:13 +0530 Subject: [PATCH 3/3] minor fix --- azure/table_azure_compute_virtual_machine.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/azure/table_azure_compute_virtual_machine.go b/azure/table_azure_compute_virtual_machine.go index 1cf7f0f3..18d8dfda 100644 --- a/azure/table_azure_compute_virtual_machine.go +++ b/azure/table_azure_compute_virtual_machine.go @@ -312,7 +312,7 @@ func tableAzureComputeVirtualMachine(_ context.Context) *plugin.Table { Name: "guest_configuration_assignments", Description: "Guest configuration assignments for a virtual machine.", Type: proto.ColumnType_JSON, - Hydrate: listComputeVirtualMachineGuestConfigurationAssignment, + Hydrate: listComputeVirtualMachineGuestConfigurationAssignments, Transform: transform.FromValue(), }, { @@ -574,8 +574,8 @@ func getAzureComputeVirtualMachineExtensions(ctx context.Context, d *plugin.Quer return extensions, nil } -func listComputeVirtualMachineGuestConfigurationAssignment(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { - plugin.Logger(ctx).Trace("listComputeVirtualMachineGuestConfigurationAssignment") +func listComputeVirtualMachineGuestConfigurationAssignments(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + plugin.Logger(ctx).Trace("listComputeVirtualMachineGuestConfigurationAssignments") virtualMachine := h.Item.(compute.VirtualMachine) resourceGroupName := strings.Split(string(*virtualMachine.ID), "/")[4] @@ -595,7 +595,7 @@ func listComputeVirtualMachineGuestConfigurationAssignment(ctx context.Context, if strings.Contains(err.Error(), "404") { return nil, nil } - plugin.Logger(ctx).Error("listComputeVirtualMachineGuestConfigurationAssignment", "get", err) + plugin.Logger(ctx).Error("listComputeVirtualMachineGuestConfigurationAssignments", "get", err) return nil, err }