Example usage for com.amazonaws.services.ec2.model NetworkInterface getAttachment

List of usage examples for com.amazonaws.services.ec2.model NetworkInterface getAttachment

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model NetworkInterface getAttachment.

Prototype


public NetworkInterfaceAttachment getAttachment() 

Source Link

Document

The network interface attachment.

Usage

From source file:com.urbancode.terraform.tasks.aws.EnvironmentTaskAWS.java

License:Apache License

private void detachENIs() {
    // detach any ENIs that we can
    if (getVpc() != null && getVpc().getId() != null) {
        List<NetworkInterface> interfaces = helper.getNetworkInterfaces(null, getVpc().getId(), ec2Client);
        List<String> detachIds = new ArrayList<String>();
        if (interfaces != null) {
            // refactor this into helper?
            for (NetworkInterface iface : interfaces) {
                if (iface.getAttachment() != null && iface.getAttachment().getDeviceIndex() != 0) {
                    detachIds.add(iface.getAttachment().getAttachmentId());
                }//from w ww.j  a  v a  2 s . co  m
            }
            helper.detachNetworkInterfaces(detachIds, ec2Client);
        }
    }
}

From source file:dsmwatcher.DSMWatcher.java

License:Open Source License

public void removeIsolation(Instance instance, AmazonEC2Client ec2) throws Exception {
    List<InstanceNetworkInterface> ienis = instance.getNetworkInterfaces();
    for (InstanceNetworkInterface ieni : ienis) {
        DescribeNetworkInterfacesRequest netReq = new DescribeNetworkInterfacesRequest()
                .withNetworkInterfaceIds(ieni.getNetworkInterfaceId());
        DescribeNetworkInterfacesResult netResult = ec2.describeNetworkInterfaces(netReq);
        List<com.amazonaws.services.ec2.model.NetworkInterface> enis = netResult.getNetworkInterfaces();
        for (com.amazonaws.services.ec2.model.NetworkInterface eni : enis) {
            List<Tag> tagSet = eni.getTagSet();
            List<Tag> tagSetRemove = new LinkedList<Tag>();
            boolean isolatedENI = false;
            boolean IRENI = false;
            String origSecGroups = null;
            List<String> origSecGroupsList = new ArrayList<String>();

            for (Tag tag : tagSet) {
                if (tag.getKey().compareTo("PreIsolationSG") == 0) {
                    origSecGroups = tag.getValue();
                    tagSetRemove.add(tag);
                    isolatedENI = true;//w  w w. j  a v a  2s . c om
                } else if (tag.getKey().compareTo("InIsolation") == 0) {
                    tagSetRemove.add(tag);
                } else if (tag.getKey().compareTo("IRENI") == 0) {
                    IRENI = true;
                }
            }
            if (isolatedENI) {
                for (String s : origSecGroups.split(",")) {
                    origSecGroupsList.add(s);
                }

                ModifyNetworkInterfaceAttributeRequest netReqest = new ModifyNetworkInterfaceAttributeRequest()
                        .withNetworkInterfaceId(eni.getNetworkInterfaceId()).withGroups(origSecGroupsList);
                ec2.modifyNetworkInterfaceAttribute(netReqest);
                DeleteTagsRequest dtr = new DeleteTagsRequest().withResources(eni.getNetworkInterfaceId())
                        .withTags(tagSetRemove);
                ec2.deleteTags(dtr);
            }
            if (IRENI) {
                DetachNetworkInterfaceRequest detachNetworkInterfaceRequest = new DetachNetworkInterfaceRequest()
                        .withAttachmentId(eni.getAttachment().getAttachmentId());
                ec2.detachNetworkInterface(detachNetworkInterfaceRequest);
                TimeUnit.SECONDS.sleep(30);
                if (eni.getStatus().compareTo("available") != 0) { //detach is taking awhile, wait another 30 seconds
                    TimeUnit.SECONDS.sleep(30);
                }
                DeleteNetworkInterfaceRequest deleteNetworkInterfaceRequest = new DeleteNetworkInterfaceRequest()
                        .withNetworkInterfaceId(eni.getNetworkInterfaceId());
                ec2.deleteNetworkInterface(deleteNetworkInterfaceRequest);
            }
        }
    }
    log("Instance " + instance.getInstanceId() + " with IP address of " + instance.getPrivateIpAddress()
            + " has been removed from isolation");
}