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

@ -252,14 +252,14 @@ type azureResource struct {
// virtualMachine represents an Azure virtual machine (which can also be created by a VMSS) // virtualMachine represents an Azure virtual machine (which can also be created by a VMSS)
type virtualMachine struct { type virtualMachine struct {
ID string ID string
Name string Name string
Type string Type string
Location string Location string
OsType string OsType string
ScaleSet string ScaleSet string
Tags map[string]*string Tags map[string]*string
NetworkProfile compute.NetworkProfile NetworkInterfaces []string
} }
// Create a new azureResource object from an ID 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. // Get the IP address information via separate call to the network provider.
for _, nic := range *vm.NetworkProfile.NetworkInterfaces { for _, nicID := range vm.NetworkInterfaces {
networkInterface, err := client.getNetworkInterfaceByID(ctx, *nic.ID) networkInterface, err := client.getNetworkInterfaceByID(ctx, nicID)
if err != nil { 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} ch <- target{labelSet: nil, err: err}
// Get out of this routine because we cannot continue without a network interface. // Get out of this routine because we cannot continue without a network interface.
return return
@ -496,40 +497,54 @@ func (client *azureClient) getScaleSetVMs(ctx context.Context, scaleSet compute.
func mapFromVM(vm compute.VirtualMachine) virtualMachine { func mapFromVM(vm compute.VirtualMachine) virtualMachine {
osType := string(vm.StorageProfile.OsDisk.OsType) osType := string(vm.StorageProfile.OsDisk.OsType)
tags := map[string]*string{} tags := map[string]*string{}
networkInterfaces := []string{}
if vm.Tags != nil { if vm.Tags != nil {
tags = vm.Tags tags = vm.Tags
} }
if vm.NetworkProfile != nil {
for _, vmNIC := range *(vm.NetworkProfile.NetworkInterfaces) {
networkInterfaces = append(networkInterfaces, *vmNIC.ID)
}
}
return virtualMachine{ return virtualMachine{
ID: *(vm.ID), ID: *(vm.ID),
Name: *(vm.Name), Name: *(vm.Name),
Type: *(vm.Type), Type: *(vm.Type),
Location: *(vm.Location), Location: *(vm.Location),
OsType: osType, OsType: osType,
ScaleSet: "", ScaleSet: "",
Tags: tags, Tags: tags,
NetworkProfile: *(vm.NetworkProfile), NetworkInterfaces: networkInterfaces,
} }
} }
func mapFromVMScaleSetVM(vm compute.VirtualMachineScaleSetVM, scaleSetName string) virtualMachine { func mapFromVMScaleSetVM(vm compute.VirtualMachineScaleSetVM, scaleSetName string) virtualMachine {
osType := string(vm.StorageProfile.OsDisk.OsType) osType := string(vm.StorageProfile.OsDisk.OsType)
tags := map[string]*string{} tags := map[string]*string{}
networkInterfaces := []string{}
if vm.Tags != nil { if vm.Tags != nil {
tags = vm.Tags tags = vm.Tags
} }
if vm.NetworkProfile != nil {
for _, vmNIC := range *(vm.NetworkProfile.NetworkInterfaces) {
networkInterfaces = append(networkInterfaces, *vmNIC.ID)
}
}
return virtualMachine{ return virtualMachine{
ID: *(vm.ID), ID: *(vm.ID),
Name: *(vm.Name), Name: *(vm.Name),
Type: *(vm.Type), Type: *(vm.Type),
Location: *(vm.Location), Location: *(vm.Location),
OsType: osType, OsType: osType,
ScaleSet: scaleSetName, ScaleSet: scaleSetName,
Tags: tags, Tags: tags,
NetworkProfile: *(vm.NetworkProfile), NetworkInterfaces: networkInterfaces,
} }
} }

View file

@ -25,7 +25,9 @@ func TestMapFromVMWithEmptyTags(t *testing.T) {
name := "name" name := "name"
vmType := "type" vmType := "type"
location := "westeurope" location := "westeurope"
networkProfile := compute.NetworkProfile{} networkProfile := compute.NetworkProfile{
NetworkInterfaces: &[]compute.NetworkInterfaceReference{},
}
properties := &compute.VirtualMachineProperties{ properties := &compute.VirtualMachineProperties{
StorageProfile: &compute.StorageProfile{ StorageProfile: &compute.StorageProfile{
OsDisk: &compute.OSDisk{ OsDisk: &compute.OSDisk{
@ -45,13 +47,13 @@ func TestMapFromVMWithEmptyTags(t *testing.T) {
} }
expectedVM := virtualMachine{ expectedVM := virtualMachine{
ID: id, ID: id,
Name: name, Name: name,
Type: vmType, Type: vmType,
Location: location, Location: location,
OsType: "Linux", OsType: "Linux",
Tags: map[string]*string{}, Tags: map[string]*string{},
NetworkProfile: networkProfile, NetworkInterfaces: []string{},
} }
actualVM := mapFromVM(testVM) actualVM := mapFromVM(testVM)
@ -69,7 +71,9 @@ func TestMapFromVMWithTags(t *testing.T) {
tags := map[string]*string{ tags := map[string]*string{
"prometheus": new(string), "prometheus": new(string),
} }
networkProfile := compute.NetworkProfile{} networkProfile := compute.NetworkProfile{
NetworkInterfaces: &[]compute.NetworkInterfaceReference{},
}
properties := &compute.VirtualMachineProperties{ properties := &compute.VirtualMachineProperties{
StorageProfile: &compute.StorageProfile{ StorageProfile: &compute.StorageProfile{
OsDisk: &compute.OSDisk{ OsDisk: &compute.OSDisk{
@ -89,13 +93,13 @@ func TestMapFromVMWithTags(t *testing.T) {
} }
expectedVM := virtualMachine{ expectedVM := virtualMachine{
ID: id, ID: id,
Name: name, Name: name,
Type: vmType, Type: vmType,
Location: location, Location: location,
OsType: "Linux", OsType: "Linux",
Tags: tags, Tags: tags,
NetworkProfile: networkProfile, NetworkInterfaces: []string{},
} }
actualVM := mapFromVM(testVM) actualVM := mapFromVM(testVM)
@ -110,7 +114,9 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) {
name := "name" name := "name"
vmType := "type" vmType := "type"
location := "westeurope" location := "westeurope"
networkProfile := compute.NetworkProfile{} networkProfile := compute.NetworkProfile{
NetworkInterfaces: &[]compute.NetworkInterfaceReference{},
}
properties := &compute.VirtualMachineScaleSetVMProperties{ properties := &compute.VirtualMachineScaleSetVMProperties{
StorageProfile: &compute.StorageProfile{ StorageProfile: &compute.StorageProfile{
OsDisk: &compute.OSDisk{ OsDisk: &compute.OSDisk{
@ -131,14 +137,14 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) {
scaleSet := "testSet" scaleSet := "testSet"
expectedVM := virtualMachine{ expectedVM := virtualMachine{
ID: id, ID: id,
Name: name, Name: name,
Type: vmType, Type: vmType,
Location: location, Location: location,
OsType: "Linux", OsType: "Linux",
Tags: map[string]*string{}, Tags: map[string]*string{},
NetworkProfile: networkProfile, NetworkInterfaces: []string{},
ScaleSet: scaleSet, ScaleSet: scaleSet,
} }
actualVM := mapFromVMScaleSetVM(testVM, scaleSet) actualVM := mapFromVMScaleSetVM(testVM, scaleSet)
@ -156,7 +162,9 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) {
tags := map[string]*string{ tags := map[string]*string{
"prometheus": new(string), "prometheus": new(string),
} }
networkProfile := compute.NetworkProfile{} networkProfile := compute.NetworkProfile{
NetworkInterfaces: &[]compute.NetworkInterfaceReference{},
}
properties := &compute.VirtualMachineScaleSetVMProperties{ properties := &compute.VirtualMachineScaleSetVMProperties{
StorageProfile: &compute.StorageProfile{ StorageProfile: &compute.StorageProfile{
OsDisk: &compute.OSDisk{ OsDisk: &compute.OSDisk{
@ -177,14 +185,14 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) {
scaleSet := "testSet" scaleSet := "testSet"
expectedVM := virtualMachine{ expectedVM := virtualMachine{
ID: id, ID: id,
Name: name, Name: name,
Type: vmType, Type: vmType,
Location: location, Location: location,
OsType: "Linux", OsType: "Linux",
Tags: tags, Tags: tags,
NetworkProfile: networkProfile, NetworkInterfaces: []string{},
ScaleSet: scaleSet, ScaleSet: scaleSet,
} }
actualVM := mapFromVMScaleSetVM(testVM, scaleSet) actualVM := mapFromVMScaleSetVM(testVM, scaleSet)