mirror of https://github.com/apache/jclouds.git
Issue 824: templateBuilder param for hypervisor
This commit is contained in:
parent
e2c5f5ebce
commit
03c9dd9915
|
@ -326,7 +326,7 @@ Here's an example of creating and running a small linux node in the group webser
|
||||||
(make-option-map
|
(make-option-map
|
||||||
kw-memfn-1arg
|
kw-memfn-1arg
|
||||||
[:from-hardware :from-image :from-template
|
[: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-name-matches :os-description-matches :os-version-matches
|
||||||
:os-arch-matches :os-64-bit :image-name-matches
|
:os-arch-matches :os-64-bit :image-name-matches
|
||||||
:image-version-matches :image-description-matches :image-matches
|
:image-version-matches :image-description-matches :image-matches
|
||||||
|
|
|
@ -69,6 +69,11 @@ public interface TemplateBuilder {
|
||||||
*/
|
*/
|
||||||
TemplateBuilder biggest();
|
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.
|
* Configure this template to use a specific operating system image.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -89,6 +89,8 @@ public class TemplateBuilderImpl implements TemplateBuilder {
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
protected String hardwareId;
|
protected String hardwareId;
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
|
protected String hypervisor;
|
||||||
|
@VisibleForTesting
|
||||||
protected String imageVersion;
|
protected String imageVersion;
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
protected OsFamily osFamily;
|
protected OsFamily osFamily;
|
||||||
|
@ -347,12 +349,17 @@ public class TemplateBuilderImpl implements TemplateBuilder {
|
||||||
return "imageDescription(" + imageDescription + ")";
|
return "imageDescription(" + imageDescription + ")";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private final Predicate<Hardware> hardwareIdPredicate = new Predicate<Hardware>() {
|
private final Predicate<Hardware> hardwareIdPredicate = new Predicate<Hardware>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean apply(Hardware input) {
|
public boolean apply(Hardware input) {
|
||||||
boolean returnVal = true;
|
boolean returnVal = true;
|
||||||
if (hardwareId != null) {
|
if (hardwareId != null) {
|
||||||
returnVal = hardwareId.equals(input.getId());
|
returnVal = hardwareId.equals(input.getId());
|
||||||
|
// match our input params so that the later predicates pass.
|
||||||
|
if (returnVal) {
|
||||||
|
fromHardware(input);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return returnVal;
|
return returnVal;
|
||||||
}
|
}
|
||||||
|
@ -363,6 +370,26 @@ public class TemplateBuilderImpl implements TemplateBuilder {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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>() {
|
private final Predicate<Hardware> hardwareCoresPredicate = new Predicate<Hardware>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean apply(Hardware input) {
|
public boolean apply(Hardware input) {
|
||||||
|
@ -406,6 +433,8 @@ public class TemplateBuilderImpl implements TemplateBuilder {
|
||||||
return locationPredicate.toString();
|
return locationPredicate.toString();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if (hypervisor != null)
|
||||||
|
predicates.add(hypervisorPredicate);
|
||||||
predicates.add(hardwareCoresPredicate);
|
predicates.add(hardwareCoresPredicate);
|
||||||
predicates.add(hardwareRamPredicate);
|
predicates.add(hardwareRamPredicate);
|
||||||
}
|
}
|
||||||
|
@ -466,6 +495,7 @@ public class TemplateBuilderImpl implements TemplateBuilder {
|
||||||
this.location = hardware.getLocation();
|
this.location = hardware.getLocation();
|
||||||
this.minCores = getCores(hardware);
|
this.minCores = getCores(hardware);
|
||||||
this.minRam = hardware.getRam();
|
this.minRam = hardware.getRam();
|
||||||
|
this.hypervisor = hardware.getHypervisor();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -901,6 +931,16 @@ public class TemplateBuilderImpl implements TemplateBuilder {
|
||||||
@Override
|
@Override
|
||||||
public TemplateBuilder hardwareId(String hardwareId) {
|
public TemplateBuilder hardwareId(String hardwareId) {
|
||||||
this.hardwareId = hardwareId;
|
this.hardwareId = hardwareId;
|
||||||
|
this.hypervisor = null;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public TemplateBuilder hypervisorMatches(String hypervisor) {
|
||||||
|
this.hypervisor = hypervisor;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -916,10 +956,10 @@ public class TemplateBuilderImpl implements TemplateBuilder {
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
boolean nothingChangedExceptOptions() {
|
boolean nothingChangedExceptOptions() {
|
||||||
return osFamily == null && location == null && imageId == null && hardwareId == null && osName == null
|
return osFamily == null && location == null && imageId == null && hardwareId == null && hypervisor == null
|
||||||
&& imagePredicate == null && osDescription == null && imageVersion == null && osVersion == null
|
&& osName == null && imagePredicate == null && osDescription == null && imageVersion == null
|
||||||
&& osArch == null && os64Bit == null && imageName == null && imageDescription == null && minCores == 0
|
&& osVersion == null && osArch == null && os64Bit == null && imageName == null && imageDescription == null
|
||||||
&& minRam == 0 && !biggest && !fastest;
|
&& minCores == 0 && minRam == 0 && !biggest && !fastest;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -936,7 +976,7 @@ public class TemplateBuilderImpl implements TemplateBuilder {
|
||||||
+ imageDescription + ", imageId=" + imageId + ", imagePredicate=" + imagePredicate + ", imageVersion=" + imageVersion + ", location=" + location
|
+ imageDescription + ", imageId=" + imageId + ", imagePredicate=" + imagePredicate + ", imageVersion=" + imageVersion + ", location=" + location
|
||||||
+ ", minCores=" + minCores + ", minRam=" + minRam + ", osFamily=" + osFamily + ", osName=" + osName
|
+ ", minCores=" + minCores + ", minRam=" + minRam + ", osFamily=" + osFamily + ", osName=" + osName
|
||||||
+ ", osDescription=" + osDescription + ", osVersion=" + osVersion + ", osArch=" + osArch + ", os64Bit="
|
+ ", osDescription=" + osDescription + ", osVersion=" + osVersion + ", osArch=" + osArch + ", os64Bit="
|
||||||
+ os64Bit + ", hardwareId=" + hardwareId + "]";
|
+ os64Bit + ", hardwareId=" + hardwareId + ", hypervisor=" + hypervisor + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -32,9 +32,13 @@ import javax.inject.Provider;
|
||||||
import org.jclouds.compute.domain.Hardware;
|
import org.jclouds.compute.domain.Hardware;
|
||||||
import org.jclouds.compute.domain.HardwareBuilder;
|
import org.jclouds.compute.domain.HardwareBuilder;
|
||||||
import org.jclouds.compute.domain.Image;
|
import org.jclouds.compute.domain.Image;
|
||||||
|
import org.jclouds.compute.domain.ImageBuilder;
|
||||||
import org.jclouds.compute.domain.OperatingSystem;
|
import org.jclouds.compute.domain.OperatingSystem;
|
||||||
import org.jclouds.compute.domain.OsFamily;
|
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.TemplateBuilder;
|
||||||
|
import org.jclouds.compute.domain.Volume;
|
||||||
import org.jclouds.compute.options.TemplateOptions;
|
import org.jclouds.compute.options.TemplateOptions;
|
||||||
import org.jclouds.compute.predicates.ImagePredicates;
|
import org.jclouds.compute.predicates.ImagePredicates;
|
||||||
import org.jclouds.domain.Location;
|
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.Supplier;
|
||||||
import com.google.common.base.Suppliers;
|
import com.google.common.base.Suppliers;
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -385,7 +390,7 @@ public class TemplateBuilderImplTest {
|
||||||
// make sure big data is not in the exception message
|
// make sure big data is not in the exception message
|
||||||
assertEquals(
|
assertEquals(
|
||||||
e.getMessage(),
|
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(image);
|
||||||
verify(os);
|
verify(os);
|
||||||
verify(defaultTemplate);
|
verify(defaultTemplate);
|
||||||
|
@ -772,4 +777,107 @@ public class TemplateBuilderImplTest {
|
||||||
verify(templateBuilderProvider);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue