Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guest_configuration_assignments column in azure_compute_virtual_machine table. closes #324 #353

Merged
merged 4 commits into from
Sep 30, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions azure/table_azure_compute_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -307,6 +308,13 @@ func tableAzureComputeVirtualMachine(_ context.Context) *plugin.Table {
Hydrate: getAzureComputeVirtualMachineExtensions,
Transform: transform.FromValue(),
},
{
Name: "guest_configuration_assignment",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Name: "guest_configuration_assignment",
Name: "guest_configuration_assignments",

Description: "Guest configuration assignments for a virtual machine.",
Type: proto.ColumnType_JSON,
Hydrate: getComputeVirtualMachineGuestConfigurationAssignment,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Hydrate: getComputeVirtualMachineGuestConfigurationAssignment,
Hydrate: listComputeVirtualMachineGuestConfigurationAssignments,

Transform: transform.FromValue(),
},
{
Name: "win_rm",
Description: "Specifies the windows remote management listeners. This enables remote windows powershell.",
Expand Down Expand Up @@ -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

bigdatasourav marked this conversation as resolved.
Show resolved Hide resolved
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment for extract

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) {
Expand Down