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

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

Introduction

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

Prototype


public String getAssociationId() 

Source Link

Document

The ID representing the association of the address with an instance in a VPC.

Usage

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

License:Apache License

/**
 * Unbind the EIP that this instance is associated with.
 *///from   w w  w . j  a v  a 2 s . com
public void unbindEIP() throws Exception {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myPublicIP = null;
    if (myInfo != null && myInfo.getDataCenterInfo().getName() == Name.Amazon) {
        myPublicIP = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.publicIpv4);
        if (myPublicIP == null) {
            logger.info("Instance is not associated with an EIP. Will not try to unbind");
            return;
        }

        try {
            AmazonEC2 ec2Service = getEC2Service();
            DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest()
                    .withPublicIps(myPublicIP);
            DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
            if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
                Address eipAddress = result.getAddresses().get(0);
                DisassociateAddressRequest dissociateRequest = new DisassociateAddressRequest();
                String domain = eipAddress.getDomain();
                if ("vpc".equals(domain)) {
                    dissociateRequest.setAssociationId(eipAddress.getAssociationId());
                } else {
                    dissociateRequest.setPublicIp(eipAddress.getPublicIp());
                }

                ec2Service.disassociateAddress(dissociateRequest);
                logger.info("Dissociated the EIP {} from this instance", myPublicIP);
            }
        } catch (Throwable e) {
            throw new RuntimeException("Cannot dissociate address from this instance", e);
        }
    }

}

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

License:Apache License

/**
 * Unbind the EIP that this instance is associated with.
 *///from w  w  w  . j  av a2s.  c  o  m
public void unbindEIP() {
    InstanceInfo myInfo = ApplicationInfoManager.getInstance().getInfo();
    String myPublicIP = null;
    if (myInfo != null && myInfo.getDataCenterInfo().getName() == Name.Amazon) {
        myPublicIP = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.publicIpv4);
        try {
            AmazonEC2 ec2Service = getEC2Service();
            DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest()
                    .withPublicIps(myPublicIP);
            DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
            if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
                Address eipAddress = result.getAddresses().get(0);
                DisassociateAddressRequest dissociateRequest = new DisassociateAddressRequest();
                String domain = eipAddress.getDomain();
                if ("vpc".equals(domain)) {
                    dissociateRequest.setAssociationId(eipAddress.getAssociationId());
                } else {
                    dissociateRequest.setPublicIp(eipAddress.getPublicIp());
                }

                ec2Service.disassociateAddress(dissociateRequest);
                logger.info("Dissociated the EIP {} from this instance", myPublicIP);
            }
        } catch (Throwable e) {
            throw new RuntimeException("Cannot dissociate address" + myPublicIP + "from this instance", e);
        }
    }

}

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. ja v  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:jp.primecloud.auto.process.aws.AwsAddressProcess.java

License:Open Source License

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

    // ??/*ww w. j  av a  2 s. c  om*/
    DisassociateAddressRequest request = new DisassociateAddressRequest();

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

    awsProcessClient.getEc2Client().disassociateAddress(request);

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

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

    // ?
    awsAddress.setInstanceId(null);
    awsAddressDao.update(awsAddress);
}