mirror of https://github.com/apache/jclouds.git
fine tuning load balancer API
This commit is contained in:
parent
861a7ba854
commit
88d5f42da2
|
@ -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.aws.ec2.util.EC2Utils.parseHandle;
|
||||||
import static org.jclouds.util.Utils.checkNotEmpty;
|
import static org.jclouds.util.Utils.checkNotEmpty;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
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.domain.RegionNameAndIngressRules;
|
||||||
import org.jclouds.aws.ec2.compute.options.EC2TemplateOptions;
|
import org.jclouds.aws.ec2.compute.options.EC2TemplateOptions;
|
||||||
import org.jclouds.aws.ec2.domain.KeyPair;
|
import org.jclouds.aws.ec2.domain.KeyPair;
|
||||||
|
import org.jclouds.aws.ec2.util.EC2Utils;
|
||||||
import org.jclouds.compute.ComputeServiceContext;
|
import org.jclouds.compute.ComputeServiceContext;
|
||||||
import org.jclouds.compute.domain.Image;
|
import org.jclouds.compute.domain.Image;
|
||||||
import org.jclouds.compute.domain.NodeMetadata;
|
import org.jclouds.compute.domain.NodeMetadata;
|
||||||
|
@ -144,36 +146,47 @@ public class EC2ComputeService extends BaseComputeService {
|
||||||
return EC2TemplateOptions.class.cast(super.templateOptions());
|
return EC2TemplateOptions.class.cast(super.templateOptions());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String loadBalanceNodesMatching(Predicate<NodeMetadata> filter, String loadBalancerName,
|
public Set<String> loadBalanceNodesMatching(Predicate<NodeMetadata> filter, String loadBalancerName,
|
||||||
String protocol, int loadBalancerPort, int instancePort) {
|
String protocol, int loadBalancerPort, int instancePort) {
|
||||||
checkNotNull(loadBalancerName, "loadBalancerName");
|
checkNotNull(loadBalancerName, "loadBalancerName");
|
||||||
checkNotNull(protocol, "protocol");
|
checkNotNull(protocol, "protocol");
|
||||||
checkArgument(protocol.toUpperCase().equals("HTTP") || protocol.toUpperCase().equals("TCP"),
|
checkArgument(protocol.toUpperCase().equals("HTTP") || protocol.toUpperCase().equals("TCP"),
|
||||||
"Acceptable values for protocol are HTTP or TCP");
|
"Acceptable values for protocol are HTTP or TCP");
|
||||||
|
|
||||||
Location location = null;
|
Map<Location, Set<String>> locationMap = new HashMap<Location, Set<String>>();
|
||||||
Set<String> ids = new HashSet<String>();
|
|
||||||
for (NodeMetadata node : Iterables.filter(super
|
for (NodeMetadata node : Iterables.filter(super
|
||||||
.listNodesDetailsMatching(NodePredicates.all()), Predicates.and(filter, Predicates
|
.listNodesDetailsMatching(NodePredicates.all()), Predicates.and(filter, Predicates
|
||||||
.not(NodePredicates.TERMINATED)))) {
|
.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);
|
Set<String> dnsNames = new HashSet<String>(0);
|
||||||
String dnsName = loadBalancerStrategy.execute(location, loadBalancerName, protocol,
|
for(Location location: locationMap.keySet())
|
||||||
loadBalancerPort, instancePort, ids);
|
{
|
||||||
logger.debug("<< created load balancer (%s) DNS (%s)", loadBalancerName, dnsName);
|
logger.debug(">> creating load balancer (%s)", loadBalancerName);
|
||||||
return dnsName;
|
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
|
@Override
|
||||||
public void deleteLoadBalancer(String loadBalancerName, Predicate<NodeMetadata> filter) {
|
public void deleteLoadBalancer(String dnsName) {
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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.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 javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import org.jclouds.aws.domain.Region;
|
import org.jclouds.aws.domain.Region;
|
||||||
|
@ -119,4 +122,24 @@ public class EC2Utils {
|
||||||
+ "s[" + i + "]"));
|
+ "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;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -194,10 +194,11 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest {
|
||||||
instanceIds.add(node.getProviderId());
|
instanceIds.add(node.getProviderId());
|
||||||
}
|
}
|
||||||
|
|
||||||
// create load balancer
|
// create load balancers
|
||||||
String dnsName = client.loadBalanceNodesMatching(NodePredicates.withTag(tag), tag, "HTTP",
|
|
||||||
|
Set<String> dnsNames = client.loadBalanceNodesMatching(NodePredicates.withTag(tag), tag, "HTTP",
|
||||||
80, 80);
|
80, 80);
|
||||||
assertNotNull(dnsName);
|
assertNotNull(dnsNames);
|
||||||
Set<ElasticLoadBalancer> elbs = elbClient.describeLoadBalancersInRegion(Region.US_EAST_1,
|
Set<ElasticLoadBalancer> elbs = elbClient.describeLoadBalancersInRegion(Region.US_EAST_1,
|
||||||
tag);
|
tag);
|
||||||
assertNotNull(elbs);
|
assertNotNull(elbs);
|
||||||
|
|
|
@ -235,10 +235,10 @@ public interface ComputeService {
|
||||||
* @return DNS Name of the load balancer
|
* @return DNS Name of the load balancer
|
||||||
*/
|
*/
|
||||||
@Beta
|
@Beta
|
||||||
String loadBalanceNodesMatching(Predicate<NodeMetadata> filter, String loadBalancerName,
|
Set<String> loadBalanceNodesMatching(Predicate<NodeMetadata> filter, String loadBalancerName,
|
||||||
String protocol, int loadBalancerPort, int instancePort);
|
String protocol, int loadBalancerPort, int instancePort);
|
||||||
|
|
||||||
@Beta
|
@Beta
|
||||||
void deleteLoadBalancer(String loadBalancerName, Predicate<NodeMetadata> filter);
|
void deleteLoadBalancer(String handle);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -424,12 +424,12 @@ public class BaseComputeService implements ComputeService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteLoadBalancer(String loadBalancerName, Predicate<NodeMetadata> filter) {
|
public void deleteLoadBalancer(String handle) {
|
||||||
throw new UnsupportedOperationException("deleteLoadBalancer not supported in this cloud");
|
throw new UnsupportedOperationException("deleteLoadBalancer not supported in this cloud");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String loadBalanceNodesMatching(Predicate<NodeMetadata> filter, String loadBalancerName,
|
public Set<String> loadBalanceNodesMatching(Predicate<NodeMetadata> filter, String loadBalancerName,
|
||||||
String protocol, int loadBalancerPort, int instancePort) {
|
String protocol, int loadBalancerPort, int instancePort) {
|
||||||
throw new UnsupportedOperationException(
|
throw new UnsupportedOperationException(
|
||||||
"loadBalanceNodesMatching not supported in this cloud");
|
"loadBalanceNodesMatching not supported in this cloud");
|
||||||
|
|
Loading…
Reference in New Issue