diff --git a/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/domain/LoadBalancer.java b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/domain/LoadBalancer.java index 3b315a3936..e83d2142f5 100644 --- a/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/domain/LoadBalancer.java +++ b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/domain/LoadBalancer.java @@ -75,6 +75,11 @@ public class LoadBalancer extends BaseLoadBalancer { this.status = status; return this; } + + public Builder algorithm(Algorithm algorithm) { + algorithm(algorithm.name()); + return this; + } public Builder virtualIPs(Iterable virtualIPs) { this.virtualIPs = ImmutableSet. copyOf(checkNotNull(virtualIPs, "virtualIPs")); @@ -207,10 +212,56 @@ public class LoadBalancer extends BaseLoadBalancer { } } + + /** + All load balancers utilize an algorithm that defines how traffic should be directed between + back-end nodes. The default algorithm for newly created load balancers is RANDOM, which can + be overridden at creation time or changed after the load balancer has been initially + provisioned. The algorithm name is to be constant within a major revision of the load + balancing API, though new algorithms may be created with a unique algorithm name within + a given major revision of the service API. + */ + public static enum Algorithm { + /** + * The node with the lowest number of connections will receive requests. + */ + LEAST_CONNECTIONS, + /** + * Back-end servers are selected at random. + */ + RANDOM, + /** + * Connections are routed to each of the back-end servers in turn. + */ + ROUND_ROBIN, + /** + * Each request will be assigned to a node based on the number of concurrent connections + * to the node and its weight. + */ + WEIGHTED_LEAST_CONNECTIONS, + /** + * A round robin algorithm, but with different proportions of traffic being directed to + * the back-end nodes. Weights must be defined as part of the load balancer's node configuration. + */ + WEIGHTED_ROUND_ROBIN, + UNRECOGNIZED; + + public static Algorithm fromValue(String status) { + try { + return valueOf(checkNotNull(status, "status")); + } catch (IllegalArgumentException e) { + return UNRECOGNIZED; + } + } + } + + public static Algorithm[] WEIGHTED_ALGORITHMS = {Algorithm.WEIGHTED_LEAST_CONNECTIONS, + Algorithm.WEIGHTED_ROUND_ROBIN}; private final String region; private final int id; private final Status status; + private final Algorithm algorithm; private final Set virtualIPs; private final String sessionPersistenceType; private final String clusterName; @@ -226,6 +277,7 @@ public class LoadBalancer extends BaseLoadBalancer { checkArgument(id != -1, "id must be specified"); this.id = id; this.status = checkNotNull(status, "status"); + this.algorithm = Algorithm.fromValue(algorithm); this.virtualIPs = ImmutableSet.copyOf(checkNotNull(virtualIPs, "virtualIPs")); this.sessionPersistenceType = sessionPersistenceType; this.clusterName = clusterName; @@ -245,6 +297,10 @@ public class LoadBalancer extends BaseLoadBalancer { public Status getStatus() { return status; } + + public Algorithm getTypedAlgorithm() { + return algorithm; + } public Set getVirtualIPs() { return virtualIPs; diff --git a/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/BaseCloudLoadBalancersClientLiveTest.java b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/BaseCloudLoadBalancersClientLiveTest.java index da9ef235b8..0ef04756d5 100644 --- a/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/BaseCloudLoadBalancersClientLiveTest.java +++ b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/BaseCloudLoadBalancersClientLiveTest.java @@ -19,7 +19,9 @@ package org.jclouds.cloudloadbalancers.features; import static com.google.common.base.Preconditions.checkNotNull; +import static org.testng.Assert.assertEquals; +import java.util.Arrays; import java.util.Properties; import java.util.concurrent.TimeUnit; @@ -36,6 +38,7 @@ import org.jclouds.predicates.RetryablePredicate; import org.jclouds.rest.RestContext; import org.testng.annotations.AfterGroups; import org.testng.annotations.BeforeGroups; +import org.testng.annotations.Test; import com.google.common.base.Predicate; import com.google.common.collect.ImmutableSet; @@ -53,6 +56,7 @@ public class BaseCloudLoadBalancersClientLiveTest { protected CloudLoadBalancersClient client; protected RestContext context; protected String provider = "cloudloadbalancers"; + protected String[] regions = {}; protected String identity; protected String credential; protected String endpoint; diff --git a/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/LoadBalancerClientLiveTest.java b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/LoadBalancerClientLiveTest.java index f3f3137bb6..34a909c72b 100644 --- a/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/LoadBalancerClientLiveTest.java +++ b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/LoadBalancerClientLiveTest.java @@ -21,6 +21,7 @@ package org.jclouds.cloudloadbalancers.features; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; +import java.util.Arrays; import java.util.Set; import java.util.logging.Logger; @@ -47,7 +48,7 @@ public class LoadBalancerClientLiveTest extends BaseCloudLoadBalancersClientLive @BeforeGroups(groups = "live") protected void setup() { - assertTrue(client.getConfiguredRegions().size() > 0, "Need to have some regions!"); + assertEquals(client.getConfiguredRegions(), Arrays.asList(regions)); Logger.getAnonymousLogger().info("running against regions "+client.getConfiguredRegions()); } diff --git a/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/NodeClientLiveTest.java b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/NodeClientLiveTest.java index d452fc3780..1ffe36590e 100644 --- a/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/NodeClientLiveTest.java +++ b/apis/cloudloadbalancers/src/test/java/org/jclouds/cloudloadbalancers/features/NodeClientLiveTest.java @@ -21,6 +21,7 @@ package org.jclouds.cloudloadbalancers.features; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -89,7 +90,8 @@ public class NodeClientLiveTest extends BaseCloudLoadBalancersClientLiveTest { assert n.getAddress() != null : n; assert n.getPort() != -1 : n; assert n.getStatus() != null : n; - assert n.getWeight() != null : n; //FIXME may fail as can be null (json response doesn't have the attribute) + assert !Arrays.asList(LoadBalancer.WEIGHTED_ALGORITHMS).contains( + lb.getTypedAlgorithm()) || n.getWeight() != null : n; Node getDetails = client.getNodeClient(lb.getRegion()).getNodeInLoadBalancer(n.getId(), lb.getId()); System.out.println(n.toString()); @@ -99,7 +101,10 @@ public class NodeClientLiveTest extends BaseCloudLoadBalancersClientLiveTest { assertEquals(getDetails.getAddress(), n.getAddress()); assertEquals(getDetails.getPort(), n.getPort()); assertEquals(getDetails.getStatus(), n.getStatus()); - assertEquals(getDetails.getWeight(), n.getWeight()); //FIXME disparity between list/get can lead these to mismatch + if(Arrays.asList(LoadBalancer.WEIGHTED_ALGORITHMS).contains( + lb.getTypedAlgorithm())) { + assertEquals(getDetails.getWeight(), n.getWeight()); + } } catch (AssertionError e) { throw new AssertionError(String.format("%s\n%s - %s", e.getMessage(),getDetails, n)); } diff --git a/providers/cloudloadbalancers-uk/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUKPropertiesBuilder.java b/providers/cloudloadbalancers-uk/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUKPropertiesBuilder.java index fc9c750a82..785e76c4b6 100644 --- a/providers/cloudloadbalancers-uk/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUKPropertiesBuilder.java +++ b/providers/cloudloadbalancers-uk/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUKPropertiesBuilder.java @@ -32,16 +32,20 @@ import java.util.Properties; import org.jclouds.cloudloadbalancers.CloudLoadBalancersPropertiesBuilder; +import com.google.common.base.Joiner; + /** * Builds properties used inRackspace Cloud Load Balancers Clients * * @author Dan Lo Bianco */ public class CloudLoadBalancersUKPropertiesBuilder extends CloudLoadBalancersPropertiesBuilder { + public static final String[] REGIONS = {LON}; + @Override protected Properties defaultProperties() { Properties properties = super.defaultProperties(); - properties.setProperty(PROPERTY_REGIONS, "UK"); + properties.setProperty(PROPERTY_REGIONS, Joiner.on(',').join(REGIONS)); properties.setProperty(PROPERTY_ENDPOINT, "https://lon.auth.api.rackspacecloud.com"); properties.setProperty(PROPERTY_ISO3166_CODES, "GB-SLG"); diff --git a/providers/cloudloadbalancers-uk/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUKLoadBalancerClientLiveTest.java b/providers/cloudloadbalancers-uk/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUKLoadBalancerClientLiveTest.java index e0211bd2d6..3ef8cfc4f6 100644 --- a/providers/cloudloadbalancers-uk/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUKLoadBalancerClientLiveTest.java +++ b/providers/cloudloadbalancers-uk/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUKLoadBalancerClientLiveTest.java @@ -29,5 +29,6 @@ import org.testng.annotations.Test; public class CloudLoadBalancersUKLoadBalancerClientLiveTest extends LoadBalancerClientLiveTest { public CloudLoadBalancersUKLoadBalancerClientLiveTest() { provider = "cloudloadbalancers-uk"; + regions = CloudLoadBalancersUKPropertiesBuilder.REGIONS; } } diff --git a/providers/cloudloadbalancers-us/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUSPropertiesBuilder.java b/providers/cloudloadbalancers-us/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUSPropertiesBuilder.java index ad7416e3fc..9b0c4d009d 100644 --- a/providers/cloudloadbalancers-us/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUSPropertiesBuilder.java +++ b/providers/cloudloadbalancers-us/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUSPropertiesBuilder.java @@ -41,11 +41,13 @@ import com.google.common.base.Joiner; * @author Adrian Cole */ public class CloudLoadBalancersUSPropertiesBuilder extends CloudLoadBalancersPropertiesBuilder { + public static final String[] REGIONS = {ORD, DFW}; + @Override protected Properties defaultProperties() { Properties properties = super.defaultProperties(); properties.setProperty(PROPERTY_ENDPOINT, "https://auth.api.rackspacecloud.com"); - properties.setProperty(PROPERTY_REGIONS, Joiner.on(',').join(ORD, DFW)); + properties.setProperty(PROPERTY_REGIONS, Joiner.on(',').join(REGIONS)); properties.setProperty(PROPERTY_ISO3166_CODES, "US-IL,US-TX"); properties.setProperty(PROPERTY_REGION + "." + ORD + "." + ISO3166_CODES, "US-IL"); diff --git a/providers/cloudloadbalancers-us/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUSLoadBalancerClientLiveTest.java b/providers/cloudloadbalancers-us/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUSLoadBalancerClientLiveTest.java index 6610ebcb0e..0d471943be 100644 --- a/providers/cloudloadbalancers-us/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUSLoadBalancerClientLiveTest.java +++ b/providers/cloudloadbalancers-us/src/test/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersUSLoadBalancerClientLiveTest.java @@ -29,5 +29,6 @@ import org.testng.annotations.Test; public class CloudLoadBalancersUSLoadBalancerClientLiveTest extends LoadBalancerClientLiveTest { public CloudLoadBalancersUSLoadBalancerClientLiveTest() { provider = "cloudloadbalancers-us"; + regions = CloudLoadBalancersUSPropertiesBuilder.REGIONS; } }