Example usage for com.amazonaws.services.ec2.model Volume getTags

List of usage examples for com.amazonaws.services.ec2.model Volume getTags

Introduction

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

Prototype


public java.util.List<Tag> getTags() 

Source Link

Document

Any tags assigned to the volume.

Usage

From source file:com.netflix.simianarmy.aws.janitor.crawler.EBSVolumeJanitorCrawler.java

License:Apache License

private List<Resource> getVolumeResources(String... volumeIds) {
    List<Resource> resources = new LinkedList<Resource>();

    AWSClient awsClient = getAWSClient();

    for (Volume volume : awsClient.describeVolumes(volumeIds)) {
        Resource volumeResource = new AWSResource().withId(volume.getVolumeId())
                .withRegion(getAWSClient().region()).withResourceType(AWSResourceType.EBS_VOLUME)
                .withLaunchTime(volume.getCreateTime());
        for (Tag tag : volume.getTags()) {
            LOGGER.info(String.format("Adding tag %s = %s to resource %s", tag.getKey(), tag.getValue(),
                    volumeResource.getId()));
            volumeResource.setTag(tag.getKey(), tag.getValue());
        }//from w w  w  .j  a  v  a 2s  . com
        volumeResource.setOwnerEmail(getOwnerEmailForResource(volumeResource));
        volumeResource.setDescription(getVolumeDescription(volume));
        ((AWSResource) volumeResource).setAWSResourceState(volume.getState());
        resources.add(volumeResource);
    }
    return resources;
}

From source file:com.netflix.simianarmy.aws.janitor.crawler.EBSVolumeJanitorCrawler.java

License:Apache License

private String getVolumeDescription(Volume volume) {
    StringBuilder description = new StringBuilder();
    Integer size = volume.getSize();
    description.append(String.format("size=%s", size == null ? "unknown" : size));
    for (Tag tag : volume.getTags()) {
        description.append(String.format("; %s=%s", tag.getKey(), tag.getValue()));
    }/*from  w  w w . j  a v  a2 s .co  m*/
    return description.toString();
}

From source file:com.netflix.simianarmy.aws.janitor.VolumeTaggingMonkey.java

License:Apache License

private void tagVolumesWithLatestAttachment(AWSClient awsClient) {
    List<Volume> volumes = awsClient.describeVolumes();
    LOGGER.info(String.format("Trying to tag %d volumes for Janitor Monkey meta data.", volumes.size()));
    Date now = calendar.now().getTime();
    for (Volume volume : volumes) {
        String owner = null, instanceId = null;
        Date lastDetachTime = null;
        List<VolumeAttachment> attachments = volume.getAttachments();
        List<Tag> tags = volume.getTags();

        // The volume can have a special tag is it does not want to be changed/tagged
        // by Janitor monkey.
        if ("donotmark".equals(getTagValue(JanitorMonkey.JANITOR_TAG, tags))) {
            LOGGER.info(//from   w w  w. j a  v  a 2  s. c  om
                    String.format("The volume %s is tagged as not handled by Janitor", volume.getVolumeId()));
            continue;
        }

        Map<String, String> janitorMetadata = parseJanitorTag(tags);
        // finding the instance attached most recently.
        VolumeAttachment latest = null;
        for (VolumeAttachment attachment : attachments) {
            if (latest == null || latest.getAttachTime().before(attachment.getAttachTime())) {
                latest = attachment;
            }
        }
        if (latest != null) {
            instanceId = latest.getInstanceId();
            owner = getOwnerEmail(instanceId, janitorMetadata, tags, awsClient);
        }

        if (latest == null || "detached".equals(latest.getState())) {
            if (janitorMetadata.get(JanitorMonkey.DETACH_TIME_TAG_KEY) == null) {
                // There is no attached instance and the last detached time is not set.
                // Use the current time as the last detached time.
                LOGGER.info(String.format("Setting the last detached time to %s for volume %s", now,
                        volume.getVolumeId()));
                lastDetachTime = now;
            } else {
                LOGGER.debug(String.format("The volume %s was already marked as detached at time %s",
                        volume.getVolumeId(), janitorMetadata.get(JanitorMonkey.DETACH_TIME_TAG_KEY)));
            }
        } else {
            // The volume is currently attached to an instance
            lastDetachTime = null;
        }
        String existingOwner = janitorMetadata.get(JanitorMonkey.OWNER_TAG_KEY);
        if (owner == null && existingOwner != null) {
            // Save the current owner in the tag when we are not able to find a owner.
            owner = existingOwner;
        }
        if (needsUpdate(janitorMetadata, owner, instanceId, lastDetachTime)) {
            Event evt = updateJanitorMetaTag(volume, instanceId, owner, lastDetachTime, awsClient);
            if (evt != null) {
                context().recorder().recordEvent(evt);
            }
        }
    }
}

From source file:com.pearson.eidetic.driver.threads.EideticSubThreadMethods.java

@Override
public Collection<Tag> getResourceTags(Volume vol) {
    List<Tag> tagsList = vol.getTags();
    Collection<Tag> tags = tagsList;
    return tags;// w w  w  .  jav  a2s  .c o  m
}

From source file:com.pearson.eidetic.driver.threads.MonitorSnapshotVolumeTime.java

private HashMap<Date, ArrayList<Volume>> extractRunAt(ArrayList<Volume> volumes) {
    JSONParser parser = new JSONParser();
    HashMap<Date, ArrayList<Volume>> returnHash = new HashMap();
    for (Volume volume : volumes) {
        for (Tag tag : volume.getTags()) {
            String tagValue = null;
            if (tag.getKey().equalsIgnoreCase("Eidetic")) {
                tagValue = tag.getValue();
            }/*from w  w  w  .jav  a 2s . c o  m*/
            if (tagValue == null) {
                continue;
            }
            JSONObject eideticParameters;
            try {
                Object obj = parser.parse(tagValue);
                eideticParameters = (JSONObject) obj;
            } catch (Exception e) {
                logger.error("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier()
                        + "\",Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + volume.getVolumeId()
                        + "\", stacktrace=\"" + e.toString() + System.lineSeparator()
                        + StackTrace.getStringFromStackTrace(e) + "\"");
                continue;
            }
            JSONObject createSnapshot;
            try {
                createSnapshot = (JSONObject) eideticParameters.get("CreateSnapshot");
            } catch (Exception e) {
                logger.error("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier()
                        + "\",Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + volume.getVolumeId()
                        + "\", stacktrace=\"" + e.toString() + System.lineSeparator()
                        + StackTrace.getStringFromStackTrace(e) + "\"");
                continue;
            }
            String runAt = null;
            if (createSnapshot.containsKey("RunAt")) {
                runAt = createSnapshot.get("RunAt").toString();
            }

            Date date = null;
            try {
                date = dayFormat_.parse(runAt);
            } catch (ParseException e) {
                logger.error("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier()
                        + "\",Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + volume.getVolumeId()
                        + "\", stacktrace=\"" + e.toString() + System.lineSeparator()
                        + StackTrace.getStringFromStackTrace(e) + "\"");
            }
            if (date == null) {
                continue;
            }

            if (returnHash.keySet().contains(date)) {
                returnHash.get(date).add(volume);
            } else {
                ArrayList<Volume> newArrayList = new ArrayList();
                newArrayList.add(volume);
                returnHash.put(date, newArrayList);
            }
            break;
        }
    }
    return returnHash;
}

From source file:com.pearson.eidetic.driver.threads.MonitorSnapshotVolumeTime.java

public String getIntTagValue(Volume vol) {
    if (vol == null) {
        return null;
    }/*  w w  w . j a v a  2 s . c  o  m*/

    String inttagvalue = null;
    for (Tag tag : vol.getTags()) {
        if ("Eidetic".equalsIgnoreCase(tag.getKey())) {
            inttagvalue = tag.getValue();
            break;
        }
    }

    return inttagvalue;

}

From source file:com.pearson.eidetic.driver.threads.RefreshAwsAccountVolumes.java

private JSONObject getEideticParameters(Volume volume, JSONParser parser) {
    JSONObject eideticParameters = null;
    for (Tag tag : volume.getTags()) {
        String tagValue = null;// ww w .j a v  a2  s  .c  o m
        if (tag.getKey().equalsIgnoreCase("Eidetic")) {
            tagValue = tag.getValue();
        }
        if (tagValue == null) {
            continue;
        }

        try {
            Object obj = parser.parse(tagValue);
            eideticParameters = (JSONObject) obj;
        } catch (Exception e) {
            logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_
                    + "\",Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + volume.getVolumeId()
                    + "\", stacktrace=\"" + e.toString() + System.lineSeparator()
                    + StackTrace.getStringFromStackTrace(e) + "\"");
            break;
        }
    }
    return eideticParameters;
}

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

License:BSD License

protected void writeVolume(Volume volume) throws XMLStreamException {
    startElement("volume");

    attribute("volume-id", volume.getVolumeId());
    attribute("snapshot-id ", volume.getSnapshotId());
    attribute("availability-zone", volume.getAvailabilityZone());
    attribute("create-date", Util.formatXSDateTime(volume.getCreateTime()));
    attribute("size", volume.getSize().toString());
    attribute("state", volume.getState());
    attribute("volume-type", volume.getVolumeType());
    attribute("iops", volume.getIops());
    attribute("encrypted", volume.getEncrypted());
    attribute("kms-key-id", volume.getKmsKeyId());

    writeAttachements(volume.getAttachments());
    writeTags(volume.getTags());
}