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

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

Introduction

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

Prototype


public java.util.List<InstanceBlockDeviceMapping> getBlockDeviceMappings() 

Source Link

Document

Any block device mapping entries 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;//w ww .ja  v a  2s  . com
}

From source file:com.netflix.simianarmy.client.aws.AWSClient.java

License:Apache License

@Override
public List<String> listAttachedVolumes(String instanceId, boolean includeRoot) {
    Validate.notEmpty(instanceId);/*from ww w  .  ja  v  a 2s. c o m*/
    LOGGER.info(String.format("Listing volumes attached to instance %s in region %s.", instanceId, region));
    try {
        List<String> volumeIds = new ArrayList<String>();
        for (Instance instance : describeInstances(instanceId)) {
            String rootDeviceName = instance.getRootDeviceName();

            for (InstanceBlockDeviceMapping ibdm : instance.getBlockDeviceMappings()) {
                EbsInstanceBlockDevice ebs = ibdm.getEbs();
                if (ebs == null) {
                    continue;
                }

                String volumeId = ebs.getVolumeId();
                if (Strings.isNullOrEmpty(volumeId)) {
                    continue;
                }

                if (!includeRoot && rootDeviceName != null && rootDeviceName.equals(ibdm.getDeviceName())) {
                    continue;
                }

                volumeIds.add(volumeId);
            }
        }
        return volumeIds;
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidInstanceID.NotFound")) {
            throw new NotFoundException("AWS instance " + instanceId + " not found", e);
        }
        throw e;
    }
}

From source file:com.pearson.eidetic.driver.threads.subthreads.TagChecker.java

private List<Volume> checkVolumes(AmazonEC2Client ec2Client, List<Instance> instances) {
    List<Volume> volumesWithNoTags = new ArrayList();

    /*/* w w w .  java  2  s .  c o m*/
     Here is what we have:
     Instance:
     -instance.id -> String
     -instance.blockdevicemapping -> InstanceBlockDeviceMapping
     -BlockDeviceMapping:
     --BlockDeviceMapping.deviceName -> String == (/dev/xvdk)
     --BlockDeviceMapping.getEbs -> EbsInstanceBlockDevice
     --EbsInstanceBLockDevice:
     ---EbsInstanceBLockDevice.id - String == (volume.id)
            
     For instances
     Set<HashMap<instance.id,HashMap<deviceName,volume.id>>>
            
     Volume:
     -volume.id -> String
     -volume.getAttachments -> volumeAttachments
     -volumeAttachments:
     --getDevice -> String == (/dev/xvdk)
     --getInstanceId -> String == instance.id
            
     For vols
     Set<HashMap<instance.id,HashMap<deviceName,volume.id>>>
                
     If anything remians in the instance set 
            
     */
    List<Volume> volumes = getEideticVolumes(ec2Client);
    //if (volumes.isEmpty()) {
    //return volumesWithNoTags;
    //}

    //Set<HashMap<instance.id,HashMap<deviceName,volume.id>>>
    Set<HashMap<String, HashMap<String, String>>> volumeSet = new HashSet();
    for (Volume volume : volumes) {
        String volumeId = volume.getVolumeId();
        List<VolumeAttachment> attachments = volume.getAttachments();
        for (VolumeAttachment attach : attachments) {
            HashMap<String, HashMap<String, String>> setMember = new HashMap();
            HashMap<String, String> internalHash = new HashMap();

            if (volumeId == null || attach.getDevice() == null || attach.getInstanceId() == null) {
                continue;
            }

            internalHash.put(attach.getDevice(), volumeId);
            setMember.put(attach.getInstanceId(), internalHash);

            volumeSet.add(setMember);
        }
    }

    //Set<HashMap<instance.id,HashMap<deviceName,volume.id>>>
    Set<HashMap<String, HashMap<String, String>>> instanceSet = new HashSet();
    for (Instance instance : instances) {
        String instanceId = instance.getInstanceId();
        List<InstanceBlockDeviceMapping> blocks = instance.getBlockDeviceMappings();

        List<Tag> tags = instance.getTags();

        Tag tagz = null;
        for (Tag t : tags) {
            if (t.getKey().equalsIgnoreCase("Data")) {
                tagz = t;
                break;
            }
        }
        if (tagz == null) {
            continue;
        }

        String[] stringtags = tagz.getValue().split(",");

        for (String tag : stringtags) {

            String deviceName;
            String volumeId;
            for (InstanceBlockDeviceMapping instanceBlockDeviceMapping : blocks) {

                deviceName = instanceBlockDeviceMapping.getDeviceName();

                if (!deviceName.equalsIgnoreCase(tag)) {
                    continue;
                }

                volumeId = instanceBlockDeviceMapping.getEbs().getVolumeId();

                HashMap<String, HashMap<String, String>> setMember = new HashMap();
                HashMap<String, String> internalHash = new HashMap();

                if (volumeId == null | volumeId == null) {
                    continue;
                }

                internalHash.put(deviceName, volumeId);
                setMember.put(instanceId, internalHash);

                instanceSet.add(setMember);
            }
        }
    }
    if (instanceSet.isEmpty()) {
        return volumesWithNoTags;
    }

    //Need to validate
    instanceSet.removeAll(volumeSet);
    if (instanceSet.isEmpty()) {
        return volumesWithNoTags;
    }

    //Instance keys, to get volumeIds
    Set<String> volumeIds = new HashSet();
    for (HashMap<String, HashMap<String, String>> i : instanceSet) {
        Set<String> curSet = i.keySet();
        for (String s : curSet) {
            Set<String> dvns = i.get(s).keySet();
            for (String dvn : dvns) {
                if (!volumeIds.contains(dvn)) {
                    volumeIds.add(i.get(s).get(dvn));
                }
            }
        }
    }

    for (String i : volumeIds) {

        DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest().withVolumeIds(i);
        DescribeVolumesResult describeVolumeResult = EC2ClientMethods.describeVolumes(ec2Client,
                describeVolumesRequest, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_);

        Volume volume = null;
        try {
            volume = describeVolumeResult.getVolumes().get(0);
        } catch (Exception e) {
            logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_
                    + "\",Event\"Error\", Error=\"volume id does not exist\", stacktrace=\"" + e.toString()
                    + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\"");
        }

        volumesWithNoTags.add(volume);
    }

    return volumesWithNoTags;
}

From source file:com.vb.aws.services.compute.ec2.EC2UtilsImpl.java

/**
 * This method returns all EBS root volumes.
 * @return /* www  . j  a va  2  s. c o  m*/
 */
public List<Volume> getAllEBSRootVolumes() {

    List<Instance> allInstances = getAllInstances();
    List<Volume> allEBSRootVolumes = new ArrayList<>();

    for (Instance instance : allInstances) {

        //We need volumes of type only EBS.
        if (instance.getRootDeviceType().equalsIgnoreCase(DeviceType.Ebs.toString())) {
            String rootDeviceName = instance.getRootDeviceName();
            List<InstanceBlockDeviceMapping> instanceBlockDeviceMappings = instance.getBlockDeviceMappings();
            for (InstanceBlockDeviceMapping instanceBlockDeviceMapping : instanceBlockDeviceMappings) {
                if (instanceBlockDeviceMapping.getDeviceName().equalsIgnoreCase(rootDeviceName)) {
                    String volumeId = instanceBlockDeviceMapping.getEbs().getVolumeId();
                    Volume volume = new Volume().withVolumeId(volumeId);
                    allEBSRootVolumes.add(volume);
                }
            }
        }
    }

    System.out.println("INFO: Number of EBS Root Volumes : " + allEBSRootVolumes.size());
    List<String> volumeIds = allEBSRootVolumes.stream().map(e -> e.getVolumeId()).collect(Collectors.toList());
    System.out.println("INFO: EBS Root Volumes : " + volumeIds);

    return allEBSRootVolumes;
}

From source file:eu.optimis.monitoring.amazoncollector.MeasurementsHelper.java

License:Apache License

public List<Measurement> getMeasurements() {
    AmazonEC2 ec2 = getAmazonEC2Client();
    List<Measurement> measurements = new LinkedList<Measurement>();

    List<Filter> filters = new LinkedList<Filter>();
    List<String> tags = new LinkedList<String>();
    tags.add(SERVICE_ID_TAG);/* w  w  w  .j  a v a2  s .  c o  m*/
    Filter f = new Filter("tag-key", tags);
    filters.add(f);
    DescribeInstancesRequest req = new DescribeInstancesRequest();
    req.setFilters(filters);

    DescribeInstancesResult res;
    try {
        res = ec2.describeInstances(req);
    } catch (AmazonServiceException se) {
        printServiceException(se);
        throw new RuntimeException("Exception while trying to get information about running instances");
    }
    List<Instance> instances = new ArrayList<Instance>();
    for (Reservation r : res.getReservations()) {
        instances.addAll(r.getInstances());
    }

    for (Instance i : instances) {
        String instance_id = i.getInstanceId();
        InstanceType type = InstanceType.fromValue(i.getInstanceType());
        String ami_id = i.getImageId();
        String architecture = i.getArchitecture();
        String state = i.getState().getName();

        List<String> volume_ids = new LinkedList<String>();
        for (InstanceBlockDeviceMapping mapping : i.getBlockDeviceMappings()) {
            volume_ids.add(mapping.getEbs().getVolumeId());
        }

        String service_id = "";
        List<Tag> itags = i.getTags();
        for (Tag t : itags) {
            if (t.getKey().equals(SERVICE_ID_TAG)) {
                service_id = t.getValue();
                break;
            }
        }

        try {
            measurements.add(
                    new Measurement("machine_type", architecture, "", new Date(), instance_id, service_id));
            measurements.add(new Measurement("vm_status", state, "", new Date(), instance_id, service_id));

            Measurement m_mem = getTotalMemory(type, instance_id, service_id);
            Measurement m_cpu = getCPUSpeed(type, instance_id, service_id);
            Measurement m_cores = getNumVCores(type, instance_id, service_id);
            Measurement m_os = getOSRelease(ami_id, instance_id, service_id);
            Measurement m_disk = getTotalDiskSize(volume_ids, instance_id, service_id);
            Measurement m_memused = getCWMeasurement("MemoryUsed", "mem_used", true, instance_id, service_id);
            Measurement m_cpuutil = getCWMeasurement("CPUUtilization", "cpu_used", false, instance_id,
                    service_id);
            Measurement m_nout = getCWMeasurement("NetworkOut", "network_out", false, instance_id, service_id);
            Measurement m_nin = getCWMeasurement("NetworkIn", "network_in", false, instance_id, service_id);

            if (m_mem != null)
                measurements.add(m_mem);
            if (m_cpu != null)
                measurements.add(m_cpu);
            if (m_cores != null)
                measurements.add(m_cores);
            if (m_os != null)
                measurements.add(m_os);
            if (m_disk != null)
                measurements.add(m_disk);
            if (m_memused != null)
                measurements.add(m_memused);
            if (m_cpuutil != null)
                measurements.add(m_cpuutil);
            if (m_nout != null)
                measurements.add(m_nout);
            if (m_nin != null)
                measurements.add(m_nin);

        } catch (AmazonServiceException se) {
            printServiceException(se);
            throw new RuntimeException("Exception while trying to retrieve some metrics");
        }
    }

    return measurements;
}

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);
    }/*w  ww.j  av a2s  .  c  om*/

    // 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  ww .ja v a  2s.  co  m
 * @return
 */
private static List<InstanceVolumeDO> BuildInstanceVolumeDOs(Instance instance) {
    List<InstanceBlockDeviceMapping> blockDeviceMappings = instance.getBlockDeviceMappings();
    String deviceName;
    InstanceVolumeDO instVolumeDO;
    EbsInstanceBlockDevice ebs;
    List<InstanceVolumeDO> instanceVolumeDOs = new ArrayList<>();
    for (InstanceBlockDeviceMapping blockDeviceMapping : blockDeviceMappings) {
        deviceName = blockDeviceMapping.getDeviceName();
        ebs = blockDeviceMapping.getEbs();

        if (ebs != null) {
            instVolumeDO = new InstanceVolumeDO();
            instVolumeDO.setAttachTime(ebs.getAttachTime());
            instVolumeDO.setDeleteOnTermination(ebs.getDeleteOnTermination());
            instVolumeDO.setStatus(ebs.getStatus());
            instVolumeDO.setVolumeId(ebs.getVolumeId());
            instVolumeDO.setDeviceName(deviceName);
            instanceVolumeDOs.add(instVolumeDO);
        }

    }
    return instanceVolumeDOs;
}

From source file:org.xmlsh.aws.util.AWSEC2Command.java

License:BSD License

protected void writeReservation(Reservation res) throws XMLStreamException {
    startElement("reservation");
    attribute("id", res.getReservationId());
    attribute("requester-id", res.getRequesterId());

    for (Instance inst : res.getInstances()) {
        startElement("instance");
        attribute("instance-id", inst.getInstanceId());
        attribute("image", inst.getImageId());
        attribute("key-name", inst.getKeyName());
        attribute("architecture", inst.getArchitecture());
        attribute("client-token", inst.getClientToken());
        attribute("lifecycle", Util.notNull(inst.getInstanceLifecycle()));
        attribute("instance-type", inst.getInstanceType());
        attribute("kernel-id", inst.getKernelId());
        attribute("platform", inst.getPlatform());
        attribute("private-dns", inst.getPrivateDnsName());
        attribute("private-ip", inst.getPrivateIpAddress());
        attribute("public-dns", inst.getPublicDnsName());
        attribute("public-ip", inst.getPublicIpAddress());
        attribute("ramdisk-id", inst.getRamdiskId());
        attribute("root-device-name", inst.getRootDeviceName());

        attribute("spot-request-id", inst.getSpotInstanceRequestId());
        attribute("state-transistion-reason", inst.getStateTransitionReason());
        attribute("subnet-id", inst.getSubnetId());
        attribute("state", inst.getState().getName());
        attribute("virtualization-type", inst.getVirtualizationType());
        attribute("vpcid", inst.getVpcId());
        attribute("ami-launch-index", inst.getAmiLaunchIndex().toString());
        attribute("launch-date", Util.formatXSDateTime(inst.getLaunchTime()));
        attribute("monitoring", inst.getMonitoring().getState());
        attribute("source-dest-check", getSourceCheck(inst));
        attribute("state-reason", getStateReason(inst));

        writeProductCodes(inst.getProductCodes());
        writeTags(inst.getTags());/*from   w ww.  j a va 2  s .  c  o  m*/

        writePlacement(inst.getPlacement());

        writeInstanceBlockDeviceMappings(inst.getBlockDeviceMappings());

        endElement();

    }

    endElement();
}