Example usage for com.amazonaws.services.ec2.model DetachNetworkInterfaceRequest DetachNetworkInterfaceRequest

List of usage examples for com.amazonaws.services.ec2.model DetachNetworkInterfaceRequest DetachNetworkInterfaceRequest

Introduction

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

Prototype

DetachNetworkInterfaceRequest

Source Link

Usage

From source file:com.urbancode.terraform.tasks.aws.helpers.AWSHelper.java

License:Apache License

/**
 *
 * @param attachIds/*from  w  w  w .j av  a  2  s .c o  m*/
 * @param ec2Client
 */
public void detachNetworkInterfaces(List<String> attachIds, AmazonEC2 ec2Client) {
    if (attachIds != null) {
        for (String attachId : attachIds) {
            DetachNetworkInterfaceRequest request = new DetachNetworkInterfaceRequest()
                    .withAttachmentId(attachId);
            ec2Client.detachNetworkInterface(request);
        }
    }
}

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;/*from  w ww  .  j av  a 2  s.  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");
}