Search in sources :

Example 1 with AttachNetworkInterfaceRequest

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

Example 2 with AttachNetworkInterfaceRequest

use of com.amazonaws.services.ec2.model.AttachNetworkInterfaceRequest in project photon-model by vmware.

the class TestAWSSetupUtils method addNICDirectlyWithEC2Client.

/**
 * Attach a provided AWS NIC to a given AWS VM with deviceIndex = number of NICs + 1
 * returns the attachment ID of the newly created and attached NIC. This is necessary for
 * removing it later for the goals of the test. The NIC is as well configured to be deleted on
 * instance termination for sanity purposes.
 */
public static String addNICDirectlyWithEC2Client(ComputeState vm, AmazonEC2Client client, VerificationHost host, String newNicId) {
    // attach the new AWS NIC to the AWS VM
    AttachNetworkInterfaceRequest attachNewNic = new AttachNetworkInterfaceRequest().withInstanceId(vm.id).withDeviceIndex(vm.networkInterfaceLinks.size()).withNetworkInterfaceId(newNicId);
    AttachNetworkInterfaceResult attachmetnResult = client.attachNetworkInterface(attachNewNic);
    String attachmentId = attachmetnResult.getAttachmentId();
    // ensure the new NIC is deleted when the VM is terminated
    NetworkInterfaceAttachmentChanges attachTerm = new NetworkInterfaceAttachmentChanges().withAttachmentId(attachmentId).withDeleteOnTermination(true);
    ModifyNetworkInterfaceAttributeRequest setDeleteOnTerm = new ModifyNetworkInterfaceAttributeRequest().withAttachment(attachTerm).withNetworkInterfaceId(newNicId);
    client.modifyNetworkInterfaceAttribute(setDeleteOnTerm);
    host.log("Created new NIC with id: %s to vm id: %s with attachment id: %s", newNicId, vm.id, attachmentId);
    return attachmentId;
}
Also used : NetworkInterfaceAttachmentChanges(com.amazonaws.services.ec2.model.NetworkInterfaceAttachmentChanges) AttachNetworkInterfaceResult(com.amazonaws.services.ec2.model.AttachNetworkInterfaceResult) AttachNetworkInterfaceRequest(com.amazonaws.services.ec2.model.AttachNetworkInterfaceRequest) ModifyNetworkInterfaceAttributeRequest(com.amazonaws.services.ec2.model.ModifyNetworkInterfaceAttributeRequest)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)1 AttachNetworkInterfaceRequest (com.amazonaws.services.ec2.model.AttachNetworkInterfaceRequest)1 AttachNetworkInterfaceResult (com.amazonaws.services.ec2.model.AttachNetworkInterfaceResult)1 ModifyNetworkInterfaceAttributeRequest (com.amazonaws.services.ec2.model.ModifyNetworkInterfaceAttributeRequest)1 NetworkInterfaceAttachmentChanges (com.amazonaws.services.ec2.model.NetworkInterfaceAttachmentChanges)1 AmazonInfo (com.netflix.appinfo.AmazonInfo)1 InstanceInfo (com.netflix.appinfo.InstanceInfo)1