List of usage examples for com.amazonaws.services.elasticloadbalancing AmazonElasticLoadBalancingClient AmazonElasticLoadBalancingClient
AmazonElasticLoadBalancingClient(AwsSyncClientParams clientParams, boolean endpointDiscoveryEnabled)
From source file:com.swap.aws.elb.client.AWSHelper.java
License:Apache License
/** * Creates a load balancer and returns its DNS name. Useful when a new * cluster is added./*from ww w . j a v a2s . co m*/ * * @param name * @param listeners * @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); Set<String> availabilityZones = new HashSet<String>(); availabilityZones.add(availabilityZone); createLoadBalancerRequest.setAvailabilityZones(availabilityZones); AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration); lbClient.setEndpoint("elasticloadbalancing." + this.region + ".amazonaws.com"); CreateLoadBalancerResult clbResult = lbClient.createLoadBalancer(createLoadBalancerRequest); System.out.println("Created load balancer : " + clbResult.getDNSName()); return clbResult.getDNSName(); } catch (Exception e) { log.error("Could not create load balancer : " + name + "."); e.printStackTrace(); return null; } }
From source file:com.swap.aws.elb.client.AWSHelper.java
License:Apache License
/** * Deletes the load balancer with the name provided. Useful when a cluster, * with which this load balancer was associated, is removed. * //from w w w .j ava 2 s . co m * @param loadBalancerName */ public void deleteLoadBalancer(String loadBalancerName) { try { DeleteLoadBalancerRequest deleteLoadBalancerRequest = new DeleteLoadBalancerRequest(); deleteLoadBalancerRequest.setLoadBalancerName(loadBalancerName); AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration); lbClient.setEndpoint("elasticloadbalancing." + this.region + ".amazonaws.com"); lbClient.deleteLoadBalancer(deleteLoadBalancerRequest); } catch (Exception e) { log.error("Could not delete load balancer : " + loadBalancerName); } }
From source file:com.swap.aws.elb.client.AWSHelper.java
License:Apache License
/** * Attaches provided instances to the load balancer. Useful when new * instances get added to the cluster with which this load balancer is * associated.//from w w w . j a v a 2 s . c o m * * @param loadBalancerName * @param instances */ public void registerInstancesToLoadBalancer(String loadBalancerName, List<Instance> instances) { try { RegisterInstancesWithLoadBalancerRequest registerInstancesWithLoadBalancerRequest = new RegisterInstancesWithLoadBalancerRequest( loadBalancerName, instances); AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration); lbClient.setEndpoint("elasticloadbalancing." + this.region + ".amazonaws.com"); RegisterInstancesWithLoadBalancerResult result = lbClient .registerInstancesWithLoadBalancer(registerInstancesWithLoadBalancerRequest); } catch (Exception e) { log.error("Could not register instances to load balancer " + loadBalancerName); } }
From source file:com.swap.aws.elb.client.AWSHelper.java
License:Apache License
/** * Detaches provided instances from the load balancer, associated with some * cluster. Useful when instances are removed from the cluster with which * this load balancer is associated.//from w ww .java 2 s. c om * * @param loadBalancerName * @param instances */ public void deregisterInstancesFromLoadBalancer(String loadBalancerName, List<Instance> instances) { try { DeregisterInstancesFromLoadBalancerRequest deregisterInstancesFromLoadBalancerRequest = new DeregisterInstancesFromLoadBalancerRequest( loadBalancerName, instances); AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration); lbClient.setEndpoint("elasticloadbalancing." + this.region + ".amazonaws.com"); DeregisterInstancesFromLoadBalancerResult result = lbClient .deregisterInstancesFromLoadBalancer(deregisterInstancesFromLoadBalancerRequest); } catch (Exception e) { log.error("Could not de-register instances from load balancer " + loadBalancerName); } }
From source file:com.swap.aws.elb.client.AWSHelper.java
License:Apache License
/** * Returns description of the load balancer which is helpful in determining * instances, listeners associated with load balancer * //from ww w. j a v a 2s . c o m * @param loadBalancerName * @return description of the load balancer */ public LoadBalancerDescription getLoadBalancerDescription(String loadBalancerName) { List<String> loadBalancers = new ArrayList<String>(); loadBalancers.add(loadBalancerName); DescribeLoadBalancersRequest describeLoadBalancersRequest = new DescribeLoadBalancersRequest(loadBalancers); AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration); lbClient.setEndpoint("elasticloadbalancing." + this.region + ".amazonaws.com"); DescribeLoadBalancersResult result = lbClient.describeLoadBalancers(describeLoadBalancersRequest); if (result.getLoadBalancerDescriptions() == null || result.getLoadBalancerDescriptions().size() == 0) return null; else return result.getLoadBalancerDescriptions().get(0); }
From source file:com.swap.aws.elb.client.AWSHelper.java
License:Apache License
/** * Adds listeners provided to the load balancer. Useful when service * definition is changed, in particular port mappings. So new listeners need * to be added./*from www .j av a 2 s . co m*/ * * @param loadBalancerName * @param listeners */ public void addListenersToLoadBalancer(String loadBalancerName, List<Listener> listeners) { if (listeners.size() == 0) return; try { CreateLoadBalancerListenersRequest createLoadBalancerListenersRequest = new CreateLoadBalancerListenersRequest(); createLoadBalancerListenersRequest.setListeners(listeners); createLoadBalancerListenersRequest.setLoadBalancerName(loadBalancerName); AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration); lbClient.setEndpoint("elasticloadbalancing." + this.region + ".amazonaws.com"); lbClient.createLoadBalancerListeners(createLoadBalancerListenersRequest); } catch (Exception e) { log.error("Could not add listeners to load balancer " + loadBalancerName); } }
From source file:com.swap.aws.elb.client.AWSHelper.java
License:Apache License
/** * Remove listeners provided from the load balancer. Useful when attached * listeners are no longer required./* ww w . j a va2s . c o m*/ * * @param loadBalancerName * @param listeners */ public void removeListenersFromLoadBalancer(String loadBalancerName, List<Listener> listeners) { if (listeners.size() == 0) return; try { DeleteLoadBalancerListenersRequest deleteLoadBalancerListenersRequest = new DeleteLoadBalancerListenersRequest(); deleteLoadBalancerListenersRequest.setLoadBalancerName(loadBalancerName); List<Integer> loadBalancerPorts = new ArrayList<Integer>(); for (Listener listener : listeners) { loadBalancerPorts.add(listener.getLoadBalancerPort()); } deleteLoadBalancerListenersRequest.setLoadBalancerPorts(loadBalancerPorts); AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration); lbClient.setEndpoint("elasticloadbalancing." + this.region + ".amazonaws.com"); lbClient.deleteLoadBalancerListeners(deleteLoadBalancerListenersRequest); } catch (Exception e) { log.error("Could not remove listeners from load balancer " + loadBalancerName); } }
From source file:com.swap.aws.elb.client.AWSHelper.java
License:Apache License
public Instance getInstanceByIP(String ip) { DescribeInstancesRequest request = new DescribeInstancesRequest(); // List<Filter> filters = new ArrayList<Filter>(); //// w w w .ja va2s.c o m // List<String> ips = new ArrayList<String>(); // ips.add(ip); // // Filter ipFilter = new Filter("ip-address", ips); // filters.add(ipFilter); // List<String> zones = new ArrayList<String>(); // zones.add("us-east-1c"); // Filter zoneFilter = new Filter("availability-zone", zones); // filters.add(zoneFilter); //request.setFilters(filters); AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration); AmazonEC2Client cl = new AmazonEC2Client(awsCredentials); DescribeInstancesResult result = cl.describeInstances(request); List<Reservation> reservations = result.getReservations(); for (Reservation reservation : reservations) { List<com.amazonaws.services.ec2.model.Instance> instances = reservation.getInstances(); for (com.amazonaws.services.ec2.model.Instance instance : instances) { System.out.println(instance.getInstanceId()); //return transformInstace(instance); } } return null; }
From source file:jp.primecloud.auto.aws.amazon.AmazonAwsClientFactory.java
License:Open Source License
/** * {@inheritDoc}/*from w w w . java2s.co m*/ */ @Override public AmazonElasticLoadBalancing createElbClient(String awsAccessId, String awsSecretKey) { AWSCredentials credentials = new BasicAWSCredentials(awsAccessId, awsSecretKey); ClientConfiguration configuration = createConfiguration(); AmazonElasticLoadBalancing client = new AmazonElasticLoadBalancingClient(credentials, configuration); if (host != null) { client.setEndpoint(AmazonElasticLoadBalancing.ENDPOINT_PREFIX + "." + host); } client = new ExceptionHandleAwsClientWrapper().wrap(client); return client; }
From source file:org.apache.stratos.aws.extension.AWSHelper.java
License:Apache License
public AWSHelper() throws LoadBalancerExtensionException { // Read values for awsAccessKey, awsSecretKey etc. from config file String awsPropertiesFile = System.getProperty(Constants.AWS_PROPERTIES_FILE); Properties properties = new Properties(); InputStream inputStream = null; try {//w w w . ja va2s . c o m inputStream = new FileInputStream(awsPropertiesFile); properties.load(inputStream); this.awsAccessKey = properties.getProperty(Constants.AWS_ACCESS_KEY); this.awsSecretKey = properties.getProperty(Constants.AWS_SECRET_KEY); if (this.awsAccessKey.isEmpty() || this.awsSecretKey.isEmpty()) { throw new LoadBalancerExtensionException("Invalid AWS credentials."); } this.lbPrefix = properties.getProperty(Constants.LB_PREFIX); if (this.lbPrefix.isEmpty() || this.lbPrefix.length() > Constants.LOAD_BALANCER_PREFIX_MAX_LENGTH) { throw new LoadBalancerExtensionException("Invalid load balancer prefix."); } lbSequence = new AtomicInteger(1); this.lbSecurityGroupName = properties.getProperty(Constants.LOAD_BALANCER_SECURITY_GROUP_NAME); lbSecurityGroupId = properties.getProperty(Constants.LOAD_BALANCER_SECURITY_GROUP_ID); if ((lbSecurityGroupId == null || lbSecurityGroupId.isEmpty()) && (this.lbSecurityGroupName.isEmpty() || this.lbSecurityGroupName.length() > Constants.SECURITY_GROUP_NAME_MAX_LENGTH)) { throw new LoadBalancerExtensionException( "Either security group name or security " + "group id is required"); } // if (this.lbSecurityGroupName.isEmpty() || this.lbSecurityGroupName.length() > // Constants.SECURITY_GROUP_NAME_MAX_LENGTH) { // throw new LoadBalancerExtensionException("Invalid load balancer security group name."); // } // Read the SSL certificate Id. This is mandatory if only we are using HTTPS as the front end protocol. // http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/using-elb-listenerconfig-quickref.html this.sslCertificateId = properties.getProperty(Constants.LOAD_BALANCER_SSL_CERTIFICATE_ID).trim(); // Cookie name for application level stickiness this.appStickySessionCookie = properties.getProperty(Constants.APP_STICKY_SESSION_COOKIE_NAME).trim(); this.allowedCidrIpForLBSecurityGroup = properties.getProperty(Constants.ALLOWED_CIDR_IP_KEY); if (this.allowedCidrIpForLBSecurityGroup.isEmpty()) { throw new LoadBalancerExtensionException("Invalid allowed CIDR IP."); } String allowedProtocols = properties.getProperty(Constants.ALLOWED_PROTOCOLS); if (allowedProtocols.isEmpty()) { throw new LoadBalancerExtensionException("Please specify at least one Internet protocol."); } String[] protocols = allowedProtocols.split(","); this.allowedProtocolsForLBSecurityGroup = new ArrayList<String>(); for (String protocol : protocols) { this.allowedProtocolsForLBSecurityGroup.add(protocol); } String interval = properties.getProperty(Constants.STATISTICS_INTERVAL); if (interval == null || interval.isEmpty()) { this.statisticsInterval = Constants.STATISTICS_INTERVAL_MULTIPLE_OF; } else { try { this.statisticsInterval = Integer.parseInt(interval); if (this.statisticsInterval % Constants.STATISTICS_INTERVAL_MULTIPLE_OF != 0) { this.statisticsInterval = Constants.STATISTICS_INTERVAL_MULTIPLE_OF; } } catch (NumberFormatException e) { log.warn("Invalid statistics interval. Setting it to 15."); this.statisticsInterval = 15; } } this.lbSecurityGroupDescription = Constants.LOAD_BALANCER_SECURITY_GROUP_DESCRIPTION; String commaSeparatedInitialZones = properties.getProperty(Constants.INITIAL_AVAILABILITY_ZONES); if (commaSeparatedInitialZones != null && !commaSeparatedInitialZones.isEmpty()) { initialZones.addAll(Arrays.asList(commaSeparatedInitialZones.trim().split("\\s*," + "\\s*"))); } String commaSeparatedSubnetIds = properties.getProperty(Constants.SUBNET_IDS); if (commaSeparatedSubnetIds != null && !commaSeparatedSubnetIds.isEmpty()) { subnetIds.addAll(Arrays.asList(commaSeparatedSubnetIds.trim().split("\\s*," + "\\s*"))); } String commaSeparatedVPCIds = properties.getProperty(Constants.VPC_IDS); if (commaSeparatedVPCIds != null && !commaSeparatedVPCIds.isEmpty()) { vpcIds.addAll(Arrays.asList(commaSeparatedVPCIds.trim().split("\\s*," + "\\s*"))); } lbScheme = properties.getProperty(Constants.LB_SCHEME); regionToSecurityGroupIdMap = new ConcurrentHashMap<String, String>(); awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey); clientConfiguration = new ClientConfiguration(); elbClient = new AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration); ec2Client = new AmazonEC2Client(awsCredentials, clientConfiguration); cloudWatchClient = new AmazonCloudWatchClient(awsCredentials, clientConfiguration); } catch (IOException e) { log.error("Error reading aws configuration file."); throw new LoadBalancerExtensionException("Error reading aws configuration file.", e); } finally { try { inputStream.close(); } catch (Exception e) { log.warn("Failed to close input stream to aws configuration file."); } } }