discovery/azure:optimize iteration logic for VMScalesets, VMScalesetVMs, and VMs (#5363)

Signed-off-by: tariqibrahim <tariq181290@gmail.com>
This commit is contained in:
Tariq Ibrahim 2019-03-20 02:03:47 -07:00 committed by Brian Brazil
parent b7e978d255
commit 0d7104b7eb

View file

@ -421,23 +421,16 @@ func (client *azureClient) getVMs(ctx context.Context) ([]virtualMachine, error)
var vms []virtualMachine var vms []virtualMachine
result, err := client.vm.ListAll(ctx) result, err := client.vm.ListAll(ctx)
if err != nil { 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() { 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() { for _, vm := range result.Values() {
vms = append(vms, mapFromVM(vm)) 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 return vms, nil
@ -447,16 +440,14 @@ func (client *azureClient) getScaleSets(ctx context.Context) ([]compute.VirtualM
var scaleSets []compute.VirtualMachineScaleSet var scaleSets []compute.VirtualMachineScaleSet
result, err := client.vmss.ListAll(ctx) result, err := client.vmss.ListAll(ctx)
if err != nil { 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() { for result.NotDone() {
scaleSets = append(scaleSets, result.Values()...)
err = result.NextWithContext(ctx) err = result.NextWithContext(ctx)
if err != nil { 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 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), "", "", "") result, err := client.vmssvm.List(ctx, r.ResourceGroup, *(scaleSet.Name), "", "", "")
if err != nil { 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() { 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() { for _, vm := range result.Values() {
vms = append(vms, mapFromVMScaleSetVM(vm, *scaleSet.Name)) 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 return vms, nil