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

    if (obj instanceof Tag == false) return false;
    Tag other = (Tag) obj;
    if (other.getKey() == null ^ this.getKey() == null) return false;
    if (other.getKey() != null && other.getKey().equals(this.getKey()) == false) return false;
    if (other.getValue() == null ^ this.getValue() == null) return false;
    if (other.getValue() != null && other.getValue().equals(this.getValue()) == false) return false;
    return true;
  }
  public Request<CreateLoadBalancerRequest> marshall(
      CreateLoadBalancerRequest createLoadBalancerRequest) {

    if (createLoadBalancerRequest == null) {
      throw new AmazonClientException("Invalid argument passed to marshall(...)");
    }

    Request<CreateLoadBalancerRequest> request =
        new DefaultRequest<CreateLoadBalancerRequest>(
            createLoadBalancerRequest, "AmazonElasticLoadBalancing");
    request.addParameter("Action", "CreateLoadBalancer");
    request.addParameter("Version", "2012-06-01");
    request.setHttpMethod(HttpMethodName.POST);

    if (createLoadBalancerRequest.getLoadBalancerName() != null) {
      request.addParameter(
          "LoadBalancerName",
          StringUtils.fromString(createLoadBalancerRequest.getLoadBalancerName()));
    }

    com.amazonaws.internal.SdkInternalList<Listener> listenersList =
        (com.amazonaws.internal.SdkInternalList<Listener>) createLoadBalancerRequest.getListeners();
    if (!listenersList.isEmpty() || !listenersList.isAutoConstruct()) {
      int listenersListIndex = 1;

      for (Listener listenersListValue : listenersList) {

        if (listenersListValue.getProtocol() != null) {
          request.addParameter(
              "Listeners.member." + listenersListIndex + ".Protocol",
              StringUtils.fromString(listenersListValue.getProtocol()));
        }

        if (listenersListValue.getLoadBalancerPort() != null) {
          request.addParameter(
              "Listeners.member." + listenersListIndex + ".LoadBalancerPort",
              StringUtils.fromInteger(listenersListValue.getLoadBalancerPort()));
        }

        if (listenersListValue.getInstanceProtocol() != null) {
          request.addParameter(
              "Listeners.member." + listenersListIndex + ".InstanceProtocol",
              StringUtils.fromString(listenersListValue.getInstanceProtocol()));
        }

        if (listenersListValue.getInstancePort() != null) {
          request.addParameter(
              "Listeners.member." + listenersListIndex + ".InstancePort",
              StringUtils.fromInteger(listenersListValue.getInstancePort()));
        }

        if (listenersListValue.getSSLCertificateId() != null) {
          request.addParameter(
              "Listeners.member." + listenersListIndex + ".SSLCertificateId",
              StringUtils.fromString(listenersListValue.getSSLCertificateId()));
        }
        listenersListIndex++;
      }
    }

    com.amazonaws.internal.SdkInternalList<String> availabilityZonesList =
        (com.amazonaws.internal.SdkInternalList<String>)
            createLoadBalancerRequest.getAvailabilityZones();
    if (!availabilityZonesList.isEmpty() || !availabilityZonesList.isAutoConstruct()) {
      int availabilityZonesListIndex = 1;

      for (String availabilityZonesListValue : availabilityZonesList) {
        if (availabilityZonesListValue != null) {
          request.addParameter(
              "AvailabilityZones.member." + availabilityZonesListIndex,
              StringUtils.fromString(availabilityZonesListValue));
        }
        availabilityZonesListIndex++;
      }
    }

    com.amazonaws.internal.SdkInternalList<String> subnetsList =
        (com.amazonaws.internal.SdkInternalList<String>) createLoadBalancerRequest.getSubnets();
    if (!subnetsList.isEmpty() || !subnetsList.isAutoConstruct()) {
      int subnetsListIndex = 1;

      for (String subnetsListValue : subnetsList) {
        if (subnetsListValue != null) {
          request.addParameter(
              "Subnets.member." + subnetsListIndex, StringUtils.fromString(subnetsListValue));
        }
        subnetsListIndex++;
      }
    }

    com.amazonaws.internal.SdkInternalList<String> securityGroupsList =
        (com.amazonaws.internal.SdkInternalList<String>)
            createLoadBalancerRequest.getSecurityGroups();
    if (!securityGroupsList.isEmpty() || !securityGroupsList.isAutoConstruct()) {
      int securityGroupsListIndex = 1;

      for (String securityGroupsListValue : securityGroupsList) {
        if (securityGroupsListValue != null) {
          request.addParameter(
              "SecurityGroups.member." + securityGroupsListIndex,
              StringUtils.fromString(securityGroupsListValue));
        }
        securityGroupsListIndex++;
      }
    }

    if (createLoadBalancerRequest.getScheme() != null) {
      request.addParameter("Scheme", StringUtils.fromString(createLoadBalancerRequest.getScheme()));
    }

    com.amazonaws.internal.SdkInternalList<Tag> tagsList =
        (com.amazonaws.internal.SdkInternalList<Tag>) createLoadBalancerRequest.getTags();
    if (!tagsList.isEmpty() || !tagsList.isAutoConstruct()) {
      int tagsListIndex = 1;

      for (Tag tagsListValue : tagsList) {

        if (tagsListValue.getKey() != null) {
          request.addParameter(
              "Tags.member." + tagsListIndex + ".Key",
              StringUtils.fromString(tagsListValue.getKey()));
        }

        if (tagsListValue.getValue() != null) {
          request.addParameter(
              "Tags.member." + tagsListIndex + ".Value",
              StringUtils.fromString(tagsListValue.getValue()));
        }
        tagsListIndex++;
      }
    }

    return request;
  }
  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();
  }