JCLOUDS-482: Add support for ProfitBricks

This commit is contained in:
Iván Lomba 2016-08-13 19:32:54 +02:00 committed by Ignasi Barrera
parent 79e95c5a4c
commit aac9b5fd5d
11 changed files with 240 additions and 60 deletions

View File

@ -16,6 +16,7 @@
*/
package org.jclouds.compute.domain.internal;
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.Hardware;
@ -46,9 +47,13 @@ public class ArbitraryCpuRamTemplateBuilderImpl extends TemplateBuilderImpl {
super(locations, images, hardwares, defaultLocation, optionsProvider, defaultTemplateProvider);
}
protected Hardware automaticHardwareForCpuAndRam(double cores, int ram) {
return new HardwareBuilder()
.id(automaticHardwareIdSpecBuilder(cores, ram).toString())
protected Hardware automaticHardware(double cores, int ram, Optional<Float> diskSize) {
HardwareBuilder builder = new HardwareBuilder();
if (diskSize.isPresent() && diskSize.get() > 0.0f) {
builder.volume(new VolumeImpl(diskSize.get(), true, true));
}
return builder
.id(automaticHardwareIdSpecBuilder(cores, ram, diskSize).toString())
.ram(ram)
.processor(new Processor(cores, 1.0))
.build();
@ -60,24 +65,24 @@ public class ArbitraryCpuRamTemplateBuilderImpl extends TemplateBuilderImpl {
} catch (NoSuchElementException ex) {
if (isAutomaticId(hardwareId)) {
AutomaticHardwareIdSpec spec = parseId(hardwareId);
return automaticHardwareForCpuAndRam(spec.getCores(), spec.getRam());
return automaticHardware(spec.getCores(), spec.getRam(), spec.getDisk());
}
else {
throw ex;
}
}
}
}
}
protected Hardware resolveHardware(Set<? extends Hardware> hardwarel, final Iterable<? extends Image> images) {
try {
return super.resolveHardware(hardwarel, images);
}
catch (NoSuchElementException ex) {
if (super.minCores != 0 && super.minRam != 0) {
return automaticHardwareForCpuAndRam(minCores, minRam);
if (minCores <= 0 || minRam == 0 || minDisk < 0) {
throw new IllegalArgumentException("No hardware profile matching the given criteria was found. If you" +
" want to use exact values, please set the minCores, minRam and minDisk to positive values.");
}
else throw new IllegalArgumentException("No hardware profile matching the given criteria was found. If " +
"you want to use exact values, please set the minCores and minRam values", ex);
return automaticHardware(minCores, minRam, minDisk == 0 ? Optional.<Float>absent() : Optional.of((float)minDisk));
}
}

View File

@ -16,6 +16,7 @@
*/
package org.jclouds.compute.util;
import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import java.util.Map;
@ -24,6 +25,7 @@ public class AutomaticHardwareIdSpec {
private double cores;
private int ram;
private Optional<Float> disk = Optional.absent();
public static boolean isAutomaticId(String id) {
return id.startsWith("automatic:");
@ -41,17 +43,30 @@ public class AutomaticHardwareIdSpec {
throw new IllegalArgumentException(String.format("Omitted keys on hardwareId: %s. Please set number " +
"of cores and ram amount.", hardwareId));
}
if (specValues.containsKey("disk")) {
float disk = Float.parseFloat(specValues.get("disk"));
if (disk > 0.0f) {
spec.disk = Optional.of(disk);
}
else {
throw new IllegalArgumentException(String.format("Invalid disk value: %s", hardwareId));
}
}
spec.ram = Integer.parseInt(specValues.get("ram"));
spec.cores = Double.parseDouble(specValues.get("cores"));
return spec;
}
public static AutomaticHardwareIdSpec automaticHardwareIdSpecBuilder(double cores, int ram) {
public static AutomaticHardwareIdSpec automaticHardwareIdSpecBuilder(double cores, int ram, Optional<Float> disk) {
AutomaticHardwareIdSpec spec = new AutomaticHardwareIdSpec();
if (cores == 0 || ram == 0) {
throw new IllegalArgumentException(String.format("Omitted or wrong minCores and minRam. If you" +
" want to use exact values, please set the minCores and minRam values."));
if (cores <= 0 || ram == 0) {
throw new IllegalArgumentException(String.format("Omitted or wrong minCores and minRam. If you want to" +
" use exact values, please set the minCores and minRam values: cores=%s, ram=%s", cores, ram));
}
if (disk.isPresent() && disk.get() <= 0.0f) {
throw new IllegalArgumentException(String.format("Invalid disk value: %.0f", disk.get()));
}
spec.disk = disk;
spec.cores = cores;
spec.ram = ram;
return spec;
@ -59,7 +74,12 @@ public class AutomaticHardwareIdSpec {
@Override
public String toString() {
return String.format("automatic:cores=%s;ram=%s", cores, ram);
if (disk.isPresent()) {
return String.format("automatic:cores=%s;ram=%s;disk=%.0f", cores, ram, disk.get().floatValue());
}
else {
return String.format("automatic:cores=%s;ram=%s", cores, ram);
}
}
public double getCores() {
@ -69,4 +89,8 @@ public class AutomaticHardwareIdSpec {
public int getRam() {
return ram;
}
public Optional<Float> getDisk() {
return disk;
}
}

View File

@ -90,8 +90,8 @@ public class ArbitraryCpuRamTemplateBuilderImplTest {
.uri(URI.create("uri"))
.build();
private final String errorMessage = "No hardware profile matching the given criteria was found. " +
"If you want to use exact values, please set the minCores and minRam values";
private final String errorMessage = "No hardware profile matching the given criteria was found. If you want to use" +
" exact values, please set the minCores, minRam and minDisk to positive values.";
@Test
public void testAutoGeneratedHardwareFromId(){
@ -111,14 +111,20 @@ public class ArbitraryCpuRamTemplateBuilderImplTest {
TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region),
optionsProvider, templateBuilderProvider);
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region),
optionsProvider, templateBuilderProvider);
Hardware hardware = templateBuilder.hardwareId("automatic:cores=2;ram=256").build().getHardware();
assertThat(hardware.getRam()).isEqualTo(256);
assertThat(hardware.getProcessors()).extracting("cores").containsExactly(2.0);
assertThat(hardware.getId()).isEqualTo("automatic:cores=2.0;ram=256");
Hardware hardware2 = templateBuilder.hardwareId("automatic:cores=2;ram=256;disk=100").build().getHardware();
assertThat(hardware2.getRam()).isEqualTo(256);
assertThat(hardware2.getProcessors()).extracting("cores").containsExactly(2.0);
assertThat(hardware2.getId()).isEqualTo("automatic:cores=2.0;ram=256;disk=100");
assertThat(hardware2.getVolumes().get(0).getSize()).isEqualTo(100);
}
@Test
@ -137,8 +143,8 @@ public class ArbitraryCpuRamTemplateBuilderImplTest {
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minRam(1024);
templateBuilder.minCores(4);
Template template = templateBuilder.build();
@ -148,6 +154,62 @@ public class ArbitraryCpuRamTemplateBuilderImplTest {
assertThat(hardware.getId()).isEqualTo("automatic:cores=4.0;ram=1024");
}
@Test
public void testAutoGeneratedHardwareWithMinCoresMinRamAndMinDisk() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>>ofInstance(ImmutableSet.of(region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>>ofInstance(ImmutableSet.of(image));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>>ofInstance(ImmutableSet
.<Hardware>of(hardware));
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(new TemplateOptions());
expect(getImageStrategy.getImage(anyObject(String.class))).andReturn(null);
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minCores(2);
templateBuilder.minRam(2048);
templateBuilder.minDisk(100);
Template template = templateBuilder.build();
Hardware hardware = template.getHardware();
assertThat(hardware.getRam()).isEqualTo(2048);
assertThat(hardware.getProcessors()).extracting("cores").containsExactly(2.0);
assertThat(hardware.getId()).isEqualTo("automatic:cores=2.0;ram=2048;disk=100");
assertThat(hardware.getVolumes().get(0).getSize()).isEqualTo(100);
}
@Test(expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = errorMessage)
public void testAutoGeneratedHardwareWithMinCoresMinRamAndInvalidMinDisk() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>>ofInstance(ImmutableSet.of(region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>>ofInstance(ImmutableSet.of(image));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>>ofInstance(ImmutableSet
.<Hardware>of(hardware));
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(new TemplateOptions());
expect(getImageStrategy.getImage(anyObject(String.class))).andReturn(null);
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minCores(2);
templateBuilder.minRam(4096);
templateBuilder.minDisk(-100f);
Template template = templateBuilder.build();
Hardware hardware = template.getHardware();
assertThat(hardware.getId()).isEqualTo("automatic:cores=2.0;ram=4096;disk=-100");
}
@Test
public void testExistingHardwareProfileMatchHardwareProfileWithMinCoresMinRam() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
@ -165,8 +227,8 @@ public class ArbitraryCpuRamTemplateBuilderImplTest {
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minCores(2);
templateBuilder.minRam(1024);
Template template = templateBuilder.build();
@ -176,8 +238,9 @@ public class ArbitraryCpuRamTemplateBuilderImplTest {
assertThat(hardware.getId()).isEqualTo("hardwareId");
}
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = errorMessage)
public void testOnlyRamTest() {
@Test(expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = errorMessage)
public void testOnlyRam() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet.of(region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.of(image));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
@ -192,14 +255,15 @@ public class ArbitraryCpuRamTemplateBuilderImplTest {
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minRam(4096);
templateBuilder.build();
}
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = errorMessage)
public void testOnlyCoresTest() {
@Test(expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = errorMessage)
public void testOnlyCores() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.of(region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.of(image));
@ -215,14 +279,14 @@ public class ArbitraryCpuRamTemplateBuilderImplTest {
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minCores(4);
templateBuilder.build();
}
@Test
public void testOnlyRamMatchHardwareProfileTest() {
public void testOnlyRamMatchHardwareProfile() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet.of(region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.of(image));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
@ -237,8 +301,8 @@ public class ArbitraryCpuRamTemplateBuilderImplTest {
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minRam(1024);
templateBuilder.build();
assertThat(hardware.getRam()).isEqualTo(2048);
@ -247,7 +311,7 @@ public class ArbitraryCpuRamTemplateBuilderImplTest {
}
@Test
public void testOnlyCoresMatchHardwareProfileTest() {
public void testOnlyCoresMatchHardwareProfile() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.of(region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.of(image));
@ -263,7 +327,7 @@ public class ArbitraryCpuRamTemplateBuilderImplTest {
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minCores(1);
templateBuilder.build();

View File

@ -953,12 +953,12 @@ public abstract class BaseComputeServiceLiveTest extends BaseComputeServiceConte
Template template = buildTemplate(templateBuilder()
.hardwareId("automatic:cores=2;ram=4096"));
try {
NodeMetadata node = getOnlyElement(client.createNodesInGroup("custom", 1, template));
NodeMetadata node = getOnlyElement(client.createNodesInGroup(group + "custom", 1, template));
assertThat(node.getHardware().getRam()).isEqualTo(4096);
assertThat(node.getHardware().getProcessors().get(0).getCores()).isEqualTo(2);
}
finally {
client.destroyNodesMatching(inGroup("custom"));
client.destroyNodesMatching(inGroup(group + "custom"));
}
}

View File

@ -242,7 +242,7 @@ public abstract class BaseTemplateBuilderLiveTest extends BaseComputeServiceCont
public void testAutoGeneratedHardwareFromId() {
Template template = view.getComputeService().templateBuilder()
.hardwareId("automatic:cores=2;ram=1024").build();
assertThat(template.getHardware().getId()).isEqualTo("automatic:cores=2;ram=1024");
assertThat(template.getHardware().getId()).isEqualTo("automatic:cores=2.0;ram=1024");
assertThat(template.getHardware().getRam()).isEqualTo(1024);
assertThat(template.getHardware().getProcessors().get(0).getCores()).isEqualTo(2);
}

View File

@ -18,6 +18,8 @@ package org.jclouds.compute.util;
import org.testng.annotations.Test;
import com.google.common.base.Optional;
import static org.assertj.core.api.Assertions.assertThat;
@Test(groups = "unit", testName = "AutomaticHardwareIdSpecTest")
@ -37,16 +39,55 @@ public class AutomaticHardwareIdSpecTest {
AutomaticHardwareIdSpec parser = AutomaticHardwareIdSpec.parseId("automatic:cores=2;ram=256");
assertThat(parser.getRam()).isEqualTo(256);
assertThat(parser.getCores()).isEqualTo(2);
AutomaticHardwareIdSpec parser2 = AutomaticHardwareIdSpec.parseId("automatic:cores=2;ram=4096;disk=100");
assertThat(parser2.getRam()).isEqualTo(4096);
assertThat(parser2.getCores()).isEqualTo(2);
assertThat(parser2.getDisk().get()).isEqualTo(100);
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void parseAutomaticIdMissingValuesTest() {
AutomaticHardwareIdSpec.parseId("automatic:cores=2");
AutomaticHardwareIdSpec.parseId("automatic:cores=2");
}
@Test(expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = "Invalid disk value: automatic:cores=2;ram=4096;disk=-100")
public void parseAutomaticIdInvalidDiskTest() {
AutomaticHardwareIdSpec.parseId("automatic:cores=2;ram=4096;disk=-100");
}
@Test
public void generateAutomaticIdTest() {
AutomaticHardwareIdSpec spec = AutomaticHardwareIdSpec.parseId("automatic:cores=2;ram=1024");
assertThat(spec.toString()).isEqualTo("automatic:cores=2.0;ram=1024");
AutomaticHardwareIdSpec spec2 = AutomaticHardwareIdSpec.parseId("automatic:cores=2;ram=4096;disk=100");
assertThat(spec2.toString()).isEqualTo("automatic:cores=2.0;ram=4096;disk=100");
}
@Test
public void automaticHardwareIdSpecBuilderTest() {
AutomaticHardwareIdSpec spec = AutomaticHardwareIdSpec.automaticHardwareIdSpecBuilder(2.0, 2048, Optional.<Float>absent());
assertThat(spec.getCores()).isEqualTo(2.0);
assertThat(spec.getRam()).isEqualTo(2048);
assertThat(spec.toString()).isEqualTo("automatic:cores=2.0;ram=2048");
AutomaticHardwareIdSpec spec2 = AutomaticHardwareIdSpec.automaticHardwareIdSpecBuilder(4.0, 4096, Optional.of(10.0f));
assertThat(spec2.getCores()).isEqualTo(4.0);
assertThat(spec2.getRam()).isEqualTo(4096);
assertThat(spec2.getDisk().get()).isEqualTo(10);
assertThat(spec2.toString()).isEqualTo("automatic:cores=4.0;ram=4096;disk=10");
}
@Test(expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = "Omitted or wrong minCores and minRam. If you want to" +
" use exact values, please set the minCores and minRam values: cores=2.0, ram=0")
public void automaticHardwareIdSpecBuilderWrongSpecsTest() {
AutomaticHardwareIdSpec.automaticHardwareIdSpecBuilder(2.0, 0, Optional.<Float>absent());
}
@Test(expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = "Invalid disk value: -10")
public void automaticHardwareIdSpecBuilderWrongDiskTest() {
AutomaticHardwareIdSpec.automaticHardwareIdSpecBuilder(2.0, 2048, Optional.of(-10.0f));
}
}

View File

@ -16,6 +16,7 @@
*/
package org.jclouds.googlecomputeengine.compute.domain.internal;
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.Hardware;
@ -42,7 +43,8 @@ public class GoogleComputeEngineArbitraryCpuRamTemplateBuilderImpl extends Arbit
super(locations, images, hardwares, defaultLocation, optionsProvider, defaultTemplateProvider);
}
protected Hardware automaticHardwareForCpuAndRam(double cores, int ram) {
@Override
protected Hardware automaticHardware(double cores, int ram, Optional<Float> disk) {
if (location == null) {
location = defaultLocation.get();
}

View File

@ -161,13 +161,13 @@ public class GoogleComputeEngineServiceLiveTest extends BaseComputeServiceLiveTe
Template template = buildTemplate(templateBuilder()
.hardwareId("automatic:cores=2;ram=4096"));
try {
NodeMetadata node = getOnlyElement(client.createNodesInGroup("custom", 1, template));
NodeMetadata node = getOnlyElement(client.createNodesInGroup(group + "custom", 1, template));
assertThat(node.getHardware().getRam()).isEqualTo(4096);
assertThat(node.getHardware().getProcessors().get(0).getCores()).isEqualTo(2);
assertThat(node.getHardware().getId()).isEqualTo(node.getLocation().getDescription() + "/machineTypes/custom-2-4096");
}
finally {
client.destroyNodesMatching(inGroup("custom"));
client.destroyNodesMatching(inGroup(group + "custom"));
}
}

View File

@ -90,8 +90,8 @@ public class GoogleComputeEngineArbitraryCpuRamTemplateBuilderImplTest {
.uri(URI.create("http://localhost/projects/party/zones/us-east-1/machineTypes/n2-standard-2"))
.build();
private final String errorMessage = "No hardware profile matching the given criteria was found. " +
"If you want to use exact values, please set the minCores and minRam values";
private final String errorMessage = "No hardware profile matching the given criteria was found. If you want to use" +
" exact values, please set the minCores, minRam and minDisk to positive values.";
@Test
public void testAutoGeneratedHardwareFromIdTest(){
@ -138,8 +138,8 @@ public class GoogleComputeEngineArbitraryCpuRamTemplateBuilderImplTest {
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new GoogleComputeEngineArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minRam(4096);
templateBuilder.minCores(2);
Hardware hardware = templateBuilder.build().getHardware();
@ -165,8 +165,8 @@ public class GoogleComputeEngineArbitraryCpuRamTemplateBuilderImplTest {
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new GoogleComputeEngineArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minRam(1024);
templateBuilder.minCores(2);
Hardware hardware = templateBuilder.build().getHardware();
@ -175,7 +175,8 @@ public class GoogleComputeEngineArbitraryCpuRamTemplateBuilderImplTest {
assertThat(hardware.getId()).isEqualTo("http://localhost/projects/party/zones/us-east-1/machineTypes/n2-standard-2");
}
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = errorMessage)
@Test(expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = errorMessage)
public void testAutoGeneratedHardwareWithOnlyMinCoresTest() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.of(region));
@ -192,13 +193,14 @@ public class GoogleComputeEngineArbitraryCpuRamTemplateBuilderImplTest {
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new GoogleComputeEngineArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minCores(4);
templateBuilder.build().getHardware();
}
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = errorMessage)
@Test(expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = errorMessage)
public void testAutoGeneratedHardwareWithOnlyMinRamTest() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.of(region));
@ -215,8 +217,8 @@ public class GoogleComputeEngineArbitraryCpuRamTemplateBuilderImplTest {
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new GoogleComputeEngineArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minRam(4096);
templateBuilder.build().getHardware();
}
@ -238,8 +240,8 @@ public class GoogleComputeEngineArbitraryCpuRamTemplateBuilderImplTest {
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new GoogleComputeEngineArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minCores(2);
Hardware hardware = templateBuilder.build().getHardware();
assertThat(hardware.getRam()).isEqualTo(2048);
@ -265,8 +267,8 @@ public class GoogleComputeEngineArbitraryCpuRamTemplateBuilderImplTest {
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl templateBuilder = new GoogleComputeEngineArbitraryCpuRamTemplateBuilderImpl(locations,
new ImageCacheSupplier(images, 60,
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares,
Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider);
templateBuilder.minRam(1024);
Hardware hardware = templateBuilder.build().getHardware();
assertThat(hardware.getRam()).isEqualTo(2048);

View File

@ -37,6 +37,8 @@ import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.Volume;
import org.jclouds.compute.domain.internal.ArbitraryCpuRamTemplateBuilderImpl;
import org.jclouds.compute.domain.internal.TemplateBuilderImpl;
import org.jclouds.compute.strategy.CreateNodesInGroupThenAddToSet;
import org.jclouds.domain.Location;
import org.jclouds.functions.IdentityFunction;
@ -79,6 +81,8 @@ public class ProfitBricksComputeServiceContextModule extends
bind(CreateNodesInGroupThenAddToSet.class).to(AssignDataCenterToTemplate.class).in(Scopes.SINGLETON);
bind(TemplateBuilderImpl.class).to(ArbitraryCpuRamTemplateBuilderImpl.class);
bind(new TypeLiteral<ComputeServiceAdapter<Server, Hardware, Provisionable, Location>>() {
}).to(ProfitBricksComputeServiceAdapter.class);

View File

@ -18,6 +18,7 @@ package org.jclouds.profitbricks.compute;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.internal.BaseComputeServiceLiveTest;
import org.jclouds.logging.config.LoggingModule;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
@ -28,6 +29,10 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
import static com.google.common.collect.Iterables.getOnlyElement;
import static org.assertj.core.api.Assertions.assertThat;
import static org.jclouds.compute.predicates.NodePredicates.inGroup;
@Test(groups = "live", singleThreaded = true, testName = "ProfitBricksComputeServiceLiveTest")
public class ProfitBricksComputeServiceLiveTest extends BaseComputeServiceLiveTest {
@ -70,4 +75,37 @@ public class ProfitBricksComputeServiceLiveTest extends BaseComputeServiceLiveTe
// Not enough description from API to match template
}
@Override
@Test(dataProvider = "onlyIfAutomaticHardwareSupported", groups = {"integration", "live"})
public void testCreateNodeWithCustomHardware() throws Exception {
Template template = buildTemplate(templateBuilder()
.hardwareId("automatic:cores=2;ram=2048;disk=10"));
try {
NodeMetadata node = getOnlyElement(client.createNodesInGroup(group + "custom", 1, template));
assertThat(node.getHardware().getRam()).isEqualTo(2048);
assertThat(node.getHardware().getProcessors().get(0).getCores()).isEqualTo(2);
assertThat(node.getHardware().getVolumes().get(0).getSize()).isEqualTo(10);
assertThat(node.getHardware().getId()).isEqualTo("automatic:cores=2;ram=2048;disk=10");
}
finally {
client.destroyNodesMatching(inGroup(group + "custom"));
}
}
@Test(dataProvider = "onlyIfAutomaticHardwareSupported", groups = {"integration", "live"})
public void testCreateNodeWithCustomHardwareUsingMins() throws Exception {
Template template = buildTemplate(templateBuilder()
.minCores(2).minRam(2048).minDisk(10));
try {
NodeMetadata node = getOnlyElement(client.createNodesInGroup(group + "custom", 1, template));
assertThat(node.getHardware().getRam()).isEqualTo(2048);
assertThat(node.getHardware().getProcessors().get(0).getCores()).isEqualTo(2);
assertThat(node.getHardware().getVolumes().get(0).getSize()).isEqualTo(10);
assertThat(node.getHardware().getId()).isEqualTo("cpu=2,ram=2048,disk=10");
}
finally {
client.destroyNodesMatching(inGroup(group + "custom"));
}
}
}