From 5f933e99d006fff329659cca91d96fb8695ea3de Mon Sep 17 00:00:00 2001 From: Tariq Ibrahim Date: Fri, 15 Mar 2019 09:18:37 -0700 Subject: [PATCH] discovery/azure: make local virtualMachine struct more generic by removing the go sdk field reference (#5350) Signed-off-by: tariqibrahim --- discovery/azure/azure.go | 69 ++++++++++++++++++------------- discovery/azure/azure_test.go | 76 +++++++++++++++++++---------------- 2 files changed, 84 insertions(+), 61 deletions(-) diff --git a/discovery/azure/azure.go b/discovery/azure/azure.go index c6de83282..2d58d7bf1 100644 --- a/discovery/azure/azure.go +++ b/discovery/azure/azure.go @@ -252,14 +252,14 @@ type azureResource struct { // virtualMachine represents an Azure virtual machine (which can also be created by a VMSS) type virtualMachine struct { - ID string - Name string - Type string - Location string - OsType string - ScaleSet string - Tags map[string]*string - NetworkProfile compute.NetworkProfile + ID string + Name string + Type string + Location string + OsType string + ScaleSet string + Tags map[string]*string + NetworkInterfaces []string } // Create a new azureResource object from an ID string. @@ -359,10 +359,11 @@ func (d *Discovery) refresh(ctx context.Context) (tg *targetgroup.Group, err err } // Get the IP address information via separate call to the network provider. - for _, nic := range *vm.NetworkProfile.NetworkInterfaces { - networkInterface, err := client.getNetworkInterfaceByID(ctx, *nic.ID) + for _, nicID := range vm.NetworkInterfaces { + networkInterface, err := client.getNetworkInterfaceByID(ctx, nicID) + if err != nil { - level.Error(d.logger).Log("msg", "Unable to get network interface", "name", *nic.ID, "err", err) + level.Error(d.logger).Log("msg", "Unable to get network interface", "name", nicID, "err", err) ch <- target{labelSet: nil, err: err} // Get out of this routine because we cannot continue without a network interface. return @@ -496,40 +497,54 @@ func (client *azureClient) getScaleSetVMs(ctx context.Context, scaleSet compute. func mapFromVM(vm compute.VirtualMachine) virtualMachine { osType := string(vm.StorageProfile.OsDisk.OsType) tags := map[string]*string{} + networkInterfaces := []string{} if vm.Tags != nil { tags = vm.Tags } + if vm.NetworkProfile != nil { + for _, vmNIC := range *(vm.NetworkProfile.NetworkInterfaces) { + networkInterfaces = append(networkInterfaces, *vmNIC.ID) + } + } + return virtualMachine{ - ID: *(vm.ID), - Name: *(vm.Name), - Type: *(vm.Type), - Location: *(vm.Location), - OsType: osType, - ScaleSet: "", - Tags: tags, - NetworkProfile: *(vm.NetworkProfile), + ID: *(vm.ID), + Name: *(vm.Name), + Type: *(vm.Type), + Location: *(vm.Location), + OsType: osType, + ScaleSet: "", + Tags: tags, + NetworkInterfaces: networkInterfaces, } } func mapFromVMScaleSetVM(vm compute.VirtualMachineScaleSetVM, scaleSetName string) virtualMachine { osType := string(vm.StorageProfile.OsDisk.OsType) tags := map[string]*string{} + networkInterfaces := []string{} if vm.Tags != nil { tags = vm.Tags } + if vm.NetworkProfile != nil { + for _, vmNIC := range *(vm.NetworkProfile.NetworkInterfaces) { + networkInterfaces = append(networkInterfaces, *vmNIC.ID) + } + } + return virtualMachine{ - ID: *(vm.ID), - Name: *(vm.Name), - Type: *(vm.Type), - Location: *(vm.Location), - OsType: osType, - ScaleSet: scaleSetName, - Tags: tags, - NetworkProfile: *(vm.NetworkProfile), + ID: *(vm.ID), + Name: *(vm.Name), + Type: *(vm.Type), + Location: *(vm.Location), + OsType: osType, + ScaleSet: scaleSetName, + Tags: tags, + NetworkInterfaces: networkInterfaces, } } diff --git a/discovery/azure/azure_test.go b/discovery/azure/azure_test.go index 54e4be349..31550f8b5 100644 --- a/discovery/azure/azure_test.go +++ b/discovery/azure/azure_test.go @@ -25,7 +25,9 @@ func TestMapFromVMWithEmptyTags(t *testing.T) { name := "name" vmType := "type" location := "westeurope" - networkProfile := compute.NetworkProfile{} + networkProfile := compute.NetworkProfile{ + NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, + } properties := &compute.VirtualMachineProperties{ StorageProfile: &compute.StorageProfile{ OsDisk: &compute.OSDisk{ @@ -45,13 +47,13 @@ func TestMapFromVMWithEmptyTags(t *testing.T) { } expectedVM := virtualMachine{ - ID: id, - Name: name, - Type: vmType, - Location: location, - OsType: "Linux", - Tags: map[string]*string{}, - NetworkProfile: networkProfile, + ID: id, + Name: name, + Type: vmType, + Location: location, + OsType: "Linux", + Tags: map[string]*string{}, + NetworkInterfaces: []string{}, } actualVM := mapFromVM(testVM) @@ -69,7 +71,9 @@ func TestMapFromVMWithTags(t *testing.T) { tags := map[string]*string{ "prometheus": new(string), } - networkProfile := compute.NetworkProfile{} + networkProfile := compute.NetworkProfile{ + NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, + } properties := &compute.VirtualMachineProperties{ StorageProfile: &compute.StorageProfile{ OsDisk: &compute.OSDisk{ @@ -89,13 +93,13 @@ func TestMapFromVMWithTags(t *testing.T) { } expectedVM := virtualMachine{ - ID: id, - Name: name, - Type: vmType, - Location: location, - OsType: "Linux", - Tags: tags, - NetworkProfile: networkProfile, + ID: id, + Name: name, + Type: vmType, + Location: location, + OsType: "Linux", + Tags: tags, + NetworkInterfaces: []string{}, } actualVM := mapFromVM(testVM) @@ -110,7 +114,9 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) { name := "name" vmType := "type" location := "westeurope" - networkProfile := compute.NetworkProfile{} + networkProfile := compute.NetworkProfile{ + NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, + } properties := &compute.VirtualMachineScaleSetVMProperties{ StorageProfile: &compute.StorageProfile{ OsDisk: &compute.OSDisk{ @@ -131,14 +137,14 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) { scaleSet := "testSet" expectedVM := virtualMachine{ - ID: id, - Name: name, - Type: vmType, - Location: location, - OsType: "Linux", - Tags: map[string]*string{}, - NetworkProfile: networkProfile, - ScaleSet: scaleSet, + ID: id, + Name: name, + Type: vmType, + Location: location, + OsType: "Linux", + Tags: map[string]*string{}, + NetworkInterfaces: []string{}, + ScaleSet: scaleSet, } actualVM := mapFromVMScaleSetVM(testVM, scaleSet) @@ -156,7 +162,9 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) { tags := map[string]*string{ "prometheus": new(string), } - networkProfile := compute.NetworkProfile{} + networkProfile := compute.NetworkProfile{ + NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, + } properties := &compute.VirtualMachineScaleSetVMProperties{ StorageProfile: &compute.StorageProfile{ OsDisk: &compute.OSDisk{ @@ -177,14 +185,14 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) { scaleSet := "testSet" expectedVM := virtualMachine{ - ID: id, - Name: name, - Type: vmType, - Location: location, - OsType: "Linux", - Tags: tags, - NetworkProfile: networkProfile, - ScaleSet: scaleSet, + ID: id, + Name: name, + Type: vmType, + Location: location, + OsType: "Linux", + Tags: tags, + NetworkInterfaces: []string{}, + ScaleSet: scaleSet, } actualVM := mapFromVMScaleSetVM(testVM, scaleSet)