Example usage for com.amazonaws.services.ec2.model Address getAllocationId

List of usage examples for com.amazonaws.services.ec2.model Address getAllocationId

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model Address getAllocationId.

Prototype


public String getAllocationId() 

Source Link

Document

The ID representing the allocation of the address for use with EC2-VPC.

Usage

From source file:aws.example.ec2.DescribeAddresses.java

License:Open Source License

public static void main(String[] args) {
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    DescribeAddressesResult response = ec2.describeAddresses();

    for (Address address : response.getAddresses()) {
        System.out.printf(/*from   w ww .  j  a  va 2 s.c o m*/
                "Found address with public IP %s, " + "domain %s, " + "allocation id %s " + "and NIC id %s",
                address.getPublicIp(), address.getDomain(), address.getAllocationId(),
                address.getNetworkInterfaceId());
    }
}

From source file:com.hpcloud.daas.ec2.AwsConsoleApp.java

License:Open Source License

public static void cleanupPublicIPs() {
    DescribeAddressesResult result = ec2.describeAddresses();
    List<Address> addresses = result.getAddresses();

    for (Address address : addresses) {

        ReleaseAddressRequest releaseReq = new ReleaseAddressRequest();
        releaseReq.setAllocationId(address.getAllocationId());
        releaseReq.setPublicIp(address.getPublicIp());

        if (address.getInstanceId() == null || address.getInstanceId().equals("")) {
            ec2.releaseAddress(releaseReq);
        }//w  ww . j  a  v  a 2  s .co m
    }
}

From source file:com.netflix.eureka.aws.EIPManager.java

License:Apache License

/**
 * Checks if an EIP is bound and optionally binds the EIP.
 *
 * The list of EIPs are arranged with the EIPs allocated in the zone first
 * followed by other EIPs.//from  w  ww. j  a  v a  2 s  . c om
 *
 * If an EIP is already bound to this instance this method simply returns. Otherwise, this method tries to find
 * an unused EIP based on information from AWS. If it cannot find any unused EIP this method, it will be retried
 * for a specified interval.
 *
 * One of the following scenarios can happen here :
 *
 *  1) If the instance is already bound to an EIP as deemed by AWS, no action is taken.
 *  2) If an EIP is already bound to another instance as deemed by AWS, that EIP is skipped.
 *  3) If an EIP is not already bound to an instance and if this instance is not bound to an EIP, then
 *     the EIP is bound to this instance.
 */
public void bindEIP() {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.instanceId);
    String myZone = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.availabilityZone);

    Collection<String> candidateEIPs = getCandidateEIPs(myInstanceId, myZone);

    AmazonEC2 ec2Service = getEC2Service();
    boolean isMyinstanceAssociatedWithEIP = false;
    Address selectedEIP = null;

    for (String eipEntry : candidateEIPs) {
        try {
            String associatedInstanceId;

            // Check with AWS, if this EIP is already been used by another instance
            DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest()
                    .withPublicIps(eipEntry);
            DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
            if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
                Address eipAddress = result.getAddresses().get(0);
                associatedInstanceId = eipAddress.getInstanceId();
                // This EIP is not used by any other instance, hence mark it for selection if it is not
                // already marked.
                if (((associatedInstanceId == null) || (associatedInstanceId.isEmpty()))) {
                    if (selectedEIP == null) {
                        selectedEIP = eipAddress;
                    }
                } else if (isMyinstanceAssociatedWithEIP = (associatedInstanceId.equals(myInstanceId))) {
                    // This EIP is associated with an instance, check if this is the same as the current instance.
                    // If it is the same, stop searching for an EIP as this instance is already associated with an
                    // EIP
                    selectedEIP = eipAddress;
                    break;
                } else {
                    // The EIP is used by some other instance, hence skip it
                    logger.warn("The selected EIP {} is associated with another instance {} according to AWS,"
                            + " hence skipping this", eipEntry, associatedInstanceId);
                }
            }
        } catch (Throwable t) {
            logger.error("Failed to bind elastic IP: {} to {}", eipEntry, myInstanceId, t);
        }
    }
    if (null != selectedEIP) {
        String publicIp = selectedEIP.getPublicIp();
        // Only bind if the EIP is not already associated
        if (!isMyinstanceAssociatedWithEIP) {

            AssociateAddressRequest associateAddressRequest = new AssociateAddressRequest()
                    .withInstanceId(myInstanceId);

            String domain = selectedEIP.getDomain();
            if ("vpc".equals(domain)) {
                associateAddressRequest.setAllocationId(selectedEIP.getAllocationId());
            } else {
                associateAddressRequest.setPublicIp(publicIp);
            }

            ec2Service.associateAddress(associateAddressRequest);
            logger.info("\n\n\nAssociated {} running in zone: {} to elastic IP: {}", myInstanceId, myZone,
                    publicIp);
        }
        logger.info("My instance {} seems to be already associated with the EIP {}", myInstanceId, publicIp);
    } else {
        logger.info("No EIP is free to be associated with this instance. Candidate EIPs are: {}",
                candidateEIPs);
    }
}

From source file:com.netflix.eureka.util.EIPManager.java

License:Apache License

/**
 * Checks if an EIP is bound and optionally binds the EIP.
 *
 * The list of EIPs are arranged with the EIPs allocated in the zone first
 * followed by other EIPs./*from   w  w  w . j a v  a2s.  c  o m*/
 *
 * If an EIP is already bound to this instance this method simply returns. Otherwise, this method tries to find
 * an unused EIP based on information from AWS. If it cannot find any unused EIP this method, it will be retried
 * for a specified interval.
 *
 * One of the following scenarios can happen here :
 *
 *  1) If the instance is already bound to an EIP as deemed by AWS, no action is taken.
 *  2) If an EIP is already bound to another instance as deemed by AWS, that EIP is skipped.
 *  3) If an EIP is not already bound to an instance and if this instance is not bound to an EIP, then
 *     the EIP is bound to this instance.
 */
public void bindEIP() {
    InstanceInfo myInfo = ApplicationInfoManager.getInstance().getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.instanceId);
    String myZone = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.availabilityZone);

    Collection<String> candidateEIPs = getCandidateEIPs(myInstanceId, myZone);

    AmazonEC2 ec2Service = getEC2Service();
    boolean isMyinstanceAssociatedWithEIP = false;
    Address selectedEIP = null;

    for (String eipEntry : candidateEIPs) {
        try {
            String associatedInstanceId;

            // Check with AWS, if this EIP is already been used by another instance
            DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest()
                    .withPublicIps(eipEntry);
            DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
            if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
                Address eipAddress = result.getAddresses().get(0);
                associatedInstanceId = eipAddress.getInstanceId();
                // This EIP is not used by any other instance, hence mark it for selection if it is not
                // already marked.
                if (((associatedInstanceId == null) || (associatedInstanceId.isEmpty()))) {
                    if (selectedEIP == null) {
                        selectedEIP = eipAddress;
                    }
                } else if (isMyinstanceAssociatedWithEIP = (associatedInstanceId.equals(myInstanceId))) {
                    // This EIP is associated with an instance, check if this is the same as the current instance.
                    // If it is the same, stop searching for an EIP as this instance is already associated with an
                    // EIP
                    selectedEIP = eipAddress;
                    break;
                } else {
                    // The EIP is used by some other instance, hence skip it
                    logger.warn(
                            "The selected EIP {} is associated with another instance {} according to AWS, hence "
                                    + "skipping this",
                            eipEntry, associatedInstanceId);
                }
            }
        } catch (Throwable t) {
            logger.error("Failed to bind elastic IP: " + eipEntry + " to " + myInstanceId, t);
        }
    }
    if (null != selectedEIP) {
        String publicIp = selectedEIP.getPublicIp();
        // Only bind if the EIP is not already associated
        if (!isMyinstanceAssociatedWithEIP) {

            AssociateAddressRequest associateAddressRequest = new AssociateAddressRequest()
                    .withInstanceId(myInstanceId);

            String domain = selectedEIP.getDomain();
            if ("vpc".equals(domain)) {
                associateAddressRequest.setAllocationId(selectedEIP.getAllocationId());
            } else {
                associateAddressRequest.setPublicIp(publicIp);
            }

            ec2Service.associateAddress(associateAddressRequest);
            logger.info("\n\n\nAssociated " + myInstanceId + " running in zone: " + myZone + " to elastic IP: "
                    + publicIp);
        }
        logger.info("My instance {} seems to be already associated with the EIP {}", myInstanceId, publicIp);
    } else {
        logger.info("No EIP is free to be associated with this instance. Candidate EIPs are: " + candidateEIPs);
    }
}

From source file:com.urbancode.terraform.tasks.aws.helpers.AWSHelper.java

License:Apache License

/**
 * Cleans up any elastic ips that are not associated with anything (via associationId).
 * The addresses are released.//from  ww w  . j  av  a  2  s .c o  m
 *
 * @param ec2Client
 */
public void cleanupElasticIps(AmazonEC2 ec2Client) {
    DescribeAddressesResult result = ec2Client.describeAddresses();
    List<Address> addresses = result.getAddresses();
    if (addresses != null) {
        for (Address address : addresses) {
            if (address.getAssociationId() != null && !address.getAssociationId().equals("")) {
                ReleaseAddressRequest request = new ReleaseAddressRequest()
                        .withAllocationId(address.getAllocationId());
                ec2Client.releaseAddress(request);
            }
        }
    }
}

From source file:ec2.DescribeAddresses.java

License:Open Source License

public static void main(String[] args) {

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    DescribeAddressesResult response = ec2.describeAddresses();

    for (Address address : response.getAddresses()) {
        System.out.printf("Found address with public IP %s, domain %s, allocation id %s and NIC id %s",
                address.getPublicIp(), address.getDomain(), address.getAllocationId(),
                address.getNetworkInterfaceId());
    }/*  www  .  jav a 2s.c  om*/
}

From source file:edu.umass.cs.aws.support.AWSEC2.java

License:Apache License

/**
 *
 * @param ec2/*from  w ww . j  av a 2s .c o m*/
 * @param ip
 * @param instance
 */
public static void associateAddress(AmazonEC2 ec2, String ip, Instance instance) {
    Address address;
    if ((address = findElasticIP(ec2, ip)) != null) {
        if (address.getDomain().equals("vpc")) {
            System.out.println("VPC Elastic IP:  " + ip);
            ec2.associateAddress(new AssociateAddressRequest().withInstanceId(instance.getInstanceId())
                    .withAllocationId(address.getAllocationId()));
        } else {
            System.out.println("EC2 Classic Elastic IP:  " + ip);
            ec2.associateAddress(new AssociateAddressRequest(instance.getInstanceId(), ip));
        }
    }
}

From source file:jp.primecloud.auto.process.aws.AwsAddressProcess.java

License:Open Source License

/**
 * TODO: //ww  w. j  av a  2 s.c  o  m
 * 
 * @param awsProcessClient
 * @param addressNo
 */
public void deleteAddress(AwsProcessClient awsProcessClient, Long addressNo) {
    // AWS??
    AwsAddress awsAddress = awsAddressDao.read(addressNo);
    if (awsAddress == null) {
        return;
    }

    // Elastic IP??
    if (StringUtils.isEmpty(awsAddress.getPublicIp())) {
        // Elastic IP??AWS??
        awsAddressDao.delete(awsAddress);
        return;
    }

    // Elastic IP
    try {
        ReleaseAddressRequest request = new ReleaseAddressRequest();

        // VPC??
        if (BooleanUtils.isTrue(awsProcessClient.getPlatformAws().getVpc())) {
            // ?ID??
            Address address = awsCommonProcess.describeAddress(awsProcessClient, awsAddress.getPublicIp());
            request.withAllocationId(address.getAllocationId());
        }
        // ?VPC??
        else {
            request.withPublicIp(awsAddress.getPublicIp());
        }

        awsProcessClient.getEc2Client().releaseAddress(request);

        // 
        processLogger.debug(null, null, "AwsElasticIpRelease",
                new Object[] { awsProcessClient.getPlatform().getPlatformName(), awsAddress.getPublicIp() });

    } catch (Exception ignore) {
        // Elastic IP???????????????????
        log.warn(ignore.getMessage());
    }

    // AWS
    awsAddressDao.delete(awsAddress);
}

From source file:jp.primecloud.auto.process.aws.AwsAddressProcess.java

License:Open Source License

public void associateAddress(AwsProcessClient awsProcessClient, Long instanceNo, Long addressNo,
        Address address) {
    AwsAddress awsAddress = awsAddressDao.read(addressNo);
    AwsInstance awsInstance = awsInstanceDao.read(instanceNo);

    // ??/*from   w ww .j  a  va  2 s  .c o m*/
    AssociateAddressRequest request = new AssociateAddressRequest();
    request.withInstanceId(awsInstance.getInstanceId());

    // VPC??
    if (BooleanUtils.isTrue(awsProcessClient.getPlatformAws().getVpc())) {
        // ?ID?
        request.withAllocationId(address.getAllocationId());
    }
    // ?VPC??
    else {
        request.withPublicIp(awsAddress.getPublicIp());
    }

    awsProcessClient.getEc2Client().associateAddress(request);

    // 
    if (log.isInfoEnabled()) {
        log.info(MessageUtils.getMessage("IPROCESS-100131", awsAddress.getPublicIp(),
                awsInstance.getInstanceId()));
    }

    // 
    Instance instance2 = instanceDao.read(instanceNo);
    processLogger.debug(null, instance2, "AwsElasticIpAssociate",
            new Object[] { awsInstance.getInstanceId(), awsAddress.getPublicIp() });

    // ?
    awsAddress.setInstanceId(awsInstance.getInstanceId());
    awsAddressDao.update(awsAddress);
}