Search in sources :

Example 1 with DisassociateAddressRequest

use of com.amazonaws.services.ec2.model.DisassociateAddressRequest in project eureka by Netflix.

the class EIPManager method unbindEIP.

/**
     * Unbind the EIP that this instance is associated with.
     */
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);
        }
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) DisassociateAddressRequest(com.amazonaws.services.ec2.model.DisassociateAddressRequest) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)1 Address (com.amazonaws.services.ec2.model.Address)1 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)1 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)1 DisassociateAddressRequest (com.amazonaws.services.ec2.model.DisassociateAddressRequest)1 InstanceInfo (com.netflix.appinfo.InstanceInfo)1