Search in sources :

Example 1 with ReleaseAddressRequest

use of software.amazon.awssdk.services.ec2.model.ReleaseAddressRequest in project photon-model by vmware.

the class AWSRemoteCleanup method deleteNetworkInterfaces.

private void deleteNetworkInterfaces(String vpcId, AmazonEC2 usEastEc2Client) {
    DescribeNetworkInterfacesRequest networkInterfacesRequest = new DescribeNetworkInterfacesRequest().withFilters(new Filter(VPC_KEY, Collections.singletonList(vpcId)));
    DescribeNetworkInterfacesResult networkInterfacesResult = usEastEc2Client.describeNetworkInterfaces(networkInterfacesRequest);
    networkInterfacesResult.getNetworkInterfaces().forEach(networkInterface -> {
        DescribeAddressesRequest addressesRequest = new DescribeAddressesRequest().withFilters(new Filter(NETWORK_INTERFACE_KEY, Collections.singletonList(networkInterface.getNetworkInterfaceId())));
        DescribeAddressesResult addressResult = usEastEc2Client.describeAddresses(addressesRequest);
        addressResult.getAddresses().forEach(address -> {
            // There is no hardcore dependency on EIP, but we may run out of addresses and
            // would be good to disassociate followed by releasing them.
            DisassociateAddressRequest disassociateAddressRequest = new DisassociateAddressRequest().withAssociationId(address.getAssociationId());
            usEastEc2Client.disassociateAddress(disassociateAddressRequest);
            ReleaseAddressRequest releaseAddressRequest = new ReleaseAddressRequest().withAllocationId(address.getAllocationId());
            usEastEc2Client.releaseAddress(releaseAddressRequest);
        });
        // Deleting Network Interfaces
        DeleteNetworkInterfaceRequest deleteNetworkInterfaceRequest = new DeleteNetworkInterfaceRequest().withNetworkInterfaceId(networkInterface.getNetworkInterfaceId());
        this.host.log("Terminating stale NIC: %s", networkInterface.getNetworkInterfaceId());
        usEastEc2Client.deleteNetworkInterface(deleteNetworkInterfaceRequest);
    });
}
Also used : Filter(com.amazonaws.services.ec2.model.Filter) DisassociateAddressRequest(com.amazonaws.services.ec2.model.DisassociateAddressRequest) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) DescribeNetworkInterfacesResult(com.amazonaws.services.ec2.model.DescribeNetworkInterfacesResult) DescribeNetworkInterfacesRequest(com.amazonaws.services.ec2.model.DescribeNetworkInterfacesRequest) ReleaseAddressRequest(com.amazonaws.services.ec2.model.ReleaseAddressRequest) DeleteNetworkInterfaceRequest(com.amazonaws.services.ec2.model.DeleteNetworkInterfaceRequest)

Example 2 with ReleaseAddressRequest

use of software.amazon.awssdk.services.ec2.model.ReleaseAddressRequest in project photon-model by vmware.

the class AWSNetworkClient method releaseElasticIPAddress.

/**
 * Release an elastic IP address
 */
public DeferredResult<Void> releaseElasticIPAddress(String allocationId) {
    ReleaseAddressRequest req = new ReleaseAddressRequest().withAllocationId(allocationId);
    String message = "Release AWS Elastic IP Address with allocation id [" + allocationId + "].";
    AWSDeferredResultAsyncHandler<ReleaseAddressRequest, ReleaseAddressResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message);
    this.client.releaseAddressAsync(req, handler);
    return handler.toDeferredResult().thenApply(ignore -> null);
}
Also used : ReleaseAddressResult(com.amazonaws.services.ec2.model.ReleaseAddressResult) ReleaseAddressRequest(com.amazonaws.services.ec2.model.ReleaseAddressRequest)

Example 3 with ReleaseAddressRequest

use of software.amazon.awssdk.services.ec2.model.ReleaseAddressRequest in project aws-doc-sdk-examples by awsdocs.

the class ReleaseAddress method releaseEC2Address.

// snippet-start:[ec2.java2.release_instance.main]
public static void releaseEC2Address(Ec2Client ec2, String allocId) {
    try {
        ReleaseAddressRequest request = ReleaseAddressRequest.builder().allocationId(allocId).build();
        ReleaseAddressResponse response = ec2.releaseAddress(request);
        System.out.printf("Successfully released elastic IP address %s", allocId);
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : ReleaseAddressResponse(software.amazon.awssdk.services.ec2.model.ReleaseAddressResponse) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) ReleaseAddressRequest(software.amazon.awssdk.services.ec2.model.ReleaseAddressRequest)

Example 4 with ReleaseAddressRequest

use of software.amazon.awssdk.services.ec2.model.ReleaseAddressRequest in project aws-doc-sdk-examples by awsdocs.

the class ReleaseAddress method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply an allocation ID.\n" + "Ex: ReleaseAddress <allocation_id>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String alloc_id = args[0];
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    ReleaseAddressRequest request = new ReleaseAddressRequest().withAllocationId(alloc_id);
    ReleaseAddressResult response = ec2.releaseAddress(request);
    System.out.printf("Successfully released elastic IP address %s", alloc_id);
}
Also used : ReleaseAddressResult(com.amazonaws.services.ec2.model.ReleaseAddressResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) ReleaseAddressRequest(com.amazonaws.services.ec2.model.ReleaseAddressRequest)

Example 5 with ReleaseAddressRequest

use of software.amazon.awssdk.services.ec2.model.ReleaseAddressRequest in project cloudbreak by hortonworks.

the class AwsResourceConnector method releaseReservedIp.

private void releaseReservedIp(AmazonEC2 client, Iterable<CloudResource> resources) {
    CloudResource elasticIpResource = getReservedIp(resources);
    if (elasticIpResource != null && elasticIpResource.getName() != null) {
        Address address;
        try {
            DescribeAddressesResult describeResult = client.describeAddresses(new DescribeAddressesRequest().withAllocationIds(elasticIpResource.getName()));
            address = describeResult.getAddresses().get(0);
        } catch (AmazonServiceException e) {
            if (e.getErrorMessage().equals("The allocation ID '" + elasticIpResource.getName() + "' does not exist")) {
                LOGGER.warn("Elastic IP with allocation ID '{}' not found. Ignoring IP release.", elasticIpResource.getName());
                return;
            } else {
                throw e;
            }
        }
        if (address.getAssociationId() != null) {
            client.disassociateAddress(new DisassociateAddressRequest().withAssociationId(elasticIpResource.getName()));
        }
        client.releaseAddress(new ReleaseAddressRequest().withAllocationId(elasticIpResource.getName()));
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) DisassociateAddressRequest(com.amazonaws.services.ec2.model.DisassociateAddressRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ReleaseAddressRequest(com.amazonaws.services.ec2.model.ReleaseAddressRequest)

Aggregations

ReleaseAddressRequest (com.amazonaws.services.ec2.model.ReleaseAddressRequest)4 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)2 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)2 DisassociateAddressRequest (com.amazonaws.services.ec2.model.DisassociateAddressRequest)2 ReleaseAddressResult (com.amazonaws.services.ec2.model.ReleaseAddressResult)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)1 Address (com.amazonaws.services.ec2.model.Address)1 DeleteNetworkInterfaceRequest (com.amazonaws.services.ec2.model.DeleteNetworkInterfaceRequest)1 DescribeNetworkInterfacesRequest (com.amazonaws.services.ec2.model.DescribeNetworkInterfacesRequest)1 DescribeNetworkInterfacesResult (com.amazonaws.services.ec2.model.DescribeNetworkInterfacesResult)1 Filter (com.amazonaws.services.ec2.model.Filter)1 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)1 Ec2Exception (software.amazon.awssdk.services.ec2.model.Ec2Exception)1 ReleaseAddressRequest (software.amazon.awssdk.services.ec2.model.ReleaseAddressRequest)1 ReleaseAddressResponse (software.amazon.awssdk.services.ec2.model.ReleaseAddressResponse)1