forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_vsphere_virtual_machine.go
123 lines (116 loc) · 4.26 KB
/
data_source_vsphere_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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package vsphere
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/virtualmachine"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/virtualdevice"
"github.com/vmware/govmomi/object"
)
func dataSourceVSphereVirtualMachine() *schema.Resource {
return &schema.Resource{
Read: dataSourceVSphereVirtualMachineRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name or path of the virtual machine.",
Required: true,
},
"datacenter_id": {
Type: schema.TypeString,
Description: "The managed object ID of the datacenter the virtual machine is in. This is not required when using ESXi directly, or if there is only one datacenter in your infrastructure.",
Optional: true,
},
"scsi_controller_scan_count": {
Type: schema.TypeInt,
Description: "The number of SCSI controllers to scan for disk sizes and controller types on.",
Optional: true,
Default: 1,
},
"guest_id": {
Type: schema.TypeString,
Description: "The guest ID of the virtual machine.",
Computed: true,
},
"alternate_guest_name": {
Type: schema.TypeString,
Description: "The alternate guest name of the virtual machine when guest_id is a non-specific operating system, like otherGuest.",
Computed: true,
},
"scsi_type": {
Type: schema.TypeString,
Computed: true,
Description: "The common SCSI bus type of all controllers on the virtual machine.",
},
"disks": {
Type: schema.TypeList,
Description: "Select configuration attributes from the disks on this virtual machine, sorted by bus and unit number.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"size": {
Type: schema.TypeInt,
Computed: true,
},
"eagerly_scrub": {
Type: schema.TypeBool,
Computed: true,
},
"thin_provisioned": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"network_interface_types": {
Type: schema.TypeList,
Description: "The types of network interfaces found on the virtual machine, sorted by unit number.",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
name := d.Get("name").(string)
log.Printf("[DEBUG] Looking for VM or template by name/path %q", name)
var dc *object.Datacenter
if dcID, ok := d.GetOk("datacenter_id"); ok {
var err error
dc, err = datacenterFromID(client, dcID.(string))
if err != nil {
return fmt.Errorf("cannot locate datacenter: %s", err)
}
log.Printf("[DEBUG] Datacenter for VM/template search: %s", dc.InventoryPath)
}
vm, err := virtualmachine.FromPath(client, name, dc)
if err != nil {
return fmt.Errorf("error fetching virtual machine: %s", err)
}
props, err := virtualmachine.Properties(vm)
if err != nil {
return fmt.Errorf("error fetching virtual machine properties: %s", err)
}
d.SetId(props.Config.Uuid)
d.Set("guest_id", props.Config.GuestId)
d.Set("alternate_guest_name", props.Config.AlternateGuestName)
d.Set("scsi_type", virtualdevice.ReadSCSIBusState(object.VirtualDeviceList(props.Config.Hardware.Device), d.Get("scsi_controller_scan_count").(int)))
disks, err := virtualdevice.ReadDiskAttrsForDataSource(object.VirtualDeviceList(props.Config.Hardware.Device), d.Get("scsi_controller_scan_count").(int))
if err != nil {
return fmt.Errorf("error reading disk sizes: %s", err)
}
nics, err := virtualdevice.ReadNetworkInterfaceTypes(object.VirtualDeviceList(props.Config.Hardware.Device))
if err != nil {
return fmt.Errorf("error reading network interface types: %s", err)
}
if d.Set("disks", disks); err != nil {
return fmt.Errorf("error setting disk sizes: %s", err)
}
if d.Set("network_interface_types", nics); err != nil {
return fmt.Errorf("error setting network interface types: %s", err)
}
log.Printf("[DEBUG] VM search for %q completed successfully (UUID %q)", name, props.Config.Uuid)
return nil
}