Add functionality to specify and mount vmdks (#6146)

User may specify a vmdk in their disk definition.
The options size, template, and vmdk are considered
to be mutually exclusive. User may also set whether each disk
associated with the vm should try to boot after creation.
Todo: Enforce mutual exclusivity, validate the bootable_vmdk_path
This commit is contained in:
dkalleg 2016-04-27 09:22:22 -07:00 committed by Paul Stack
parent 2c29a35751
commit 6f62248471
3 changed files with 126 additions and 8 deletions

View File

@ -42,6 +42,7 @@ type hardDisk struct {
size int64 size int64
iops int64 iops int64
initType string initType string
vmdkPath string
} }
//Additional options Vsphere can use clones of windows machines //Additional options Vsphere can use clones of windows machines
@ -81,6 +82,7 @@ type virtualMachine struct {
timeZone string timeZone string
dnsSuffixes []string dnsSuffixes []string
dnsServers []string dnsServers []string
bootableVmdk bool
linkedClone bool linkedClone bool
windowsOptionalConfig windowsOptConfig windowsOptionalConfig windowsOptConfig
customConfigurations map[string](types.AnyType) customConfigurations map[string](types.AnyType)
@ -342,6 +344,21 @@ func resourceVSphereVirtualMachine() *schema.Resource {
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
}, },
"vmdk": &schema.Schema{
// TODO: Add ValidateFunc to confirm path exists
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "",
},
"bootable": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
},
}, },
}, },
}, },
@ -508,8 +525,13 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
} else { } else {
if v, ok := disk["size"].(int); ok && v != 0 { if v, ok := disk["size"].(int); ok && v != 0 {
disks[i].size = int64(v) disks[i].size = int64(v)
} else if v, ok := disk["vmdk"].(string); ok && v != "" {
disks[i].vmdkPath = v
if v, ok := disk["bootable"].(bool); ok {
vm.bootableVmdk = v
}
} else { } else {
return fmt.Errorf("If template argument is not specified, size argument is required.") return fmt.Errorf("template, size, or vmdk argument is required")
} }
} }
if v, ok := disk["datastore"].(string); ok && v != "" { if v, ok := disk["datastore"].(string); ok && v != "" {
@ -518,8 +540,10 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
} else { } else {
if v, ok := disk["size"].(int); ok && v != 0 { if v, ok := disk["size"].(int); ok && v != 0 {
disks[i].size = int64(v) disks[i].size = int64(v)
} else if v, ok := disk["vmdk"].(string); ok && v != "" {
disks[i].vmdkPath = v
} else { } else {
return fmt.Errorf("Size argument is required.") return fmt.Errorf("size or vmdk argument is required")
} }
} }
@ -774,7 +798,7 @@ func waitForNetworkingActive(client *govmomi.Client, datacenter, name string) re
} }
// addHardDisk adds a new Hard Disk to the VirtualMachine. // addHardDisk adds a new Hard Disk to the VirtualMachine.
func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string) error { func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string, datastore *object.Datastore, diskPath string) error {
devices, err := vm.Device(context.TODO()) devices, err := vm.Device(context.TODO())
if err != nil { if err != nil {
return err return err
@ -787,7 +811,8 @@ func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string) e
} }
log.Printf("[DEBUG] disk controller: %#v\n", controller) log.Printf("[DEBUG] disk controller: %#v\n", controller)
disk := devices.CreateDisk(controller, "") // TODO Check if diskPath & datastore exist
disk := devices.CreateDisk(controller, fmt.Sprintf("[%v] %v", datastore.Name(), diskPath))
existing := devices.SelectByBackingInfo(disk.Backing) existing := devices.SelectByBackingInfo(disk.Backing)
log.Printf("[DEBUG] disk: %#v\n", disk) log.Printf("[DEBUG] disk: %#v\n", disk)
@ -1214,7 +1239,7 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error {
for _, hd := range vm.hardDisks { for _, hd := range vm.hardDisks {
log.Printf("[DEBUG] add hard disk: %v", hd.size) log.Printf("[DEBUG] add hard disk: %v", hd.size)
log.Printf("[DEBUG] add hard disk: %v", hd.iops) log.Printf("[DEBUG] add hard disk: %v", hd.iops)
err = addHardDisk(newVM, hd.size, hd.iops, "thin") err = addHardDisk(newVM, hd.size, hd.iops, "thin", datastore, hd.vmdkPath)
if err != nil { if err != nil {
return err return err
} }
@ -1225,6 +1250,15 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error {
return err return err
} }
if vm.bootableVmdk {
newVM.PowerOn(context.TODO())
ip, err := newVM.WaitForIP(context.TODO())
if err != nil {
return err
}
log.Printf("[DEBUG] ip address: %v", ip)
}
return nil return nil
} }
@ -1546,7 +1580,7 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error {
log.Printf("[DEBUG] VM customization finished") log.Printf("[DEBUG] VM customization finished")
for i := 1; i < len(vm.hardDisks); i++ { for i := 1; i < len(vm.hardDisks); i++ {
err = addHardDisk(newVM, vm.hardDisks[i].size, vm.hardDisks[i].iops, vm.hardDisks[i].initType) err = addHardDisk(newVM, vm.hardDisks[i].size, vm.hardDisks[i].iops, vm.hardDisks[i].initType, datastore, vm.hardDisks[i].vmdkPath)
if err != nil { if err != nil {
return err return err
} }

View File

@ -455,6 +455,68 @@ func TestAccVSphereVirtualMachine_createWithCdrom(t *testing.T) {
}) })
} }
func TestAccVSphereVirtualMachine_createWithExistingVmdk(t *testing.T) {
vmdk_path := os.Getenv("VSPHERE_VMDK_PATH")
gateway := os.Getenv("VSPHERE_NETWORK_GATEWAY")
label := os.Getenv("VSPHERE_NETWORK_LABEL")
ip_address := os.Getenv("VSPHERE_NETWORK_IP_ADDRESS")
var vm virtualMachine
var locationOpt string
var datastoreOpt string
if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v)
}
if v := os.Getenv("VSPHERE_CLUSTER"); v != "" {
locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v)
}
if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" {
locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v)
}
if v := os.Getenv("VSPHERE_DATASTORE"); v != "" {
datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v)
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(
testAccCheckVSphereVirtualMachineConfig_withExistingVmdk,
locationOpt,
gateway,
label,
ip_address,
datastoreOpt,
vmdk_path,
),
Check: resource.ComposeTestCheckFunc(
testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.with_existing_vmdk", &vm),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.with_existing_vmdk", "name", "terraform-test-with-existing-vmdk"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.with_existing_vmdk", "vcpu", "2"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.with_existing_vmdk", "memory", "4096"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.with_existing_vmdk", "disk.#", "1"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.with_existing_vmdk", "disk.0.vmdk", vmdk_path),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.with_existing_vmdk", "disk.0.bootable", "true"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.with_existing_vmdk", "network_interface.#", "1"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.with_existing_vmdk", "network_interface.0.label", label),
),
},
},
})
}
func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error { func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*govmomi.Client) client := testAccProvider.Meta().(*govmomi.Client)
finder := find.NewFinder(client.Client, true) finder := find.NewFinder(client.Client, true)
@ -771,3 +833,23 @@ resource "vsphere_virtual_machine" "with_cdrom" {
} }
} }
` `
const testAccCheckVSphereVirtualMachineConfig_withExistingVmdk = `
resource "vsphere_virtual_machine" "with_existing_vmdk" {
name = "terraform-test-with-existing-vmdk"
%s
vcpu = 2
memory = 4096
gateway = "%s"
network_interface {
label = "%s"
ipv4_address = "%s"
ipv4_prefix_length = 24
}
disk {
%s
vmdk = "%s"
bootable = true
}
}
`

View File

@ -106,11 +106,13 @@ The `windows_opt_config` block supports:
The `disk` block supports: The `disk` block supports:
* `template` - (Required if size not provided) Template for this disk. * `template` - (Required if size and bootable_vmdk_path not provided) Template for this disk.
* `datastore` - (Optional) Datastore for this disk * `datastore` - (Optional) Datastore for this disk
* `size` - (Required if template not provided) Size of this disk (in GB). * `size` - (Required if template and bootable_vmdks_path not provided) Size of this disk (in GB).
* `iops` - (Optional) Number of virtual iops to allocate for this disk. * `iops` - (Optional) Number of virtual iops to allocate for this disk.
* `type` - (Optional) 'eager_zeroed' (the default), or 'thin' are supported options. * `type` - (Optional) 'eager_zeroed' (the default), or 'thin' are supported options.
* `vmdk` - (Required if template and size not provided) Path to a vmdk in a vSphere datastore.
* `bootable` - (Optional) Set to 'true' if a vmdk was given and it should attempt to boot after creation.
<a id="cdrom"></a> <a id="cdrom"></a>
## CDROM ## CDROM