Added support for memory to VmSpec.

This commit is contained in:
Mattias Holmqvist 2011-12-15 23:15:29 +01:00
parent 91e5351ef3
commit 6d435742e1
4 changed files with 35 additions and 18 deletions

View File

@ -23,6 +23,7 @@ import com.google.common.base.Objects;
import java.util.*;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
/**
@ -33,14 +34,16 @@ public class VmSpec {
private final String vmName;
private final String osTypeId;
private final String vmId;
private final long memory;
private final boolean forceOverwrite;
private final Map<Long, NatAdapter> natNetworkAdapters;
private final Set<StorageController> controllers;
public VmSpec(String vmId, String vmName, String osTypeId, boolean forceOverwrite, Set<StorageController> controllers, Map<Long, NatAdapter> natNetworkAdapters) {
public VmSpec(String vmId, String vmName, String osTypeId, long memory, boolean forceOverwrite, Set<StorageController> controllers, Map<Long, NatAdapter> natNetworkAdapters) {
this.vmId = vmId;
this.vmName = vmName;
this.osTypeId = osTypeId;
this.memory = memory;
this.controllers = controllers;
this.forceOverwrite = forceOverwrite;
this.natNetworkAdapters = natNetworkAdapters;
@ -53,12 +56,13 @@ public class VmSpec {
public static class Builder {
private Set<StorageController> controllers = new HashSet<StorageController>();
private String name;
private String id;
private String osTypeId = "";
private boolean forceOverwrite;
private Map<Long, NatAdapter> natNetworkAdapters = new HashMap<Long, NatAdapter>();
private long memory;
public Builder controller(StorageController controller) {
controllers.add(controller);
return this;
@ -89,12 +93,22 @@ public class VmSpec {
return this;
}
public Builder memoryMB(int memorySize) {
this.memory = (long) memorySize;
return this;
}
public VmSpec build() {
checkNotNull(name, "name");
checkNotNull(id, "id");
return new VmSpec(id, name, osTypeId, forceOverwrite, controllers, natNetworkAdapters);
checkArgument(memory > 0, "Memory must be set");
return new VmSpec(id, name, osTypeId, memory, forceOverwrite, controllers, natNetworkAdapters);
}
}
public String getVmId() {
return vmId;
}
public String getVmName() {
@ -105,8 +119,8 @@ public class VmSpec {
return osTypeId;
}
public String getVmId() {
return vmId;
public long getMemory() {
return memory;
}
public boolean isForceOverwrite() {
@ -129,6 +143,7 @@ public class VmSpec {
return Objects.equal(vmId, other.vmId) &&
Objects.equal(vmName, other.vmName) &&
Objects.equal(osTypeId, other.osTypeId) &&
Objects.equal(memory, other.memory) &&
Objects.equal(forceOverwrite, other.forceOverwrite) &&
Objects.equal(natNetworkAdapters, other.natNetworkAdapters) &&
Objects.equal(controllers, other.controllers);
@ -138,7 +153,7 @@ public class VmSpec {
@Override
public int hashCode() {
return Objects.hashCode(vmId, vmName, osTypeId, forceOverwrite, natNetworkAdapters, controllers);
return Objects.hashCode(vmId, vmName, osTypeId, memory, forceOverwrite, natNetworkAdapters, controllers);
}
@Override
@ -146,6 +161,7 @@ public class VmSpec {
return "VmSpecification{" +
"vmName='" + vmName + '\'' +
", osTypeId='" + osTypeId + '\'' +
", memory='" + memory + '\'' +
", vmId='" + vmId + '\'' +
", forceOverwrite=" + forceOverwrite +
", natNetworkAdapters=" + natNetworkAdapters +

View File

@ -77,28 +77,28 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
}
@Override
public IMachine apply(VmSpec vmSpecification) {
public IMachine apply(VmSpec vmSpec) {
ensureWebServerIsRunning();
final IMachine vm = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(vmSpecification);
final IMachine vm = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(vmSpec);
String vmName = vmSpecification.getVmName();
String vmName = vmSpec.getVmName();
// Change RAM
ensureMachineHasMemory(vmName, 1024l);
ensureMachineHasMemory(vmName, vmSpec.getMemory());
Set<StorageController> controllers = vmSpecification.getControllers();
Set<StorageController> controllers = vmSpec.getControllers();
if (controllers.isEmpty()) {
throw new IllegalStateException(missingIDEControllersMessage(vmSpecification));
throw new IllegalStateException(missingIDEControllersMessage(vmSpec));
}
StorageController controller = controllers.iterator().next();
ensureMachineHasIDEControllerNamed(vmName, controller);
setupHardDisksForController(vmName, controller);
setupDvdsForController(vmSpecification, vmName, controller);
setupDvdsForController(vmSpec, vmName, controller);
// NAT
Map<Long, NatAdapter> natNetworkAdapters = vmSpecification.getNatNetworkAdapters();
Map<Long, NatAdapter> natNetworkAdapters = vmSpec.getNatNetworkAdapters();
for (Map.Entry<Long, NatAdapter> natAdapterAndSlot : natNetworkAdapters.entrySet()) {
long slotId = natAdapterAndSlot.getKey();
NatAdapter natAdapter = natAdapterAndSlot.getValue();

View File

@ -67,6 +67,7 @@ public class VmSpecTest {
.id("MyVmId")
.name("My VM")
.osTypeId("Ubuntu")
.memoryMB(1024)
.natNetworkAdapter(
0,
NatAdapter.builder().tcpRedirectRule("localhost", 2222, "", 22).build())

View File

@ -1,4 +1,4 @@
/**
/*
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@ -49,7 +49,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
IVirtualBox vBox = createMock(IVirtualBox.class);
String vmName = "jclouds-image-my-ubuntu-image";
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).osTypeId("").build();
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).osTypeId("").memoryMB(1024).build();
IMachine createdMachine = createMock(IMachine.class);
@ -90,7 +90,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
replay(manager, vBox);
VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").build();
VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").memoryMB(1024).build();
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification);
}
@ -111,7 +111,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
replay(manager, vBox);
VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").build();
VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").memoryMB(1024).build();
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification);
}