stratos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject [14/32] stratos git commit: Setting endpoint, adding logging for AWS API Calls. Deleting load balancer for inactive cluster.
Date Fri, 09 Oct 2015 14:35:27 GMT
Setting endpoint, adding logging for AWS API Calls. Deleting load balancer for inactive cluster.


Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/31acd2a4
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/31acd2a4
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/31acd2a4

Branch: refs/heads/gsoc-projects-2015
Commit: 31acd2a4401ac9aea23e1b8d2b1f476666dd80e7
Parents: 7ea76ec
Author: swapnilpatilRajaram <swapnil.patil@students.iiit.ac.in>
Authored: Sat Jul 18 16:59:12 2015 +0000
Committer: swapnilpatilRajaram <swapnil.patil@students.iiit.ac.in>
Committed: Sat Jul 18 16:59:12 2015 +0000

----------------------------------------------------------------------
 .../apache/stratos/aws/extension/AWSHelper.java | 165 ++++++++-----
 .../stratos/aws/extension/AWSLoadBalancer.java  | 239 +++++++++++--------
 2 files changed, 244 insertions(+), 160 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/31acd2a4/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
----------------------------------------------------------------------
diff --git a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
index 438289a..924d3bd 100644
--- a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
+++ b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
@@ -38,10 +38,6 @@ import com.amazonaws.auth.BasicAWSCredentials;
 import com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingClient;
 import com.amazonaws.services.elasticloadbalancing.model.*;
 
-/**
- * @author swapnil
- * 
- */
 public class AWSHelper {
 	private String awsAccessKey;
 	private String awsSecretKey;
@@ -51,6 +47,8 @@ public class AWSHelper {
 	private BasicAWSCredentials awsCredentials;
 	private ClientConfiguration clientConfiguration;
 
+	AmazonElasticLoadBalancingClient lbClient;
+
 	private static final Log log = LogFactory.getLog(AWSHelper.class);
 
 	public AWSHelper() throws LoadBalancerExtensionException {
@@ -76,6 +74,15 @@ public class AWSHelper {
 			this.availabilityZone = properties
 					.getProperty(Constants.AVAILABILITY_ZONE_KEY);
 			this.region = properties.getProperty(Constants.REGION_KEY);
+
+			awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
+			clientConfiguration = new ClientConfiguration();
+
+			lbClient = new AmazonElasticLoadBalancingClient(awsCredentials,
+					clientConfiguration);
+			lbClient.setEndpoint("elasticloadbalancing." + this.region
+					+ ".amazonaws.com");
+
 		} catch (IOException e) {
 			log.error("Error reading aws configuration file.");
 			throw new LoadBalancerExtensionException(
@@ -87,9 +94,6 @@ public class AWSHelper {
 				log.warn("Failed to close input stream to aws configuration file.");
 			}
 		}
-
-		awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
-		clientConfiguration = new ClientConfiguration();
 	}
 
 	/**
@@ -101,31 +105,32 @@ public class AWSHelper {
 	 * @return DNS name of newly created load balancer
 	 */
 	public String createLoadBalancer(String name, List<Listener> listeners) {
-		try {
-			CreateLoadBalancerRequest createLoadBalancerRequest = new CreateLoadBalancerRequest(
-					name);
 
-			createLoadBalancerRequest.setListeners(listeners);
+		log.info("Creating load balancer " + name);
 
-			Set<String> availabilityZones = new HashSet<String>();
-			availabilityZones.add(availabilityZone);
+		CreateLoadBalancerRequest createLoadBalancerRequest = new CreateLoadBalancerRequest(
+				name);
 
-			createLoadBalancerRequest.setAvailabilityZones(availabilityZones);
+		createLoadBalancerRequest.setListeners(listeners);
 
-			AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(
-					awsCredentials, clientConfiguration);
-			lbClient.setEndpoint("elasticloadbalancing." + this.region
-					+ ".amazonaws.com");
+		Set<String> availabilityZones = new HashSet<String>();
+		availabilityZones.add(availabilityZone);
+
+		createLoadBalancerRequest.setAvailabilityZones(availabilityZones);
+
+		try {
 
 			CreateLoadBalancerResult clbResult = lbClient
 					.createLoadBalancer(createLoadBalancerRequest);
 
 			return clbResult.getDNSName();
+
 		} catch (Exception e) {
 			log.error("Could not create load balancer : " + name + ".");
 			e.printStackTrace();
 			return null;
 		}
+
 	}
 
 	/**
@@ -135,16 +140,19 @@ public class AWSHelper {
 	 * @param loadBalancerName
 	 */
 	public void deleteLoadBalancer(String loadBalancerName) {
-		try {
-			DeleteLoadBalancerRequest deleteLoadBalancerRequest = new DeleteLoadBalancerRequest();
-			deleteLoadBalancerRequest.setLoadBalancerName(loadBalancerName);
 
-			AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(
-					awsCredentials, clientConfiguration);
+		log.info("Deleting load balancer " + loadBalancerName);
+
+		DeleteLoadBalancerRequest deleteLoadBalancerRequest = new DeleteLoadBalancerRequest();
+		deleteLoadBalancerRequest.setLoadBalancerName(loadBalancerName);
 
+		try {
 			lbClient.deleteLoadBalancer(deleteLoadBalancerRequest);
+			return;
+
 		} catch (Exception e) {
 			log.error("Could not delete load balancer : " + loadBalancerName);
+			e.printStackTrace();
 		}
 	}
 
@@ -158,18 +166,23 @@ public class AWSHelper {
 	 */
 	public void registerInstancesToLoadBalancer(String loadBalancerName,
 			List<Instance> instances) {
-		try {
-			RegisterInstancesWithLoadBalancerRequest registerInstancesWithLoadBalancerRequest = new
RegisterInstancesWithLoadBalancerRequest(
-					loadBalancerName, instances);
 
-			AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(
-					awsCredentials, clientConfiguration);
+		log.info("Attaching instance " + instances.get(0)
+				+ " to load balancer + " + loadBalancerName);
+
+		RegisterInstancesWithLoadBalancerRequest registerInstancesWithLoadBalancerRequest = new
RegisterInstancesWithLoadBalancerRequest(
+				loadBalancerName, instances);
+
+		try {
 
 			RegisterInstancesWithLoadBalancerResult result = lbClient
 					.registerInstancesWithLoadBalancer(registerInstancesWithLoadBalancerRequest);
+			return;
+
 		} catch (Exception e) {
 			log.error("Could not register instances to load balancer "
 					+ loadBalancerName);
+			e.printStackTrace();
 		}
 	}
 
@@ -183,18 +196,22 @@ public class AWSHelper {
 	 */
 	public void deregisterInstancesFromLoadBalancer(String loadBalancerName,
 			List<Instance> instances) {
-		try {
-			DeregisterInstancesFromLoadBalancerRequest deregisterInstancesFromLoadBalancerRequest
= new DeregisterInstancesFromLoadBalancerRequest(
-					loadBalancerName, instances);
 
-			AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(
-					awsCredentials, clientConfiguration);
+		log.info("Detaching instance " + instances.get(0)
+				+ " from load balancer + " + loadBalancerName);
+
+		DeregisterInstancesFromLoadBalancerRequest deregisterInstancesFromLoadBalancerRequest =
new DeregisterInstancesFromLoadBalancerRequest(
+				loadBalancerName, instances);
 
+		try {
 			DeregisterInstancesFromLoadBalancerResult result = lbClient
 					.deregisterInstancesFromLoadBalancer(deregisterInstancesFromLoadBalancerRequest);
+			return;
+
 		} catch (Exception e) {
 			log.error("Could not de-register instances from load balancer "
 					+ loadBalancerName);
+			e.printStackTrace();
 		}
 	}
 
@@ -207,6 +224,7 @@ public class AWSHelper {
 	 */
 	private LoadBalancerDescription getLoadBalancerDescription(
 			String loadBalancerName) {
+
 		List<String> loadBalancers = new ArrayList<String>();
 
 		loadBalancers.add(loadBalancerName);
@@ -214,17 +232,21 @@ public class AWSHelper {
 		DescribeLoadBalancersRequest describeLoadBalancersRequest = new DescribeLoadBalancersRequest(
 				loadBalancers);
 
-		AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(
-				awsCredentials, clientConfiguration);
-
-		DescribeLoadBalancersResult result = lbClient
-				.describeLoadBalancers(describeLoadBalancersRequest);
+		try {
+			DescribeLoadBalancersResult result = lbClient
+					.describeLoadBalancers(describeLoadBalancersRequest);
 
-		if (result.getLoadBalancerDescriptions() == null
-				|| result.getLoadBalancerDescriptions().size() == 0)
+			if (result.getLoadBalancerDescriptions() == null
+					|| result.getLoadBalancerDescriptions().size() == 0)
+				return null;
+			else
+				return result.getLoadBalancerDescriptions().get(0);
+		} catch (Exception e) {
+			log.error("Could not find description of load balancer "
+					+ loadBalancerName);
+			e.printStackTrace();
 			return null;
-		else
-			return result.getLoadBalancerDescriptions().get(0);
+		}
 	}
 
 	/**
@@ -247,8 +269,9 @@ public class AWSHelper {
 			return lbDescription.getInstances();
 
 		} catch (Exception e) {
-			log.error("Could not find description of load balancer "
+			log.error("Could not find instances attached  load balancer "
 					+ loadBalancerName);
+			e.printStackTrace();
 			return null;
 		}
 	}
@@ -266,19 +289,20 @@ public class AWSHelper {
 		if (listeners.size() == 0)
 			return;
 
-		try {
-			CreateLoadBalancerListenersRequest createLoadBalancerListenersRequest = new CreateLoadBalancerListenersRequest();
-			createLoadBalancerListenersRequest.setListeners(listeners);
-			createLoadBalancerListenersRequest
-					.setLoadBalancerName(loadBalancerName);
+		CreateLoadBalancerListenersRequest createLoadBalancerListenersRequest = new CreateLoadBalancerListenersRequest();
+		createLoadBalancerListenersRequest.setListeners(listeners);
+		createLoadBalancerListenersRequest
+				.setLoadBalancerName(loadBalancerName);
 
-			AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(
-					awsCredentials, clientConfiguration);
+		try {
 
 			lbClient.createLoadBalancerListeners(createLoadBalancerListenersRequest);
+			return;
+
 		} catch (Exception e) {
 			log.error("Could not add listeners to load balancer "
 					+ loadBalancerName);
+			e.printStackTrace();
 		}
 	}
 
@@ -294,28 +318,28 @@ public class AWSHelper {
 		if (listeners.size() == 0)
 			return;
 
-		try {
-			DeleteLoadBalancerListenersRequest deleteLoadBalancerListenersRequest = new DeleteLoadBalancerListenersRequest();
-			deleteLoadBalancerListenersRequest
-					.setLoadBalancerName(loadBalancerName);
+		DeleteLoadBalancerListenersRequest deleteLoadBalancerListenersRequest = new DeleteLoadBalancerListenersRequest();
+		deleteLoadBalancerListenersRequest
+				.setLoadBalancerName(loadBalancerName);
 
-			List<Integer> loadBalancerPorts = new ArrayList<Integer>();
+		List<Integer> loadBalancerPorts = new ArrayList<Integer>();
 
-			for (Listener listener : listeners) {
-				loadBalancerPorts.add(listener.getLoadBalancerPort());
-			}
+		for (Listener listener : listeners) {
+			loadBalancerPorts.add(listener.getLoadBalancerPort());
+		}
 
-			deleteLoadBalancerListenersRequest
-					.setLoadBalancerPorts(loadBalancerPorts);
+		deleteLoadBalancerListenersRequest
+				.setLoadBalancerPorts(loadBalancerPorts);
 
-			AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(
-					awsCredentials, clientConfiguration);
+		try {
 
 			lbClient.deleteLoadBalancerListeners(deleteLoadBalancerListenersRequest);
+			return;
 
 		} catch (Exception e) {
 			log.error("Could not remove listeners from load balancer "
 					+ loadBalancerName);
+			e.printStackTrace();
 		}
 	}
 
@@ -368,9 +392,11 @@ public class AWSHelper {
 		for (Port port : service.getPorts()) {
 			int instancePort = port.getValue();
 			int proxyPort = port.getProxy();
-			String protocol = port.getProtocol();
+			String protocol = port.getProtocol().toUpperCase();
+			String instanceProtocol = protocol;
 
 			Listener listener = new Listener(protocol, proxyPort, instancePort);
+			listener.setInstanceProtocol(instanceProtocol);
 
 			listeners.add(listener);
 		}
@@ -393,4 +419,17 @@ public class AWSHelper {
 
 		return name;
 	}
+
+	/**
+	 * @param memberInstanceName
+	 * @return
+	 */
+	public String getAWSInstanceName(final String memberInstanceName) {
+		if (memberInstanceName.contains("/")) {
+			return memberInstanceName
+					.substring(memberInstanceName.indexOf("/") + 1);
+		} else {
+			return memberInstanceName;
+		}
+	}
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/31acd2a4/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
----------------------------------------------------------------------
diff --git a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
index 8aafce2..99d6906 100644
--- a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
+++ b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
@@ -22,6 +22,7 @@ package org.apache.stratos.aws.extension;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 
 import org.apache.commons.logging.Log;
@@ -52,149 +53,193 @@ public class AWSLoadBalancer implements LoadBalancer {
 
 		log.info("AWS load balancer extension re-configured.");
 
-		for (Service service : topology.getServices()) {
+		try {
+			// Thread.sleep(10000);
 
-			List<Listener> listenersForThisService = awsHelper
-					.getRequiredListeners(service);
+			HashSet<String> activeClusters = new HashSet<String>();
 
-			for (Cluster cluster : service.getClusters()) {
+			for (Service service : topology.getServices()) {
 
-				// cluster.getHostNames()
+				List<Listener> listenersForThisService = awsHelper
+						.getRequiredListeners(service);
 
-				// Check if a load balancer is created for this cluster
+				for (Cluster cluster : service.getClusters()) {
+					// Check if a load balancer is created for this cluster
+					if (clusterIdToLoadBalancerMap.containsKey(cluster
+							.getClusterId())) {
+						// A load balancer is already present for this cluster
+						// Get the load balancer and update it.
 
-				if (clusterIdToLoadBalancerMap.containsKey(cluster
-						.getClusterId())) {
-					// A load balancer is already present for this cluster
-					// Get the load balancer and update it.
+						String loadBalancerName = clusterIdToLoadBalancerMap
+								.get(cluster.getClusterId());
 
-					String loadBalancerName = clusterIdToLoadBalancerMap
-							.get(cluster.getClusterId());
+						// 1. Get all the instances attached
+						// Add/remove instances as necessary
 
-					// 1. Get all the instances attached
-					// Add/remove instances as necessary
+						List<Instance> attachedInstances = awsHelper
+								.getAttachedInstances(loadBalancerName);
 
-					List<Instance> attachedInstances = awsHelper
-							.getAttachedInstances(loadBalancerName);
+						Collection<Member> clusterInstances = cluster
+								.getMembers();
 
-					Collection<Member> clusterInstances = cluster.getMembers();
+						if (clusterInstances.size() > 0) {
+							activeClusters.add(cluster.getClusterId());
+						} else {
+							break;
+						}
 
-					List<Instance> instancesToAddToLoadBalancer = new ArrayList<Instance>();
+						List<Instance> instancesToAddToLoadBalancer = new ArrayList<Instance>();
 
-					for (Member member : clusterInstances) {
-						// if instance id of member is not in attachedInstances
-						// add this to instancesToAddToLoadBalancer
+						for (Member member : clusterInstances) {
+							// if instance id of member is not in
+							// attachedInstances
+							// add this to instancesToAddToLoadBalancer
 
-						System.out.println("Instance Id :");
-						System.out.println(member.getInstanceId());
-						
-						Instance instance = new Instance(member.getInstanceId());
+							System.out.println("Instance Id : "
+									+ member.getInstanceId());
+							System.out.println("New instance id : "
+									+ awsHelper.getAWSInstanceName(member
+											.getInstanceId()));
 
-						if (!attachedInstances.contains(instance)) {
-							instancesToAddToLoadBalancer.add(instance);
-						}
-						
-					}
+							Instance instance = new Instance(
+									awsHelper.getAWSInstanceName(member
+											.getInstanceId()));
 
-					List<Instance> instancesToRemoveFromLoadBalancer = new ArrayList<Instance>();
+							if (attachedInstances == null
+									|| !attachedInstances.contains(instance)) {
+								instancesToAddToLoadBalancer.add(instance);
+							}
 
-					for (Instance instance : attachedInstances) {
-						if (!clusterInstances
-								.contains(instance.getInstanceId())) {
-							instancesToRemoveFromLoadBalancer.add(instance);
 						}
-					}
 
-					if (instancesToRemoveFromLoadBalancer.size() > 0)
-						awsHelper.deregisterInstancesFromLoadBalancer(
-								loadBalancerName,
-								instancesToRemoveFromLoadBalancer);
+						List<Instance> instancesToRemoveFromLoadBalancer = new ArrayList<Instance>();
 
-					if (instancesToAddToLoadBalancer.size() > 0)
-						awsHelper.registerInstancesToLoadBalancer(
-								loadBalancerName, instancesToAddToLoadBalancer);
+						for (Instance instance : attachedInstances) {
+							if (!clusterInstances.contains(instance
+									.getInstanceId())) {
+								instancesToRemoveFromLoadBalancer.add(instance);
+							}
+						}
 
-					// 2. Get all the listeners
-					// Add/Remove listeners as necessary
+						if (instancesToRemoveFromLoadBalancer.size() > 0)
+							awsHelper.deregisterInstancesFromLoadBalancer(
+									loadBalancerName,
+									instancesToRemoveFromLoadBalancer);
 
-					// Is it really necessary to add/remove listeners from a lb
-					// to a cluster
-					// Need to add only if a cluster can be used for more than
-					// one service (because a service my get added later)
-					// or service port mappings may change
+						if (instancesToAddToLoadBalancer.size() > 0)
+							awsHelper.registerInstancesToLoadBalancer(
+									loadBalancerName,
+									instancesToAddToLoadBalancer);
 
-					// Need to remove only if ... same for above reason
+						// 2. Get all the listeners
+						// Add/Remove listeners as necessary
 
-					List<Listener> attachedListeners = awsHelper
-							.getAttachedListeners(loadBalancerName);
+						// Is it really necessary to add/remove listeners from a
+						// lb
+						// to a cluster
+						// Need to add only if a cluster can be used for more
+						// than
+						// one service (because a service my get added later)
+						// or service port mappings may change
 
-					List<Listener> listenersToAddToLoadBalancer = new ArrayList<Listener>();
+						// Need to remove only if ... same for above reason
 
-					for (Listener listener : listenersForThisService) {
-						// Need to check if Listener class supports equals
-						// method or not
+						List<Listener> attachedListeners = awsHelper
+								.getAttachedListeners(loadBalancerName);
 
-						// if listener required for this service is not in
-						// attachedListeners
-						// add this to listenersToAddToLoadBalancer
-					}
+						List<Listener> listenersToAddToLoadBalancer = new ArrayList<Listener>();
 
-					List<Listener> listenersToRemoveFromLoadBalancer = new ArrayList<Listener>();
+						for (Listener listener : listenersForThisService) {
+							if (attachedListeners == null
+									|| !attachedListeners.contains(listener)) {
+								listenersToAddToLoadBalancer.add(listener);
+							}
+						}
 
-					for (Listener listener : attachedListeners) {
-						// Need to check if Listener class supports equals
-						// method or not
+						List<Listener> listenersToRemoveFromLoadBalancer = new ArrayList<Listener>();
 
-						if (!listenersForThisService.contains(listener)) {
-							listenersToRemoveFromLoadBalancer.add(listener);
+						for (Listener listener : attachedListeners) {
+							if (!listenersForThisService.contains(listener)) {
+								listenersToRemoveFromLoadBalancer.add(listener);
+							}
 						}
-					}
 
-					if (listenersToRemoveFromLoadBalancer.size() > 0)
-						awsHelper.removeListenersFromLoadBalancer(
-								loadBalancerName,
-								listenersToRemoveFromLoadBalancer);
+						if (listenersToRemoveFromLoadBalancer.size() > 0)
+							awsHelper.removeListenersFromLoadBalancer(
+									loadBalancerName,
+									listenersToRemoveFromLoadBalancer);
 
-					if (listenersToAddToLoadBalancer.size() > 0)
-						awsHelper.addListenersToLoadBalancer(loadBalancerName,
-								listenersToAddToLoadBalancer);
+						if (listenersToAddToLoadBalancer.size() > 0)
+							awsHelper.addListenersToLoadBalancer(
+									loadBalancerName,
+									listenersToAddToLoadBalancer);
 
-					// Update domain mappings
+						// Update domain mappings
 
-				} else {
-					// Create a new load balancer for this cluster
+					} else {
+						// Create a new load balancer for this cluster
+						Collection<Member> clusterInstances = cluster
+								.getMembers();
 
-					String loadBalancerName = awsHelper.getLoadBalancerName(cluster.getClusterId());
+						if (clusterInstances.size() == 0)
+							break;
 
-					String loadBalancerDNSName = awsHelper.createLoadBalancer(
-							loadBalancerName, listenersForThisService);
+						String loadBalancerName = awsHelper
+								.getLoadBalancerName(cluster.getClusterId());
 
-					// register instances to LB
+						String loadBalancerDNSName = awsHelper
+								.createLoadBalancer(loadBalancerName,
+										listenersForThisService);
 
-					List<Instance> instances = new ArrayList<Instance>();
+						log.info("Load balancer '" + loadBalancerDNSName
+								+ "' created for cluster '"
+								+ cluster.getClusterId());
 
-					for (Member member : cluster.getMembers()) {
-						String instanceId = member.getInstanceId();
+						// register instances to LB
+						List<Instance> instances = new ArrayList<Instance>();
 
-						System.out.println("Instance id : " + instanceId);
-						
-						Instance instance = new Instance();
-						instance.setInstanceId(instanceId);
+						for (Member member : clusterInstances) {
+							String instanceId = member.getInstanceId();
 
-						instances.add(instance);
-					}
+							System.out.println("Instance id : " + instanceId);
+							System.out.println("New instance id : "
+									+ awsHelper.getAWSInstanceName(instanceId));
 
-					awsHelper.registerInstancesToLoadBalancer(loadBalancerName,
-							instances);
+							Instance instance = new Instance();
+							instance.setInstanceId(awsHelper
+									.getAWSInstanceName(instanceId));
 
-					// Create domain mappings
+							instances.add(instance);
+						}
+
+						awsHelper.registerInstancesToLoadBalancer(
+								loadBalancerName, instances);
+
+						// Create domain mappings
+
+						clusterIdToLoadBalancerMap.put(cluster.getClusterId(),
+								loadBalancerName);
+						activeClusters.add(cluster.getClusterId());
+					}
 				}
+			}
+
+			// Find out clusters which were present earlier but are not now.
+			// Delete load balancers associated with those clusters.
 
+			for (String clusterId : clusterIdToLoadBalancerMap.keySet()) {
+				if (!activeClusters.contains(clusterId)) {
+					// Remove load balancer for that cluster.
+					awsHelper.deleteLoadBalancer(clusterIdToLoadBalancerMap
+							.get(clusterId));
+					clusterIdToLoadBalancerMap.remove(clusterId);
+				}
 			}
-		}
 
-		// Find out clusters which were present earlier but are not now.
-		// Delete load balancers associated with those clusters.
+			activeClusters.clear();
+		} catch (Exception e) {
+			throw new LoadBalancerExtensionException(e);
+		}
 
 		return true;
 	}


Mime
View raw message