mirror of https://github.com/apache/jclouds.git
Merge pull request #99 from jsonking/158-add-softlayer-provider
Issue 158: Fixed regex for finding the cpuitem
This commit is contained in:
commit
9adcd47bdb
|
@ -25,14 +25,12 @@ import static com.google.common.collect.Iterables.getOnlyElement;
|
|||
import static org.jclouds.softlayer.predicates.ProductItemPredicates.categoryCode;
|
||||
import static org.jclouds.softlayer.predicates.ProductItemPredicates.categoryCodeMatches;
|
||||
import static org.jclouds.softlayer.predicates.ProductItemPredicates.matches;
|
||||
import static org.jclouds.softlayer.reference.SoftLayerConstants.PROPERTY_SOFTLAYER_VIRTUALGUEST_CPU_REGEX;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.jclouds.compute.domain.Hardware;
|
||||
|
@ -56,44 +54,52 @@ import com.google.common.collect.Iterables;
|
|||
@Singleton
|
||||
public class ProductItemsToHardware implements Function<Iterable<ProductItem>, Hardware> {
|
||||
|
||||
private final Pattern cpuRegex;
|
||||
private final Pattern categoryCodeMatches;
|
||||
private static final String GUEST_DISK_CATEGORY_REGEX = "guest_disk[0-9]";
|
||||
private static final String FIRST_GUEST_DISK = "guest_disk0";
|
||||
private static final String STORAGE_AREA_NETWORK = "SAN";
|
||||
|
||||
private static final String RAM_CATEGORY = "ram";
|
||||
|
||||
private static final String CPU_DESCRIPTION_REGEX = "(Private )?[0-9]+ x ([.0-9]+) GHz Core[s]?";
|
||||
private static final double DEFAULT_CORE_SPEED = 2.0;
|
||||
|
||||
private final Pattern cpuDescriptionRegex;
|
||||
private final Pattern diskCategoryRegex;
|
||||
|
||||
@Inject
|
||||
public ProductItemsToHardware(@Named(PROPERTY_SOFTLAYER_VIRTUALGUEST_CPU_REGEX) String cpuRegex) {
|
||||
this(Pattern.compile(checkNotNull(cpuRegex, "cpuRegex")), Pattern.compile("guest_disk[0-9]"));
|
||||
public ProductItemsToHardware() {
|
||||
this(Pattern.compile(CPU_DESCRIPTION_REGEX), Pattern.compile(GUEST_DISK_CATEGORY_REGEX));
|
||||
}
|
||||
|
||||
public ProductItemsToHardware(Pattern cpuRegex, Pattern categoryCodeMatches) {
|
||||
this.cpuRegex = checkNotNull(cpuRegex, "cpuRegex");
|
||||
this.categoryCodeMatches = checkNotNull(categoryCodeMatches, "categoryCodeMatches");
|
||||
public ProductItemsToHardware(Pattern cpuDescriptionRegex, Pattern diskCategoryRegex) {
|
||||
this.cpuDescriptionRegex = checkNotNull(cpuDescriptionRegex, "cpuDescriptionRegex");
|
||||
this.diskCategoryRegex = checkNotNull(diskCategoryRegex, "diskCategoryRegex");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Hardware apply(Iterable<ProductItem> items) {
|
||||
|
||||
ProductItem coresItem = getOnlyElement(filter(items, matches(cpuRegex)));
|
||||
ProductItem ramItem = getOnlyElement(filter(items, categoryCode("ram")));
|
||||
ProductItem volumeItem = get(filter(items, categoryCode("guest_disk0")), 0);
|
||||
ProductItem coresItem = getOnlyElement(filter(items, matches(cpuDescriptionRegex)));
|
||||
ProductItem ramItem = getOnlyElement(filter(items, categoryCode(RAM_CATEGORY)));
|
||||
ProductItem volumeItem = get(filter(items, categoryCode(FIRST_GUEST_DISK)), 0);
|
||||
|
||||
String hardwareId = hardwareId().apply(ImmutableList.of(coresItem, ramItem, volumeItem));
|
||||
double cores = ProductItems.capacity().apply(coresItem).doubleValue();
|
||||
Matcher cpuMatcher = cpuRegex.matcher(coresItem.getDescription());
|
||||
double coreSpeed = (cpuMatcher.matches()) ? Double.parseDouble(cpuMatcher.group(cpuMatcher.groupCount())) : 2.0;
|
||||
Matcher cpuMatcher = cpuDescriptionRegex.matcher(coresItem.getDescription());
|
||||
double coreSpeed = (cpuMatcher.matches()) ? Double.parseDouble(cpuMatcher.group(cpuMatcher.groupCount())) : DEFAULT_CORE_SPEED;
|
||||
int ram = ProductItems.capacity().apply(ramItem).intValue();
|
||||
|
||||
return new HardwareBuilder().ids(hardwareId).processors(ImmutableList.of(new Processor(cores, coreSpeed))).ram(
|
||||
ram).volumes(
|
||||
Iterables.transform(filter(items, categoryCodeMatches(categoryCodeMatches)),
|
||||
Iterables.transform(filter(items, categoryCodeMatches(diskCategoryRegex)),
|
||||
new Function<ProductItem, Volume>() {
|
||||
|
||||
@Override
|
||||
public Volume apply(ProductItem arg0) {
|
||||
float volumeSize = ProductItems.capacity().apply(arg0);
|
||||
public Volume apply(ProductItem item) {
|
||||
float volumeSize = ProductItems.capacity().apply(item);
|
||||
return new VolumeImpl(
|
||||
arg0.getId() + "",
|
||||
arg0.getDescription().indexOf("SAN") != -1 ? Volume.Type.SAN : Volume.Type.LOCAL,
|
||||
volumeSize, null, categoryCode("guest_disk0").apply(arg0), false);
|
||||
item.getId() + "",
|
||||
item.getDescription().indexOf(STORAGE_AREA_NETWORK) != -1 ? Volume.Type.SAN : Volume.Type.LOCAL,
|
||||
volumeSize, null, categoryCode(FIRST_GUEST_DISK).apply(item), false);
|
||||
}
|
||||
})).build();
|
||||
}
|
||||
|
|
|
@ -18,13 +18,10 @@
|
|||
*/
|
||||
package org.jclouds.softlayer.compute.functions;
|
||||
|
||||
import static com.google.inject.name.Names.bindProperties;
|
||||
import static org.jclouds.softlayer.compute.functions.ProductItemsToHardware.hardwareId;
|
||||
import static org.testng.AssertJUnit.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
import org.jclouds.compute.domain.Hardware;
|
||||
import org.jclouds.compute.domain.Processor;
|
||||
import org.jclouds.compute.domain.Volume;
|
||||
|
@ -32,12 +29,15 @@ import org.jclouds.softlayer.SoftLayerPropertiesBuilder;
|
|||
import org.jclouds.softlayer.domain.ProductItem;
|
||||
import org.jclouds.softlayer.domain.ProductItemCategory;
|
||||
import org.jclouds.softlayer.domain.ProductItemPrice;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import static com.google.inject.name.Names.bindProperties;
|
||||
import static org.jclouds.softlayer.compute.functions.ProductItemsToHardware.hardwareId;
|
||||
import static org.testng.AssertJUnit.assertEquals;
|
||||
|
||||
/**
|
||||
* Tests {@code ProductItemsToHardware}
|
||||
|
@ -47,6 +47,41 @@ import com.google.inject.Guice;
|
|||
@Test(groups = "unit")
|
||||
public class ProductItemsToHardwareTest {
|
||||
|
||||
private ProductItemsToHardware toHardware;
|
||||
private ProductItem cpuItem;
|
||||
private ProductItem ramItem;
|
||||
private ProductItem volumeItem;
|
||||
|
||||
@BeforeMethod
|
||||
public void setup() {
|
||||
|
||||
toHardware = Guice.createInjector(new AbstractModule() {
|
||||
@Override
|
||||
protected void configure() {
|
||||
bindProperties(binder(), new SoftLayerPropertiesBuilder(new Properties()).build());
|
||||
}
|
||||
}).getInstance(ProductItemsToHardware.class);
|
||||
|
||||
|
||||
cpuItem = ProductItem.builder()
|
||||
.id(1)
|
||||
.description("2 x 2.0 GHz Cores")
|
||||
.capacity(2F)
|
||||
.category(ProductItemCategory.builder().categoryCode("guest_core").build())
|
||||
.price(ProductItemPrice.builder().id(123).build())
|
||||
.build();
|
||||
|
||||
ramItem = ProductItem.builder().id(2).description("2GB ram").capacity(2F).category(
|
||||
ProductItemCategory.builder().categoryCode("ram").build()).price(
|
||||
ProductItemPrice.builder().id(456).build()).build();
|
||||
|
||||
volumeItem = ProductItem.builder().id(3).description("100 GB (SAN)").capacity(100F).price(
|
||||
ProductItemPrice.builder().id(789).build()).category(
|
||||
ProductItemCategory.builder().categoryCode("guest_disk0").build()).build();
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHardwareId() {
|
||||
ProductItem item1 = ProductItem.builder().price(ProductItemPrice.builder().id(123).build()).build();
|
||||
|
@ -59,25 +94,8 @@ public class ProductItemsToHardwareTest {
|
|||
|
||||
@Test
|
||||
public void testHardware() {
|
||||
ProductItem cpuItem = ProductItem.builder().id(1).description("2 x 2.0 GHz Cores").units("PRIVATE_CORE")
|
||||
.capacity(2F).price(ProductItemPrice.builder().id(123).build()).build();
|
||||
|
||||
ProductItem ramItem = ProductItem.builder().id(2).description("2GB ram").capacity(2F).category(
|
||||
ProductItemCategory.builder().categoryCode("ram").build()).price(
|
||||
ProductItemPrice.builder().id(456).build()).build();
|
||||
|
||||
ProductItem volumeItem = ProductItem.builder().id(3).description("100 GB (SAN)").capacity(100F).price(
|
||||
ProductItemPrice.builder().id(789).build()).category(
|
||||
ProductItemCategory.builder().categoryCode("guest_disk0").build()).build();
|
||||
|
||||
Hardware hardware = Guice.createInjector(new AbstractModule() {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bindProperties(binder(), new SoftLayerPropertiesBuilder(new Properties()).build());
|
||||
}
|
||||
|
||||
}).getInstance(ProductItemsToHardware.class).apply(ImmutableSet.of(cpuItem, ramItem, volumeItem));
|
||||
Hardware hardware = toHardware.apply(ImmutableSet.of(cpuItem, ramItem, volumeItem));
|
||||
|
||||
assertEquals("123,456,789", hardware.getId());
|
||||
|
||||
|
@ -89,6 +107,53 @@ public class ProductItemsToHardwareTest {
|
|||
|
||||
List<? extends Volume> volumes = hardware.getVolumes();
|
||||
assertEquals(1, volumes.size());
|
||||
assertEquals(100F, volumes.get(0).getSize());
|
||||
Volume volume = volumes.get(0);
|
||||
assertEquals(100F, volume.getSize());
|
||||
assertEquals(Volume.Type.SAN, volume.getType());
|
||||
assertEquals(true, volume.isBootDevice());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHardwareWithPrivateCore() {
|
||||
|
||||
cpuItem = cpuItem.toBuilder()
|
||||
.description("Private 2 x 2.0 GHz Cores")
|
||||
.build();
|
||||
|
||||
Hardware hardware = toHardware.apply(ImmutableSet.of(cpuItem, ramItem, volumeItem));
|
||||
|
||||
assertEquals("123,456,789", hardware.getId());
|
||||
|
||||
List<? extends Processor> processors = hardware.getProcessors();
|
||||
assertEquals(1, processors.size());
|
||||
assertEquals(2.0, processors.get(0).getCores());
|
||||
|
||||
assertEquals(2, hardware.getRam());
|
||||
|
||||
List<? extends Volume> volumes = hardware.getVolumes();
|
||||
assertEquals(1, volumes.size());
|
||||
assertEquals(100F, volumes.get(0).getSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHardwareWithTwoDisks() {
|
||||
ProductItem localVolumeItem = ProductItem.builder().id(4).description("25 GB").capacity(25F).price(
|
||||
ProductItemPrice.builder().id(987).build()).category(
|
||||
ProductItemCategory.builder().categoryCode("guest_disk1").build()).build();
|
||||
|
||||
Hardware hardware = toHardware.apply(ImmutableSet.of(cpuItem, ramItem, volumeItem,localVolumeItem));
|
||||
|
||||
List<? extends Volume> volumes = hardware.getVolumes();
|
||||
assertEquals(2, volumes.size());
|
||||
Volume volume = volumes.get(0);
|
||||
assertEquals(100F, volume.getSize());
|
||||
assertEquals(Volume.Type.SAN, volume.getType());
|
||||
assertEquals(true, volume.isBootDevice());
|
||||
|
||||
Volume volume1 = volumes.get(1);
|
||||
assertEquals(25F, volume1.getSize());
|
||||
assertEquals(Volume.Type.LOCAL, volume1.getType());
|
||||
assertEquals(false, volume1.isBootDevice());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue