Example usage for com.amazonaws.services.elasticloadbalancing.model CreateLoadBalancerRequest setSubnets

List of usage examples for com.amazonaws.services.elasticloadbalancing.model CreateLoadBalancerRequest setSubnets

Introduction

In this page you can find the example usage for com.amazonaws.services.elasticloadbalancing.model CreateLoadBalancerRequest setSubnets.

Prototype


public void setSubnets(java.util.Collection<String> subnets) 

Source Link

Document

The IDs of the subnets in your VPC to attach to the load balancer.

Usage

From source file:org.apache.stratos.aws.extension.AWSHelper.java

License:Apache License

/**
 * Creates a load balancer and returns its DNS name. Useful when a new
 * cluster is added.//  w  w w.j  ava2 s .c om
 *
 * @param name      of the load balancer to be created
 * @param listeners to be attached to the load balancer
 * @param region    in which the load balancer needs to be created
 * @return DNS name of newly created load balancer
 * @throws LoadBalancerExtensionException
 */
public String createLoadBalancer(String name, List<Listener> listeners, String region,
        Set<String> availabilityZones, boolean inVPC) throws LoadBalancerExtensionException {

    log.info("Creating load balancer " + name);

    CreateLoadBalancerRequest createLoadBalancerRequest = new CreateLoadBalancerRequest(name);

    createLoadBalancerRequest.setListeners(listeners);

    // don't need this now since we are anyway updating zone according to the member
    //      Set<String> availabilityZones = new HashSet<String>();
    //      availabilityZones.add(getAvailabilityZoneFromRegion(region));
    //
    //      createLoadBalancerRequest.setAvailabilityZones(availabilityZones);

    try {
        if (inVPC) {

            List<String> securityGroups = new ArrayList<String>();
            if (!vpcIds.isEmpty()) {
                for (String vpcId : vpcIds) {
                    String securityGroupId = getSecurityGroupIdForRegion(region, vpcId);
                    securityGroups.add(securityGroupId);
                }
            } else {
                String securityGroupId = getSecurityGroupIdForRegion(region, null);
                securityGroups.add(securityGroupId);
            }

            createLoadBalancerRequest.setSecurityGroups(securityGroups);

            // set subnet ids
            if (!getSubnetIds().isEmpty()) {
                createLoadBalancerRequest.setSubnets(subnetIds);
            }

            // set scheme to 'internal' if specified
            if (getLbScheme() != null && getLbScheme().equals(Constants.LB_SCHEME_INTERNAL)) {
                createLoadBalancerRequest.setScheme(getLbScheme());
            }
        } else {
            // set initial availability zones
            createLoadBalancerRequest.setAvailabilityZones(availabilityZones);
        }

        elbClient.setEndpoint(String.format(Constants.ELB_ENDPOINT_URL_FORMAT, region));

        CreateLoadBalancerResult clbResult = elbClient.createLoadBalancer(createLoadBalancerRequest);

        return clbResult.getDNSName();

    } catch (AmazonClientException e) {
        String errorMsg = "Could not create load balancer " + name;
        log.error(errorMsg, e);
        throw new LoadBalancerExtensionException(errorMsg, e);
    }
}