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

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

Introduction

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

Prototype


public String getStatus() 

Source Link

Document

The status of the network interface.

Usage

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  ww .  j  av a2 s.c  o m
                } 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");
}