diff --git a/labs/aws-elb/src/test/java/org/jclouds/aws/elb/features/AWSAvailabilityZoneClientLiveTest.java b/labs/aws-elb/src/test/java/org/jclouds/aws/elb/features/AWSAvailabilityZoneClientLiveTest.java new file mode 100644 index 0000000000..f8f667ae97 --- /dev/null +++ b/labs/aws-elb/src/test/java/org/jclouds/aws/elb/features/AWSAvailabilityZoneClientLiveTest.java @@ -0,0 +1,35 @@ +/** + * 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.aws.elb.features; + +import org.jclouds.elb.features.AvailabilityZoneClientLiveTest; +import org.testng.annotations.Test; + +/** + * + * @author Adrian Cole + */ +@Test(groups = "live", testName = "AWSAvailabilityZoneClientLiveTest") +public class AWSAvailabilityZoneClientLiveTest extends AvailabilityZoneClientLiveTest { + + public AWSAvailabilityZoneClientLiveTest() { + provider = "aws-elb"; + } + +} diff --git a/labs/elb/src/main/java/org/jclouds/elb/ELBAsyncClient.java b/labs/elb/src/main/java/org/jclouds/elb/ELBAsyncClient.java index 65ec547641..7f8fa980b3 100644 --- a/labs/elb/src/main/java/org/jclouds/elb/ELBAsyncClient.java +++ b/labs/elb/src/main/java/org/jclouds/elb/ELBAsyncClient.java @@ -26,6 +26,7 @@ import org.jclouds.aws.filters.FormSigner; import org.jclouds.elb.features.InstanceAsyncClient; import org.jclouds.elb.features.LoadBalancerAsyncClient; import org.jclouds.elb.features.PolicyAsyncClient; +import org.jclouds.elb.features.AvailabilityZoneAsyncClient; import org.jclouds.location.Region; import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull; import org.jclouds.rest.annotations.Delegate; @@ -86,4 +87,14 @@ public interface ELBAsyncClient { InstanceAsyncClient getInstanceClientForRegion( @EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region); + /** + * Provides asynchronous access to AvailabilityZone features. + */ + @Delegate + AvailabilityZoneAsyncClient getAvailabilityZoneClient(); + + @Delegate + AvailabilityZoneAsyncClient getAvailabilityZoneClientForRegion( + @EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region); + } diff --git a/labs/elb/src/main/java/org/jclouds/elb/ELBClient.java b/labs/elb/src/main/java/org/jclouds/elb/ELBClient.java index d256c33055..5016c1269c 100644 --- a/labs/elb/src/main/java/org/jclouds/elb/ELBClient.java +++ b/labs/elb/src/main/java/org/jclouds/elb/ELBClient.java @@ -27,6 +27,7 @@ import org.jclouds.concurrent.Timeout; import org.jclouds.elb.features.InstanceClient; import org.jclouds.elb.features.LoadBalancerClient; import org.jclouds.elb.features.PolicyClient; +import org.jclouds.elb.features.AvailabilityZoneClient; import org.jclouds.location.Region; import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull; import org.jclouds.rest.annotations.Delegate; @@ -82,5 +83,15 @@ public interface ELBClient { @Delegate InstanceClient getInstanceClientForRegion( @EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region); + + /** + * Provides synchronous access to Zone features. + */ + @Delegate + AvailabilityZoneClient getAvailabilityZoneClient(); + + @Delegate + AvailabilityZoneClient getAvailabilityZoneClientForRegion( + @EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region); } diff --git a/labs/elb/src/main/java/org/jclouds/elb/config/ELBRestClientModule.java b/labs/elb/src/main/java/org/jclouds/elb/config/ELBRestClientModule.java index c5d5ce832a..97136c2c77 100644 --- a/labs/elb/src/main/java/org/jclouds/elb/config/ELBRestClientModule.java +++ b/labs/elb/src/main/java/org/jclouds/elb/config/ELBRestClientModule.java @@ -29,6 +29,8 @@ import org.jclouds.elb.features.LoadBalancerAsyncClient; import org.jclouds.elb.features.LoadBalancerClient; import org.jclouds.elb.features.PolicyAsyncClient; import org.jclouds.elb.features.PolicyClient; +import org.jclouds.elb.features.AvailabilityZoneAsyncClient; +import org.jclouds.elb.features.AvailabilityZoneClient; import org.jclouds.rest.ConfiguresRestClient; import com.google.common.collect.ImmutableMap; @@ -45,6 +47,7 @@ public class ELBRestClientModule extends FormSigningRestClientModule reasonCode; protected final String state; - protected InstanceState(String description, String instanceId, Optional reasonCode, String state) { + protected InstanceHealth(String description, String instanceId, Optional reasonCode, String state) { this.description = checkNotNull(description, "description"); this.instanceId = checkNotNull(instanceId, "instanceId"); this.reasonCode = checkNotNull(reasonCode, "reasonCode"); @@ -151,7 +151,7 @@ public class InstanceState { return false; if (getClass() != obj.getClass()) return false; - InstanceState other = InstanceState.class.cast(obj); + InstanceHealth other = InstanceHealth.class.cast(obj); return Objects.equal(this.description, other.description) && Objects.equal(this.instanceId, other.instanceId) && Objects.equal(this.reasonCode, other.reasonCode) && Objects.equal(this.state, other.state); } diff --git a/labs/elb/src/main/java/org/jclouds/elb/features/AvailabilityZoneAsyncClient.java b/labs/elb/src/main/java/org/jclouds/elb/features/AvailabilityZoneAsyncClient.java new file mode 100644 index 0000000000..f2d8eb4461 --- /dev/null +++ b/labs/elb/src/main/java/org/jclouds/elb/features/AvailabilityZoneAsyncClient.java @@ -0,0 +1,97 @@ +/** + * 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.elb.features; + +import static org.jclouds.aws.reference.FormParameters.ACTION; + +import java.util.Set; + +import javax.ws.rs.FormParam; +import javax.ws.rs.POST; +import javax.ws.rs.Path; + +import org.jclouds.aws.filters.FormSigner; +import org.jclouds.elb.binders.BindAvailabilityZonesToIndexedFormParams; +import org.jclouds.elb.xml.AvailabilityZonesResultHandler; +import org.jclouds.rest.annotations.BinderParam; +import org.jclouds.rest.annotations.FormParams; +import org.jclouds.rest.annotations.RequestFilters; +import org.jclouds.rest.annotations.VirtualHost; +import org.jclouds.rest.annotations.XMLResponseParser; + +import com.google.common.util.concurrent.ListenableFuture; + +/** + * Provides access to Amazon ELB via the Query API + *

+ * + * @see doc + * @see AvailabilityZoneClient + * @author Adrian Cole + */ +@RequestFilters(FormSigner.class) +@VirtualHost +public interface AvailabilityZoneAsyncClient { + + /** + * @see AvailabilityZoneClient#addAvailabilityZonesToLoadBalancer + */ + @POST + @Path("/") + @XMLResponseParser(AvailabilityZonesResultHandler.class) + @FormParams(keys = ACTION, values = "EnableAvailabilityZonesForLoadBalancer") + ListenableFuture> addAvailabilityZonesToLoadBalancer( + @BinderParam(BindAvailabilityZonesToIndexedFormParams.class) Iterable zones, + @FormParam("LoadBalancerName") String loadBalancerName); + + + /** + * @see AvailabilityZoneClient#addAvailabilityZoneToLoadBalancer + */ + @POST + @Path("/") + @XMLResponseParser(AvailabilityZonesResultHandler.class) + @FormParams(keys = ACTION, values = "EnableAvailabilityZonesForLoadBalancer") + ListenableFuture> addAvailabilityZoneToLoadBalancer( + @FormParam("AvailabilityZones.member.1") String zone, + @FormParam("LoadBalancerName") String loadBalancerName); + + /** + * @see AvailabilityZoneClient#removeAvailabilityZonesFromLoadBalancer + */ + @POST + @Path("/") + @XMLResponseParser(AvailabilityZonesResultHandler.class) + @FormParams(keys = ACTION, values = "DisableAvailabilityZonesForLoadBalancer") + ListenableFuture> removeAvailabilityZonesFromLoadBalancer( + @BinderParam(BindAvailabilityZonesToIndexedFormParams.class) Iterable zones, + @FormParam("LoadBalancerName") String loadBalancerName); + + /** + * @see AvailabilityZoneClient#removeAvailabilityZoneFromLoadBalancer + */ + @POST + @Path("/") + @XMLResponseParser(AvailabilityZonesResultHandler.class) + @FormParams(keys = ACTION, values = "DisableAvailabilityZonesForLoadBalancer") + ListenableFuture> removeAvailabilityZoneFromLoadBalancer( + @FormParam("AvailabilityZones.member.1") String zone, + @FormParam("LoadBalancerName") String loadBalancerName); +} diff --git a/labs/elb/src/main/java/org/jclouds/elb/features/AvailabilityZoneClient.java b/labs/elb/src/main/java/org/jclouds/elb/features/AvailabilityZoneClient.java new file mode 100644 index 0000000000..6939f2513e --- /dev/null +++ b/labs/elb/src/main/java/org/jclouds/elb/features/AvailabilityZoneClient.java @@ -0,0 +1,102 @@ +/** + * 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.elb.features; + +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import org.jclouds.concurrent.Timeout; + +/** + * Provides access to Amazon ELB via the Query API + *

+ * + * @see + * @see AvailabilityZoneAsyncClient + * @author Adrian Cole + */ +@Timeout(duration = 30, timeUnit = TimeUnit.SECONDS) +public interface AvailabilityZoneClient { + + /** + * Adds one or more EC2 Availability Zones to the LoadBalancer. + * + * The LoadBalancer evenly distributes requests across all its registered Availability Zones that + * contain instances. As a result, the client must ensure that its LoadBalancer is appropriately + * scaled for each registered Availability Zone. + * + *

Note

+ * + * The new EC2 Availability Zones to be added must be in the same EC2 Region as the Availability + * Zones for which the LoadBalancer was created. + * + * @param zones + * A list of new Availability Zones for the LoadBalancer. Each Availability Zone must + * be in the same Region as the LoadBalancer. + * + * @param loadBalancerName + * The name associated with the LoadBalancer. The name must be unique within the client + * AWS account. + * + * @return An updated list of Availability Zones for the LoadBalancer. + */ + Set addAvailabilityZonesToLoadBalancer(Iterable zones, String loadBalancerName); + + Set addAvailabilityZoneToLoadBalancer(String zone, String loadBalancerName); + + /** + * Removes the specified EC2 Availability Zones from the set of configured Availability Zones for + * the LoadBalancer. + * + * + * There must be at least one Availability Zone registered with a LoadBalancer at all times. A + * client cannot remove all the Availability Zones from a LoadBalancer. Once an Availability Zone + * is removed, all the instances registered with the LoadBalancer that are in the removed + * Availability Zone go into the OutOfService state. Upon Availability Zone removal, the + * LoadBalancer attempts to equally balance the traffic among its remaining usable Availability + * Zones. Trying to remove an Availability Zone that was not associated with the LoadBalancer + * does nothing. + * + *

Note

+ * + * In order for this call to be successful, the client must have created the LoadBalancer. The + * client must provide the same account credentials as those that were used to create the + * LoadBalancer. + * + * @param zones + * A list of Availability Zones to be removed from the LoadBalancer. + * + *

Note

+ * + * There must be at least one Availability Zone registered with a LoadBalancer at all + * times. The client cannot remove all the Availability Zones from a LoadBalancer. + * Specified Availability Zones must be in the same Region. + * + * + * @param loadBalancerName + * The name associated with the LoadBalancer. The name must be unique within the client + * AWS account. + * + * @return A list of updated Availability Zones for the LoadBalancer. + */ + Set removeAvailabilityZonesFromLoadBalancer(Iterable zones, String loadBalancerName); + + Set removeAvailabilityZoneFromLoadBalancer(String zone, String loadBalancerName); + +} diff --git a/labs/elb/src/main/java/org/jclouds/elb/features/InstanceAsyncClient.java b/labs/elb/src/main/java/org/jclouds/elb/features/InstanceAsyncClient.java index a6892d86e4..fbd25f79e0 100644 --- a/labs/elb/src/main/java/org/jclouds/elb/features/InstanceAsyncClient.java +++ b/labs/elb/src/main/java/org/jclouds/elb/features/InstanceAsyncClient.java @@ -28,8 +28,8 @@ import javax.ws.rs.Path; import org.jclouds.aws.filters.FormSigner; import org.jclouds.elb.binders.BindInstanceIdsToIndexedFormParams; -import org.jclouds.elb.domain.InstanceState; -import org.jclouds.elb.xml.DescribeLoadBalancerPoliciesResultHandler; +import org.jclouds.elb.domain.InstanceHealth; +import org.jclouds.elb.xml.DescribeInstanceHealthResultHandler; import org.jclouds.elb.xml.InstancesResultHandler; import org.jclouds.rest.annotations.BinderParam; import org.jclouds.rest.annotations.ExceptionParser; @@ -55,25 +55,25 @@ import com.google.common.util.concurrent.ListenableFuture; public interface InstanceAsyncClient { /** - * @see InstanceClient#getStateOfInstancesOfLoadBalancer(String) + * @see InstanceClient#getHealthOfInstancesOfLoadBalancer(String) */ @POST @Path("/") - @XMLResponseParser(DescribeLoadBalancerPoliciesResultHandler.class) + @XMLResponseParser(DescribeInstanceHealthResultHandler.class) @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) @FormParams(keys = "Action", values = "DescribeInstanceHealth") - ListenableFuture> getStateOfInstancesOfLoadBalancer( + ListenableFuture> getHealthOfInstancesOfLoadBalancer( @FormParam("LoadBalancerName") String loadBalancerName); /** - * @see InstanceClient#getStateOfInstancesOfLoadBalancer(Iterable, String) + * @see InstanceClient#getHealthOfInstancesOfLoadBalancer(Iterable, String) */ @POST @Path("/") - @XMLResponseParser(DescribeLoadBalancerPoliciesResultHandler.class) + @XMLResponseParser(DescribeInstanceHealthResultHandler.class) @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) @FormParams(keys = "Action", values = "DescribeInstanceHealth") - ListenableFuture> getStateOfInstancesOfLoadBalancer( + ListenableFuture> getHealthOfInstancesOfLoadBalancer( @BinderParam(BindInstanceIdsToIndexedFormParams.class) Iterable instanceIds, @FormParam("LoadBalancerName") String loadBalancerName); diff --git a/labs/elb/src/main/java/org/jclouds/elb/features/InstanceClient.java b/labs/elb/src/main/java/org/jclouds/elb/features/InstanceClient.java index fbb50f831e..f532dbfb12 100644 --- a/labs/elb/src/main/java/org/jclouds/elb/features/InstanceClient.java +++ b/labs/elb/src/main/java/org/jclouds/elb/features/InstanceClient.java @@ -22,7 +22,7 @@ import java.util.Set; import java.util.concurrent.TimeUnit; import org.jclouds.concurrent.Timeout; -import org.jclouds.elb.domain.InstanceState; +import org.jclouds.elb.domain.InstanceHealth; /** * Provides access to Amazon ELB via the Query API @@ -50,7 +50,7 @@ public interface InstanceClient { * * @return state of all instances of the load balancer */ - Set getStateOfInstancesOfLoadBalancer(String loadBalancerName); + Set getHealthOfInstancesOfLoadBalancer(String loadBalancerName); /** * Returns the current state of the instances of the specified LoadBalancer. @@ -69,7 +69,7 @@ public interface InstanceClient { * * @return state of all instances of the load balancer */ - Set getStateOfInstancesOfLoadBalancer(Iterable instanceIds, String loadBalancerName); + Set getHealthOfInstancesOfLoadBalancer(Iterable instanceIds, String loadBalancerName); /** * Adds new instances to the LoadBalancer. diff --git a/labs/elb/src/main/java/org/jclouds/elb/loadbalancer/strategy/ELBLoadBalanceNodesStrategy.java b/labs/elb/src/main/java/org/jclouds/elb/loadbalancer/strategy/ELBLoadBalanceNodesStrategy.java index 31fa508856..ec5cd27ba3 100644 --- a/labs/elb/src/main/java/org/jclouds/elb/loadbalancer/strategy/ELBLoadBalanceNodesStrategy.java +++ b/labs/elb/src/main/java/org/jclouds/elb/loadbalancer/strategy/ELBLoadBalanceNodesStrategy.java @@ -45,6 +45,7 @@ import org.jclouds.logging.Logger; import com.google.common.base.Function; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; /** * @@ -71,7 +72,7 @@ public class ELBLoadBalanceNodesStrategy implements LoadBalanceNodesStrategy { checkNotNull(location, "location"); String region = getRegionFromLocationOrNull(location); - Set availabilityZones = ImmutableSet.copyOf(transform(nodes, new Function() { + Set zonesDesired = ImmutableSet.copyOf(transform(nodes, new Function() { @Override public String apply(NodeMetadata from) { @@ -79,16 +80,22 @@ public class ELBLoadBalanceNodesStrategy implements LoadBalanceNodesStrategy { } })); - logger.debug(">> creating loadBalancer(%s)", name); + logger.debug(">> creating loadBalancer(%s) in zones(%s)", name, zonesDesired); try { String dnsName = client.getLoadBalancerClientForRegion(region).createLoadBalancerListeningInAvailabilityZones( name, ImmutableSet.of(Listener.builder().port(loadBalancerPort).instancePort(instancePort) - .protocol(Protocol.valueOf(protocol)).build()), availabilityZones); + .protocol(Protocol.valueOf(protocol)).build()), zonesDesired); logger.debug("<< created loadBalancer(%s) dnsName(%s)", name, dnsName); } catch (IllegalStateException e) { - logger.debug("<< reusing loadBalancer(%s)", name); - // TODO: converge availability zones + logger.debug("<< converging zones(%s) in loadBalancer(%s)", zonesDesired, name); + Set currentZones = client.getLoadBalancerClient().get(name).getAvailabilityZones(); + Set zonesToAdd = Sets.difference(zonesDesired, currentZones); + if (zonesToAdd.size() > 0) + currentZones = client.getAvailabilityZoneClient().addAvailabilityZonesToLoadBalancer(zonesToAdd, name); + Set zonesToRemove = Sets.difference(currentZones, zonesDesired); + if (zonesToRemove.size() > 0) + client.getAvailabilityZoneClient().addAvailabilityZonesToLoadBalancer(zonesToRemove, name); } Set instanceIds = ImmutableSet.copyOf(transform(nodes, new Function() { diff --git a/labs/elb/src/main/java/org/jclouds/elb/xml/AvailabilityZonesResultHandler.java b/labs/elb/src/main/java/org/jclouds/elb/xml/AvailabilityZonesResultHandler.java new file mode 100644 index 0000000000..547ed6224d --- /dev/null +++ b/labs/elb/src/main/java/org/jclouds/elb/xml/AvailabilityZonesResultHandler.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.elb.xml; + +import java.util.Set; + +import org.jclouds.http.functions.ParseSax; + +import com.google.common.collect.Sets; + +public class AvailabilityZonesResultHandler extends ParseSax.HandlerWithResult> { + + private Set zones = Sets.newLinkedHashSet(); + private StringBuilder currentText = new StringBuilder(); + + public void endElement(String uri, String localName, String qName) { + if (qName.equals("member")) + zones.add(currentText.toString().trim()); + currentText = new StringBuilder(); + } + + @Override + public Set getResult() { + return zones; + } + + public void characters(char ch[], int start, int length) { + currentText.append(ch, start, length); + } +} diff --git a/labs/elb/src/main/java/org/jclouds/elb/xml/DescribeInstanceHealthResultHandler.java b/labs/elb/src/main/java/org/jclouds/elb/xml/DescribeInstanceHealthResultHandler.java index b7c1fd9c6e..579172059e 100644 --- a/labs/elb/src/main/java/org/jclouds/elb/xml/DescribeInstanceHealthResultHandler.java +++ b/labs/elb/src/main/java/org/jclouds/elb/xml/DescribeInstanceHealthResultHandler.java @@ -22,7 +22,7 @@ import static org.jclouds.util.SaxUtils.equalsOrSuffix; import java.util.Set; -import org.jclouds.elb.domain.InstanceState; +import org.jclouds.elb.domain.InstanceHealth; import org.jclouds.http.functions.ParseSax; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -39,12 +39,12 @@ import com.google.inject.Inject; * @author Adrian Cole */ public class DescribeInstanceHealthResultHandler extends - ParseSax.HandlerForGeneratedRequestWithResult> { + ParseSax.HandlerForGeneratedRequestWithResult> { private final InstanceStateHandler instanceStateHandler; private StringBuilder currentText = new StringBuilder(); - private Builder instanceStates = ImmutableSet. builder(); + private Builder instanceStates = ImmutableSet. builder(); private boolean inStates; protected int memberDepth; @@ -58,7 +58,7 @@ public class DescribeInstanceHealthResultHandler extends * {@inheritDoc} */ @Override - public Set getResult() { + public Set getResult() { return instanceStates.build(); } diff --git a/labs/elb/src/main/java/org/jclouds/elb/xml/InstanceStateHandler.java b/labs/elb/src/main/java/org/jclouds/elb/xml/InstanceStateHandler.java index 26d4b9d7c8..4df2752ed1 100644 --- a/labs/elb/src/main/java/org/jclouds/elb/xml/InstanceStateHandler.java +++ b/labs/elb/src/main/java/org/jclouds/elb/xml/InstanceStateHandler.java @@ -21,7 +21,7 @@ package org.jclouds.elb.xml; import static org.jclouds.util.SaxUtils.currentOrNull; import static org.jclouds.util.SaxUtils.equalsOrSuffix; -import org.jclouds.elb.domain.InstanceState; +import org.jclouds.elb.domain.InstanceHealth; import org.jclouds.http.functions.ParseSax; import org.xml.sax.SAXException; @@ -32,20 +32,20 @@ import org.xml.sax.SAXException; * * @author Adrian Cole */ -public class InstanceStateHandler extends ParseSax.HandlerForGeneratedRequestWithResult { +public class InstanceStateHandler extends ParseSax.HandlerForGeneratedRequestWithResult { private StringBuilder currentText = new StringBuilder(); - private InstanceState.Builder builder = InstanceState.builder(); + private InstanceHealth.Builder builder = InstanceHealth.builder(); /** * {@inheritDoc} */ @Override - public InstanceState getResult() { + public InstanceHealth getResult() { try { return builder.build(); } finally { - builder = InstanceState.builder(); + builder = InstanceHealth.builder(); } } diff --git a/labs/elb/src/test/java/org/jclouds/elb/features/AvailabilityZoneClientExpectTest.java b/labs/elb/src/test/java/org/jclouds/elb/features/AvailabilityZoneClientExpectTest.java new file mode 100644 index 0000000000..3993f4a9b3 --- /dev/null +++ b/labs/elb/src/test/java/org/jclouds/elb/features/AvailabilityZoneClientExpectTest.java @@ -0,0 +1,221 @@ +/** + * 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 + * + * Unles 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 expres or implied. See the License for the + * specific language governing permisions and limitations + * under the License. + */ +package org.jclouds.elb.features; + +import static org.testng.Assert.assertEquals; + +import java.net.URI; +import java.util.TimeZone; + +import org.jclouds.elb.ELBClient; +import org.jclouds.elb.internal.BaseELBClientExpectTest; +import org.jclouds.elb.parse.AvailabilityZonesResultHandlerTest; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.jclouds.rest.ResourceNotFoundException; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.ImmutableSet; + +/** + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "AvailabilityZoneClientExpectTest") +public class AvailabilityZoneClientExpectTest extends BaseELBClientExpectTest { + + public AvailabilityZoneClientExpectTest() { + TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles")); + } + + HttpRequest addZoneToLoadBalancer = HttpRequest.builder() + .method("POST") + .endpoint(URI.create("https://elasticloadbalancing.us-east-1.amazonaws.com/")) + .headers(ImmutableMultimap. builder() + .put("Host", "elasticloadbalancing.us-east-1.amazonaws.com") + .build()) + .payload( + payloadFromStringWithContentType( + "Action=EnableAvailabilityZonesForLoadBalancer" + + "&AvailabilityZones.member.1=us-east-1a" + + "&LoadBalancerName=name" + + "&Signature=lay8JNIpYsgWjiTbA4%2FrgKrQPWhFKToPxw%2FfCLld4SE%3D" + + "&SignatureMethod=HmacSHA256" + + "&SignatureVersion=2" + + "&Timestamp=2009-11-08T15%3A54%3A08.897Z" + + "&Version=2012-06-01" + + "&AWSAccessKeyId=identity", + "application/x-www-form-urlencoded")) + .build(); + + + public void testAddZoneToLoadBalancerWhenResponseIs2xx() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/zones.xml", "text/xml")).build(); + + ELBClient clientWhenExist = requestSendsResponse( + addZoneToLoadBalancer, getResponse); + + assertEquals(clientWhenExist.getAvailabilityZoneClient().addAvailabilityZoneToLoadBalancer("us-east-1a", "name").toString(), new AvailabilityZonesResultHandlerTest().expected().toString()); + } + + @Test(expectedExceptions = ResourceNotFoundException.class) + public void testAddZoneToLoadBalancerWhenResponseIs404() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(404).build(); + + ELBClient clientWhenDontExist = requestSendsResponse( + addZoneToLoadBalancer, getResponse); + + clientWhenDontExist.getAvailabilityZoneClient().addAvailabilityZoneToLoadBalancer("us-east-1a", "name"); + } + + HttpRequest addZonesToLoadBalancer = HttpRequest.builder() + .method("POST") + .endpoint(URI.create("https://elasticloadbalancing.us-east-1.amazonaws.com/")) + .headers(ImmutableMultimap. builder() + .put("Host", "elasticloadbalancing.us-east-1.amazonaws.com") + .build()) + .payload( + payloadFromStringWithContentType( + "Action=EnableAvailabilityZonesForLoadBalancer" + + "&AvailabilityZones.member.1=us-east-1a" + + "&AvailabilityZones.member.2=us-east-1b" + + "&LoadBalancerName=name" + + "&Signature=RAX1VLJU30B47RFUiywtknhgD2DxZygJ2niOO4UnW3U%3D" + + "&SignatureMethod=HmacSHA256" + + "&SignatureVersion=2" + + "&Timestamp=2009-11-08T15%3A54%3A08.897Z" + + "&Version=2012-06-01" + + "&AWSAccessKeyId=identity", + "application/x-www-form-urlencoded")) + .build(); + + + public void testAddZonesToLoadBalancerWhenResponseIs2xx() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/zones.xml", "text/xml")).build(); + + ELBClient clientWhenExist = requestSendsResponse(addZonesToLoadBalancer, getResponse); + + assertEquals( + clientWhenExist.getAvailabilityZoneClient().addAvailabilityZonesToLoadBalancer(ImmutableSet.of("us-east-1a", "us-east-1b"), "name") + .toString(), new AvailabilityZonesResultHandlerTest().expected().toString()); + } + + @Test(expectedExceptions = ResourceNotFoundException.class) + public void testAddZonesToLoadBalancerWhenResponseIs404() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(404).build(); + + ELBClient clientWhenDontExist = requestSendsResponse(addZonesToLoadBalancer, getResponse); + + clientWhenDontExist.getAvailabilityZoneClient().addAvailabilityZonesToLoadBalancer(ImmutableSet.of("us-east-1a", "us-east-1b"), "name"); + } + + + HttpRequest removeZoneFromLoadBalancer = HttpRequest.builder() + .method("POST") + .endpoint(URI.create("https://elasticloadbalancing.us-east-1.amazonaws.com/")) + .headers(ImmutableMultimap. builder() + .put("Host", "elasticloadbalancing.us-east-1.amazonaws.com") + .build()) + .payload( + payloadFromStringWithContentType( + "Action=DisableAvailabilityZonesForLoadBalancer" + + "&AvailabilityZones.member.1=us-east-1a" + + "&LoadBalancerName=name" + + "&Signature=tjzaFDhUghKwTpe%2F9OC8JK%2BJsRMCkF3Kh5YkvPEDPbg%3D" + + "&SignatureMethod=HmacSHA256" + + "&SignatureVersion=2" + + "&Timestamp=2009-11-08T15%3A54%3A08.897Z" + + "&Version=2012-06-01" + + "&AWSAccessKeyId=identity", + "application/x-www-form-urlencoded")) + .build(); + + + public void testRemoveZoneFromLoadBalancerWhenResponseIs2xx() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/zones.xml", "text/xml")).build(); + + ELBClient clientWhenExist = requestSendsResponse( + removeZoneFromLoadBalancer, getResponse); + + assertEquals(clientWhenExist.getAvailabilityZoneClient().removeAvailabilityZoneFromLoadBalancer("us-east-1a", "name").toString(), new AvailabilityZonesResultHandlerTest().expected().toString()); + } + + @Test(expectedExceptions = ResourceNotFoundException.class) + public void testRemoveZoneFromLoadBalancerWhenResponseIs404() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(404).build(); + + ELBClient clientWhenDontExist = requestSendsResponse( + removeZoneFromLoadBalancer, getResponse); + + clientWhenDontExist.getAvailabilityZoneClient().removeAvailabilityZoneFromLoadBalancer("us-east-1a", "name"); + } + + HttpRequest removeZonesFromLoadBalancer = HttpRequest.builder() + .method("POST") + .endpoint(URI.create("https://elasticloadbalancing.us-east-1.amazonaws.com/")) + .headers(ImmutableMultimap. builder() + .put("Host", "elasticloadbalancing.us-east-1.amazonaws.com") + .build()) + .payload( + payloadFromStringWithContentType( + "Action=DisableAvailabilityZonesForLoadBalancer" + + "&AvailabilityZones.member.1=us-east-1a" + + "&AvailabilityZones.member.2=us-east-1b" + + "&LoadBalancerName=name" + + "&Signature=5yUJQXjfntl0ptL%2BDv3p2jYpDSr%2BmV8hASIS7wtvkOI%3D" + + "&SignatureMethod=HmacSHA256" + + "&SignatureVersion=2" + + "&Timestamp=2009-11-08T15%3A54%3A08.897Z" + + "&Version=2012-06-01" + + "&AWSAccessKeyId=identity", + "application/x-www-form-urlencoded")) + .build(); + + + public void testRemoveZonesFromLoadBalancerWhenResponseIs2xx() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/zones.xml", "text/xml")).build(); + + ELBClient clientWhenExist = requestSendsResponse(removeZonesFromLoadBalancer, getResponse); + + assertEquals( + clientWhenExist.getAvailabilityZoneClient().removeAvailabilityZonesFromLoadBalancer(ImmutableSet.of("us-east-1a", "us-east-1b"), "name") + .toString(), new AvailabilityZonesResultHandlerTest().expected().toString()); + } + + @Test(expectedExceptions = ResourceNotFoundException.class) + public void testRemoveZonesFromLoadBalancerWhenResponseIs404() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(404).build(); + + ELBClient clientWhenDontExist = requestSendsResponse(removeZonesFromLoadBalancer, getResponse); + + clientWhenDontExist.getAvailabilityZoneClient().removeAvailabilityZonesFromLoadBalancer(ImmutableSet.of("us-east-1a", "us-east-1b"), "name"); + } +} diff --git a/labs/elb/src/test/java/org/jclouds/elb/features/AvailabilityZoneClientLiveTest.java b/labs/elb/src/test/java/org/jclouds/elb/features/AvailabilityZoneClientLiveTest.java new file mode 100644 index 0000000000..e8668bd5ae --- /dev/null +++ b/labs/elb/src/test/java/org/jclouds/elb/features/AvailabilityZoneClientLiveTest.java @@ -0,0 +1,33 @@ +/** + * 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.elb.features; + +import org.jclouds.elb.internal.BaseELBClientLiveTest; +import org.testng.annotations.Test; + +/** + * @author Adrian Cole + */ +@Test(groups = "live", testName = "AvailabilityZoneClientLiveTest") +public class AvailabilityZoneClientLiveTest extends BaseELBClientLiveTest { + + protected AvailabilityZoneClient client() { + return context.getApi().getAvailabilityZoneClient(); + } +} diff --git a/labs/elb/src/test/java/org/jclouds/elb/features/InstanceClientExpectTest.java b/labs/elb/src/test/java/org/jclouds/elb/features/InstanceClientExpectTest.java new file mode 100644 index 0000000000..4773ca70d6 --- /dev/null +++ b/labs/elb/src/test/java/org/jclouds/elb/features/InstanceClientExpectTest.java @@ -0,0 +1,263 @@ +/** + * 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 + * + * Unles 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 expres or implied. See the License for the + * specific language governing permisions and limitations + * under the License. + */ +package org.jclouds.elb.features; + +import static org.testng.Assert.assertEquals; + +import java.net.URI; +import java.util.TimeZone; + +import org.jclouds.elb.ELBClient; +import org.jclouds.elb.internal.BaseELBClientExpectTest; +import org.jclouds.elb.parse.DescribeInstanceHealthResponseTest; +import org.jclouds.elb.parse.InstancesResultHandlerTest; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.jclouds.rest.ResourceNotFoundException; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.ImmutableSet; + +/** + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "InstanceClientExpectTest") +public class InstanceClientExpectTest extends BaseELBClientExpectTest { + + public InstanceClientExpectTest() { + TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles")); + } + + HttpRequest get = HttpRequest.builder() + .method("POST") + .endpoint(URI.create("https://elasticloadbalancing.us-east-1.amazonaws.com/")) + .headers(ImmutableMultimap. builder() + .put("Host", "elasticloadbalancing.us-east-1.amazonaws.com") + .build()) + .payload( + payloadFromStringWithContentType( + "Action=DescribeInstanceHealth" + + "&LoadBalancerName=name" + + "&Signature=zIwSuvkooYRNPLyDrPCF8%2BbMLA8t0n9hIlS6K2aahuA%3D" + + "&SignatureMethod=HmacSHA256" + + "&SignatureVersion=2" + + "&Timestamp=2009-11-08T15%3A54%3A08.897Z" + + "&Version=2012-06-01" + + "&AWSAccessKeyId=identity", + "application/x-www-form-urlencoded")) + .build(); + + + public void testGetHealthOfInstancesOfLoadBalancerIs2xx() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/describe_instancehealth.xml", "text/xml")).build(); + + ELBClient clientWhenExist = requestSendsResponse( + get, getResponse); + + assertEquals(clientWhenExist.getInstanceClient().getHealthOfInstancesOfLoadBalancer("name").toString(), new DescribeInstanceHealthResponseTest().expected().toString()); + } + + public void testGetHealthOfInstancesOfLoadBalancerWhenResponseIs404() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(404).build(); + + ELBClient clientWhenDontExist = requestSendsResponse( + get, getResponse); + + assertEquals(clientWhenDontExist.getInstanceClient().getHealthOfInstancesOfLoadBalancer("name"), ImmutableSet.of()); + } + + HttpRequest registerInstanceWithLoadBalancer = HttpRequest.builder() + .method("POST") + .endpoint(URI.create("https://elasticloadbalancing.us-east-1.amazonaws.com/")) + .headers(ImmutableMultimap. builder() + .put("Host", "elasticloadbalancing.us-east-1.amazonaws.com") + .build()) + .payload( + payloadFromStringWithContentType( + "Action=RegisterInstancesWithLoadBalancer" + + "&Instances.member.1.InstanceId=i-6055fa09" + + "&LoadBalancerName=name" + + "&Signature=YRYjrZGMNoeyghtfKvbMZbRrbIgCuxsCQeYdtai0chY%3D" + + "&SignatureMethod=HmacSHA256" + + "&SignatureVersion=2" + + "&Timestamp=2009-11-08T15%3A54%3A08.897Z" + + "&Version=2012-06-01" + + "&AWSAccessKeyId=identity", + "application/x-www-form-urlencoded")) + .build(); + + + public void testRegisterInstanceWithLoadBalancerWhenResponseIs2xx() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/instances.xml", "text/xml")).build(); + + ELBClient clientWhenExist = requestSendsResponse( + registerInstanceWithLoadBalancer, getResponse); + + assertEquals(clientWhenExist.getInstanceClient().registerInstanceWithLoadBalancer("i-6055fa09", "name").toString(), new InstancesResultHandlerTest().expected().toString()); + } + + @Test(expectedExceptions = ResourceNotFoundException.class) + public void testRegisterInstanceWithLoadBalancerWhenResponseIs404() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(404).build(); + + ELBClient clientWhenDontExist = requestSendsResponse( + registerInstanceWithLoadBalancer, getResponse); + + clientWhenDontExist.getInstanceClient().registerInstanceWithLoadBalancer("i-6055fa09", "name"); + } + + HttpRequest registerInstancesWithLoadBalancer = HttpRequest.builder() + .method("POST") + .endpoint(URI.create("https://elasticloadbalancing.us-east-1.amazonaws.com/")) + .headers(ImmutableMultimap. builder() + .put("Host", "elasticloadbalancing.us-east-1.amazonaws.com") + .build()) + .payload( + payloadFromStringWithContentType( + "Action=RegisterInstancesWithLoadBalancer" + + "&Instances.member.1.InstanceId=i-6055fa09" + + "&Instances.member.2.InstanceId=i-9055fa55" + + "&LoadBalancerName=name" + + "&Signature=Yfqg8TxL1J1Ug8SimY%2F30rnbt%2FUVygTEa0vhMT5Fz1Y%3D" + + "&SignatureMethod=HmacSHA256" + + "&SignatureVersion=2" + + "&Timestamp=2009-11-08T15%3A54%3A08.897Z" + + "&Version=2012-06-01" + + "&AWSAccessKeyId=identity", + "application/x-www-form-urlencoded")) + .build(); + + + public void testRegisterInstancesWithLoadBalancerWhenResponseIs2xx() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/instances.xml", "text/xml")).build(); + + ELBClient clientWhenExist = requestSendsResponse(registerInstancesWithLoadBalancer, getResponse); + + assertEquals( + clientWhenExist.getInstanceClient().registerInstancesWithLoadBalancer(ImmutableSet.of("i-6055fa09", "i-9055fa55"), "name") + .toString(), new InstancesResultHandlerTest().expected().toString()); + } + + @Test(expectedExceptions = ResourceNotFoundException.class) + public void testRegisterInstancesWithLoadBalancerWhenResponseIs404() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(404).build(); + + ELBClient clientWhenDontExist = requestSendsResponse(registerInstancesWithLoadBalancer, getResponse); + + clientWhenDontExist.getInstanceClient().registerInstancesWithLoadBalancer(ImmutableSet.of("i-6055fa09", "i-9055fa55"), "name"); + } + + + HttpRequest deregisterInstanceFromLoadBalancer = HttpRequest.builder() + .method("POST") + .endpoint(URI.create("https://elasticloadbalancing.us-east-1.amazonaws.com/")) + .headers(ImmutableMultimap. builder() + .put("Host", "elasticloadbalancing.us-east-1.amazonaws.com") + .build()) + .payload( + payloadFromStringWithContentType( + "Action=DeregisterInstancesFromLoadBalancer" + + "&Instances.member.1.InstanceId=i-6055fa09" + + "&LoadBalancerName=name" + + "&Signature=d%2BK6b2ggJLEekW8wLyRnm%2FpcEpZvc8VNI%2FW0bpYBGUk%3D" + + "&SignatureMethod=HmacSHA256" + + "&SignatureVersion=2" + + "&Timestamp=2009-11-08T15%3A54%3A08.897Z" + + "&Version=2012-06-01" + + "&AWSAccessKeyId=identity", + "application/x-www-form-urlencoded")) + .build(); + + + public void testDeregisterInstanceFromLoadBalancerWhenResponseIs2xx() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/instances.xml", "text/xml")).build(); + + ELBClient clientWhenExist = requestSendsResponse( + deregisterInstanceFromLoadBalancer, getResponse); + + assertEquals(clientWhenExist.getInstanceClient().deregisterInstanceFromLoadBalancer("i-6055fa09", "name").toString(), new InstancesResultHandlerTest().expected().toString()); + } + + @Test(expectedExceptions = ResourceNotFoundException.class) + public void testDeregisterInstanceFromLoadBalancerWhenResponseIs404() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(404).build(); + + ELBClient clientWhenDontExist = requestSendsResponse( + deregisterInstanceFromLoadBalancer, getResponse); + + clientWhenDontExist.getInstanceClient().deregisterInstanceFromLoadBalancer("i-6055fa09", "name"); + } + + HttpRequest deregisterInstancesFromLoadBalancer = HttpRequest.builder() + .method("POST") + .endpoint(URI.create("https://elasticloadbalancing.us-east-1.amazonaws.com/")) + .headers(ImmutableMultimap. builder() + .put("Host", "elasticloadbalancing.us-east-1.amazonaws.com") + .build()) + .payload( + payloadFromStringWithContentType( + "Action=DeregisterInstancesFromLoadBalancer" + + "&Instances.member.1.InstanceId=i-6055fa09" + + "&Instances.member.2.InstanceId=i-9055fa55" + + "&LoadBalancerName=name" + + "&Signature=nqn8iH70979k%2Fu%2FKXEcMlT1Zd%2FPaNK6ZBwFDjvbuMRo%3D" + + "&SignatureMethod=HmacSHA256" + + "&SignatureVersion=2" + + "&Timestamp=2009-11-08T15%3A54%3A08.897Z" + + "&Version=2012-06-01" + + "&AWSAccessKeyId=identity", + "application/x-www-form-urlencoded")) + .build(); + + + public void testDeregisterInstancesFromLoadBalancerWhenResponseIs2xx() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/instances.xml", "text/xml")).build(); + + ELBClient clientWhenExist = requestSendsResponse(deregisterInstancesFromLoadBalancer, getResponse); + + assertEquals( + clientWhenExist.getInstanceClient().deregisterInstancesFromLoadBalancer(ImmutableSet.of("i-6055fa09", "i-9055fa55"), "name") + .toString(), new InstancesResultHandlerTest().expected().toString()); + } + + @Test(expectedExceptions = ResourceNotFoundException.class) + public void testDeregisterInstancesFromLoadBalancerWhenResponseIs404() throws Exception { + + HttpResponse getResponse = HttpResponse.builder().statusCode(404).build(); + + ELBClient clientWhenDontExist = requestSendsResponse(deregisterInstancesFromLoadBalancer, getResponse); + + clientWhenDontExist.getInstanceClient().deregisterInstancesFromLoadBalancer(ImmutableSet.of("i-6055fa09", "i-9055fa55"), "name"); + } +} diff --git a/labs/elb/src/test/java/org/jclouds/elb/features/InstanceClientLiveTest.java b/labs/elb/src/test/java/org/jclouds/elb/features/InstanceClientLiveTest.java index b426fc2ed8..dc6e689a2c 100644 --- a/labs/elb/src/test/java/org/jclouds/elb/features/InstanceClientLiveTest.java +++ b/labs/elb/src/test/java/org/jclouds/elb/features/InstanceClientLiveTest.java @@ -22,7 +22,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.util.Set; -import org.jclouds.elb.domain.InstanceState; +import org.jclouds.elb.domain.InstanceHealth; import org.jclouds.elb.domain.LoadBalancer; import org.jclouds.elb.internal.BaseELBClientLiveTest; import org.testng.Assert; @@ -36,7 +36,7 @@ import com.google.common.collect.ImmutableSet; @Test(groups = "live", testName = "InstanceClientLiveTest") public class InstanceClientLiveTest extends BaseELBClientLiveTest { - private void checkInstanceState(InstanceState instanceState) { + private void checkInstanceState(InstanceHealth instanceState) { checkNotNull(instanceState.getDescription(), "Description cannot be null for InstanceState"); checkNotNull(instanceState.getInstanceId(), "InstanceId cannot be null for InstanceState"); checkNotNull(instanceState.getReasonCode(), @@ -46,24 +46,24 @@ public class InstanceClientLiveTest extends BaseELBClientLiveTest { @Test protected void testListInstanceStates() { - for (LoadBalancer loadBalancer : context.getApi().getLoadBalancerClientForRegion(null).list()) { - Set response = client().getStateOfInstancesOfLoadBalancer(loadBalancer.getName()); + for (LoadBalancer loadBalancer : context.getApi().getLoadBalancerClient().list()) { + Set response = client().getHealthOfInstancesOfLoadBalancer(loadBalancer.getName()); - for (InstanceState instanceState : response) { + for (InstanceHealth instanceState : response) { checkInstanceState(instanceState); } if (response.size() > 0) { - InstanceState instanceState = response.iterator().next(); + InstanceHealth instanceState = response.iterator().next(); Assert.assertEquals( - client().getStateOfInstancesOfLoadBalancer(ImmutableSet.of(instanceState.getInstanceId()), - loadBalancer.getName()), instanceState); + ImmutableSet.of(client().getHealthOfInstancesOfLoadBalancer(ImmutableSet.of(instanceState.getInstanceId()), + loadBalancer.getName())), instanceState); } } } protected InstanceClient client() { - return context.getApi().getInstanceClientForRegion(null); + return context.getApi().getInstanceClient(); } } diff --git a/labs/elb/src/test/java/org/jclouds/elb/features/LoadBalancerClientExpectTest.java b/labs/elb/src/test/java/org/jclouds/elb/features/LoadBalancerClientExpectTest.java index b436c203f6..d985d97ee7 100644 --- a/labs/elb/src/test/java/org/jclouds/elb/features/LoadBalancerClientExpectTest.java +++ b/labs/elb/src/test/java/org/jclouds/elb/features/LoadBalancerClientExpectTest.java @@ -74,7 +74,7 @@ public class LoadBalancerClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenExist = requestSendsResponse( get, getResponse); - assertEquals(clientWhenExist.getLoadBalancerClientForRegion(null).get("name").toString(), new GetLoadBalancerResponseTest().expected().toString()); + assertEquals(clientWhenExist.getLoadBalancerClient().get("name").toString(), new GetLoadBalancerResponseTest().expected().toString()); } public void testGetWhenResponseIs404() throws Exception { @@ -84,7 +84,7 @@ public class LoadBalancerClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenDontExist = requestSendsResponse( get, getResponse); - assertNull(clientWhenDontExist.getLoadBalancerClientForRegion(null).get("name")); + assertNull(clientWhenDontExist.getLoadBalancerClient().get("name")); } HttpRequest list = HttpRequest.builder() @@ -113,7 +113,7 @@ public class LoadBalancerClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenExist = requestSendsResponse( list, listResponse); - assertEquals(clientWhenExist.getLoadBalancerClientForRegion(null).list().toString(), new DescribeLoadBalancersResponseTest().expected().toString()); + assertEquals(clientWhenExist.getLoadBalancerClient().list().toString(), new DescribeLoadBalancersResponseTest().expected().toString()); } // TODO: this should really be an empty set @@ -125,7 +125,7 @@ public class LoadBalancerClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenDontExist = requestSendsResponse( list, listResponse); - clientWhenDontExist.getLoadBalancerClientForRegion(null).list(); + clientWhenDontExist.getLoadBalancerClient().list(); } public void testListWithOptionsWhenResponseIs2xx() throws Exception { @@ -154,7 +154,7 @@ public class LoadBalancerClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenWithOptionsExist = requestSendsResponse(listWithOptions, listWithOptionsResponse); - assertEquals(clientWhenWithOptionsExist.getLoadBalancerClientForRegion(null).list(afterMarker("MARKER")).toString(), + assertEquals(clientWhenWithOptionsExist.getLoadBalancerClient().list(afterMarker("MARKER")).toString(), new DescribeLoadBalancersResponseTest().expected().toString()); } } diff --git a/labs/elb/src/test/java/org/jclouds/elb/features/LoadBalancerClientLiveTest.java b/labs/elb/src/test/java/org/jclouds/elb/features/LoadBalancerClientLiveTest.java index a1bc3c77e7..cbacc919e1 100644 --- a/labs/elb/src/test/java/org/jclouds/elb/features/LoadBalancerClientLiveTest.java +++ b/labs/elb/src/test/java/org/jclouds/elb/features/LoadBalancerClientLiveTest.java @@ -94,6 +94,6 @@ public class LoadBalancerClientLiveTest extends BaseELBClientLiveTest { } protected LoadBalancerClient client() { - return context.getApi().getLoadBalancerClientForRegion(null); + return context.getApi().getLoadBalancerClient(); } } diff --git a/labs/elb/src/test/java/org/jclouds/elb/features/PolicyClientExpectTest.java b/labs/elb/src/test/java/org/jclouds/elb/features/PolicyClientExpectTest.java index 92b444b1ca..545f2dca4a 100644 --- a/labs/elb/src/test/java/org/jclouds/elb/features/PolicyClientExpectTest.java +++ b/labs/elb/src/test/java/org/jclouds/elb/features/PolicyClientExpectTest.java @@ -76,7 +76,7 @@ public class PolicyClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenExist = requestSendsResponse( get, getResponse); - assertEquals(clientWhenExist.getPolicyClientForRegion(null).get("name").toString(), new GetPolicyResponseTest().expected().toString()); + assertEquals(clientWhenExist.getPolicyClient().get("name").toString(), new GetPolicyResponseTest().expected().toString()); } public void testGetWhenResponseIs404() throws Exception { @@ -86,7 +86,7 @@ public class PolicyClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenDontExist = requestSendsResponse( get, getResponse); - assertNull(clientWhenDontExist.getPolicyClientForRegion(null).get("name")); + assertNull(clientWhenDontExist.getPolicyClient().get("name")); } HttpRequest list = HttpRequest.builder() @@ -115,7 +115,7 @@ public class PolicyClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenExist = requestSendsResponse( list, listResponse); - assertEquals(clientWhenExist.getPolicyClientForRegion(null).list().toString(), new DescribeLoadBalancerPoliciesResponseTest().expected().toString()); + assertEquals(clientWhenExist.getPolicyClient().list().toString(), new DescribeLoadBalancerPoliciesResponseTest().expected().toString()); } public void testListWhenResponseIs404() throws Exception { @@ -125,7 +125,7 @@ public class PolicyClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenDontExist = requestSendsResponse( list, listResponse); - clientWhenDontExist.getPolicyClientForRegion(null).list(); + clientWhenDontExist.getPolicyClient().list(); } public void testListWithOptionsWhenResponseIs2xx() throws Exception { @@ -154,7 +154,7 @@ public class PolicyClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenWithOptionsExist = requestSendsResponse(listWithOptions, listWithOptionsResponse); - assertEquals(clientWhenWithOptionsExist.getPolicyClientForRegion(null).list(loadBalancerName("moo")).toString(), + assertEquals(clientWhenWithOptionsExist.getPolicyClient().list(loadBalancerName("moo")).toString(), new DescribeLoadBalancerPoliciesResponseTest().expected().toString()); } @@ -187,7 +187,7 @@ public class PolicyClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenExist = requestSendsResponse( getType, getResponse); - assertEquals(clientWhenExist.getPolicyClientForRegion(null).getType("name").toString(), new GetPolicyTypeResponseTest().expected().toString()); + assertEquals(clientWhenExist.getPolicyClient().getType("name").toString(), new GetPolicyTypeResponseTest().expected().toString()); } public void testGetTypeWhenResponseIs404() throws Exception { @@ -197,7 +197,7 @@ public class PolicyClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenDontExist = requestSendsResponse( getType, getResponse); - assertNull(clientWhenDontExist.getPolicyClientForRegion(null).getType("name")); + assertNull(clientWhenDontExist.getPolicyClient().getType("name")); } HttpRequest listTypes = HttpRequest.builder() @@ -226,7 +226,7 @@ public class PolicyClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenExist = requestSendsResponse( listTypes, listResponse); - assertEquals(clientWhenExist.getPolicyClientForRegion(null).listTypes().toString(), new DescribeLoadBalancerPolicyTypesResponseTest().expected().toString()); + assertEquals(clientWhenExist.getPolicyClient().listTypes().toString(), new DescribeLoadBalancerPolicyTypesResponseTest().expected().toString()); } public void testListTypesWhenResponseIs404() throws Exception { @@ -236,7 +236,7 @@ public class PolicyClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenDontExist = requestSendsResponse( listTypes, listResponse); - clientWhenDontExist.getPolicyClientForRegion(null).listTypes(); + clientWhenDontExist.getPolicyClient().listTypes(); } public void testListTypesByNamesWhenResponseIs2xx() throws Exception { @@ -265,7 +265,7 @@ public class PolicyClientExpectTest extends BaseELBClientExpectTest { ELBClient clientWhenWithOptionsExist = requestSendsResponse(listWithOptions, listWithOptionsResponse); - assertEquals(clientWhenWithOptionsExist.getPolicyClientForRegion(null).listTypes(ImmutableSet.of("moo")).toString(), + assertEquals(clientWhenWithOptionsExist.getPolicyClient().listTypes(ImmutableSet.of("moo")).toString(), new DescribeLoadBalancerPolicyTypesResponseTest().expected().toString()); } } diff --git a/labs/elb/src/test/java/org/jclouds/elb/features/PolicyClientLiveTest.java b/labs/elb/src/test/java/org/jclouds/elb/features/PolicyClientLiveTest.java index a453df0c06..bae7efd696 100644 --- a/labs/elb/src/test/java/org/jclouds/elb/features/PolicyClientLiveTest.java +++ b/labs/elb/src/test/java/org/jclouds/elb/features/PolicyClientLiveTest.java @@ -105,6 +105,6 @@ public class PolicyClientLiveTest extends BaseELBClientLiveTest { } protected PolicyClient client() { - return context.getApi().getPolicyClientForRegion(null); + return context.getApi().getPolicyClient(); } } diff --git a/labs/elb/src/test/java/org/jclouds/elb/loadbalancer/ELBLoadBalancerServiceLiveTest.java b/labs/elb/src/test/java/org/jclouds/elb/loadbalancer/ELBLoadBalancerServiceLiveTest.java index 0b7b5def46..b1f3e1678b 100644 --- a/labs/elb/src/test/java/org/jclouds/elb/loadbalancer/ELBLoadBalancerServiceLiveTest.java +++ b/labs/elb/src/test/java/org/jclouds/elb/loadbalancer/ELBLoadBalancerServiceLiveTest.java @@ -60,7 +60,7 @@ public class ELBLoadBalancerServiceLiveTest extends BaseLoadBalancerServiceLiveT instanceIds.add(node.getProviderId()); } - PaginatedIterable elbs = elbClient.getLoadBalancerClientForRegion(null).list(); + PaginatedIterable elbs = elbClient.getLoadBalancerClient().list(); for (LoadBalancer elb : elbs) { if (elb.getName().equals(group)) assertEquals(elb.getInstanceIds(), instanceIds.build()); diff --git a/labs/elb/src/test/java/org/jclouds/elb/parse/AvailabilityZonesResultHandlerTest.java b/labs/elb/src/test/java/org/jclouds/elb/parse/AvailabilityZonesResultHandlerTest.java new file mode 100644 index 0000000000..dd1a3f6ca7 --- /dev/null +++ b/labs/elb/src/test/java/org/jclouds/elb/parse/AvailabilityZonesResultHandlerTest.java @@ -0,0 +1,54 @@ +/** + * 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.elb.parse; + +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; +import java.util.Set; + +import org.jclouds.elb.xml.AvailabilityZonesResultHandler; +import org.jclouds.http.functions.BaseHandlerTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; + +/** + * @author Adrian Cole + */ +// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire +@Test(groups = "unit", testName = "ZonesResultHandlerTest") +public class AvailabilityZonesResultHandlerTest extends BaseHandlerTest { + + public void test() { + InputStream is = getClass().getResourceAsStream("/zones.xml"); + + Set expected = expected(); + + AvailabilityZonesResultHandler handler = injector.getInstance(AvailabilityZonesResultHandler.class); + Set result = factory.create(handler).parse(is); + + assertEquals(result.toString(), expected.toString()); + + } + + public Set expected() { + return ImmutableSet.of("us-east-1c"); + } +} diff --git a/labs/elb/src/test/java/org/jclouds/elb/parse/DescribeInstanceHealthResponseTest.java b/labs/elb/src/test/java/org/jclouds/elb/parse/DescribeInstanceHealthResponseTest.java new file mode 100644 index 0000000000..394fb33ce2 --- /dev/null +++ b/labs/elb/src/test/java/org/jclouds/elb/parse/DescribeInstanceHealthResponseTest.java @@ -0,0 +1,61 @@ +/** + * 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.elb.parse; + +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; +import java.util.Set; + +import org.jclouds.elb.domain.InstanceHealth; +import org.jclouds.elb.xml.DescribeInstanceHealthResultHandler; +import org.jclouds.http.functions.BaseHandlerTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; + +/** + * @author Adrian Cole + */ +// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire +@Test(groups = "unit", testName = "DescribeInstanceHealthResponseTest") +public class DescribeInstanceHealthResponseTest extends BaseHandlerTest { + + public void test() { + InputStream is = getClass().getResourceAsStream("/describe_instancehealth.xml"); + + Set expected = expected(); + + DescribeInstanceHealthResultHandler handler = injector.getInstance(DescribeInstanceHealthResultHandler.class); + Set result = factory.create(handler).parse(is); + + assertEquals(result.toString(), expected.toString()); + + } + + public Set expected() { + return ImmutableSet.builder() + .add(InstanceHealth.builder() + .description("Instance is in terminated state.") + .instanceId("i-64bd081c") + .state("OutOfService") + .reasonCode("Instance") + .build()).build(); + } +} diff --git a/labs/elb/src/test/resources/describe_instancehealth.xml b/labs/elb/src/test/resources/describe_instancehealth.xml new file mode 100644 index 0000000000..98b81f0214 --- /dev/null +++ b/labs/elb/src/test/resources/describe_instancehealth.xml @@ -0,0 +1,15 @@ + + + + + Instance is in terminated state. + i-64bd081c + OutOfService + Instance + + + + + 6fb803d5-ca15-11e1-ba7e-87298bc8b11d + + \ No newline at end of file diff --git a/labs/elb/src/test/resources/zones.xml b/labs/elb/src/test/resources/zones.xml new file mode 100644 index 0000000000..d542e018d0 --- /dev/null +++ b/labs/elb/src/test/resources/zones.xml @@ -0,0 +1,10 @@ + + + + us-east-1c + + + + 04343154-ca27-11e1-b4e4-0b6ac7b9ca3a + +