Fixes the hasBootableVmdk flag when attaching multiple disks (#7804)

The hasBootableFlag logic had a bug where it would only be set properly
if the bootable disk was the last specified.  Adding some bool logic
resolves the issue.  Also adding check to ensure only one bootable disk
is given, and cleaning up a redundant var.
This commit is contained in:
Dan Allegood 2016-07-26 16:25:56 -07:00 committed by Paul Stack
parent 5cacd788b5
commit 4b9b42dbf0
2 changed files with 14 additions and 8 deletions

View File

@ -789,7 +789,6 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
if diskSet, ok := vL.(*schema.Set); ok { if diskSet, ok := vL.(*schema.Set); ok {
disks := []hardDisk{} disks := []hardDisk{}
hasBootableDisk := false
for _, value := range diskSet.List() { for _, value := range diskSet.List() {
disk := value.(map[string]interface{}) disk := value.(map[string]interface{})
newDisk := hardDisk{} newDisk := hardDisk{}
@ -799,10 +798,10 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
return fmt.Errorf("Cannot specify name of a template") return fmt.Errorf("Cannot specify name of a template")
} }
vm.template = v vm.template = v
if hasBootableDisk { if vm.hasBootableVmdk {
return fmt.Errorf("[ERROR] Only one bootable disk or template may be given") return fmt.Errorf("[ERROR] Only one bootable disk or template may be given")
} }
hasBootableDisk = true vm.hasBootableVmdk = true
} }
if v, ok := disk["type"].(string); ok && v != "" { if v, ok := disk["type"].(string); ok && v != "" {
@ -846,9 +845,11 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
return fmt.Errorf("Cannot specify name of a vmdk") return fmt.Errorf("Cannot specify name of a vmdk")
} }
if vBootable, ok := disk["bootable"].(bool); ok { if vBootable, ok := disk["bootable"].(bool); ok {
hasBootableDisk = true if vBootable && vm.hasBootableVmdk {
return fmt.Errorf("[ERROR] Only one bootable disk or template may be given")
}
newDisk.bootable = vBootable newDisk.bootable = vBootable
vm.hasBootableVmdk = vBootable vm.hasBootableVmdk = vm.hasBootableVmdk || vBootable
} }
newDisk.vmdkPath = vVmdk newDisk.vmdkPath = vVmdk
} }

View File

@ -606,7 +606,12 @@ resource "vsphere_virtual_machine" "with_existing_vmdk" {
disk { disk {
%s %s
vmdk = "%s" vmdk = "%s"
bootable = true bootable = true
}
disk {
size = 1
iops = 500
name = "one"
} }
} }
` `
@ -635,7 +640,7 @@ func TestAccVSphereVirtualMachine_createWithExistingVmdk(t *testing.T) {
Config: config, Config: config,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
TestFuncData{vm: vm, label: data.label, vmName: "vsphere_virtual_machine.with_existing_vmdk", TestFuncData{vm: vm, label: data.label, vmName: "vsphere_virtual_machine.with_existing_vmdk",
vmResource: "terraform-test-with-existing-vmdk"}.testCheckFuncBasic(), vmResource: "terraform-test-with-existing-vmdk", numDisks: "2"}.testCheckFuncBasic(),
//resource.TestCheckResourceAttr( //resource.TestCheckResourceAttr(
// "vsphere_virtual_machine.with_existing_vmdk", "disk.2393891804.vmdk", vmdk_path), // "vsphere_virtual_machine.with_existing_vmdk", "disk.2393891804.vmdk", vmdk_path),
//resource.TestCheckResourceAttr( //resource.TestCheckResourceAttr(
@ -766,7 +771,7 @@ resource "vsphere_virtual_machine" "ipv4ipv6" {
disk { disk {
size = 1 size = 1
iops = 500 iops = 500
name = "one" name = "one"
} }
} }
` `