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)
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,
}
}

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{
@ -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)