Added ProductItemPredicates.units, function ProductItemPriceFromProductItem and updated the live test to show how to get prices

This commit is contained in:
Jason King 2011-09-17 22:57:33 +01:00
parent 4fc4cb9a75
commit 4790e588b4
3 changed files with 149 additions and 9 deletions

View File

@ -0,0 +1,46 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.softlayer.compute.functions;
import com.google.common.base.Function;
import org.jclouds.softlayer.domain.ProductItem;
import org.jclouds.softlayer.domain.ProductItemPrice;
import java.util.NoSuchElementException;
import java.util.Set;
/**
* Returns the ProductItemPrice for the ProductItem.
* @author Jason King
*/
public class ProductItemPriceFromProductItem implements Function<ProductItem,ProductItemPrice> {
/**
*
* @param productItem the productItem to use
* @return the productItemPrice
* @throws java.util.NoSuchElementException if not only 1 price
*/
@Override
public ProductItemPrice apply(ProductItem productItem) {
Set<ProductItemPrice> prices = productItem.getPrices();
if(prices.size()!=1) throw new NoSuchElementException();
return prices.iterator().next();
}
}

View File

@ -66,4 +66,26 @@ public class ProductItemPredicates {
} }
}; };
} }
/**
* Tests if the ProductItem has the required units.
* TODO Write test method
* @param units
* @return true if it does, otherwise false.
*/
public static Predicate<ProductItem> units(final String units) {
return new Predicate<ProductItem>() {
@Override
public boolean apply(ProductItem productItem) {
String productItemUnits = productItem.getUnits();
if (productItemUnits == null) return false;
return productItemUnits.equals(units);
}
@Override
public String toString() {
return "units("+units+")";
}
};
}
} }

View File

@ -18,16 +18,24 @@
*/ */
package org.jclouds.softlayer.features; package org.jclouds.softlayer.features;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.jclouds.softlayer.compute.functions.CapacityFromProductItem;
import org.jclouds.softlayer.compute.functions.DescriptionFromProductItem;
import org.jclouds.softlayer.compute.functions.ProductItemPriceFromProductItem;
import org.jclouds.softlayer.domain.*; import org.jclouds.softlayer.domain.*;
import org.jclouds.softlayer.predicates.ProductPackagePredicates; import org.jclouds.softlayer.reference.SoftLayerConstants;
import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.util.Map;
import java.util.Set; import java.util.Set;
import static org.jclouds.softlayer.predicates.ProductItemPredicates.*;
import static org.jclouds.softlayer.predicates.ProductPackagePredicates.named;
import static org.testng.Assert.*; import static org.testng.Assert.*;
/** /**
@ -42,6 +50,10 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
super.setupClient(); super.setupClient();
client = context.getApi().getProductPackageClient(); client = context.getApi().getProductPackageClient();
accountClient = context.getApi().getAccountClient(); accountClient = context.getApi().getAccountClient();
// This is used several times, so cache to speed up the test.
cloudServerPackageId = Iterables.find(accountClient.getActivePackages(),named(CLOUD_SERVER_PACKAGE_NAME)).getId();
cloudServerProductPackage = client.getProductPackage(cloudServerPackageId);
} }
private static final String CLOUD_SERVER_PACKAGE_NAME = "Cloud Server"; private static final String CLOUD_SERVER_PACKAGE_NAME = "Cloud Server";
@ -49,6 +61,9 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
private ProductPackageClient client; private ProductPackageClient client;
private AccountClient accountClient; private AccountClient accountClient;
private long cloudServerPackageId;
private ProductPackage cloudServerProductPackage;
@Test @Test
public void testGetProductPackage() { public void testGetProductPackage() {
for (ProductPackage productPackage : accountClient.getActivePackages()) { for (ProductPackage productPackage : accountClient.getActivePackages()) {
@ -85,13 +100,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
Set<Datacenter> expected = builder.build(); Set<Datacenter> expected = builder.build();
// note we don't need to check null, as this will throw NoSuchElementException if Set<Datacenter> datacenters = cloudServerProductPackage.getDatacenters();
// a product package named as below isn't found.
long productPackageId = Iterables.find(accountClient.getActivePackages(),
ProductPackagePredicates.named(CLOUD_SERVER_PACKAGE_NAME)).getId();
ProductPackage productPackage = client.getProductPackage(productPackageId);
Set<Datacenter> datacenters = productPackage.getDatacenters();
assertEquals(datacenters.size(), expected.size()); assertEquals(datacenters.size(), expected.size());
assertTrue(datacenters.containsAll(expected)); assertTrue(datacenters.containsAll(expected));
@ -102,6 +111,69 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
} }
} }
@Test
public void testGetOneGBRamPrice() {
//Predicate p = Predicates.and(ProductItemPredicates.categoryCode("ram"),ProductItemPredicates.capacity(1.0f));
Iterable<ProductItem> ramItems = Iterables.filter(cloudServerProductPackage.getItems(),
Predicates.and(categoryCode("ram"), capacity(1.0f)));
// capacity is key in GB (1Gb = 1.0f)
Map<Float, ProductItem> ramToProductItem = Maps.uniqueIndex(ramItems, new CapacityFromProductItem());
ProductItemPrice price = new ProductItemPriceFromProductItem().apply(ramToProductItem.get(1.0f));
assert new Long(1644L).equals(price.getId());
}
@Test
public void testGetTwoCPUCoresPrice() {
// If use ProductItemPredicates.categoryCode("guest_core") get duplicate capacities (units = PRIVATE_CORE and N/A)
Iterable<ProductItem> cpuItems = Iterables.filter(cloudServerProductPackage.getItems(), Predicates.and(units("PRIVATE_CORE"), capacity(2.0f)));
// number of cores is the key
Map<Float, ProductItem> coresToProductItem = Maps.uniqueIndex(cpuItems, new CapacityFromProductItem());
ProductItemPrice price = new ProductItemPriceFromProductItem().apply(coresToProductItem.get(2.0f));
assert new Long(1963L).equals(price.getId());
}
@Test
public void testGetUbuntuPrice() {
Iterable<ProductItem> operatingSystems = Iterables.filter(cloudServerProductPackage.getItems(), categoryCode("os"));
Map<String, ProductItem> osToProductItem = Maps.uniqueIndex(operatingSystems, new DescriptionFromProductItem());
ProductItemPrice price = new ProductItemPriceFromProductItem().apply(osToProductItem.get("Ubuntu Linux 8 LTS Hardy Heron - Minimal Install (64 bit)"));
assert new Long(1693L).equals(price.getId());
}
@Test
public void testPricesForLaunchingGuestVM() {
Iterable<ProductItem> ramItems = Iterables.filter(cloudServerProductPackage.getItems(),
Predicates.and(categoryCode("ram"), capacity(1.0f)));
Map<Float, ProductItem> ramToProductItem = Maps.uniqueIndex(ramItems, new CapacityFromProductItem());
ProductItemPrice ramPrice = new ProductItemPriceFromProductItem().apply(ramToProductItem.get(1.0f));
Iterable<ProductItem> cpuItems = Iterables.filter(cloudServerProductPackage.getItems(), Predicates.and(units("PRIVATE_CORE"), capacity(2.0f)));
Map<Float, ProductItem> coresToProductItem = Maps.uniqueIndex(cpuItems, new CapacityFromProductItem());
ProductItemPrice cpuPrice = new ProductItemPriceFromProductItem().apply(coresToProductItem.get(2.0f));
Iterable<ProductItem> operatingSystems = Iterables.filter(cloudServerProductPackage.getItems(), categoryCode("os"));
Map<String, ProductItem> osToProductItem = Maps.uniqueIndex(operatingSystems, new DescriptionFromProductItem());
ProductItemPrice osPrice = new ProductItemPriceFromProductItem().apply(osToProductItem.get("Ubuntu Linux 8 LTS Hardy Heron - Minimal Install (64 bit)"));
Set<Long> prices = Sets.<Long>newLinkedHashSet();
prices.addAll(SoftLayerConstants.DEFAULT_VIRTUAL_GUEST_PRICES);
prices.add(ramPrice.getId());
prices.add(cpuPrice.getId());
prices.add(osPrice.getId());
//This should be everything needed to launch Ubuntu Hardy Heron with 1GB Ram and 2 CPU Cores
//TODO: There must be a more concise way of expressing this logic.
}
private void checkProductItem(ProductItem item) { private void checkProductItem(ProductItem item) {
assert item.getId() > 0 : item; assert item.getId() > 0 : item;
assert item.getDescription() != null : item; assert item.getDescription() != null : item;