From c34ea953b0bd9afb07e783889f99f370c6825e2b Mon Sep 17 00:00:00 2001 From: dkalleg Date: Fri, 20 May 2016 23:21:42 -0700 Subject: [PATCH] provider/vSphere Disk Controller Type (#6785) Gives the user the ability to specify the controller type they would like to connect their disk to. Supported options are scsi and ide. --- .../resource_vsphere_virtual_machine.go | 33 +++++++++++++++---- .../resource_vsphere_virtual_machine_test.go | 11 ++++++- .../vsphere/r/virtual_machine.html.markdown | 1 + 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index 513a354b5..eade05c81 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -39,10 +39,11 @@ type networkInterface struct { } type hardDisk struct { - size int64 - iops int64 - initType string - vmdkPath string + size int64 + iops int64 + initType string + vmdkPath string + controller string } //Additional options Vsphere can use clones of windows machines @@ -377,6 +378,21 @@ func resourceVSphereVirtualMachine() *schema.Resource { Default: false, ForceNew: true, }, + + "controller_type": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Default: "scsi", + ForceNew: true, + ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + if value != "scsi" && value != "ide" { + errors = append(errors, fmt.Errorf( + "only 'scsi' and 'ide' are supported values for 'controller_type'")) + } + return + }, + }, }, }, }, @@ -667,6 +683,9 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ if v, ok := disk["type"].(string); ok && v != "" { disks[i].initType = v } + if v, ok := disk["controller_type"].(string); ok && v != "" { + disks[i].controller = v + } } vm.hardDisks = disks log.Printf("[DEBUG] disk init: %v", disks) @@ -873,14 +892,14 @@ func resourceVSphereVirtualMachineDelete(d *schema.ResourceData, meta interface{ } // addHardDisk adds a new Hard Disk to the VirtualMachine. -func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string, datastore *object.Datastore, diskPath string) error { +func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string, datastore *object.Datastore, diskPath string, controller_type string) error { devices, err := vm.Device(context.TODO()) if err != nil { return err } log.Printf("[DEBUG] vm devices: %#v\n", devices) - controller, err := devices.FindDiskController("scsi") + controller, err := devices.FindDiskController(controller_type) if err != nil { return err } @@ -1463,7 +1482,7 @@ func (vm *virtualMachine) setupVirtualMachine(c *govmomi.Client) error { } for i := firstDisk; i < len(vm.hardDisks); i++ { log.Printf("[DEBUG] disk index: %v", i) - err = addHardDisk(newVM, vm.hardDisks[i].size, vm.hardDisks[i].iops, vm.hardDisks[i].initType, datastore, vm.hardDisks[i].vmdkPath) + err = addHardDisk(newVM, vm.hardDisks[i].size, vm.hardDisks[i].iops, vm.hardDisks[i].initType, datastore, vm.hardDisks[i].vmdkPath, vm.hardDisks[i].controller) if err != nil { return err } diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go index c048e8ac9..817d3da21 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go @@ -125,13 +125,17 @@ func TestAccVSphereVirtualMachine_diskInitType(t *testing.T) { resource.TestCheckResourceAttr( "vsphere_virtual_machine.thin", "memory", "4096"), resource.TestCheckResourceAttr( - "vsphere_virtual_machine.thin", "disk.#", "2"), + "vsphere_virtual_machine.thin", "disk.#", "3"), resource.TestCheckResourceAttr( "vsphere_virtual_machine.thin", "disk.0.template", template), resource.TestCheckResourceAttr( "vsphere_virtual_machine.thin", "disk.0.type", "thin"), resource.TestCheckResourceAttr( "vsphere_virtual_machine.thin", "disk.1.type", "eager_zeroed"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.thin", "disk.1.controller_type", "scsi"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.thin", "disk.2.controller_type", "ide"), resource.TestCheckResourceAttr( "vsphere_virtual_machine.thin", "network_interface.#", "1"), resource.TestCheckResourceAttr( @@ -968,6 +972,11 @@ resource "vsphere_virtual_machine" "thin" { disk { size = 1 iops = 500 + controller_type = "scsi" + } + disk { + size = 1 + controller_type = "ide" } } ` diff --git a/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown b/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown index 4f80926bd..123920555 100644 --- a/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown +++ b/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown @@ -117,6 +117,7 @@ The `disk` block supports: * `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. +* `controller_type` = (Optional) Controller type to attach the disk to. 'scsi' (the default), or 'ide' are supported options. ## CDROM