From b3001aa9eecf22ecdb1381162cf3c57617a5a1f2 Mon Sep 17 00:00:00 2001 From: Jason King Date: Fri, 30 Sep 2011 13:57:04 +0100 Subject: [PATCH] Issue 158: Removed function to find virtual guest - no longer needed --- .../SoftLayerComputeServiceAdapter.java | 40 ++--------- .../softlayer/domain/ProductOrderReceipt.java | 26 +++++-- .../softlayer/util/SoftLayerUtils.java | 67 ------------------- ...oftLayerComputeServiceAdapterLiveTest.java | 6 +- .../softlayer/util/SoftLayerUtilsTest.java | 57 ---------------- 5 files changed, 30 insertions(+), 166 deletions(-) delete mode 100644 sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/util/SoftLayerUtils.java delete mode 100644 sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/util/SoftLayerUtilsTest.java diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/strategy/SoftLayerComputeServiceAdapter.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/strategy/SoftLayerComputeServiceAdapter.java index 3bbc274834..dece28b39a 100644 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/strategy/SoftLayerComputeServiceAdapter.java +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/strategy/SoftLayerComputeServiceAdapter.java @@ -18,7 +18,6 @@ */ package org.jclouds.softlayer.compute.strategy; -import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableSet; @@ -29,7 +28,6 @@ import org.jclouds.compute.ComputeService; import org.jclouds.compute.ComputeServiceAdapter; import org.jclouds.compute.domain.Template; import org.jclouds.domain.Credentials; -import org.jclouds.predicates.RetryablePredicate; import org.jclouds.softlayer.SoftLayerClient; import org.jclouds.softlayer.compute.functions.ProductItems; import org.jclouds.softlayer.compute.options.SoftLayerTemplateOptions; @@ -37,7 +35,6 @@ import org.jclouds.softlayer.domain.*; import org.jclouds.softlayer.features.AccountClient; import org.jclouds.softlayer.features.ProductPackageClient; import org.jclouds.softlayer.reference.SoftLayerConstants; -import org.jclouds.softlayer.util.SoftLayerUtils; import javax.inject.Inject; import javax.inject.Named; @@ -45,7 +42,8 @@ import javax.inject.Singleton; import java.util.Map; import java.util.Set; -import static com.google.common.base.Preconditions.*; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; import static org.jclouds.softlayer.predicates.ProductItemPredicates.*; import static org.jclouds.softlayer.predicates.ProductPackagePredicates.named; @@ -63,20 +61,12 @@ public class SoftLayerComputeServiceAdapter implements private final SoftLayerClient client; private final String virtualGuestPackageName; - private final RetryablePredicate orderTester; - private final long guestOrderDelay; @Inject public SoftLayerComputeServiceAdapter(SoftLayerClient client, - @Named(SoftLayerConstants.PROPERTY_SOFTLAYER_VIRTUALGUEST_PACKAGE_NAME) String virtualGuestPackageName, - OnlyOneVirtualGuestPresentWithHostAndDomainName onlyOneVirtualGuestPresentWithHostAndDomainName, - @Named(SoftLayerConstants.PROPERTY_SOFTLAYER_VIRTUALGUEST_ORDER_DELAY) long guestOrderDelay) { + @Named(SoftLayerConstants.PROPERTY_SOFTLAYER_VIRTUALGUEST_PACKAGE_NAME) String virtualGuestPackageName) { this.client = checkNotNull(client, "client"); this.virtualGuestPackageName = checkNotNull(virtualGuestPackageName, "virtualGuestPackageName"); - checkArgument(guestOrderDelay > 500, "guestOrderDelay must be in milliseconds and greater than 500"); - this.orderTester = new RetryablePredicate(onlyOneVirtualGuestPresentWithHostAndDomainName, - guestOrderDelay); - this.guestOrderDelay=guestOrderDelay; } @Override @@ -96,13 +86,9 @@ public class SoftLayerComputeServiceAdapter implements template.getLocation().getId()).quantity(1).useHourlyPricing(true).prices(getPrices(template)) .virtualGuest(newGuest).build(); - client.getVirtualGuestClient().orderVirtualGuest(order); - - boolean orderInSystem = orderTester.apply(newGuest); - checkState(orderInSystem, "order for guest %s didn't complete within %dms", newGuest, guestOrderDelay); + ProductOrderReceipt productOrderReceipt = client.getVirtualGuestClient().orderVirtualGuest(order); + VirtualGuest result = Iterables.get(productOrderReceipt.getOrderDetails().getVirtualGuests(), 0); - Iterable allGuests = client.getVirtualGuestClient().listVirtualGuests(); - VirtualGuest result = SoftLayerUtils.findVirtualGuest(allGuests, name, domainName); Credentials credentials = new Credentials(null, null); @@ -119,22 +105,6 @@ public class SoftLayerComputeServiceAdapter implements return result; } - public static class OnlyOneVirtualGuestPresentWithHostAndDomainName implements Predicate { - private final SoftLayerClient client; - - @Inject - public OnlyOneVirtualGuestPresentWithHostAndDomainName(SoftLayerClient client) { - this.client = checkNotNull(client, "client was null"); - } - - @Override - public boolean apply(VirtualGuest guest) { - checkNotNull(guest, "virtual guest was null"); - Iterable allGuests = client.getVirtualGuestClient().listVirtualGuests(); - return SoftLayerUtils.findVirtualGuest(allGuests, guest.getHostname(), guest.getDomain()) != null; - } - - } private Iterable getPrices(Template template) { Set result = Sets.newLinkedHashSet(); diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/ProductOrderReceipt.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/ProductOrderReceipt.java index ff89d1fbea..64b3456ee2 100644 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/ProductOrderReceipt.java +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/ProductOrderReceipt.java @@ -31,30 +31,38 @@ public class ProductOrderReceipt implements Comparable { public static class Builder { private int orderId = -1; + private ProductOrder orderDetails; public Builder orderId(int orderId) { this.orderId = orderId; return this; } + public Builder orderDetails(ProductOrder orderDetails) { + this.orderDetails = orderDetails; + return this; + } + public ProductOrderReceipt build() { - return new ProductOrderReceipt(orderId); + return new ProductOrderReceipt(orderId,orderDetails); } public static Builder fromAddress(ProductOrderReceipt in) { - return ProductOrderReceipt.builder().orderId(in.getOrderId()); + return ProductOrderReceipt.builder().orderId(in.getOrderId()).orderDetails(in.getOrderDetails()); } } private int orderId = -1; + private ProductOrder orderDetails; // for deserializer ProductOrderReceipt() { } - public ProductOrderReceipt(int orderId) { + public ProductOrderReceipt(int orderId,ProductOrder orderDetails) { this.orderId = orderId; + this.orderDetails = orderDetails; } @Override @@ -69,6 +77,16 @@ public class ProductOrderReceipt implements Comparable { return orderId; } + /** + * This is a copy of the SoftLayer_Container_Product_Order + * which holds all the data related to an order. + * This will only return when an order is processed successfully. + * It will contain all the items in an order as well as the order totals. + */ + public ProductOrder getOrderDetails() { + return orderDetails; + } + public Builder toBuilder() { return Builder.fromAddress(this); } @@ -97,7 +115,7 @@ public class ProductOrderReceipt implements Comparable { @Override public String toString() { - return "[orderId=" + orderId + "]"; + return "[orderId=" + orderId + ", orderDetails="+orderDetails+"]"; } diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/util/SoftLayerUtils.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/util/SoftLayerUtils.java deleted file mode 100644 index 3bd1660466..0000000000 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/util/SoftLayerUtils.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * 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.util; - -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; -import org.jclouds.softlayer.domain.VirtualGuest; - -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * Utils for the SoftLayer project - * @author Jason King - */ -public class SoftLayerUtils { - - /** - * Finds the virtual guest with given hostname and domainName - * @param allGuests the VirtualGuests to search - * @param hostname the virtual guest's hostname - * @param domainName the virtual guest's domain name - * @return the matching virtual guest or null if not found - * @throws IllegalStateException if more than one match is found - */ - public static VirtualGuest findVirtualGuest(Iterable allGuests, - final String hostname, final String domainName) { - - checkNotNull(allGuests,"allGuests"); - checkNotNull(hostname,"hostname"); - checkNotNull(domainName,"domainName"); - - Iterable guests = Iterables.filter(allGuests, new Predicate() { - - @Override - public boolean apply(VirtualGuest arg0) { - return hostname.equals(arg0.getHostname()) && domainName.equals(arg0.getDomain()); - } - - }); - switch (Iterables.size(guests)) { - case 0: - return null; - case 1: - return Iterables.get(guests, 0); - default: - throw new IllegalStateException(String.format( - "expected only 1 virtual guest with hostname %s and domainname %s: %s", hostname, domainName, - guests)); - } - } -} diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/SoftLayerComputeServiceAdapterLiveTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/SoftLayerComputeServiceAdapterLiveTest.java index 66d87c26f2..bbb37beca0 100644 --- a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/SoftLayerComputeServiceAdapterLiveTest.java +++ b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/SoftLayerComputeServiceAdapterLiveTest.java @@ -24,6 +24,7 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import java.util.Map; +import java.util.Random; import java.util.Set; import org.jclouds.compute.domain.ExecResponse; @@ -32,7 +33,6 @@ import org.jclouds.domain.Credentials; import org.jclouds.net.IPSocket; import org.jclouds.softlayer.compute.options.SoftLayerTemplateOptions; import org.jclouds.softlayer.compute.strategy.SoftLayerComputeServiceAdapter; -import org.jclouds.softlayer.compute.strategy.SoftLayerComputeServiceAdapter.OnlyOneVirtualGuestPresentWithHostAndDomainName; import org.jclouds.softlayer.domain.ProductItem; import org.jclouds.softlayer.domain.VirtualGuest; import org.jclouds.softlayer.features.BaseSoftLayerClientLiveTest; @@ -56,7 +56,7 @@ public class SoftLayerComputeServiceAdapterLiveTest extends BaseSoftLayerClientL public void setupClient() { super.setupClient(); adapter = new SoftLayerComputeServiceAdapter(context.getApi(), - ProductPackageClientLiveTest.CLOUD_SERVER_PACKAGE_NAME, new OnlyOneVirtualGuestPresentWithHostAndDomainName(context.getApi()), 5000l); + ProductPackageClientLiveTest.CLOUD_SERVER_PACKAGE_NAME); } @Test @@ -67,7 +67,7 @@ public class SoftLayerComputeServiceAdapterLiveTest extends BaseSoftLayerClientL @Test public void testCreateNodeWithGroupEncodedIntoNameThenStoreCredentials() { String group = "foo"; - String name = "foo-ef4"; + String name = "node"+new Random().nextInt(); Template template = computeContext.getComputeService().templateBuilder() .locationId("3") // the default (singapore) doesn't work. .build(); diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/util/SoftLayerUtilsTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/util/SoftLayerUtilsTest.java deleted file mode 100644 index 897c7b2d7d..0000000000 --- a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/util/SoftLayerUtilsTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * 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.util; - -import com.google.common.collect.ImmutableSet; -import org.jclouds.softlayer.domain.VirtualGuest; -import org.testng.annotations.Test; - -import static org.testng.Assert.assertEquals; -import static org.testng.AssertJUnit.assertNull; - -@Test(groups = "unit") -public class SoftLayerUtilsTest { - - @Test - public void testMatch() { - VirtualGuest guest1 = new VirtualGuest.Builder().id(1).hostname("host1").domain("domain").build(); - VirtualGuest guest2 = new VirtualGuest.Builder().id(2).hostname("host2").domain("domain").build(); - - VirtualGuest result = SoftLayerUtils.findVirtualGuest(ImmutableSet.of(guest1,guest2),"host1","domain"); - assertEquals(guest1,result); - } - - @Test - public void testMissing() { - VirtualGuest guest1 = new VirtualGuest.Builder().id(1).hostname("host1").domain("domain").build(); - VirtualGuest guest2 = new VirtualGuest.Builder().id(2).hostname("host2").domain("domain").build(); - - VirtualGuest result = SoftLayerUtils.findVirtualGuest(ImmutableSet.of(guest1,guest2),"missing","domain"); - assertNull(result); - } - - @Test(expectedExceptions = IllegalStateException.class) - public void testDuplicate() { - VirtualGuest guest1 = new VirtualGuest.Builder().id(1).hostname("host1").domain("domain").build(); - VirtualGuest guest2 = new VirtualGuest.Builder().id(2).hostname("host1").domain("domain").build(); - - SoftLayerUtils.findVirtualGuest(ImmutableSet.of(guest1,guest2),"host1","domain"); - } - -}