Issue 824: templateBuilder param for hypervisor

This commit is contained in:
Adrian Cole 2012-02-15 11:55:30 +01:00
parent 95935f861c
commit 6c8ff2a9fd
4 changed files with 161 additions and 8 deletions

View File

@ -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

View File

@ -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.

View File

@ -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<Hardware> hardwareIdPredicate = new Predicate<Hardware>() {
@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<Hardware> hypervisorPredicate = new Predicate<Hardware>() {
@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<Hardware> hardwareCoresPredicate = new Predicate<Hardware>() {
@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

View File

@ -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<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of());
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of());
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of());
Location defaultLocation = createMock(Location.class);
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> 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<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(defaultLocation));
final Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
.<Image> 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<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> 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.<Volume> 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.<Volume> of(new VolumeImpl((float) 5, true, true))).hypervisor("OpenVZ")
.location(defaultLocation)
.supportsImage(ImagePredicates.idIn(ImmutableSet.of("Ubuntu 11.04 64-bit"))).build()));
final Provider<TemplateOptions> optionsProvider = new Provider<TemplateOptions>() {
@Override
public TemplateOptions get() {
return new TemplateOptions();
}
};
Provider<TemplateBuilder> templateBuilderProvider = new Provider<TemplateBuilder>() {
@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);
}
}