enhanced ec2 lb logic

This commit is contained in:
Adrian Cole 2010-05-23 16:38:46 -07:00
parent a4bdd433f4
commit 4fc5f3cf2e
4 changed files with 82 additions and 25 deletions

View File

@ -55,7 +55,7 @@ public class EC2DestroyLoadBalancerStrategy implements DestroyLoadBalancerStrate
@Override @Override
public boolean execute(InetAddress loadBalancer) { public boolean execute(InetAddress loadBalancer) {
Map<String, String> tuple = EC2Utils.getLoadBalancerNameAndRegionFromDnsName(loadBalancer Map<String, String> tuple = EC2Utils.getLoadBalancerNameAndRegionFromDnsName(loadBalancer
.getCanonicalHostName()); .getHostName());
// Only one load balancer per DNS name is expected // Only one load balancer per DNS name is expected
for (String key : tuple.keySet()) { for (String key : tuple.keySet()) {
elbClient.deleteLoadBalancerInRegion(key, tuple.get(key)); elbClient.deleteLoadBalancerInRegion(key, tuple.get(key));

View File

@ -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.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -35,6 +36,7 @@ import org.jclouds.domain.LocationScope;
import org.jclouds.rest.internal.GeneratedHttpRequest; import org.jclouds.rest.internal.GeneratedHttpRequest;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
/** /**
@ -90,16 +92,19 @@ public class EC2Utils {
for (Object arg : gRequest.getArgs()) { for (Object arg : gRequest.getArgs()) {
if (arg instanceof String) { if (arg instanceof String) {
String regionName = (String) arg; String regionName = (String) arg;
if (Region.EU_WEST_1.equals(regionName) || Region.US_WEST_1.equals(regionName) if (isRegion(regionName))
|| Region.US_EAST_1.equals(regionName)
|| Region.US_STANDARD.equals(regionName)
|| Region.AP_SOUTHEAST_1.equals(regionName))
return regionName; return regionName;
} }
} }
return null; 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) { public static String findAvailabilityZoneInArgsOrNull(GeneratedHttpRequest<?> gRequest) {
for (Object arg : gRequest.getArgs()) { for (Object arg : gRequest.getArgs()) {
if (arg instanceof String) { if (arg instanceof String) {
@ -122,24 +127,18 @@ public class EC2Utils {
+ "s[" + i + "]")); + "s[" + i + "]"));
} }
} }
public static Map<String, String> getLoadBalancerNameAndRegionFromDnsName(String handle) private static final Pattern ELB_PATTERN = Pattern
{ .compile("([^.]+)-[^.]+\\.([^.]+)\\.elb\\.amazonaws\\.com");
String[] parts = checkNotNull(handle, "handle").split(".");
checkArgument(parts.length == 5, "handle syntax is my-load-balancer-1277832914.us-east-1.elb.amazonaws.com"); public static Map<String, String> getLoadBalancerNameAndRegionFromDnsName(String dnsName) {
Matcher matcher = ELB_PATTERN.matcher(checkNotNull(dnsName, "dnsName"));
String loadBalancerName = parts[0].substring(0, parts[0].lastIndexOf("-")); checkArgument(matcher.find(), "dnsName syntax is " + ELB_PATTERN + " didn't match: "
+ dnsName);
String regionName = parts[1]; String loadBalancerName = matcher.group(1);
String regionName = matcher.group(2);
checkArgument((Region.EU_WEST_1.equals(regionName) || Region.US_WEST_1.equals(regionName) checkArgument((isRegion(regionName)), String.format(
|| Region.US_EAST_1.equals(regionName) "Region (%s) parsed from (%s) is not a valid region", regionName, dnsName));
|| Region.US_STANDARD.equals(regionName) return ImmutableMap.<String, String> of(regionName, loadBalancerName);
|| Region.AP_SOUTHEAST_1.equals(regionName)), String.format("Region (%s, args) is not a valid region", regionName));
Map<String, String> map = new HashMap<String, String>();
map.put(regionName, loadBalancerName);
return map;
} }
} }

View File

@ -0,0 +1,51 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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.<String, String> of("us-east-1", "my-load-balancer"),
EC2Utils
.getLoadBalancerNameAndRegionFromDnsName("my-load-balancer-1277832914.us-east-1.elb.amazonaws.com"));
assertEquals(
ImmutableMap.<String, String> 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");
}
}

View File

@ -185,11 +185,17 @@ public class ComputeUtils {
} }
if (options.getPort() > 0) { if (options.getPort() > 0) {
checkNodeHasPublicIps(node);
blockUntilPortIsListeningOnPublicIp(options.getPort(), options.getSeconds(), Iterables blockUntilPortIsListeningOnPublicIp(options.getPort(), options.getSeconds(), Iterables
.get(node.getPublicAddresses(), 0)); .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) { private void blockUntilPortIsListeningOnPublicIp(int port, int seconds, InetAddress inetAddress) {
logger.debug(">> blocking on port %s:%d for %d seconds", inetAddress, port, seconds); logger.debug(">> blocking on port %s:%d for %d seconds", inetAddress, port, seconds);
RetryablePredicate<InetSocketAddress> tester = new RetryablePredicate<InetSocketAddress>( RetryablePredicate<InetSocketAddress> tester = new RetryablePredicate<InetSocketAddress>(
@ -222,6 +228,7 @@ public class ComputeUtils {
public Map<SshCallable<?>, ?> runCallablesOnNode(NodeMetadata node, public Map<SshCallable<?>, ?> runCallablesOnNode(NodeMetadata node,
Iterable<? extends SshCallable<?>> parallel, @Nullable SshCallable<?> last) { Iterable<? extends SshCallable<?>> parallel, @Nullable SshCallable<?> last) {
checkState(this.sshFactory != null, "runScript requested, but no SshModule configured"); checkState(this.sshFactory != null, "runScript requested, but no SshModule configured");
checkNodeHasPublicIps(node);
checkNotNull(node.getCredentials().key, "credentials.key for node " + node.getProviderId()); checkNotNull(node.getCredentials().key, "credentials.key for node " + node.getProviderId());
SshClient ssh = createSshClientOncePortIsListeningOnNode(node); SshClient ssh = createSshClientOncePortIsListeningOnNode(node);
try { try {