Merge upstream changes to template builder

This commit is contained in:
vijaykiran 2012-06-18 10:12:11 +02:00
parent 39e21ad652
commit bd675a22eb
4 changed files with 83 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;
}
/**

View File

@ -53,6 +53,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 +73,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 +124,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 +167,26 @@ 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_minRamRepeated() {
try {
parse("minRam=10, minRam=20");
@ -178,6 +201,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 +230,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 +300,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 +329,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 +359,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 +388,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 +417,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 +446,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 +475,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 +506,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 +538,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 +570,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 +600,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 +621,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);