Azure / azure-sdk-for-go

This repository is for active development of the Azure SDK for Go. For consumers of the SDK we recommend visiting our public developer docs at:
https://docs.microsoft.com/azure/developer/go/
MIT License
1.64k stars 840 forks source link

With Client/Secret Authentication the API compute:InstanceView does not return power status. #23298

Open ParthaI opened 2 months ago

ParthaI commented 2 months ago

Dear team,

I am currently using the Azure Compute Go SDK package, version v68.0.0+incompatible.

With this package, I am making the VirtualMachinesClient.InstanceView API call to retrieve the instance power state.

When I use Azure CLI for authentication, the results are as expected. However, when using Client/Secret authentication, I do not receive any status information for the instances.

I would greatly appreciate any insights or suggestions you might have on this matter.

Here is the standalone Go code I am using.

With Azure CLI authentication(Working fine and returning the result as expected):

package main

import (
    "context"
    "fmt"
    "strings"

    "github.com/Azure/azure-sdk-for-go/sdk/azcore"
    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    compute "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6"
)

func main() {
    ctx := context.Background()

    subscriptionID := "YOUR-SUBSCRIPTION-ID"

    // Configure the environment (using Azure Public Cloud as an example)
    cred, err := azidentity.NewAzureCLICredential(nil)

    if err != nil {
        fmt.Errorf("Failed to create authorizer: %v", err)
    }

    // List Virtual Machines
    vms, err := listComputeVirtualMachines(ctx, subscriptionID, cred)
    if err != nil {
        fmt.Printf("Error listing VMs: %v\n", err)
    }

    for _, vm := range vms {

        vmId := *vm.Id
        resourceGroupName := strings.Split(string(vmId), "/")[4]
        // Get a specific Virtual Machine
        result, err := getComputeVirtualMachineInstanceView(ctx, subscriptionID, resourceGroupName, *vm.Name, cred)
        if err != nil {
            fmt.Printf("Error getting VM: %v\n", err)
        }

        if result.Statuses != nil {
            fmt.Printf("\n\nVM Name: %s\n====\n", *vm.Name)
            for _, st := range result.Statuses {
                // fmt.Printf("Status : %+v", st)

                if st.Code != nil {
                    fmt.Printf("Status Code: %s\n", *st.Code)
                }
                if st.Level != nil {
                    fmt.Printf("Status Level: %s\n", *st.Level)
                }
                if st.DisplayStatus != nil {
                    fmt.Printf("DisplayStatus: %s\n", *st.DisplayStatus)
                }
                if st.Message != nil {
                    fmt.Printf("Message: %s\n", *st.Message)
                }
                fmt.Println("\n----\n")
            }
        }
    }

}

type vm struct {
    Id   *string
    Name *string
}

func listComputeVirtualMachines(ctx context.Context, subscriptionID string, auth azcore.TokenCredential) ([]vm, error) {

    client, err := compute.NewVirtualMachinesClient(subscriptionID, auth, nil)

    result := client.NewListAllPager(nil)
    if err != nil {
        return []vm{}, fmt.Errorf("failed to list virtual machines: %v", err)
    }
    var vms []vm
    for result.More() {
        page, err := result.NextPage(ctx)
        if err != nil {
            return []vm{}, fmt.Errorf("failed to list virtual machines page: %v", err)
        }
        for _, vmt := range page.Value {
            v := vm{
                Id:   vmt.ID,
                Name: vmt.Name,
            }
            vms = append(vms, v)
            // fmt.Printf("VM Name: %s\n", *vmt.Name)
        }
    }

    return vms, nil
}

func getComputeVirtualMachineInstanceView(ctx context.Context, subscriptionID, resourceGroup, vmName string, auth azcore.TokenCredential) (compute.VirtualMachinesClientInstanceViewResponse, error) {

    client, err := compute.NewVirtualMachinesClient(subscriptionID, auth, nil)

    if err != nil {
        return compute.VirtualMachinesClientInstanceViewResponse{}, err
    }

    vm, err := client.InstanceView(ctx, resourceGroup, vmName, nil)
    if err != nil {
        return compute.VirtualMachinesClientInstanceViewResponse{}, fmt.Errorf("failed to get virtual machine: %v", err)
    }

    return vm, nil
}

With Client/Secret authentication(Didn't work as expected):

package main

import (
    "context"
    "fmt"
    "strings"

    "github.com/Azure/azure-sdk-for-go/sdk/azcore"
    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    compute "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6"
)

func main() {
    ctx := context.Background()

    clientID := "your-client-id"
    clientSecret := "your-client-secret"
    tenantID := "your-tenant-id"
    subscriptionID := "your-subscription-id"

    // Configure the environment (using Azure Public Cloud as an example)
    cred, err := azidentity.NewClientSecretCredential(
        tenantID,
        clientID,
        clientSecret,
        nil,
    )

    if err != nil {
        fmt.Errorf("Failed to create authorizer: %v", err)
    }

    vms, err := listComputeVirtualMachines(ctx, subscriptionID, cred)
    if err != nil {
        fmt.Printf("Error listing VMs: %v\n", err)
    }

    for _, vm := range vms {

        vmId := *vm.Id
        resourceGroupName := strings.Split(string(vmId), "/")[4]
        // Get a specific Virtual Machine
        result, err := getComputeVirtualMachineInstanceView(ctx, subscriptionID, resourceGroupName, *vm.Name, cred)
        if err != nil {
            fmt.Printf("Error getting VM: %v\n", err)
        }

        if result.Statuses != nil {
            fmt.Printf("\n\nVM Name: %s\n====\n", *vm.Name)
            for _, st := range result.Statuses {
                // fmt.Printf("Status : %+v", st)

                if st.Code != nil {
                    fmt.Printf("Status Code: %s\n", *st.Code)
                }
                if st.Level != nil {
                    fmt.Printf("Status Level: %s\n", *st.Level)
                }
                if st.DisplayStatus != nil {
                    fmt.Printf("DisplayStatus: %s\n", *st.DisplayStatus)
                }
                if st.Message != nil {
                    fmt.Printf("Message: %s\n", *st.Message)
                }
                fmt.Println("\n----\n")
            }
        }
    }

}

type vm struct {
    Id   *string
    Name *string
}

func listComputeVirtualMachines(ctx context.Context, subscriptionID string, auth azcore.TokenCredential) ([]vm, error) {

    client, err := compute.NewVirtualMachinesClient(subscriptionID, auth, nil)

    result := client.NewListAllPager(nil)
    if err != nil {
        return []vm{}, fmt.Errorf("failed to list virtual machines: %v", err)
    }
    var vms []vm
    for result.More() {
        page, err := result.NextPage(ctx)
        if err != nil {
            return []vm{}, fmt.Errorf("failed to list virtual machines page: %v", err)
        }
        for _, vmt := range page.Value {
            v := vm{
                Id:   vmt.ID,
                Name: vmt.Name,
            }
            vms = append(vms, v)
            // fmt.Printf("VM Name: %s\n", *vmt.Name)
        }
    }

    return vms, nil
}

func getComputeVirtualMachineInstanceView(ctx context.Context, subscriptionID, resourceGroup, vmName string, auth *azidentity.ClientSecretCredential) (compute.VirtualMachinesClientInstanceViewResponse, error) {

    client, err := compute.NewVirtualMachinesClient(subscriptionID, auth, nil)

    vm, err := client.InstanceView(ctx, resourceGroup, vmName, nil)
    if err != nil {
        return compute.VirtualMachinesClientInstanceViewResponse{}, fmt.Errorf("failed to get virtual machine: %v", err)
    }

    return vm, nil
}

Note: The App has the appropriate permission to make the API call.

Could you please assist with what is going wrong with client/secret authentication?

@halit-c4c, Please include the details if I missed anything.

Thanks!

github-actions[bot] commented 2 months ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @avirishuv @Drewm3.

RickWinter commented 2 months ago

What is the failure you are receiving and on which call does it fail. Can you share the exception message? Please ensure you don't share on this thread any sensitive information such as tokens.

github-actions[bot] commented 2 months ago

Hi @ParthaI. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

RickWinter commented 2 months ago

Your comment states you're using v68 which is the old mgmt libraries, however the code you shared is using the latest library versions. Please confirm which package of armcompute you are using.

ParthaI commented 2 months ago

Hello, @RickWinter. Thank you for your quick response.

In our project, we're using the v68.0.0+incompatible package. I've created two separate standalone codes with the resourcemanager package, each using different authentication modules, to verify whether the latest version of resourcemanager provides the necessary details.

However, I'm unable to retrieve the VM statuses when using client/secret authentication with any of the packages. In contrast, when using CLI authentication, I'm getting the expected results for all the packages.

What is the failure you are receiving and on which call does it fail. Can you share the exception message? Please ensure you don't share on this thread any sensitive information such as tokens.

I’m not encountering any errors, but I'm receiving an empty response for the statuses property.

Note: Additionally, with client/secret authentication, I'm only receiving an EMPTY statuses value for a specific subscription, not for all subscriptions.

Thanks!

RickWinter commented 2 months ago

The v68.0.0+incompatible package you are using is deprecated.

The current library you should use is: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute We strongly encourage you to upgrade to continue receiving updates.

ParthaI commented 2 months ago

Thank you for the suggestion. We'll give it a try and let you know if the package helps us.

dnaeon commented 3 weeks ago

Hey there,

Having the same issues with latest github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6 version of the module.

The VirtualMachineInstanceView is nil for all VMs.

github-actions[bot] commented 3 weeks ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @avirishuv @Drewm3.