Example usage for com.amazonaws.services.ec2.model Instance getNetworkInterfaces

List of usage examples for com.amazonaws.services.ec2.model Instance getNetworkInterfaces

Introduction

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

Prototype


public java.util.List<InstanceNetworkInterface> getNetworkInterfaces() 

Source Link

Document

[EC2-VPC] The network interfaces for the instance.

Usage

From source file:com.clouck.model.aws.ec2.Ec2Instance.java

@Override
@SuppressWarnings("rawtypes")
protected boolean isEqual(AbstractResource newResource) {
    Instance oldInstance = this.getResource();
    Ec2Instance newEc2Instance = (Ec2Instance) newResource;
    Instance newInstance = newEc2Instance.getResource();

    if (notEqual(oldInstance.getInstanceId(), newInstance.getInstanceId()))
        return false;
    if (notEqual(oldInstance.getImageId(), newInstance.getImageId()))
        return false;
    if (notEqual(oldInstance.getState(), newInstance.getState()))
        return false;
    if (notEqual(oldInstance.getPrivateDnsName(), newInstance.getPrivateDnsName()))
        return false;
    if (notEqual(oldInstance.getPublicDnsName(), newInstance.getPublicDnsName()))
        return false;
    if (notEqual(oldInstance.getStateTransitionReason(), newInstance.getStateTransitionReason()))
        return false;
    if (notEqual(oldInstance.getKeyName(), newInstance.getKeyName()))
        return false;
    if (notEqual(oldInstance.getAmiLaunchIndex(), newInstance.getAmiLaunchIndex()))
        return false;
    if (notEqualCollection(oldInstance.getProductCodes(), newInstance.getProductCodes()))
        return false;
    if (notEqual(oldInstance.getInstanceType(), newInstance.getInstanceType()))
        return false;
    if (notEqual(oldInstance.getLaunchTime(), newInstance.getLaunchTime()))
        return false;
    if (notEqual(oldInstance.getPlacement(), newInstance.getPlacement()))
        return false;
    if (notEqual(oldInstance.getKernelId(), newInstance.getKernelId()))
        return false;
    if (notEqual(oldInstance.getRamdiskId(), newInstance.getRamdiskId()))
        return false;
    if (notEqual(oldInstance.getPlatform(), newInstance.getPlatform()))
        return false;
    if (notEqual(oldInstance.getMonitoring(), newInstance.getMonitoring()))
        return false;
    if (notEqual(oldInstance.getSubnetId(), newInstance.getSubnetId()))
        return false;
    if (notEqual(oldInstance.getVpcId(), newInstance.getVpcId()))
        return false;
    if (notEqual(oldInstance.getPrivateIpAddress(), newInstance.getPrivateIpAddress()))
        return false;
    if (notEqual(oldInstance.getPublicIpAddress(), newInstance.getPublicIpAddress()))
        return false;
    if (notEqual(oldInstance.getStateReason(), newInstance.getStateReason()))
        return false;
    if (notEqual(oldInstance.getArchitecture(), newInstance.getArchitecture()))
        return false;
    if (notEqual(oldInstance.getRootDeviceType(), newInstance.getRootDeviceType()))
        return false;
    if (notEqual(oldInstance.getRootDeviceName(), newInstance.getRootDeviceName()))
        return false;
    if (notEqualCollection(oldInstance.getBlockDeviceMappings(), newInstance.getBlockDeviceMappings()))
        return false;
    if (notEqual(oldInstance.getVirtualizationType(), newInstance.getVirtualizationType()))
        return false;
    if (notEqual(oldInstance.getInstanceLifecycle(), newInstance.getInstanceLifecycle()))
        return false;
    if (notEqual(oldInstance.getSpotInstanceRequestId(), newInstance.getSpotInstanceRequestId()))
        return false;
    if (notEqual(oldInstance.getLicense(), newInstance.getLicense()))
        return false;
    if (notEqual(oldInstance.getClientToken(), newInstance.getClientToken()))
        return false;
    if (notEqualCollection(oldInstance.getTags(), newInstance.getTags()))
        return false;
    if (notEqualCollection(oldInstance.getSecurityGroups(), newInstance.getSecurityGroups()))
        return false;
    if (notEqual(oldInstance.getSourceDestCheck(), newInstance.getSourceDestCheck()))
        return false;
    if (notEqual(oldInstance.getHypervisor(), newInstance.getHypervisor()))
        return false;
    if (notEqualNetworkInterfaces(oldInstance.getNetworkInterfaces(), newInstance.getNetworkInterfaces()))
        return false;
    if (notEqual(oldInstance.getIamInstanceProfile(), newInstance.getIamInstanceProfile()))
        return false;
    if (notEqual(oldInstance.getEbsOptimized(), newInstance.getEbsOptimized()))
        return false;
    if (notEqual(this.getTerminationProtection(), newEc2Instance.getTerminationProtection()))
        return false;
    if (notEqual(this.getShutdownBehavior(), newEc2Instance.getShutdownBehavior()))
        return false;
    if (notEqual(this.getUserData(), newEc2Instance.getUserData()))
        return false;

    return true;//from  w ww  .java2s.c om
}

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 w  w  .j a  v  a  2  s. co 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");
}

From source file:dsmwatcher.DSMWatcher.java

License:Open Source License

public Boolean checkIfIsolated(Instance instance, AmazonEC2Client ec2) throws Exception {
    boolean inIRSubnet = false;
    boolean hasDenySG = false;
    //check for tags on other ENIs
    List<InstanceNetworkInterface> ienis = instance.getNetworkInterfaces();
    for (InstanceNetworkInterface ieni : ienis) {
        for (String IRSubnet : IRSubnets) {
            if (IRSubnet.compareTo(ieni.getSubnetId()) == 0) {
                inIRSubnet = true;/*  w ww .j  av a 2  s . c  o  m*/
            }
        }
        List<GroupIdentifier> inititalSecGroups = ieni.getGroups();
        for (GroupIdentifier secGroup : inititalSecGroups) {
            if (secGroup.getGroupId().equals(denyAllSG)) {
                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();
                    for (Tag tag : tagSet) {
                        if (tag.getKey().compareTo("InIsolation") == 0) {
                            hasDenySG = true;
                        }
                    }
                }
            }
        }

    }
    return (inIRSubnet && hasDenySG);
}

From source file:dsmwatcher.DSMWatcher.java

License:Open Source License

public void isolateInstance(Instance instance, AmazonEC2Client ec2) throws Exception {
    Subnet targetIRSubnet = null;/*from  w w w. j a  v a  2  s.c  o  m*/
    handleAutoScaledInstance(instance); //check for autoscaling, if autoscaled instance detach first 
                                        // to prevent heathcheck failure and termination
    DescribeSubnetsRequest subnetRequest = new DescribeSubnetsRequest().withSubnetIds(instance.getSubnetId());
    List<Subnet> sourceSubnet = ec2.describeSubnets(subnetRequest).getSubnets();
    String targetAZStr = sourceSubnet.get(0).getAvailabilityZone();
    for (String IRSubnet : IRSubnets) {
        subnetRequest = new DescribeSubnetsRequest().withSubnetIds(IRSubnet);
        if (targetAZStr
                .compareTo(ec2.describeSubnets(subnetRequest).getSubnets().get(0).getAvailabilityZone()) == 0) {
            targetIRSubnet = ec2.describeSubnets(subnetRequest).getSubnets().get(0);
        }
    }
    if (targetIRSubnet == null) {
        error("Unable to find an isolation subnet for instance " + instance.getInstanceId());
        return;
    }
    List<InstanceNetworkInterface> ienis = instance.getNetworkInterfaces();
    for (InstanceNetworkInterface ieni : ienis) {
        String eniTag = "";
        List<GroupIdentifier> inititalSecGroups = ieni.getGroups();
        for (GroupIdentifier secGroup : inititalSecGroups) {
            eniTag += secGroup.getGroupId() + ",";
        }
        eniTag = eniTag.substring(0, eniTag.length() - 1);
        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();
            Tag saveSGTag = new Tag().withKey("PreIsolationSG").withValue(eniTag);
            Tag isolationTag = new Tag().withKey("InIsolation").withValue("True");
            tagSet.add(saveSGTag);
            tagSet.add(isolationTag);
            CreateTagsRequest ctr = new CreateTagsRequest().withResources(eni.getNetworkInterfaceId())
                    .withTags(tagSet);
            ec2.createTags(ctr);
            ModifyNetworkInterfaceAttributeRequest netReqest = new ModifyNetworkInterfaceAttributeRequest()
                    .withNetworkInterfaceId(eni.getNetworkInterfaceId()).withGroups(denyAllSG);
            ec2.modifyNetworkInterfaceAttribute(netReqest);
        }
    }
    CreateNetworkInterfaceRequest newENIReq = new CreateNetworkInterfaceRequest()
            .withSubnetId(targetIRSubnet.getSubnetId()).withGroups(IRSecGrp);
    CreateNetworkInterfaceResult newENIResult = ec2.createNetworkInterface(newENIReq);
    AttachNetworkInterfaceRequest attachReq = new AttachNetworkInterfaceRequest()
            .withNetworkInterfaceId(newENIResult.getNetworkInterface().getNetworkInterfaceId())
            .withInstanceId(instance.getInstanceId())
            .withDeviceIndex(instance.getNetworkInterfaces().size() + 1);
    AttachNetworkInterfaceResult attachResults = ec2.attachNetworkInterface(attachReq);
    NetworkInterfaceAttachmentChanges attachTerm = new NetworkInterfaceAttachmentChanges()
            .withAttachmentId(attachResults.getAttachmentId()).withDeleteOnTermination(true);
    ModifyNetworkInterfaceAttributeRequest setDeleteOnTerm = new ModifyNetworkInterfaceAttributeRequest()
            .withAttachment(attachTerm)
            .withNetworkInterfaceId(newENIResult.getNetworkInterface().getNetworkInterfaceId());
    ec2.modifyNetworkInterfaceAttribute(setDeleteOnTerm);
    CreateTagsRequest tagNewENIReq = new CreateTagsRequest();
    List<Tag> isolationENITags = newENIResult.getNetworkInterface().getTagSet();
    Tag newENITag = new Tag().withKey("IRENI").withValue("True");
    isolationENITags.add(newENITag);
    tagNewENIReq.setTags(isolationENITags);
    tagNewENIReq.withResources(newENIResult.getNetworkInterface().getNetworkInterfaceId());
    ec2.createTags(tagNewENIReq);
}

From source file:org.occiware.clouddriver.util.InstanceDataFactory.java

License:Apache License

public static InstanceDO buildInstanceDataFromModel(Instance instance) {
    InstanceDO instanceDO = new InstanceDO();
    buildBasicInstanceData(instance, instanceDO);

    Placement placement = instance.getPlacement();
    if (placement != null) {
        PlacementDO placementDO = buildPlacementDO(instanceDO, placement);
        instanceDO.setPlacement(placementDO);
    }/*from  w w  w.java  2 s  .co  m*/

    // Ebs volumes attached on instance.
    if (instance.getBlockDeviceMappings() != null && !instance.getBlockDeviceMappings().isEmpty()) {
        List<InstanceVolumeDO> instanceVolumeDOs = BuildInstanceVolumeDOs(instance);
        instanceDO.setVolumes(instanceVolumeDOs);
    }

    if (instance.getIamInstanceProfile() != null) {
        IamInstanceProfileDO profileDO = buildIamInstanceProfileDO(instance);
        instanceDO.setIamInstanceProfile(profileDO);
    }

    if (instance.getMonitoring() != null) {
        Monitoring monitoring = instance.getMonitoring();
        instanceDO.setMonitoringState(monitoring.getState());
    }

    // Network part.
    if (instance.getNetworkInterfaces() != null && !instance.getNetworkInterfaces().isEmpty()) {
        List<NetworkInterfaceDO> networkInterfaceDOs = buildNetworkInterfacesDatas(instance);
        instanceDO.setNetworkAdapters(networkInterfaceDOs);
    }

    List<ProductCode> productCodes = instance.getProductCodes();
    if (productCodes != null && !productCodes.isEmpty()) {
        List<ProductCodeDO> productCodeDOs = buildProductCodesDatas(productCodes);
        instanceDO.setProductCodes(productCodeDOs);
    }

    List<GroupIdentifier> groups = instance.getSecurityGroups();
    if (groups != null && !groups.isEmpty()) {
        List<GroupIdentifierDO> groupIdentifierDOs = buildSecurityGroupsDatas(groups);
        instanceDO.setSecurityGroups(groupIdentifierDOs);
    }

    InstanceState state = instance.getState();
    if (state != null) {
        instanceDO.setInstanceState(state.getName());
        instanceDO.setInstanceStateCode(state.getCode());
        StateReason stateReason = instance.getStateReason();
        if (stateReason != null) {
            instanceDO.setInstanceStateReasonMessage(stateReason.getMessage());
            instanceDO.setInstanceStateReasonCode(stateReason.getCode());
        }
    }

    List<Tag> tags = instance.getTags();
    if (tags != null && !tags.isEmpty()) {
        List<TagDO> tagDOs = buildTagsDatas(tags);
        instanceDO.setTags(tagDOs);
    }
    return instanceDO;
}

From source file:org.occiware.clouddriver.util.InstanceDataFactory.java

License:Apache License

/**
 *
 * @param instance//from  w w w  .j a  va  2 s . c  o  m
 * @return
 */
private static List<NetworkInterfaceDO> buildNetworkInterfacesDatas(Instance instance) {
    List<InstanceNetworkInterface> netInts = instance.getNetworkInterfaces();
    InstanceNetworkInterfaceAssociation netIntAsso;
    InstanceNetworkInterfaceAttachment netIntAttach;

    List<GroupIdentifier> groupIdentifiers;
    List<NetworkInterfaceDO> networkInterfaceDOs = new ArrayList<>();
    NetworkInterfaceDO netDO;
    List<InstancePrivateIpAddress> ipAddresses;
    List<InstanceIpv6Address> ipv6Addresses;
    for (InstanceNetworkInterface netInt : netInts) {
        netDO = new NetworkInterfaceDO();
        netDO.setDescription(netInt.getDescription());
        netDO.setMacAddress(netInt.getMacAddress());
        netDO.setNetworkInterfaceId(netInt.getNetworkInterfaceId());
        netDO.setOwnerId(netInt.getOwnerId());
        netDO.setPrivateDnsName(netInt.getPrivateDnsName());
        netDO.setPrivateIpAddress(netInt.getPrivateIpAddress());
        netDO.setSourceDestCheck(netInt.getSourceDestCheck());
        netDO.setStatus(netInt.getStatus());
        netDO.setSubnetId(netInt.getSubnetId());
        netDO.setVpcId(netInt.getVpcId());

        netIntAsso = netInt.getAssociation();
        if (netIntAsso != null) {
            NetAssociationDO associationDO = new NetAssociationDO();
            associationDO.setIpOwnerId(netIntAsso.getIpOwnerId());
            associationDO.setPublicDnsName(netIntAsso.getPublicDnsName());
            associationDO.setPublicIp(netIntAsso.getPublicIp());
            netDO.setNetAssociation(associationDO);
        }

        netIntAttach = netInt.getAttachment();
        if (netIntAttach != null) {
            netDO.setAttachmentId(netIntAttach.getAttachmentId());
            netDO.setAttachTime(netIntAttach.getAttachTime());
            netDO.setDeleteOnTermination(netIntAttach.getDeleteOnTermination());
            netDO.setDeviceIndex(netIntAttach.getDeviceIndex());
            netDO.setAttachmentStatus(netIntAttach.getStatus());
        }

        groupIdentifiers = netInt.getGroups();
        if (groupIdentifiers != null && !groupIdentifiers.isEmpty()) {
            List<GroupIdentifierDO> grpDOs = buildSecurityGroupsDatas(groupIdentifiers);
            netDO.setSecurityGroups(grpDOs);
        }

        ipv6Addresses = netInt.getIpv6Addresses();
        if (ipv6Addresses != null && !ipv6Addresses.isEmpty()) {
            List<String> ipv6AddressesStr = new ArrayList<>();
            for (InstanceIpv6Address ipv6Address : ipv6Addresses) {
                ipv6AddressesStr.add(ipv6Address.getIpv6Address());
            }
            netDO.setIpv6Addresses(ipv6AddressesStr);
        }

        ipAddresses = netInt.getPrivateIpAddresses();
        if (ipAddresses != null && !ipAddresses.isEmpty()) {
            List<IpAddressDO> ipAddressDOs = new ArrayList<>();
            IpAddressDO addressDO;
            InstanceNetworkInterfaceAssociation netAsso;
            for (InstancePrivateIpAddress ipAddress : ipAddresses) {
                addressDO = new IpAddressDO();
                addressDO.setPrimary(ipAddress.isPrimary());
                addressDO.setPrivateDnsName(ipAddress.getPrivateDnsName());
                addressDO.setPrivateIpAddress(ipAddress.getPrivateIpAddress());
                netAsso = ipAddress.getAssociation();
                if (netAsso != null) {
                    NetAssociationDO associationDO = new NetAssociationDO();
                    associationDO.setIpOwnerId(netAsso.getIpOwnerId());
                    associationDO.setPublicDnsName(netAsso.getPublicDnsName());
                    associationDO.setPublicIp(netAsso.getPublicIp());
                    addressDO.setNetAssociation(associationDO);
                }
                ipAddressDOs.add(addressDO);
            }
            netDO.setIpAddresses(ipAddressDOs);
        }

        networkInterfaceDOs.add(netDO);
    }
    return networkInterfaceDOs;
}

From source file:org.onebusaway.admin.service.server.impl.BundleServerServiceImpl.java

License:Apache License

@Override
public String findPublicDns(String instanceId) {
    if (LOCAL_HOST.equalsIgnoreCase(instanceId)) {
        return instanceId;
    }/* ww  w  .  ja v  a2s  . co  m*/

    Instance i = getInstance(instanceId);
    if (i != null && i.getPublicDnsName() != null) {
        return i.getPublicDnsName();
    }
    if (i != null && !i.getNetworkInterfaces().isEmpty()) {

        // if you need public IP, you need to lookup the association
        return i.getNetworkInterfaces().get(0).getPrivateDnsName();
    }
    return null;
}

From source file:org.onebusaway.admin.service.server.impl.BundleServerServiceImpl.java

License:Apache License

@Override
public String findPublicIp(String instanceId) {
    if (LOCAL_HOST.equalsIgnoreCase(instanceId)) {
        return instanceId;
    }/*ww  w.  j a  v a2 s  . com*/

    Instance i = getInstance(instanceId);
    if (i != null && i.getPublicIpAddress() != null) {
        return i.getPublicDnsName();
    }
    if (i != null && !i.getNetworkInterfaces().isEmpty()) {
        if (i.getNetworkInterfaces().get(0).getAssociation() != null) {
            return i.getNetworkInterfaces().get(0).getAssociation().getPublicIp();
        }
    }
    return null;
}

From source file:tech.greenfield.aws.route53.Tools.java

License:Open Source License

public static String getIPv6Address(com.amazonaws.services.ec2.model.Instance i) {
    return i.getNetworkInterfaces().stream().flatMap(in -> in.getIpv6Addresses().stream()).findFirst()
            .map(addr -> addr.getIpv6Address()).orElse(null);
}