Issue 158: Removed function to find virtual guest - no longer needed

This commit is contained in:
Jason King 2011-09-30 13:57:04 +01:00
parent 1e2e41f7c1
commit b3001aa9ee
5 changed files with 30 additions and 166 deletions

View File

@ -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<VirtualGuest> 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<VirtualGuest>(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<VirtualGuest> 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<VirtualGuest> {
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<VirtualGuest> allGuests = client.getVirtualGuestClient().listVirtualGuests();
return SoftLayerUtils.findVirtualGuest(allGuests, guest.getHostname(), guest.getDomain()) != null;
}
}
private Iterable<ProductItemPrice> getPrices(Template template) {
Set<ProductItemPrice> result = Sets.newLinkedHashSet();

View File

@ -31,30 +31,38 @@ public class ProductOrderReceipt implements Comparable<ProductOrderReceipt> {
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<ProductOrderReceipt> {
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<ProductOrderReceipt> {
@Override
public String toString() {
return "[orderId=" + orderId + "]";
return "[orderId=" + orderId + ", orderDetails="+orderDetails+"]";
}

View File

@ -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<VirtualGuest> allGuests,
final String hostname, final String domainName) {
checkNotNull(allGuests,"allGuests");
checkNotNull(hostname,"hostname");
checkNotNull(domainName,"domainName");
Iterable<VirtualGuest> guests = Iterables.filter(allGuests, new Predicate<VirtualGuest>() {
@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));
}
}
}

View File

@ -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();

View File

@ -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");
}
}