From 4790e588b4164e9451125e900a89d3fe93ec8192 Mon Sep 17 00:00:00 2001 From: Jason King Date: Sat, 17 Sep 2011 22:57:33 +0100 Subject: [PATCH] Added ProductItemPredicates.units, function ProductItemPriceFromProductItem and updated the live test to show how to get prices --- .../ProductItemPriceFromProductItem.java | 46 ++++++++++ .../predicates/ProductItemPredicates.java | 22 +++++ .../ProductPackageClientLiveTest.java | 90 +++++++++++++++++-- 3 files changed, 149 insertions(+), 9 deletions(-) create mode 100644 sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/ProductItemPriceFromProductItem.java diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/ProductItemPriceFromProductItem.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/ProductItemPriceFromProductItem.java new file mode 100644 index 0000000000..25489eebfb --- /dev/null +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/ProductItemPriceFromProductItem.java @@ -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 { + + /** + * + * @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 prices = productItem.getPrices(); + if(prices.size()!=1) throw new NoSuchElementException(); + return prices.iterator().next(); + } +} diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/predicates/ProductItemPredicates.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/predicates/ProductItemPredicates.java index d0694651ad..3b291a93a9 100644 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/predicates/ProductItemPredicates.java +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/predicates/ProductItemPredicates.java @@ -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 units(final String units) { + return new Predicate() { + @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+")"; + } + }; + } } diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/features/ProductPackageClientLiveTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/features/ProductPackageClientLiveTest.java index 076b420a58..1355cb50d7 100644 --- a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/features/ProductPackageClientLiveTest.java +++ b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/features/ProductPackageClientLiveTest.java @@ -18,16 +18,24 @@ */ package org.jclouds.softlayer.features; +import com.google.common.base.Predicates; import com.google.common.collect.ImmutableSet; 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.predicates.ProductPackagePredicates; +import org.jclouds.softlayer.reference.SoftLayerConstants; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; +import java.util.Map; import java.util.Set; +import static org.jclouds.softlayer.predicates.ProductItemPredicates.*; +import static org.jclouds.softlayer.predicates.ProductPackagePredicates.named; import static org.testng.Assert.*; /** @@ -42,6 +50,10 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest { super.setupClient(); client = context.getApi().getProductPackageClient(); 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"; @@ -49,6 +61,9 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest { private ProductPackageClient client; private AccountClient accountClient; + private long cloudServerPackageId; + private ProductPackage cloudServerProductPackage; + @Test public void testGetProductPackage() { for (ProductPackage productPackage : accountClient.getActivePackages()) { @@ -85,13 +100,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest { Set expected = builder.build(); - // note we don't need to check null, as this will throw NoSuchElementException if - // 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 datacenters = productPackage.getDatacenters(); + Set datacenters = cloudServerProductPackage.getDatacenters(); assertEquals(datacenters.size(), expected.size()); 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 ramItems = Iterables.filter(cloudServerProductPackage.getItems(), + Predicates.and(categoryCode("ram"), capacity(1.0f))); + + // capacity is key in GB (1Gb = 1.0f) + Map 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 cpuItems = Iterables.filter(cloudServerProductPackage.getItems(), Predicates.and(units("PRIVATE_CORE"), capacity(2.0f))); + + // number of cores is the key + Map 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 operatingSystems = Iterables.filter(cloudServerProductPackage.getItems(), categoryCode("os")); + + Map 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 ramItems = Iterables.filter(cloudServerProductPackage.getItems(), + Predicates.and(categoryCode("ram"), capacity(1.0f))); + + Map ramToProductItem = Maps.uniqueIndex(ramItems, new CapacityFromProductItem()); + + ProductItemPrice ramPrice = new ProductItemPriceFromProductItem().apply(ramToProductItem.get(1.0f)); + + Iterable cpuItems = Iterables.filter(cloudServerProductPackage.getItems(), Predicates.and(units("PRIVATE_CORE"), capacity(2.0f))); + Map coresToProductItem = Maps.uniqueIndex(cpuItems, new CapacityFromProductItem()); + + ProductItemPrice cpuPrice = new ProductItemPriceFromProductItem().apply(coresToProductItem.get(2.0f)); + + Iterable operatingSystems = Iterables.filter(cloudServerProductPackage.getItems(), categoryCode("os")); + Map osToProductItem = Maps.uniqueIndex(operatingSystems, new DescriptionFromProductItem()); + ProductItemPrice osPrice = new ProductItemPriceFromProductItem().apply(osToProductItem.get("Ubuntu Linux 8 LTS Hardy Heron - Minimal Install (64 bit)")); + + Set prices = Sets.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) { assert item.getId() > 0 : item; assert item.getDescription() != null : item;