From 0d7104b7ebadc7f15173218d69e5e30e9dd9f504 Mon Sep 17 00:00:00 2001 From: Tariq Ibrahim Date: Wed, 20 Mar 2019 02:03:47 -0700 Subject: [PATCH] discovery/azure:optimize iteration logic for VMScalesets, VMScalesetVMs, and VMs (#5363) Signed-off-by: tariqibrahim --- discovery/azure/azure.go | 41 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/discovery/azure/azure.go b/discovery/azure/azure.go index 2d58d7bf1..35090731a 100644 --- a/discovery/azure/azure.go +++ b/discovery/azure/azure.go @@ -421,23 +421,16 @@ func (client *azureClient) getVMs(ctx context.Context) ([]virtualMachine, error) var vms []virtualMachine result, err := client.vm.ListAll(ctx) if err != nil { - return nil, fmt.Errorf("could not list virtual machines: %s", err) + return nil, fmt.Errorf("could not list virtual machines: %s", err.Error()) } - - for _, vm := range result.Values() { - vms = append(vms, mapFromVM(vm)) - } - - // If we still have results, keep going until we have no more. for result.NotDone() { - err = result.NextWithContext(ctx) - if err != nil { - return nil, fmt.Errorf("could not list virtual machines: %s", err) - } - for _, vm := range result.Values() { vms = append(vms, mapFromVM(vm)) } + err = result.NextWithContext(ctx) + if err != nil { + return nil, fmt.Errorf("could not list virtual machines: %s", err.Error()) + } } return vms, nil @@ -447,16 +440,14 @@ func (client *azureClient) getScaleSets(ctx context.Context) ([]compute.VirtualM var scaleSets []compute.VirtualMachineScaleSet result, err := client.vmss.ListAll(ctx) if err != nil { - return nil, fmt.Errorf("could not list virtual machine scale sets: %s", err) + return nil, fmt.Errorf("could not list virtual machine scale sets: %s", err.Error()) } - scaleSets = append(scaleSets, result.Values()...) - for result.NotDone() { + scaleSets = append(scaleSets, result.Values()...) err = result.NextWithContext(ctx) if err != nil { - return nil, fmt.Errorf("could not list virtual machine scale sets: %s", err) + return nil, fmt.Errorf("could not list virtual machine scale sets: %s", err.Error()) } - scaleSets = append(scaleSets, result.Values()...) } return scaleSets, nil @@ -473,22 +464,16 @@ func (client *azureClient) getScaleSetVMs(ctx context.Context, scaleSet compute. result, err := client.vmssvm.List(ctx, r.ResourceGroup, *(scaleSet.Name), "", "", "") if err != nil { - return nil, fmt.Errorf("could not list virtual machine scale set vms: %s", err) + return nil, fmt.Errorf("could not list virtual machine scale set vms: %s", err.Error()) } - - for _, vm := range result.Values() { - vms = append(vms, mapFromVMScaleSetVM(vm, *scaleSet.Name)) - } - for result.NotDone() { - err = result.NextWithContext(ctx) - if err != nil { - return nil, fmt.Errorf("could not list virtual machine scale set vms: %s", err) - } - for _, vm := range result.Values() { vms = append(vms, mapFromVMScaleSetVM(vm, *scaleSet.Name)) } + err = result.NextWithContext(ctx) + if err != nil { + return nil, fmt.Errorf("could not list virtual machine scale set vms: %s", err.Error()) + } } return vms, nil