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.63k stars 837 forks source link

Empty `Subnet` for Network Interfaces #23611

Open dnaeon opened 6 days ago

dnaeon commented 6 days ago

Bug Report

I'm using sdk/resourcemanager/network/armnetwork/v6 SDK in order to get the existing Network Interfaces.

Go version: go version go1.23.2 darwin/arm64

SDK version: github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6 v6.1.0

When getting the Network Interfaces via the SDK the Subnet field is always nil.

The following example program prints the network interfaces from a given subscription and resource group.

package main

import (
    "context"
    "fmt"
    "os"

    "github.com/Azure/azure-sdk-for-go/sdk/azcore"
    "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    armnetwork "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6"
)

func printErr(err error) {
    fmt.Println(err)
    os.Exit(1)
}

func main() {
    opts := &azidentity.DefaultAzureCredentialOptions{
        TenantID: "my-tenant-uuid",
    }

    creds, err := azidentity.NewDefaultAzureCredential(opts)
    if err != nil {
        printErr(err)
    }

    printNetworkInterfaces(context.Background(), creds, "my-subscription-uuid", "my-rg")
}

func printNetworkInterfaces(ctx context.Context, creds azcore.TokenCredential, subscriptionID string, resourceGroup string) {
    factory, err := armnetwork.NewClientFactory(subscriptionID, creds, &arm.ClientOptions{})
    if err != nil {
        printErr(err)
    }

    client := factory.NewInterfacesClient()
    pager := client.NewListPager(resourceGroup, &armnetwork.InterfacesClientListOptions{})
    for pager.More() {
        page, err := pager.NextPage(ctx)
        if err != nil {
            printErr(err)
        }

        for _, item := range page.Value {
            fmt.Printf("ID: %s\n", *item.ID)
            if item.Properties != nil {
                if item.Properties.IPConfigurations != nil {
                    for _, ip := range item.Properties.IPConfigurations {
                        if ip.Properties != nil {
                            fmt.Printf("\tPrivate IP: %s\n", *ip.Properties.PrivateIPAddress)
                            fmt.Printf("\tAllocation method: %s\n", *ip.Properties.PrivateIPAllocationMethod)
                            fmt.Printf("\tSubnet: %v\n", ip.Properties.Subnet.Name) // Always `nil'
                        }
                    }
                }
            }
        }
    }
}

Sample result.

ID: /subscriptions/my-subscription-uuid/resourceGroups/my-rg/providers/Microsoft.Network/networkInterfaces/my-nic-1
        Private IP: 10.xxx.yyy.zzz
        Alloc method: Dynamic
        Subnet: <nil>

ID: /subscriptions/my-subscription-uuid/resourceGroups/my-rg/providers/Microsoft.Network/networkInterfaces/my-nic-2
        Private IP: 10.aaa.bbb.ccc
        Alloc method: Dynamic
        Subnet: <nil>

Getting the Network Interfaces using the CLI (az network nic list) works as expected, however when using the SDK I always get nil values for the Subnet.

lirenhe commented 4 days ago

@dnaeon, could you turn on logging by setting environment variable AZURE_SDK_GO_LOGGING to all and configure policy.LogOption to print the body of this call? Please help to check whether you could receive subnet info in the raw response.

https://github.com/Azure/azure-sdk-for-go/blob/main/documentation/new-version-guideline.md#logging

dnaeon commented 4 days ago

Hey @lirenhe ,

I've just enabled logging and printed the body of the response. I can see the subnet being part of the body, so this seems like an issue related to the SDK itself.

Thanks!