1

I'm testing a function I have that gets all azure vms under a specific subscription. It uses azure sdk for go's compute.VirtualMachinesClient to do so.

My problem is with the vm clients ListAllComplete function. It's not returning an error. The code just doesn't seem to be able to make it passed that line. Any suggestions on the source of the problem would be appreciated.

This is the code, I've used the fmt package to follow how far it gets:

func GetAllAzureVms() ([]compute.VirtualMachine, error) {
    fmt.Printf("In getAllAzureVm\n")
    var vmList []compute.VirtualMachine
    vmClient, err := GetAzureVmClient()
    fmt.Printf("Out of GetAzureVmClient\n")
    if err != nil {
        return nil, err
    }
    fmt.Print("No error from getazurevmclient\n")
    vmListComplete, err := vmClient.ListAllComplete(context.Background(), "statusOnly=false")
    fmt.Print("vmClient.ListAllComplete done")
    if err != nil {
        fmt.Print("vmClient.ListAllComplete error")
        return nil, err
    }
    fmt.Print("here")
    for vmListComplete.NotDone() {
        vmList = append(vmList, vmListComplete.Value())
        err := vmListComplete.NextWithContext(context.Background())
        if err != nil {
            return nil, err
        }
    }
    fmt.Print("here2")
    return vmList, nil
}

It cant make it passed the line:

vmListComplete, err := vmClient.ListAllComplete(context.Background(), "statusOnly=false")

No error is returned.

Bryan
  • 41
  • 4

1 Answers1

0

I have a similar piece of code, not for vmClient unluckily, but for securityCustomRules.
What I've found useful was to use ListComplete() instead of ListAll() and the print values through JSON marhsalling.
I hope you can find it useful anyway.

import (
"context"
"fmt"
"os"
"testing"

"github.com/gruntwork-io/terratest/modules/azure"
"github.com/gruntwork-io/terratest/modules/terraform"
)

securityCustomRulesList, err := securityCustomRulesClient.ListComplete(context.Background(), tfResourceGroupName, tfVnetName+"-"+fmt.Sprint(vnetIndex)+"-subnet-02-nsg")
// - Scan iterator items [https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-09-01/network#ApplicationGatewayListResultIterator]
if err != nil {
  fmt.Fprintf(os.Stderr, ">>>> Error parsing securityCustomRulesClient:: %s", err)
  return
}
for securityCustomRulesList.NotDone() {
  //   securityCustomRulesList.Value() -> securityRule [https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-09-01/network#SecurityRule]
  v := securityCustomRulesList.Value()
  vJson, _ := v.MarshalJSON()
  fmt.Printf(">> Network securityCustomRulesList JSON %s \n", string(vJson))
  //fmt.Printf(">> Network securityCustomRulesList %s - %s\n", *v.Name, *v.SecurityRulePropertiesFormat.Description)
  securityCustomRulesList.NextWithContext(context.Background())
}

Which gives me an output like:

>> Creating security custom rules list instance 'securityCustomRulesList'..
>> Network securityCustomRulesList JSON {"id":"/subscriptions/8f7d6be2/resourceGroups/unit-tests-tfm-azure-network-resource-group/providers/Microsoft.Network/networkSecurityGroups/unit-tests-tfm-azure-network-vnet-0-subnet-02-nsg/securityRules/test-02","name":"test-02","properties":{"access":"Deny","description":"Deny access","destinationAddressPrefix":"10.0.2.1","destinationAddressPrefixes":[],"destinationPortRange":"*","destinationPortRanges":[],"direction":"Inbound","priority":111,"protocol":"*","sourceAddressPrefix":"10.0.1.0/24","sourceAddressPrefixes":[],"sourcePortRange":"*","sourcePortRanges":[]}} 

Useful links:

tuxErrante
  • 1,274
  • 12
  • 19