Search in sources :

Example 1 with DetachNetworkInterfaceRequest

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

the class TestAWSSetupUtils method detachNICDirectlyWithEC2Client.

/**
 * Removes a specified AWS NIC from the VM it is currently attached to
 */
public static void detachNICDirectlyWithEC2Client(String instanceId, String nicAttachmentId, String nicId, AmazonEC2Client client, VerificationHost host) {
    // detach the new AWS NIC to the AWS VM
    DetachNetworkInterfaceRequest detachNic = new DetachNetworkInterfaceRequest();
    detachNic.withAttachmentId(nicAttachmentId);
    host.log("Detaching NIC with id: %s and attachment id: %s", nicId, nicAttachmentId);
    client.detachNetworkInterface(detachNic);
    host.waitFor("Timeout waiting for AWS to detach a NIC from " + instanceId + " with attachment id: " + nicAttachmentId, () -> {
        DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withFilters(new Filter("instance-id", Collections.singletonList(instanceId)));
        DescribeInstancesResult result = client.describeInstances(describeInstancesRequest);
        Instance currentInstance = result.getReservations().get(0).getInstances().get(0);
        for (InstanceNetworkInterface awsNic : currentInstance.getNetworkInterfaces()) {
            if (awsNic.getNetworkInterfaceId().equals(nicId)) {
                // Requested NIC was not detached from the instance
                return false;
            }
        }
        host.log("Detached NIC with attachment id: %s", nicAttachmentId);
        return true;
    });
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AWSUtils.getAWSNonTerminatedInstancesFilter(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) DetachNetworkInterfaceRequest(com.amazonaws.services.ec2.model.DetachNetworkInterfaceRequest) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) InstanceNetworkInterface(com.amazonaws.services.ec2.model.InstanceNetworkInterface)

Example 2 with DetachNetworkInterfaceRequest

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

the class ElasticNetworkInterfaceBinder method unbind.

/**
     * Unbind the IP that this instance is associated with.
     */
public void unbind() throws Exception {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.instanceId);
    AmazonEC2 ec2 = getEC2Service();
    List<InstanceNetworkInterface> result = instanceData(myInstanceId, ec2).getNetworkInterfaces();
    List<String> ips = getCandidateIps();
    for (InstanceNetworkInterface networkInterface : result) {
        if (ips.contains(networkInterface.getPrivateIpAddress())) {
            String attachmentId = networkInterface.getAttachment().getAttachmentId();
            ec2.detachNetworkInterface(new DetachNetworkInterfaceRequest().withAttachmentId(attachmentId));
            break;
        }
    }
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)1 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)1 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)1 DetachNetworkInterfaceRequest (com.amazonaws.services.ec2.model.DetachNetworkInterfaceRequest)1 Filter (com.amazonaws.services.ec2.model.Filter)1 Instance (com.amazonaws.services.ec2.model.Instance)1 InstanceNetworkInterface (com.amazonaws.services.ec2.model.InstanceNetworkInterface)1 AmazonInfo (com.netflix.appinfo.AmazonInfo)1 InstanceInfo (com.netflix.appinfo.InstanceInfo)1 AWSUtils.getAWSNonTerminatedInstancesFilter (com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter)1