/
resolve_virtual_machine.go
44 lines (38 loc) · 1.26 KB
/
resolve_virtual_machine.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
package resolve
import (
"context"
"fmt"
"github.com/magodo/armid"
"github.com/magodo/aztft/internal/client"
)
type virtualMachinesResolver struct{}
func (virtualMachinesResolver) ResourceTypes() []string {
return []string{"azurerm_linux_virtual_machine", "azurerm_windows_virtual_machine"}
}
func (virtualMachinesResolver) Resolve(b *client.ClientBuilder, id armid.ResourceId) (string, error) {
resourceGroupId := id.RootScope().(*armid.ResourceGroup)
client, err := b.NewVirtualMachinesClient(resourceGroupId.SubscriptionId)
if err != nil {
return "", err
}
resp, err := client.Get(context.Background(), resourceGroupId.Name, id.Names()[0], nil)
if err != nil {
return "", fmt.Errorf("retrieving %q: %v", id, err)
}
props := resp.VirtualMachine.Properties
if props == nil {
return "", fmt.Errorf("unexpected nil property in response")
}
osProfile := props.OSProfile
if osProfile == nil {
return "", fmt.Errorf("unexpected nil OS profile in response")
}
switch {
case osProfile.LinuxConfiguration != nil:
return "azurerm_linux_virtual_machine", nil
case osProfile.WindowsConfiguration != nil:
return "azurerm_windows_virtual_machine", nil
default:
return "", fmt.Errorf("both windowsConfiguration and linuxConfiguration in OS profile is null")
}
}