Issue 158: Fixed regex for finding the cpuitem, decoupled from value used to order it+tests

This commit is contained in:
Jason King 2011-10-06 15:01:02 +01:00
parent 9982065d13
commit 03f3f9562f
2 changed files with 123 additions and 52 deletions

View File

@ -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.categoryCode;
import static org.jclouds.softlayer.predicates.ProductItemPredicates.categoryCodeMatches; import static org.jclouds.softlayer.predicates.ProductItemPredicates.categoryCodeMatches;
import static org.jclouds.softlayer.predicates.ProductItemPredicates.matches; 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.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Hardware;
@ -56,51 +54,59 @@ import com.google.common.collect.Iterables;
@Singleton @Singleton
public class ProductItemsToHardware implements Function<Iterable<ProductItem>, Hardware> { public class ProductItemsToHardware implements Function<Iterable<ProductItem>, Hardware> {
private final Pattern cpuRegex; private static final String GUEST_DISK_CATEGORY_REGEX = "guest_disk[0-9]";
private final Pattern categoryCodeMatches; 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 @Inject
public ProductItemsToHardware(@Named(PROPERTY_SOFTLAYER_VIRTUALGUEST_CPU_REGEX) String cpuRegex) { public ProductItemsToHardware() {
this(Pattern.compile(checkNotNull(cpuRegex, "cpuRegex")), Pattern.compile("guest_disk[0-9]")); this(Pattern.compile(CPU_DESCRIPTION_REGEX), Pattern.compile(GUEST_DISK_CATEGORY_REGEX));
} }
public ProductItemsToHardware(Pattern cpuRegex, Pattern categoryCodeMatches) { public ProductItemsToHardware(Pattern cpuDescriptionRegex, Pattern diskCategoryRegex) {
this.cpuRegex = checkNotNull(cpuRegex, "cpuRegex"); this.cpuDescriptionRegex = checkNotNull(cpuDescriptionRegex, "cpuDescriptionRegex");
this.categoryCodeMatches = checkNotNull(categoryCodeMatches, "categoryCodeMatches"); this.diskCategoryRegex = checkNotNull(diskCategoryRegex, "diskCategoryRegex");
} }
@Override @Override
public Hardware apply(Iterable<ProductItem> items) { public Hardware apply(Iterable<ProductItem> items) {
ProductItem coresItem = getOnlyElement(filter(items, matches(cpuRegex))); ProductItem coresItem = getOnlyElement(filter(items, matches(cpuDescriptionRegex)));
ProductItem ramItem = getOnlyElement(filter(items, categoryCode("ram"))); ProductItem ramItem = getOnlyElement(filter(items, categoryCode(RAM_CATEGORY)));
ProductItem volumeItem = get(filter(items, categoryCode("guest_disk0")), 0); ProductItem volumeItem = get(filter(items, categoryCode(FIRST_GUEST_DISK)), 0);
String hardwareId = hardwareId().apply(ImmutableList.of(coresItem, ramItem, volumeItem)); String hardwareId = hardwareId().apply(ImmutableList.of(coresItem, ramItem, volumeItem));
double cores = ProductItems.capacity().apply(coresItem).doubleValue(); double cores = ProductItems.capacity().apply(coresItem).doubleValue();
Matcher cpuMatcher = cpuRegex.matcher(coresItem.getDescription()); Matcher cpuMatcher = cpuDescriptionRegex.matcher(coresItem.getDescription());
double coreSpeed = (cpuMatcher.matches()) ? Double.parseDouble(cpuMatcher.group(cpuMatcher.groupCount())) : 2.0; double coreSpeed = (cpuMatcher.matches()) ? Double.parseDouble(cpuMatcher.group(cpuMatcher.groupCount())) : DEFAULT_CORE_SPEED;
int ram = ProductItems.capacity().apply(ramItem).intValue(); int ram = ProductItems.capacity().apply(ramItem).intValue();
return new HardwareBuilder().ids(hardwareId).processors(ImmutableList.of(new Processor(cores, coreSpeed))).ram( return new HardwareBuilder().ids(hardwareId).processors(ImmutableList.of(new Processor(cores, coreSpeed))).ram(
ram).volumes( ram).volumes(
Iterables.transform(filter(items, categoryCodeMatches(categoryCodeMatches)), Iterables.transform(filter(items, categoryCodeMatches(diskCategoryRegex)),
new Function<ProductItem, Volume>() { new Function<ProductItem, Volume>() {
@Override @Override
public Volume apply(ProductItem arg0) { public Volume apply(ProductItem item) {
float volumeSize = ProductItems.capacity().apply(arg0); float volumeSize = ProductItems.capacity().apply(item);
return new VolumeImpl( return new VolumeImpl(
arg0.getId() + "", item.getId() + "",
arg0.getDescription().indexOf("SAN") != -1 ? Volume.Type.SAN : Volume.Type.LOCAL, item.getDescription().indexOf(STORAGE_AREA_NETWORK) != -1 ? Volume.Type.SAN : Volume.Type.LOCAL,
volumeSize, null, categoryCode("guest_disk0").apply(arg0), false); volumeSize, null, categoryCode(FIRST_GUEST_DISK).apply(item), false);
} }
})).build(); })).build();
} }
/** /**
* Generates a hardwareId based on the priceId's of the items in the list * Generates a hardwareId based on the priceId's of the items in the list
* *
* @return comma separated list of priceid's * @return comma separated list of priceid's
*/ */
public static Function<List<ProductItem>, String> hardwareId() { public static Function<List<ProductItem>, String> hardwareId() {

View File

@ -18,13 +18,10 @@
*/ */
package org.jclouds.softlayer.compute.functions; package org.jclouds.softlayer.compute.functions;
import static com.google.inject.name.Names.bindProperties; import com.google.common.collect.ImmutableList;
import static org.jclouds.softlayer.compute.functions.ProductItemsToHardware.hardwareId; import com.google.common.collect.ImmutableSet;
import static org.testng.AssertJUnit.assertEquals; import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import java.util.List;
import java.util.Properties;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Processor; import org.jclouds.compute.domain.Processor;
import org.jclouds.compute.domain.Volume; 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.ProductItem;
import org.jclouds.softlayer.domain.ProductItemCategory; import org.jclouds.softlayer.domain.ProductItemCategory;
import org.jclouds.softlayer.domain.ProductItemPrice; import org.jclouds.softlayer.domain.ProductItemPrice;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList; import java.util.List;
import com.google.common.collect.ImmutableSet; import java.util.Properties;
import com.google.inject.AbstractModule;
import com.google.inject.Guice; 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} * Tests {@code ProductItemsToHardware}
@ -47,6 +47,41 @@ import com.google.inject.Guice;
@Test(groups = "unit") @Test(groups = "unit")
public class ProductItemsToHardwareTest { 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 @Test
public void testHardwareId() { public void testHardwareId() {
ProductItem item1 = ProductItem.builder().price(ProductItemPrice.builder().id(123).build()).build(); ProductItem item1 = ProductItem.builder().price(ProductItemPrice.builder().id(123).build()).build();
@ -59,25 +94,8 @@ public class ProductItemsToHardwareTest {
@Test @Test
public void testHardware() { 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( Hardware hardware = toHardware.apply(ImmutableSet.of(cpuItem, ramItem, volumeItem));
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));
assertEquals("123,456,789", hardware.getId()); assertEquals("123,456,789", hardware.getId());
@ -89,6 +107,53 @@ public class ProductItemsToHardwareTest {
List<? extends Volume> volumes = hardware.getVolumes(); List<? extends Volume> volumes = hardware.getVolumes();
assertEquals(1, volumes.size()); 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());
} }
} }