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

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

Introduction

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

Prototype

InstanceBlockDeviceMappingSpecification

Source Link

Usage

From source file:com.cloudera.director.aws.ec2.ebs.EBSAllocator.java

License:Apache License

/**
 * Adds a delete on termination flag to all volumes in an {@code InstanceEbsVolumes} list
 * that have the ATTACHED status. This makes sure that the volumes associated with the
 * instance will be automatically cleaned up upon instance termination.
 *
 * @param instanceEbsVolumesList list of instances along with their associated volumes
 *//*from w  w w  .j  ava 2 s.  c o  m*/
public void addDeleteOnTerminationFlag(List<InstanceEbsVolumes> instanceEbsVolumesList) {
    Set<String> volumesToFlag = getAllVolumeIdsWithStatus(instanceEbsVolumesList,
            InstanceEbsVolumes.Status.ATTACHED);

    if (!volumesToFlag.isEmpty()) {
        for (InstanceEbsVolumes instanceEbsVolumes : instanceEbsVolumesList) {
            String ec2InstanceId = instanceEbsVolumes.getEc2InstanceId();

            DescribeInstanceAttributeRequest instanceAttributeRequest = new DescribeInstanceAttributeRequest()
                    .withAttribute(InstanceAttributeName.BlockDeviceMapping).withInstanceId(ec2InstanceId);

            List<InstanceBlockDeviceMapping> blockDeviceMappings = client
                    .describeInstanceAttribute(instanceAttributeRequest).getInstanceAttribute()
                    .getBlockDeviceMappings();

            for (InstanceBlockDeviceMapping blockDeviceMapping : blockDeviceMappings) {
                String volumeId = blockDeviceMapping.getEbs().getVolumeId();

                // The block device mapping may have volumes associated with it that were not
                // provisioned by us. We skip marking those volumes for deletion.

                if (!volumesToFlag.contains(volumeId)) {
                    continue;
                }

                InstanceBlockDeviceMappingSpecification updatedSpec = new InstanceBlockDeviceMappingSpecification()
                        .withEbs(new EbsInstanceBlockDeviceSpecification().withDeleteOnTermination(true)
                                .withVolumeId(volumeId))
                        .withDeviceName(blockDeviceMapping.getDeviceName());

                ModifyInstanceAttributeRequest modifyRequest = new ModifyInstanceAttributeRequest()
                        .withBlockDeviceMappings(updatedSpec).withInstanceId(ec2InstanceId);

                client.modifyInstanceAttribute(modifyRequest);
            }
        }
    }
}

From source file:net.roboconf.target.ec2.internal.Ec2MachineConfigurator.java

License:Apache License

/**
 * Attaches volume(s) for EBS./*w w w  . j  a  va  2 s  .  co m*/
 * @return true if successful attachment, or nothing to do. false otherwise
 */
private boolean attachVolumes() {

    // If volume is found in map, it has been successfully created (no need to check here)
    for (Map.Entry<String, String> entry : this.storageIdToVolumeId.entrySet()) {

        String volumeId = entry.getValue();
        String storageId = entry.getKey();

        // Give a name to the volume before attaching
        String nameTemplate = Ec2IaasHandler.findStorageProperty(this.targetProperties, storageId,
                VOLUME_NAME_PREFIX);
        String name = Ec2IaasHandler.expandVolumeName(nameTemplate, this.applicationName,
                this.scopedInstance.getName());
        if (Utils.isEmptyOrWhitespaces(name))
            name = "Created by Roboconf for " + this.tagName;
        tagResource(volumeId, name);

        // Attach volume now
        String mountPoint = Ec2IaasHandler.findStorageProperty(this.targetProperties, storageId,
                VOLUME_MOUNT_POINT_PREFIX);
        if (Utils.isEmptyOrWhitespaces(mountPoint))
            mountPoint = "/dev/sdf";

        AttachVolumeRequest attachRequest = new AttachVolumeRequest().withInstanceId(this.machineId)
                .withDevice(mountPoint).withVolumeId(volumeId);

        try {
            this.ec2Api.attachVolume(attachRequest);
        } catch (Exception e) {
            this.logger.warning("EBS Volume attachment error: " + e);
        }

        // Set deleteOnTermination flag ?
        if (Boolean.parseBoolean(Ec2IaasHandler.findStorageProperty(this.targetProperties, storageId,
                VOLUME_DELETE_OT_PREFIX))) {
            EbsInstanceBlockDeviceSpecification ebsSpecification = new EbsInstanceBlockDeviceSpecification()
                    .withVolumeId(volumeId).withDeleteOnTermination(true);

            InstanceBlockDeviceMappingSpecification mappingSpecification = new InstanceBlockDeviceMappingSpecification()
                    .withDeviceName(mountPoint).withEbs(ebsSpecification);

            ModifyInstanceAttributeRequest request = new ModifyInstanceAttributeRequest()
                    .withInstanceId(this.machineId).withBlockDeviceMappings(mappingSpecification);

            this.ec2Api.modifyInstanceAttribute(request);
        }
    }

    return true;
}