From 03e0bd46ba387166e5ddc4a13847479f7ec43ee8 Mon Sep 17 00:00:00 2001 From: Mattias Holmqvist Date: Mon, 24 Oct 2011 00:10:00 +0200 Subject: [PATCH] Virtualbox: Re-used AttachMediumToMachineIfNotAlreadyAttached for attaching guest additions DVD. --- ...hMediumToMachineIfNotAlreadyAttached.java} | 21 +++++++------- .../virtualbox/functions/IsoToIMachine.java | 29 +++++++++---------- ...iumToMachineIfNotAlreadyAttachedTest.java} | 17 ++++++----- 3 files changed, 33 insertions(+), 34 deletions(-) rename sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/{AttachHardDiskToMachineIfNotAlreadyAttached.java => AttachMediumToMachineIfNotAlreadyAttached.java} (76%) rename sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/{AttachHardDiskToMachineIfNotAlreadyAttachedTest.java => AttachMediumToMachineIfNotAlreadyAttachedTest.java} (86%) diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachHardDiskToMachineIfNotAlreadyAttached.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachMediumToMachineIfNotAlreadyAttached.java similarity index 76% rename from sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachHardDiskToMachineIfNotAlreadyAttached.java rename to sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachMediumToMachineIfNotAlreadyAttached.java index 9c87cc5bd9..4d88b5e2e0 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachHardDiskToMachineIfNotAlreadyAttached.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachMediumToMachineIfNotAlreadyAttached.java @@ -29,28 +29,27 @@ import static org.virtualbox_4_1.DeviceType.HardDisk; /** * @author Mattias Holmqvist */ -public class AttachHardDiskToMachineIfNotAlreadyAttached implements Function { +public class AttachMediumToMachineIfNotAlreadyAttached implements Function { private String controllerIDE; private IMedium hardDisk; - private String adminDiskPath; - private String diskFormat; - private VirtualBoxManager manager; + private int controllerPort; + private int device; + private DeviceType deviceType; - public AttachHardDiskToMachineIfNotAlreadyAttached(String controllerIDE, IMedium hardDisk, VirtualBoxManager manager) { + public AttachMediumToMachineIfNotAlreadyAttached(String controllerIDE, IMedium hardDisk, int controllerPort, int device, DeviceType deviceType) { this.controllerIDE = controllerIDE; this.hardDisk = hardDisk; - this.manager = manager; + this.controllerPort = controllerPort; + this.device = device; + this.deviceType = deviceType; } @Override public Void apply(@Nullable IMachine machine) { - - // Create and attach hard disk - int controllerPort = 0; - int device = 1; + // Create and attach medium try { - machine.attachDevice(controllerIDE, controllerPort, device, HardDisk, hardDisk); + machine.attachDevice(controllerIDE, controllerPort, device, deviceType, hardDisk); machine.saveSettings(); } catch (VBoxException e) { if (!alreadyAttached(e)) diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java index e5d33fc08d..caddb16998 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java @@ -27,6 +27,7 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTA import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_WORKINGDIR; import static org.virtualbox_4_1.AccessMode.ReadOnly; import static org.virtualbox_4_1.DeviceType.DVD; +import static org.virtualbox_4_1.DeviceType.HardDisk; import static org.virtualbox_4_1.LockType.Shared; import static org.virtualbox_4_1.LockType.Write; @@ -119,8 +120,11 @@ public class IsoToIMachine implements Function { new File(adminDiskPath).delete(); } + // Create hard disk + IMedium hardDisk = new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(adminDiskPath); + // Attach hard disk to machine - ensureMachineHasHardDiskAttached(vmName, adminDiskPath); + ensureMachineHasHardDiskAttached(vmName, hardDisk); // NAT ensureNATNetworkingIsAppliedToMachine(vmName); @@ -129,16 +133,8 @@ public class IsoToIMachine implements Function { final IMedium guestAdditionsDvdMedium = manager.getVBox().openMedium(guestAdditionsDvd, DeviceType.DVD, AccessMode.ReadOnly, forceOverwrite); - lockMachineAndApply(manager, Write, vmName, new Function() { - - @Override - public Void apply(IMachine machine) { - machine.attachDevice(controllerIDE, 1, 1, DeviceType.DVD, guestAdditionsDvdMedium); - machine.saveSettings(); - return null; - } - - }); + // Guest additions + ensureGuestAdditionsMediumIsAttached(vmName, guestAdditionsDvdMedium); IProgress prog = vm.launchVMProcess(manager.getSessionObject(), "gui", ""); prog.waitForCompletion(-1); @@ -184,11 +180,14 @@ public class IsoToIMachine implements Function { return vm; } - private void ensureMachineHasHardDiskAttached(String vmName, String path) { - // Create hard disk - IMedium hardDisk = new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(path); + private void ensureGuestAdditionsMediumIsAttached(String vmName, final IMedium guestAdditionsDvdMedium) { lockMachineAndApply(manager, Write, vmName, - new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager)); + new AttachMediumToMachineIfNotAlreadyAttached(controllerIDE, guestAdditionsDvdMedium, 1, 1, DeviceType.DVD)); + } + + private void ensureMachineHasHardDiskAttached(String vmName, IMedium hardDisk) { + lockMachineAndApply(manager, Write, vmName, + new AttachMediumToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, 0, 1, HardDisk)); } private void ensureMachineHasMemory(String vmName, final long memorySize) { diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachHardDiskToMachineIfNotAlreadyAttachedTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachMediumToMachineIfNotAlreadyAttachedTest.java similarity index 86% rename from sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachHardDiskToMachineIfNotAlreadyAttachedTest.java rename to sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachMediumToMachineIfNotAlreadyAttachedTest.java index 0b519f2762..c684c44028 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachHardDiskToMachineIfNotAlreadyAttachedTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachMediumToMachineIfNotAlreadyAttachedTest.java @@ -23,12 +23,13 @@ import org.testng.annotations.Test; import org.virtualbox_4_1.*; import static org.easymock.classextension.EasyMock.*; +import static org.virtualbox_4_1.DeviceType.HardDisk; /** * @author Mattias Holmqvist */ -@Test(groups = "unit", testName = "AttachHardDiskToMachineIfNotAlreadyAttachedTest") -public class AttachHardDiskToMachineIfNotAlreadyAttachedTest { +@Test(groups = "unit", testName = "AttachMediumToMachineIfNotAlreadyAttachedTest") +public class AttachMediumToMachineIfNotAlreadyAttachedTest { @Test public void testAttachHardDiskIfNotAttached() throws Exception { @@ -49,11 +50,11 @@ public class AttachHardDiskToMachineIfNotAlreadyAttachedTest { expect(vBox.createHardDisk(diskFormat, adminDiskPath)).andReturn(hardDisk); expect(hardDisk.createBaseStorage(anyLong(), anyLong())).andReturn(progress); - machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.HardDisk, hardDisk); + machine.attachDevice(controllerIDE, controllerPort, device, HardDisk, hardDisk); machine.saveSettings(); replay(manager, machine, vBox, hardDisk); - new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine); + new AttachMediumToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, controllerPort, device, HardDisk).apply(machine); verify(machine); @@ -79,12 +80,12 @@ public class AttachHardDiskToMachineIfNotAlreadyAttachedTest { String isoAlreadyAttachedException = errorBuilder.toString(); VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), isoAlreadyAttachedException); - machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.HardDisk, hardDisk); + machine.attachDevice(controllerIDE, controllerPort, device, HardDisk, hardDisk); expectLastCall().andThrow(isoAttachedException); replay(manager, machine, vBox, hardDisk); - new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine); + new AttachMediumToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, controllerPort, device, HardDisk).apply(machine); verify(machine); @@ -108,12 +109,12 @@ public class AttachHardDiskToMachineIfNotAlreadyAttachedTest { String isoAlreadyAttachedException = errorBuilder.toString(); VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), isoAlreadyAttachedException); - machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.HardDisk, hardDisk); + machine.attachDevice(controllerIDE, controllerPort, device, HardDisk, hardDisk); expectLastCall().andThrow(isoAttachedException); replay(manager, machine, vBox, hardDisk); - new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine); + new AttachMediumToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, controllerPort, device, HardDisk).apply(machine); }