Moved the VmSpecification from the constructor in IsoToIMachine to apply().

This commit is contained in:
Mattias Holmqvist 2011-12-15 01:55:02 +01:00
parent 6712e661ce
commit 22195894cc
4 changed files with 16 additions and 22 deletions

View File

@ -48,7 +48,7 @@ import static org.jclouds.virtualbox.util.MachineUtils.*;
import static org.virtualbox_4_1.LockType.Shared; import static org.virtualbox_4_1.LockType.Shared;
import static org.virtualbox_4_1.LockType.Write; import static org.virtualbox_4_1.LockType.Write;
public class IsoToIMachine implements Function<String, IMachine> { public class IsoToIMachine implements Function<VmSpecification, IMachine> {
@Resource @Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER) @Named(ComputeServiceConstants.COMPUTE_LOGGER)
@ -56,7 +56,6 @@ public class IsoToIMachine implements Function<String, IMachine> {
private final VirtualBoxManager manager; private final VirtualBoxManager manager;
private String guestId; private String guestId;
private final VmSpecification vmSpecification;
private final ComputeServiceContext context; private final ComputeServiceContext context;
private final String hostId; private final String hostId;
private final Predicate<IPSocket> socketTester; private final Predicate<IPSocket> socketTester;
@ -65,13 +64,11 @@ public class IsoToIMachine implements Function<String, IMachine> {
private final ExecutionType executionType; private final ExecutionType executionType;
@Inject @Inject
public IsoToIMachine(VirtualBoxManager manager, String guestId, public IsoToIMachine(VirtualBoxManager manager, String guestId, ComputeServiceContext context,
VmSpecification vmSpecification, ComputeServiceContext context,
String hostId, Predicate<IPSocket> socketTester, String hostId, Predicate<IPSocket> socketTester,
String webServerHost, int webServerPort, ExecutionType executionType) { String webServerHost, int webServerPort, ExecutionType executionType) {
this.manager = manager; this.manager = manager;
this.guestId = guestId; this.guestId = guestId;
this.vmSpecification = vmSpecification;
this.context = context; this.context = context;
this.hostId = hostId; this.hostId = hostId;
this.socketTester = socketTester; this.socketTester = socketTester;
@ -81,7 +78,7 @@ public class IsoToIMachine implements Function<String, IMachine> {
} }
@Override @Override
public IMachine apply(@Nullable String isoName) { public IMachine apply(VmSpecification vmSpecification) {
ensureWebServerIsRunning(); ensureWebServerIsRunning();
@ -94,14 +91,12 @@ public class IsoToIMachine implements Function<String, IMachine> {
Set<StorageController> controllers = vmSpecification.getControllers(); Set<StorageController> controllers = vmSpecification.getControllers();
if (controllers.isEmpty()) { if (controllers.isEmpty()) {
throw new IllegalStateException(missingIDEControllersMessage()); throw new IllegalStateException(missingIDEControllersMessage(vmSpecification));
} }
StorageController controller = controllers.iterator().next(); StorageController controller = controllers.iterator().next();
ensureMachineHasIDEControllerNamed(vmName, controller); ensureMachineHasIDEControllerNamed(vmName, controller);
setupHardDisksForController(vmName, controller); setupHardDisksForController(vmName, controller);
setupDvdsForController(vmName, controller); setupDvdsForController(vmSpecification, vmName, controller);
missingIDEControllersMessage();
// NAT // NAT
Map<Long, NatAdapter> natNetworkAdapters = vmSpecification.getNatNetworkAdapters(); Map<Long, NatAdapter> natNetworkAdapters = vmSpecification.getNatNetworkAdapters();
@ -143,7 +138,7 @@ public class IsoToIMachine implements Function<String, IMachine> {
return vm; return vm;
} }
private void setupDvdsForController(String vmName, StorageController controller) { private void setupDvdsForController(VmSpecification vmSpecification, String vmName, StorageController controller) {
Set<IsoImage> dvds = controller.getIsoImages(); Set<IsoImage> dvds = controller.getIsoImages();
for (IsoImage dvd : dvds) { for (IsoImage dvd : dvds) {
String dvdSource = dvd.getSourcePath(); String dvdSource = dvd.getSourcePath();
@ -168,7 +163,7 @@ public class IsoToIMachine implements Function<String, IMachine> {
} }
} }
private String missingIDEControllersMessage() { private String missingIDEControllersMessage(VmSpecification vmSpecification) {
return String.format("First controller is not an IDE controller. Please verify that the VM spec is a correct master node: %s", vmSpecification); return String.format("First controller is not an IDE controller. Please verify that the VM spec is a correct master node: %s", vmSpecification);
} }

View File

@ -92,9 +92,8 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
VmSpecification vmSpecification = VmSpecification.builder().id(vmId).name(vmName).osTypeId(osTypeId) VmSpecification vmSpecification = VmSpecification.builder().id(vmId).name(vmName).osTypeId(osTypeId)
.controller(ideController) .controller(ideController)
.forceOverwrite(true).build(); .forceOverwrite(true).build();
return new IsoToIMachine(manager, guestId, return new IsoToIMachine(manager, guestId, localHostContext, hostId, socketTester,
vmSpecification, localHostContext, hostId, socketTester, "127.0.0.1", 8080, HEADLESS).apply(vmSpecification);
"127.0.0.1", 8080, HEADLESS).apply(isoName);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// already created // already created
return manager.getVBox().findMachine(vmName); return manager.getVBox().findMachine(vmName);

View File

@ -95,9 +95,9 @@ public class IsoToIMachineLiveTest extends BaseVirtualBoxClientLiveTest {
.controller(ideController) .controller(ideController)
.forceOverwrite(true) .forceOverwrite(true)
.natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()).build(); .natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()).build();
IMachine imageMachine = new IsoToIMachine(manager, guestId, vmSpecification, localHostContext, hostId, IMachine imageMachine = new IsoToIMachine(manager, guestId, localHostContext, hostId,
socketTester, "127.0.0.1", 8080, HEADLESS) socketTester, "127.0.0.1", 8080, HEADLESS)
.apply("ubuntu-11.04-server-i386.iso"); .apply(vmSpecification);
IMachineToImage iMachineToImage = new IMachineToImage(manager, map); IMachineToImage iMachineToImage = new IMachineToImage(manager, map);
Image newImage = iMachineToImage.apply(imageMachine); Image newImage = iMachineToImage.apply(imageMachine);

View File

@ -38,7 +38,7 @@ public class SshAvailableLiveTest extends BaseVirtualBoxClientLiveTest {
public void testSshDaemonIsRunning() { public void testSshDaemonIsRunning() {
VirtualBoxManager manager = (VirtualBoxManager) context.getProviderSpecificContext().getApi(); VirtualBoxManager manager = (VirtualBoxManager) context.getProviderSpecificContext().getApi();
ComputeServiceContext localHostContext = computeServiceForLocalhostAndGuest( ComputeServiceContext localHostContext = computeServiceForLocalhostAndGuest(
hostId, "localhost", guestId, "localhost", new Credentials("toor","password")); hostId, "localhost", guestId, "localhost", new Credentials("toor", "password"));
getNodeWithSshDaemonRunning(manager, localHostContext); getNodeWithSshDaemonRunning(manager, localHostContext);
ensureMachineIsLaunched(vmName); ensureMachineIsLaunched(vmName);
@ -68,14 +68,14 @@ public class SshAvailableLiveTest extends BaseVirtualBoxClientLiveTest {
String workingDir = PropertyUtils.getWorkingDirFromProperty(); String workingDir = PropertyUtils.getWorkingDirFromProperty();
StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE) StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE)
.attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso") .attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso")
.attachHardDisk(0, 1, workingDir + "/testadmin.vdi").build(); .attachHardDisk(0, 1, workingDir + "/testadmin.vdi").build();
VmSpecification vmSpecification = VmSpecification.builder().id(vmId).name(vmName).osTypeId("") VmSpecification vmSpecification = VmSpecification.builder().id(vmId).name(vmName).osTypeId("")
.controller(ideController) .controller(ideController)
.forceOverwrite(true).build(); .forceOverwrite(true).build();
return new IsoToIMachine(manager, guestId, vmSpecification, localHostContext, return new IsoToIMachine(manager, guestId, localHostContext,
hostId, socketTester, "127.0.0.1", 8080, HEADLESS).apply(isoName); hostId, socketTester, "127.0.0.1", 8080, HEADLESS).apply(vmSpecification);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// already created // already created
return manager.getVBox().findMachine(vmName); return manager.getVBox().findMachine(vmName);