forked from gruntwork-io/terratest
/
compute.go
102 lines (81 loc) · 3.03 KB
/
compute.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package azure
import (
"context"
"testing"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-06-01/compute"
"github.com/stretchr/testify/require"
)
// GetVirtualMachineClient is a helper function that will setup an Azure Virtual Machine client on your behalf
func GetVirtualMachineClient(subscriptionID string) (*compute.VirtualMachinesClient, error) {
// Validate Azure subscription ID
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
if err != nil {
return nil, err
}
// Create a VM client
vmClient := compute.NewVirtualMachinesClient(subscriptionID)
// Create an authorizer
authorizer, err := NewAuthorizer()
if err != nil {
return nil, err
}
// Attach authorizer to the client
vmClient.Authorizer = *authorizer
return &vmClient, nil
}
// GetSizeOfVirtualMachine gets the size type of the given Azure Virtual Machine
func GetSizeOfVirtualMachine(t *testing.T, vmName string, resGroupName string, subscriptionID string) compute.VirtualMachineSizeTypes {
size, err := GetSizeOfVirtualMachineE(t, vmName, resGroupName, subscriptionID)
require.NoError(t, err)
return size
}
// GetSizeOfVirtualMachineE gets the size type of the given Azure Virtual Machine
func GetSizeOfVirtualMachineE(t *testing.T, vmName string, resGroupName string, subscriptionID string) (compute.VirtualMachineSizeTypes, error) {
// Validate resource group name and subscription ID
resGroupName, err := getTargetAzureResourceGroupName(resGroupName)
if err != nil {
return "", err
}
// Create a VM client
vmClient, err := GetVirtualMachineClient(subscriptionID)
if err != nil {
return "", err
}
// Get the details of the target virtual machine
vm, err := vmClient.Get(context.Background(), resGroupName, vmName, compute.InstanceView)
if err != nil {
return "", err
}
return vm.VirtualMachineProperties.HardwareProfile.VMSize, nil
}
// GetTagsForVirtualMachine gets the tags of the given Virtual Machine as a map
func GetTagsForVirtualMachine(t *testing.T, vmName string, resGroupName string, subscriptionID string) map[string]string {
tags, err := GetTagsForVirtualMachineE(t, vmName, resGroupName, subscriptionID)
require.NoError(t, err)
return tags
}
// GetTagsForVirtualMachineE gets the tags of the given Virtual Machine as a map
func GetTagsForVirtualMachineE(t *testing.T, vmName string, resGroupName string, subscriptionID string) (map[string]string, error) {
// Setup a blank map to populate and return
tags := make(map[string]string)
// Validate resource group name and subscription ID
resGroupName, err := getTargetAzureResourceGroupName(resGroupName)
if err != nil {
return tags, err
}
// Create a VM client
vmClient, err := GetVirtualMachineClient(subscriptionID)
if err != nil {
return tags, err
}
// Get the details of the target virtual machine
vm, err := vmClient.Get(context.Background(), resGroupName, vmName, compute.InstanceView)
if err != nil {
return tags, err
}
// Range through existing tags and populate above map accordingly
for k, v := range vm.Tags {
tags[k] = *v
}
return tags, nil
}