fine tuning load balancer API

This commit is contained in:
Lili Nader 2010-05-21 21:23:19 -04:00
parent 861a7ba854
commit 88d5f42da2
5 changed files with 61 additions and 24 deletions

View File

@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.aws.ec2.util.EC2Utils.parseHandle;
import static org.jclouds.util.Utils.checkNotEmpty;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@ -40,6 +41,7 @@ import org.jclouds.aws.ec2.compute.domain.RegionAndName;
import org.jclouds.aws.ec2.compute.domain.RegionNameAndIngressRules;
import org.jclouds.aws.ec2.compute.options.EC2TemplateOptions;
import org.jclouds.aws.ec2.domain.KeyPair;
import org.jclouds.aws.ec2.util.EC2Utils;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata;
@ -144,36 +146,47 @@ public class EC2ComputeService extends BaseComputeService {
return EC2TemplateOptions.class.cast(super.templateOptions());
}
@Override
public String loadBalanceNodesMatching(Predicate<NodeMetadata> filter, String loadBalancerName,
public Set<String> loadBalanceNodesMatching(Predicate<NodeMetadata> filter, String loadBalancerName,
String protocol, int loadBalancerPort, int instancePort) {
checkNotNull(loadBalancerName, "loadBalancerName");
checkNotNull(protocol, "protocol");
checkArgument(protocol.toUpperCase().equals("HTTP") || protocol.toUpperCase().equals("TCP"),
"Acceptable values for protocol are HTTP or TCP");
Location location = null;
Set<String> ids = new HashSet<String>();
Map<Location, Set<String>> locationMap = new HashMap<Location, Set<String>>();
for (NodeMetadata node : Iterables.filter(super
.listNodesDetailsMatching(NodePredicates.all()), Predicates.and(filter, Predicates
.not(NodePredicates.TERMINATED)))) {
ids.add(node.getProviderId());
location = node.getLocation();
Set<String> ids = locationMap.get(node.getLocation());
if(ids == null)
ids = new HashSet<String>();
ids.add(node.getProviderId());
locationMap.put(node.getLocation(), ids);
}
logger.debug(">> creating load balancer (%s)", loadBalancerName);
String dnsName = loadBalancerStrategy.execute(location, loadBalancerName, protocol,
loadBalancerPort, instancePort, ids);
logger.debug("<< created load balancer (%s) DNS (%s)", loadBalancerName, dnsName);
return dnsName;
Set<String> dnsNames = new HashSet<String>(0);
for(Location location: locationMap.keySet())
{
logger.debug(">> creating load balancer (%s)", loadBalancerName);
String dnsName = loadBalancerStrategy.execute(location, loadBalancerName, protocol,
loadBalancerPort, instancePort, locationMap.get(location));
dnsNames.add(dnsName);
logger.debug("<< created load balancer (%s) DNS (%s)", loadBalancerName, dnsName);
}
return dnsNames;
}
@Override
public void deleteLoadBalancer(String loadBalancerName, Predicate<NodeMetadata> filter) {
String region = parseHandle(Iterables.get(
Iterables.filter(super.listNodesDetailsMatching(NodePredicates.all()), Predicates
.and(filter, Predicates.not(NodePredicates.TERMINATED))), 0).getId())[0];
ec2Client.getElasticLoadBalancerServices().deleteLoadBalancerInRegion(region,
loadBalancerName);
}
public void deleteLoadBalancer(String dnsName) {
Map<String, String> tuple = EC2Utils.getLoadBalancerNameAndRegionFromDnsName(dnsName);
//Only one load balancer per DNS name is expected
for(String key: tuple.keySet())
{
ec2Client.getElasticLoadBalancerServices().deleteLoadBalancerInRegion(key,
tuple.get(key));
}
}
}

View File

@ -21,6 +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 javax.inject.Singleton;
import org.jclouds.aws.domain.Region;
@ -119,4 +122,24 @@ public class EC2Utils {
+ "s[" + i + "]"));
}
}
public static Map<String, String> 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<String, String> map = new HashMap<String, String>();
map.put(regionName, loadBalancerName);
return map;
}
}

View File

@ -194,10 +194,11 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest {
instanceIds.add(node.getProviderId());
}
// create load balancer
String dnsName = client.loadBalanceNodesMatching(NodePredicates.withTag(tag), tag, "HTTP",
// create load balancers
Set<String> dnsNames = client.loadBalanceNodesMatching(NodePredicates.withTag(tag), tag, "HTTP",
80, 80);
assertNotNull(dnsName);
assertNotNull(dnsNames);
Set<ElasticLoadBalancer> elbs = elbClient.describeLoadBalancersInRegion(Region.US_EAST_1,
tag);
assertNotNull(elbs);

View File

@ -235,10 +235,10 @@ public interface ComputeService {
* @return DNS Name of the load balancer
*/
@Beta
String loadBalanceNodesMatching(Predicate<NodeMetadata> filter, String loadBalancerName,
Set<String> loadBalanceNodesMatching(Predicate<NodeMetadata> filter, String loadBalancerName,
String protocol, int loadBalancerPort, int instancePort);
@Beta
void deleteLoadBalancer(String loadBalancerName, Predicate<NodeMetadata> filter);
void deleteLoadBalancer(String handle);
}

View File

@ -424,12 +424,12 @@ public class BaseComputeService implements ComputeService {
}
@Override
public void deleteLoadBalancer(String loadBalancerName, Predicate<NodeMetadata> filter) {
public void deleteLoadBalancer(String handle) {
throw new UnsupportedOperationException("deleteLoadBalancer not supported in this cloud");
}
@Override
public String loadBalanceNodesMatching(Predicate<NodeMetadata> filter, String loadBalancerName,
public Set<String> loadBalanceNodesMatching(Predicate<NodeMetadata> filter, String loadBalancerName,
String protocol, int loadBalancerPort, int instancePort) {
throw new UnsupportedOperationException(
"loadBalanceNodesMatching not supported in this cloud");