Search in sources :

Example 1 with DescribeNetworkInterfacesResult

use of com.amazonaws.services.ec2.model.DescribeNetworkInterfacesResult 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 DescribeNetworkInterfacesResult

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

the class ElasticNetworkInterfaceBinder method bind.

/**
 * Binds an ENI to the instance.
 *
 * The candidate ENI's are deduced in the same wa the EIP binder works: Via dns records or via service urls,
 * depending on configuration.
 *
 * It will try to attach the first ENI that is:
 *      Available
 *      For this subnet
 *      In the list of candidate ENI's
 *
 * @throws MalformedURLException
 */
public void bind() throws MalformedURLException {
    InstanceInfo myInfo = ApplicationInfoManager.getInstance().getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.instanceId);
    String myZone = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.availabilityZone);
    final List<String> ips = getCandidateIps();
    Ordering<NetworkInterface> ipsOrder = Ordering.natural().onResultOf(new Function<NetworkInterface, Integer>() {

        public Integer apply(NetworkInterface networkInterface) {
            return ips.indexOf(networkInterface.getPrivateIpAddress());
        }
    });
    AmazonEC2 ec2Service = getEC2Service();
    String subnetId = instanceData(myInstanceId, ec2Service).getSubnetId();
    DescribeNetworkInterfacesResult result = ec2Service.describeNetworkInterfaces(new DescribeNetworkInterfacesRequest().withFilters(new Filter("private-ip-address", ips)).withFilters(new Filter("status", Lists.newArrayList("available"))).withFilters(new Filter("subnet-id", Lists.newArrayList(subnetId))));
    if (result.getNetworkInterfaces().isEmpty()) {
        logger.info("No ip is free to be associated with this instance. Candidate ips are: {} for zone: {}", ips, myZone);
    } else {
        NetworkInterface selected = ipsOrder.min(result.getNetworkInterfaces());
        ec2Service.attachNetworkInterface(new AttachNetworkInterfaceRequest().withNetworkInterfaceId(selected.getNetworkInterfaceId()).withDeviceIndex(1).withInstanceId(myInstanceId));
    }
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) InstanceInfo(com.netflix.appinfo.InstanceInfo) AmazonInfo(com.netflix.appinfo.AmazonInfo)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)1 DeleteNetworkInterfaceRequest (com.amazonaws.services.ec2.model.DeleteNetworkInterfaceRequest)1 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)1 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)1 DescribeNetworkInterfacesRequest (com.amazonaws.services.ec2.model.DescribeNetworkInterfacesRequest)1 DescribeNetworkInterfacesResult (com.amazonaws.services.ec2.model.DescribeNetworkInterfacesResult)1 DisassociateAddressRequest (com.amazonaws.services.ec2.model.DisassociateAddressRequest)1 Filter (com.amazonaws.services.ec2.model.Filter)1 ReleaseAddressRequest (com.amazonaws.services.ec2.model.ReleaseAddressRequest)1 AmazonInfo (com.netflix.appinfo.AmazonInfo)1 InstanceInfo (com.netflix.appinfo.InstanceInfo)1