Ejemplo n.º 1
0
  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;

    if (obj instanceof AddTagsRequest == false) return false;
    AddTagsRequest other = (AddTagsRequest) obj;

    if (other.getLoadBalancerNames() == null ^ this.getLoadBalancerNames() == null) return false;
    if (other.getLoadBalancerNames() != null
        && other.getLoadBalancerNames().equals(this.getLoadBalancerNames()) == false) return false;
    if (other.getTags() == null ^ this.getTags() == null) return false;
    if (other.getTags() != null && other.getTags().equals(this.getTags()) == false) return false;
    return true;
  }
  public static String setUpElbWithLoadGenerator(String loadGenerator) throws Exception {

    String loadBalancerName = "projecttwoelb";

    // Create the ELB client.
    elbClient = new AmazonElasticLoadBalancingClient(awsCredentials());
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    elbClient.setRegion(usEast1);

    CreateLoadBalancerRequest elbRequest = new CreateLoadBalancerRequest();
    elbRequest.setLoadBalancerName(loadBalancerName);

    // Security Group

    DescribeSecurityGroupsRequest sgRequest = new DescribeSecurityGroupsRequest();
    ArrayList<String> list = new ArrayList<String>();
    list.add("SecurityGroupForELBAndAutoscale");
    sgRequest.setGroupNames(list);

    DescribeSecurityGroupsResult result = ec2.describeSecurityGroups(sgRequest);
    List<SecurityGroup> groups = result.getSecurityGroups();

    ArrayList<String> securityGroups = new ArrayList<String>();
    securityGroups.add(groups.get(0).getGroupId());
    elbRequest.setSecurityGroups(securityGroups);

    // Adding tags
    Collection<com.amazonaws.services.elasticloadbalancing.model.Tag> tags =
        new ArrayList<com.amazonaws.services.elasticloadbalancing.model.Tag>();

    com.amazonaws.services.elasticloadbalancing.model.Tag tag =
        new com.amazonaws.services.elasticloadbalancing.model.Tag();
    tag.setKey("Project");
    tag.setValue("2.1");
    tags.add(tag);

    AddTagsRequest tagReq = new AddTagsRequest();
    tagReq.withTags(tags);
    elbRequest.setTags(tags);

    // Specify ports
    List<Listener> listener = new ArrayList<Listener>(1);
    listener.add(new Listener("HTTP", 80, 80));
    elbRequest.setListeners(listener);

    // Speciy availability zones
    List<String> zones = new ArrayList<String>(1);
    zones.add("us-east-1c");
    elbRequest.setAvailabilityZones(zones);

    CreateLoadBalancerResult lbResult = elbClient.createLoadBalancer(elbRequest);

    // Disable Cross Zone
    ModifyLoadBalancerAttributesRequest attribRequest = new ModifyLoadBalancerAttributesRequest();
    LoadBalancerAttributes attribs = new LoadBalancerAttributes();
    CrossZoneLoadBalancing crossZone = new CrossZoneLoadBalancing();
    crossZone.setEnabled(false);
    attribs.setCrossZoneLoadBalancing(crossZone);
    attribRequest.setLoadBalancerAttributes(attribs);
    attribRequest.setLoadBalancerName(loadBalancerName);
    elbClient.modifyLoadBalancerAttributes(attribRequest);

    // Specify Health Check
    String target = "HTTP:80/heartbeat?lg=" + URLEncoder.encode(loadGenerator, "UTF-8");
    ConfigureHealthCheckRequest healthCheckRequest = new ConfigureHealthCheckRequest();
    HealthCheck healthCheck = new HealthCheck(target, 5, 2, 2, 10);
    healthCheckRequest.setHealthCheck(healthCheck);
    healthCheckRequest.setLoadBalancerName(loadBalancerName);
    elbClient.configureHealthCheck(healthCheckRequest);

    System.out.println("Load balancer created with name: " + lbResult.getDNSName());

    return lbResult.getDNSName();
  }