From 03c9dd9915238ab9580c8c2cabdd6ca4d73bf726 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 15 Feb 2012 11:55:30 +0100 Subject: [PATCH] Issue 824: templateBuilder param for hypervisor --- .../src/main/clojure/org/jclouds/compute2.clj | 2 +- .../compute/domain/TemplateBuilder.java | 5 + .../domain/internal/TemplateBuilderImpl.java | 52 ++++++++- .../internal/TemplateBuilderImplTest.java | 110 +++++++++++++++++- 4 files changed, 161 insertions(+), 8 deletions(-) diff --git a/compute/src/main/clojure/org/jclouds/compute2.clj b/compute/src/main/clojure/org/jclouds/compute2.clj index bb02907863..70026a97e4 100644 --- a/compute/src/main/clojure/org/jclouds/compute2.clj +++ b/compute/src/main/clojure/org/jclouds/compute2.clj @@ -326,7 +326,7 @@ Here's an example of creating and running a small linux node in the group webser (make-option-map kw-memfn-1arg [:from-hardware :from-image :from-template - :os-family :location-id :image-id :hardware-id + :os-family :location-id :image-id :hardware-id :hardware-matches :os-name-matches :os-description-matches :os-version-matches :os-arch-matches :os-64-bit :image-name-matches :image-version-matches :image-description-matches :image-matches diff --git a/compute/src/main/java/org/jclouds/compute/domain/TemplateBuilder.java b/compute/src/main/java/org/jclouds/compute/domain/TemplateBuilder.java index d99ddb589d..69251b70b7 100644 --- a/compute/src/main/java/org/jclouds/compute/domain/TemplateBuilder.java +++ b/compute/src/main/java/org/jclouds/compute/domain/TemplateBuilder.java @@ -68,6 +68,11 @@ public interface TemplateBuilder { * configure this template to the largest hardware, based on cores, ram, then disk */ TemplateBuilder biggest(); + + /** + * Configure this template to have an hypervisor that matches the regular expression + */ + TemplateBuilder hypervisorMatches(String hypervisorRegex); /** * Configure this template to use a specific operating system image. diff --git a/compute/src/main/java/org/jclouds/compute/domain/internal/TemplateBuilderImpl.java b/compute/src/main/java/org/jclouds/compute/domain/internal/TemplateBuilderImpl.java index 6c3e0f3c3a..17f1da2d43 100644 --- a/compute/src/main/java/org/jclouds/compute/domain/internal/TemplateBuilderImpl.java +++ b/compute/src/main/java/org/jclouds/compute/domain/internal/TemplateBuilderImpl.java @@ -89,6 +89,8 @@ public class TemplateBuilderImpl implements TemplateBuilder { @VisibleForTesting protected String hardwareId; @VisibleForTesting + protected String hypervisor; + @VisibleForTesting protected String imageVersion; @VisibleForTesting protected OsFamily osFamily; @@ -347,12 +349,17 @@ public class TemplateBuilderImpl implements TemplateBuilder { return "imageDescription(" + imageDescription + ")"; } }; + private final Predicate hardwareIdPredicate = new Predicate() { @Override public boolean apply(Hardware input) { boolean returnVal = true; if (hardwareId != null) { returnVal = hardwareId.equals(input.getId()); + // match our input params so that the later predicates pass. + if (returnVal) { + fromHardware(input); + } } return returnVal; } @@ -362,6 +369,26 @@ public class TemplateBuilderImpl implements TemplateBuilder { return "hardwareId(" + hardwareId + ")"; } }; + + private final Predicate hypervisorPredicate = new Predicate() { + @Override + public boolean apply(Hardware input) { + boolean returnVal = true; + if (hypervisor != null) { + if (input.getHypervisor() == null) + returnVal = false; + else + returnVal = input.getHypervisor().contains(hypervisor) + || input.getHypervisor().matches(hypervisor); + } + return returnVal; + } + + @Override + public String toString() { + return "hypervisorMatches(" + hypervisor + ")"; + } + }; private final Predicate hardwareCoresPredicate = new Predicate() { @Override @@ -406,6 +433,8 @@ public class TemplateBuilderImpl implements TemplateBuilder { return locationPredicate.toString(); } }); + if (hypervisor != null) + predicates.add(hypervisorPredicate); predicates.add(hardwareCoresPredicate); predicates.add(hardwareRamPredicate); } @@ -466,6 +495,7 @@ public class TemplateBuilderImpl implements TemplateBuilder { this.location = hardware.getLocation(); this.minCores = getCores(hardware); this.minRam = hardware.getRam(); + this.hypervisor = hardware.getHypervisor(); return this; } @@ -901,9 +931,19 @@ public class TemplateBuilderImpl implements TemplateBuilder { @Override public TemplateBuilder hardwareId(String hardwareId) { this.hardwareId = hardwareId; + this.hypervisor = null; return this; } - + + /** + * {@inheritDoc} + */ + @Override + public TemplateBuilder hypervisorMatches(String hypervisor) { + this.hypervisor = hypervisor; + return this; + } + /** * {@inheritDoc} */ @@ -916,10 +956,10 @@ public class TemplateBuilderImpl implements TemplateBuilder { @VisibleForTesting boolean nothingChangedExceptOptions() { - return osFamily == null && location == null && imageId == null && hardwareId == 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; + 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; } /** @@ -936,7 +976,7 @@ public class TemplateBuilderImpl implements TemplateBuilder { + imageDescription + ", imageId=" + imageId + ", imagePredicate=" + imagePredicate + ", imageVersion=" + imageVersion + ", location=" + location + ", minCores=" + minCores + ", minRam=" + minRam + ", osFamily=" + osFamily + ", osName=" + osName + ", osDescription=" + osDescription + ", osVersion=" + osVersion + ", osArch=" + osArch + ", os64Bit=" - + os64Bit + ", hardwareId=" + hardwareId + "]"; + + os64Bit + ", hardwareId=" + hardwareId + ", hypervisor=" + hypervisor + "]"; } @Override diff --git a/compute/src/test/java/org/jclouds/compute/domain/internal/TemplateBuilderImplTest.java b/compute/src/test/java/org/jclouds/compute/domain/internal/TemplateBuilderImplTest.java index c979c78489..20eb3528b6 100644 --- a/compute/src/test/java/org/jclouds/compute/domain/internal/TemplateBuilderImplTest.java +++ b/compute/src/test/java/org/jclouds/compute/domain/internal/TemplateBuilderImplTest.java @@ -32,9 +32,13 @@ import javax.inject.Provider; import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.ImageBuilder; import org.jclouds.compute.domain.OperatingSystem; import org.jclouds.compute.domain.OsFamily; +import org.jclouds.compute.domain.Processor; +import org.jclouds.compute.domain.Template; import org.jclouds.compute.domain.TemplateBuilder; +import org.jclouds.compute.domain.Volume; import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.predicates.ImagePredicates; import org.jclouds.domain.Location; @@ -43,6 +47,7 @@ import org.testng.annotations.Test; import com.google.common.base.Supplier; import com.google.common.base.Suppliers; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; /** @@ -385,7 +390,7 @@ public class TemplateBuilderImplTest { // make sure big data is not in the exception message assertEquals( e.getMessage(), - "no hardware profiles support images matching params: [biggest=false, fastest=false, imageName=null, imageDescription=null, imageId=myregion/imageId, imagePredicate=null, imageVersion=null, location=EasyMock for interface org.jclouds.domain.Location, minCores=0.0, minRam=0, osFamily=null, osName=null, osDescription=null, osVersion=null, osArch=null, os64Bit=false, hardwareId=null]"); + "no hardware profiles support images matching params: [biggest=false, fastest=false, imageName=null, imageDescription=null, imageId=myregion/imageId, imagePredicate=null, imageVersion=null, location=EasyMock for interface org.jclouds.domain.Location, minCores=0.0, minRam=0, osFamily=null, osName=null, osDescription=null, osVersion=null, osArch=null, os64Bit=false, hardwareId=null, hypervisor=null]"); verify(image); verify(os); verify(defaultTemplate); @@ -771,5 +776,108 @@ public class TemplateBuilderImplTest { verify(optionsProvider); verify(templateBuilderProvider); } + + @SuppressWarnings("unchecked") + @Test + public void testHardwareIdNullsHypervisor() { + Supplier> locations = Suppliers.> ofInstance(ImmutableSet + . of()); + Supplier> images = Suppliers.> ofInstance(ImmutableSet. of()); + Supplier> hardwares = Suppliers.> ofInstance(ImmutableSet + . of()); + Location defaultLocation = createMock(Location.class); + Provider optionsProvider = createMock(Provider.class); + Provider templateBuilderProvider = createMock(Provider.class); + replay(defaultLocation); + replay(optionsProvider); + replay(templateBuilderProvider); + + TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation, + optionsProvider, templateBuilderProvider); + + + template.hypervisorMatches("OpenVZ"); + + assertEquals(template.hardwareId, null); + assertEquals(template.hypervisor, "OpenVZ"); + + template.hardwareId("myid"); + assertEquals(template.hardwareId, "myid"); + assertEquals(template.hypervisor, null); + + + verify(defaultLocation); + verify(optionsProvider); + verify(templateBuilderProvider); + } + + @Test + public void testMatchesHardwareWithIdPredicate() { + final Location defaultLocation = createMock(Location.class); + + final Supplier> locations = Suppliers.> ofInstance(ImmutableSet + . of(defaultLocation)); + final Supplier> images = Suppliers.> ofInstance(ImmutableSet + . of( + new ImageBuilder() + .ids("Ubuntu 11.04 x64") + .name("Ubuntu 11.04 x64") + .description("Ubuntu 11.04 x64") + .operatingSystem( + OperatingSystem.builder().name("Ubuntu 11.04 x64").description("Ubuntu 11.04 x64") + .is64Bit(true).version("11.04").family(OsFamily.UBUNTU).build()).build(), + new ImageBuilder() + .ids("Ubuntu 11.04 64-bit") + .name("Ubuntu 11.04 64-bit") + .description("Ubuntu 11.04 64-bit") + .operatingSystem( + OperatingSystem.builder().name("Ubuntu 11.04 64-bit").description("Ubuntu 11.04 64-bit") + .is64Bit(true).version("11.04").family(OsFamily.UBUNTU).build()).build())); + + final Supplier> hardwares = Suppliers.> ofInstance(ImmutableSet + . of( + new HardwareBuilder() + .ids(String.format("datacenter(%s)platform(%s)cpuCores(%d)memorySizeMB(%d)diskSizeGB(%d)", + "Falkenberg", "Xen", 1, 512, 5)).ram(512) + .processors(ImmutableList.of(new Processor(1, 1.0))) + .volumes(ImmutableList. of(new VolumeImpl((float) 5, true, true))).hypervisor("Xen") + .location(defaultLocation) + .supportsImage(ImagePredicates.idIn(ImmutableSet.of("Ubuntu 11.04 x64"))).build(), + new HardwareBuilder() + .ids(String.format("datacenter(%s)platform(%s)cpuCores(%d)memorySizeMB(%d)diskSizeGB(%d)", + "Falkenberg", "OpenVZ", 1, 512, 5)).ram(512) + .processors(ImmutableList.of(new Processor(1, 1.0))) + .volumes(ImmutableList. of(new VolumeImpl((float) 5, true, true))).hypervisor("OpenVZ") + .location(defaultLocation) + .supportsImage(ImagePredicates.idIn(ImmutableSet.of("Ubuntu 11.04 64-bit"))).build())); + + final Provider optionsProvider = new Provider() { + + @Override + public TemplateOptions get() { + return new TemplateOptions(); + } + + }; + Provider templateBuilderProvider = new Provider() { + + @Override + public TemplateBuilder get() { + return createTemplateBuilder(null, locations, images, hardwares, defaultLocation, optionsProvider, this); + } + + }; + + replay(defaultLocation); + + TemplateBuilder templateBuilder = templateBuilderProvider.get().minRam(512).osFamily(OsFamily.UBUNTU) + .hypervisorMatches("OpenVZ").osVersionMatches("1[10].[10][04]").os64Bit(true); + + Template template = templateBuilder.build(); + assertEquals(template.getHardware().getHypervisor(), "OpenVZ"); + assertEquals(template.getImage().getId(), "Ubuntu 11.04 64-bit"); + + verify(defaultLocation); + } }