Merge pull request #675 from vijaykiran/issue-305

Issue 305  - minDisk in TemplateBuilder Spec
This commit is contained in:
Adrian Cole 2012-06-18 09:14:05 -07:00
commit f686737df3
4 changed files with 94 additions and 1 deletions

View File

@ -181,6 +181,11 @@ public interface TemplateBuilder {
*/
TemplateBuilder minRam(int megabytes);
/**
* Configure this template with required minimum disk space in gigabytes
*/
TemplateBuilder minDisk(double gigabytes);
/**
* Generate an immutable template from the current builder.
*/

View File

@ -140,6 +140,7 @@ public class TemplateBuilderSpec implements Serializable {
.put("hardwareId", new HardwareIdParser())
.put("minCores", new MinCoresParser())
.put("minRam", new MinRamParser())
.put("minDisk", new MinDiskParser())
.put("hypervisorMatches", new HypervisorMatchesMatchesParser())
.put("imageId", new ImageIdParser())
.put("imageNameMatches", new ImageNameMatchesParser())
@ -160,6 +161,8 @@ public class TemplateBuilderSpec implements Serializable {
@VisibleForTesting
Integer minRam;
@VisibleForTesting
Double minDisk;
@VisibleForTesting
String hypervisorMatches;
@VisibleForTesting
String imageId;
@ -238,6 +241,9 @@ public class TemplateBuilderSpec implements Serializable {
if (minRam != null) {
builder.minRam(minRam);
}
if (minDisk != null) {
builder.minDisk(minDisk);
}
if (hypervisorMatches != null) {
builder.hypervisorMatches(hypervisorMatches);
}
@ -435,6 +441,16 @@ public class TemplateBuilderSpec implements Serializable {
spec.minRam = value;
}
}
/** Parse minDisk */
static class MinDiskParser extends DoubleParser {
@Override
protected void parseDouble(TemplateBuilderSpec spec, double value) {
checkArgument(spec.minDisk == null, "min disk was already set to ", spec.minDisk);
checkArgument(spec.hardwareId == null, "hardware id was already set to ", spec.hardwareId);
spec.minDisk = value;
}
}
/** Parse hypervisorMatches */
static class HypervisorMatchesMatchesParser extends StringParser {

View File

@ -119,6 +119,8 @@ public class TemplateBuilderImpl implements TemplateBuilder {
@VisibleForTesting
protected int minRam;
@VisibleForTesting
protected double minDisk;
@VisibleForTesting
protected boolean biggest;
@VisibleForTesting
protected boolean fastest;
@ -392,6 +394,18 @@ public class TemplateBuilderImpl implements TemplateBuilder {
}
};
private final Predicate<Hardware> hardwareDiskPredicate = new Predicate<Hardware>() {
@Override
public boolean apply(Hardware input) {
return getSpace(input) >= TemplateBuilderImpl.this.minDisk;
}
@Override
public String toString() {
return "minDisk(" + minDisk + ")";
}
};
private final Predicate<Hardware> hardwareRamPredicate = new Predicate<Hardware>() {
@Override
public boolean apply(Hardware input) {
@ -480,6 +494,7 @@ public class TemplateBuilderImpl implements TemplateBuilder {
this.location = hardware.getLocation();
this.minCores = getCores(hardware);
this.minRam = hardware.getRam();
this.minDisk = getSpace(hardware);
this.hypervisor = hardware.getHypervisor();
return this;
}
@ -932,6 +947,15 @@ public class TemplateBuilderImpl implements TemplateBuilder {
return this;
}
/**
* {@inheritDoc}
*/
@Override
public TemplateBuilder minDisk(double gigabytes) {
this.minDisk = gigabytes;
return this;
}
/**
* {@inheritDoc}
*/
@ -984,7 +1008,7 @@ public class TemplateBuilderImpl implements TemplateBuilder {
return osFamily == null && location == null && imageId == null && hardwareId == null && hypervisor == null
&& osName == null && imagePredicate == null && osDescription == null && imageVersion == null
&& osVersion == null && osArch == null && os64Bit == null && imageName == null && imageDescription == null
&& minCores == 0 && minRam == 0 && !biggest && !fastest;
&& minCores == 0 && minRam == 0 && minDisk == 0 && !biggest && !fastest;
}
/**
@ -1022,6 +1046,8 @@ public class TemplateBuilderImpl implements TemplateBuilder {
toString.add("minRam", minRam);
if (minRam >0) //TODO: make non-primitive
toString.add("minRam", minRam);
if (minDisk >0) //TODO: make non-primitive
toString.add("minDisk", minDisk);
toString.add("osFamily", osFamily);
toString.add("osName", osName);
toString.add("osDescription", osDescription);

View File

@ -23,6 +23,7 @@ import static org.jclouds.compute.domain.TemplateBuilderSpec.parse;
import static org.jclouds.compute.options.TemplateOptions.Builder.overrideLoginCredentials;
import static org.jclouds.compute.options.TemplateOptions.Builder.overrideLoginUser;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.fail;
@ -53,6 +54,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
@ -72,6 +74,7 @@ public class TemplateBuilderSpecTest {
assertEquals(spec.hardwareId, "m1.small");
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
@ -122,6 +125,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertEquals(32, spec.minCores.intValue());
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
@ -164,6 +168,34 @@ public class TemplateBuilderSpecTest {
assertTemplateBuilderEquivalence(templateBuilders.get().minRam(10), templateBuilders.get().from(spec));
}
public void testParse_minDisk() {
TemplateBuilderSpec spec = parse("minDisk=10");
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertEquals(spec.minDisk.doubleValue(), 10.0);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
assertNull(spec.osFamily);
assertNull(spec.osVersionMatches);
assertNull(spec.os64Bit);
assertNull(spec.osArchMatches);
assertNull(spec.osDescriptionMatches);
assertNull(spec.loginUser);
assertNull(spec.authenticateSudo);
assertNull(spec.locationId);
assertTemplateBuilderEquivalence(templateBuilders.get().minRam(10), templateBuilders.get().from(spec));
}
public void testParse_minDiskIsNotEqual() {
TemplateBuilderSpec spec1 = parse("minDisk=10");
TemplateBuilderSpec spec2 = parse("minDisk=20");
assertTemplateBuilderEquivalence(templateBuilders.get().minDisk(10), templateBuilders.get().from(spec1));
assertTemplateBuilderEquivalence(templateBuilders.get().minDisk(20), templateBuilders.get().from(spec2));
assertNotEquals(spec1.minDisk, spec2.minDisk);
}
public void testParse_minRamRepeated() {
try {
parse("minRam=10, minRam=20");
@ -178,6 +210,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertEquals(spec.hypervisorMatches, "OpenVZ");
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
@ -206,6 +239,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertEquals(spec.imageId, "us-east-1/ami-fffffff");
assertNull(spec.imageNameMatches);
@ -275,6 +309,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertEquals(spec.imageNameMatches, ".*w/ None.*");
@ -303,6 +338,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
@ -332,6 +368,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
@ -360,6 +397,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
@ -388,6 +426,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
@ -416,6 +455,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
@ -444,6 +484,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
@ -474,6 +515,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
@ -505,6 +547,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
assertNull(spec.hypervisorMatches);
@ -536,6 +579,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);
@ -565,6 +609,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertNull(spec.minRam);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertEquals(spec.imageNameMatches, ".*w/ None.*");
@ -585,6 +630,7 @@ public class TemplateBuilderSpecTest {
assertNull(spec.hardwareId);
assertNull(spec.minCores);
assertEquals(spec.minRam.intValue(), 10);
assertNull(spec.minDisk);
assertNull(spec.hypervisorMatches);
assertNull(spec.imageId);
assertNull(spec.imageNameMatches);