use of software.amazon.awssdk.services.s3.model.Tag in project SimianArmy by Netflix.
the class VolumeTaggingMonkey method tagVolumesWithLatestAttachment.
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();
// by Janitor monkey.
if ("donotmark".equals(getTagValue(JanitorMonkey.JANITOR_TAG, tags))) {
LOGGER.info(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(BasicSimianArmyContext.GLOBAL_OWNER_TAGKEY);
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);
}
}
}
}
use of software.amazon.awssdk.services.s3.model.Tag in project aws-doc-sdk-examples by awsdocs.
the class CreateInstance method createEC2Instance.
// snippet-start:[ec2.java2.create_instance.main]
public static String createEC2Instance(Ec2Client ec2, String name, String amiId) {
RunInstancesRequest runRequest = RunInstancesRequest.builder().imageId(amiId).instanceType(InstanceType.T1_MICRO).maxCount(1).minCount(1).build();
RunInstancesResponse response = ec2.runInstances(runRequest);
String instanceId = response.instances().get(0).instanceId();
Tag tag = Tag.builder().key("Name").value(name).build();
CreateTagsRequest tagRequest = CreateTagsRequest.builder().resources(instanceId).tags(tag).build();
try {
ec2.createTags(tagRequest);
System.out.printf("Successfully started EC2 Instance %s based on AMI %s", instanceId, amiId);
return instanceId;
} catch (Ec2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
}
use of software.amazon.awssdk.services.s3.model.Tag in project aws-doc-sdk-examples by awsdocs.
the class S3Service method tagAssets.
// tag assets with labels in the given list.
public void tagAssets(List myList, String bucketName) {
try {
S3Client s3 = getClient();
int len = myList.size();
String assetName = "";
String labelName = "";
String labelValue = "";
// tag all the assets in the list.
for (Object o : myList) {
// Need to get the WorkItem from each list.
List innerList = (List) o;
for (Object value : innerList) {
WorkItem workItem = (WorkItem) value;
assetName = workItem.getKey();
labelName = workItem.getName();
labelValue = workItem.getConfidence();
tagExistingObject(s3, bucketName, assetName, labelName, labelValue);
}
}
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.Tag in project aws-doc-sdk-examples by awsdocs.
the class PutObject method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " <bucketName> <objectKey> <objectPath> \n\n" + "Where:\n" + " bucketName - the Amazon S3 bucket to upload an object into.\n" + " objectKey - the object to upload (for example, book.pdf).\n" + " objectPath - the path where the file is located (for example, C:/AWS/book2.pdf). \n\n";
if (args.length != 3) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String objectKey = args[1];
String objectPath = args[2];
System.out.println("Putting object " + objectKey + " into bucket " + bucketName);
System.out.println(" in bucket: " + bucketName);
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder().region(region).build();
String result = putS3Object(s3, bucketName, objectKey, objectPath);
System.out.println("Tag information: " + result);
s3.close();
}
use of software.amazon.awssdk.services.s3.model.Tag in project aws-doc-sdk-examples by awsdocs.
the class ManagingObjectTags method updateObjectTags.
public static void updateObjectTags(S3Client s3, String bucketName, String objectKey) {
try {
// Retrieve the object's tags.
GetObjectTaggingRequest taggingRequest = GetObjectTaggingRequest.builder().bucket(bucketName).key(objectKey).build();
GetObjectTaggingResponse getTaggingRes = s3.getObjectTagging(taggingRequest);
// Write out the tags.
List<Tag> obTags = getTaggingRes.tagSet();
for (Tag sinTag : obTags) {
System.out.println("The tag key is: " + sinTag.key());
System.out.println("The tag value is: " + sinTag.value());
}
// Replace the object's tags with two new tags.
Tag tag3 = Tag.builder().key("Tag 3").value("This is tag 3").build();
Tag tag4 = Tag.builder().key("Tag 4").value("This is tag 4").build();
List<Tag> tags = new ArrayList<Tag>();
tags.add(tag3);
tags.add(tag4);
Tagging updatedTags = Tagging.builder().tagSet(tags).build();
PutObjectTaggingRequest taggingRequest1 = PutObjectTaggingRequest.builder().bucket(bucketName).key(objectKey).tagging(updatedTags).build();
s3.putObjectTagging(taggingRequest1);
// Write out the modified tags.
GetObjectTaggingResponse getTaggingRes2 = s3.getObjectTagging(taggingRequest);
List<Tag> modTags = getTaggingRes2.tagSet();
for (Tag sinTag : modTags) {
System.out.println("The tag key is: " + sinTag.key());
System.out.println("The tag value is: " + sinTag.value());
}
} catch (S3Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
Aggregations