From 4fc5f3cf2e1dff88e92944bd59085bb8b3ebda8c Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sun, 23 May 2010 16:38:46 -0700 Subject: [PATCH] enhanced ec2 lb logic --- .../EC2DestroyLoadBalancerStrategy.java | 2 +- .../org/jclouds/aws/ec2/util/EC2Utils.java | 47 +++++++++-------- .../jclouds/aws/ec2/utils/EC2UtilsTest.java | 51 +++++++++++++++++++ .../jclouds/compute/util/ComputeUtils.java | 7 +++ 4 files changed, 82 insertions(+), 25 deletions(-) create mode 100644 aws/core/src/test/java/org/jclouds/aws/ec2/utils/EC2UtilsTest.java diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/strategy/EC2DestroyLoadBalancerStrategy.java b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/strategy/EC2DestroyLoadBalancerStrategy.java index e1f05efaa3..ced5d3972b 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/strategy/EC2DestroyLoadBalancerStrategy.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/strategy/EC2DestroyLoadBalancerStrategy.java @@ -55,7 +55,7 @@ public class EC2DestroyLoadBalancerStrategy implements DestroyLoadBalancerStrate @Override public boolean execute(InetAddress loadBalancer) { Map tuple = EC2Utils.getLoadBalancerNameAndRegionFromDnsName(loadBalancer - .getCanonicalHostName()); + .getHostName()); // Only one load balancer per DNS name is expected for (String key : tuple.keySet()) { elbClient.deleteLoadBalancerInRegion(key, tuple.get(key)); diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/util/EC2Utils.java b/aws/core/src/main/java/org/jclouds/aws/ec2/util/EC2Utils.java index 4f167a8f1a..070f650f3d 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/util/EC2Utils.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/util/EC2Utils.java @@ -21,8 +21,9 @@ package org.jclouds.aws.ec2.util; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import java.util.HashMap; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.inject.Singleton; @@ -35,6 +36,7 @@ import org.jclouds.domain.LocationScope; import org.jclouds.rest.internal.GeneratedHttpRequest; import com.google.common.base.Function; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; /** @@ -90,16 +92,19 @@ public class EC2Utils { for (Object arg : gRequest.getArgs()) { if (arg instanceof String) { String regionName = (String) arg; - if (Region.EU_WEST_1.equals(regionName) || Region.US_WEST_1.equals(regionName) - || Region.US_EAST_1.equals(regionName) - || Region.US_STANDARD.equals(regionName) - || Region.AP_SOUTHEAST_1.equals(regionName)) + if (isRegion(regionName)) return regionName; } } return null; } + public static boolean isRegion(String regionName) { + return Region.EU_WEST_1.equals(regionName) || Region.US_WEST_1.equals(regionName) + || Region.US_EAST_1.equals(regionName) || Region.US_STANDARD.equals(regionName) + || Region.AP_SOUTHEAST_1.equals(regionName); + } + public static String findAvailabilityZoneInArgsOrNull(GeneratedHttpRequest gRequest) { for (Object arg : gRequest.getArgs()) { if (arg instanceof String) { @@ -122,24 +127,18 @@ public class EC2Utils { + "s[" + i + "]")); } } - - public static Map getLoadBalancerNameAndRegionFromDnsName(String handle) - { - String[] parts = checkNotNull(handle, "handle").split("."); - checkArgument(parts.length == 5, "handle syntax is my-load-balancer-1277832914.us-east-1.elb.amazonaws.com"); - - String loadBalancerName = parts[0].substring(0, parts[0].lastIndexOf("-")); - - String regionName = parts[1]; - - checkArgument((Region.EU_WEST_1.equals(regionName) || Region.US_WEST_1.equals(regionName) - || Region.US_EAST_1.equals(regionName) - || Region.US_STANDARD.equals(regionName) - || Region.AP_SOUTHEAST_1.equals(regionName)), String.format("Region (%s, args) is not a valid region", regionName)); - - Map map = new HashMap(); - map.put(regionName, loadBalancerName); - return map; - + + private static final Pattern ELB_PATTERN = Pattern + .compile("([^.]+)-[^.]+\\.([^.]+)\\.elb\\.amazonaws\\.com"); + + public static Map getLoadBalancerNameAndRegionFromDnsName(String dnsName) { + Matcher matcher = ELB_PATTERN.matcher(checkNotNull(dnsName, "dnsName")); + checkArgument(matcher.find(), "dnsName syntax is " + ELB_PATTERN + " didn't match: " + + dnsName); + String loadBalancerName = matcher.group(1); + String regionName = matcher.group(2); + checkArgument((isRegion(regionName)), String.format( + "Region (%s) parsed from (%s) is not a valid region", regionName, dnsName)); + return ImmutableMap. of(regionName, loadBalancerName); } } \ No newline at end of file diff --git a/aws/core/src/test/java/org/jclouds/aws/ec2/utils/EC2UtilsTest.java b/aws/core/src/test/java/org/jclouds/aws/ec2/utils/EC2UtilsTest.java new file mode 100644 index 0000000000..ac4e11dbb8 --- /dev/null +++ b/aws/core/src/test/java/org/jclouds/aws/ec2/utils/EC2UtilsTest.java @@ -0,0 +1,51 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ec2.utils; + +import static org.testng.Assert.assertEquals; + +import org.jclouds.aws.ec2.util.EC2Utils; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; + +/** + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "ec2.EC2UtilsTest") +public class EC2UtilsTest { + + public void testGetLoadBalancerNameAndRegionFromDnsName() { + assertEquals( + ImmutableMap. of("us-east-1", "my-load-balancer"), + EC2Utils + .getLoadBalancerNameAndRegionFromDnsName("my-load-balancer-1277832914.us-east-1.elb.amazonaws.com")); + assertEquals( + ImmutableMap. of("us-east-1", "ec2lb"), + EC2Utils + .getLoadBalancerNameAndRegionFromDnsName("ec2lb-915583419.us-east-1.elb.amazonaws.com")); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testGetLoadBalancerNameAndRegionFromDnsNameFail() { + EC2Utils + .getLoadBalancerNameAndRegionFromDnsName("my-load-balancer-1277832914.us-east-1.microsoft.com"); + } + +} diff --git a/compute/src/main/java/org/jclouds/compute/util/ComputeUtils.java b/compute/src/main/java/org/jclouds/compute/util/ComputeUtils.java index 27b68cf295..7b0cdaec3d 100644 --- a/compute/src/main/java/org/jclouds/compute/util/ComputeUtils.java +++ b/compute/src/main/java/org/jclouds/compute/util/ComputeUtils.java @@ -185,11 +185,17 @@ public class ComputeUtils { } if (options.getPort() > 0) { + checkNodeHasPublicIps(node); blockUntilPortIsListeningOnPublicIp(options.getPort(), options.getSeconds(), Iterables .get(node.getPublicAddresses(), 0)); } } + private void checkNodeHasPublicIps(NodeMetadata node) { + checkState(node.getPublicAddresses().size() > 0, + "node does not have IP addresses configured: " + node); + } + private void blockUntilPortIsListeningOnPublicIp(int port, int seconds, InetAddress inetAddress) { logger.debug(">> blocking on port %s:%d for %d seconds", inetAddress, port, seconds); RetryablePredicate tester = new RetryablePredicate( @@ -222,6 +228,7 @@ public class ComputeUtils { public Map, ?> runCallablesOnNode(NodeMetadata node, Iterable> parallel, @Nullable SshCallable last) { checkState(this.sshFactory != null, "runScript requested, but no SshModule configured"); + checkNodeHasPublicIps(node); checkNotNull(node.getCredentials().key, "credentials.key for node " + node.getProviderId()); SshClient ssh = createSshClientOncePortIsListeningOnNode(node); try {