discovery/azure: make local virtualMachine struct more generic by removing the go sdk field reference (#5350)

Signed-off-by: tariqibrahim <tariq181290@gmail.com>
This commit is contained in:
Tariq Ibrahim 2019-03-15 09:18:37 -07:00 committed by Brian Brazil
parent e15d8c5802
commit 5f933e99d0
2 changed files with 84 additions and 61 deletions

View file

@ -259,7 +259,7 @@ type virtualMachine struct {
OsType string
ScaleSet string
Tags map[string]*string
NetworkProfile compute.NetworkProfile
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,11 +497,18 @@ 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),
@ -509,18 +517,25 @@ func mapFromVM(vm compute.VirtualMachine) virtualMachine {
OsType: osType,
ScaleSet: "",
Tags: tags,
NetworkProfile: *(vm.NetworkProfile),
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),
@ -529,7 +544,7 @@ func mapFromVMScaleSetVM(vm compute.VirtualMachineScaleSetVM, scaleSetName strin
OsType: osType,
ScaleSet: scaleSetName,
Tags: tags,
NetworkProfile: *(vm.NetworkProfile),
NetworkInterfaces: networkInterfaces,
}
}

View file

@ -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{
@ -51,7 +53,7 @@ func TestMapFromVMWithEmptyTags(t *testing.T) {
Location: location,
OsType: "Linux",
Tags: map[string]*string{},
NetworkProfile: networkProfile,
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{
@ -95,7 +99,7 @@ func TestMapFromVMWithTags(t *testing.T) {
Location: location,
OsType: "Linux",
Tags: tags,
NetworkProfile: networkProfile,
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{
@ -137,7 +143,7 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) {
Location: location,
OsType: "Linux",
Tags: map[string]*string{},
NetworkProfile: networkProfile,
NetworkInterfaces: []string{},
ScaleSet: 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{
@ -183,7 +191,7 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) {
Location: location,
OsType: "Linux",
Tags: tags,
NetworkProfile: networkProfile,
NetworkInterfaces: []string{},
ScaleSet: scaleSet,
}