use of software.amazon.awssdk.services.ec2.model.Tag in project herd by FINRAOS.
the class S3DaoImplTest method testTagObjects.
@Test
public void testTagObjects() {
// Create an S3 file transfer request parameters DTO to access S3 objects.
S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME);
s3FileTransferRequestParamsDto.setFiles(Arrays.asList(new File(S3_KEY_PREFIX + "/" + LOCAL_FILE)));
// Create an S3 file transfer request parameters DTO to tag S3 objects.
S3FileTransferRequestParamsDto s3ObjectTaggerParamsDto = new S3FileTransferRequestParamsDto();
s3ObjectTaggerParamsDto.setAwsAccessKeyId(AWS_ASSUMED_ROLE_ACCESS_KEY);
s3ObjectTaggerParamsDto.setAwsSecretKey(AWS_ASSUMED_ROLE_SECRET_KEY);
s3ObjectTaggerParamsDto.setSessionToken(AWS_ASSUMED_ROLE_SESSION_TOKEN);
// Create an S3 object tag.
Tag tag = new Tag(S3_OBJECT_TAG_KEY, S3_OBJECT_TAG_VALUE);
// Create a retry policy.
RetryPolicy retryPolicy = new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true);
// Create a get object tagging result.
GetObjectTaggingResult getObjectTaggingResult = new GetObjectTaggingResult(null);
// Create a set object tagging result.
SetObjectTaggingResult setObjectTaggingResult = new SetObjectTaggingResult();
// Mock the external calls.
when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy);
when(s3Operations.getObjectTagging(any(GetObjectTaggingRequest.class), any(AmazonS3Client.class))).thenReturn(getObjectTaggingResult);
when(s3Operations.setObjectTagging(any(SetObjectTaggingRequest.class), any(AmazonS3Client.class))).thenReturn(setObjectTaggingResult);
// Call the method under test.
s3DaoImpl.tagObjects(s3FileTransferRequestParamsDto, s3ObjectTaggerParamsDto, tag);
// Verify the external calls.
verify(retryPolicyFactory, times(2)).getRetryPolicy();
verify(s3Operations).getObjectTagging(any(GetObjectTaggingRequest.class), any(AmazonS3Client.class));
verify(s3Operations).setObjectTagging(any(SetObjectTaggingRequest.class), any(AmazonS3Client.class));
verifyNoMoreInteractionsHelper();
}
use of software.amazon.awssdk.services.ec2.model.Tag in project herd by FINRAOS.
the class S3DaoImpl method tagObjects.
@Override
public void tagObjects(final S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto, final S3FileTransferRequestParamsDto s3ObjectTaggerParamsDto, final Tag tag) {
LOGGER.info("Tagging objects in S3... s3BucketName=\"{}\" s3KeyCount={} s3ObjectTagKey=\"{}\" s3ObjectTagValue=\"{}\"", s3FileTransferRequestParamsDto.getS3BucketName(), s3FileTransferRequestParamsDto.getFiles().size(), tag.getKey(), tag.getValue());
if (!CollectionUtils.isEmpty(s3FileTransferRequestParamsDto.getFiles())) {
// Initialize a key value pair for the error message in the catch block.
String s3Key = s3FileTransferRequestParamsDto.getFiles().get(0).getPath().replaceAll("\\\\", "/");
// Amazon S3 client to access S3 objects.
AmazonS3Client s3Client = null;
// Amazon S3 client for S3 object tagging.
AmazonS3Client s3ObjectTaggerClient = null;
try {
// Create an S3 client to access S3 objects.
s3Client = getAmazonS3(s3FileTransferRequestParamsDto);
// Create an S3 client for S3 object tagging.
s3ObjectTaggerClient = getAmazonS3(s3ObjectTaggerParamsDto);
// Create a get object tagging request.
GetObjectTaggingRequest getObjectTaggingRequest = new GetObjectTaggingRequest(s3FileTransferRequestParamsDto.getS3BucketName(), null);
// Create a restore object request.
SetObjectTaggingRequest setObjectTaggingRequest = new SetObjectTaggingRequest(s3FileTransferRequestParamsDto.getS3BucketName(), null, null);
for (File file : s3FileTransferRequestParamsDto.getFiles()) {
// Prepare an S3 key.
s3Key = file.getPath().replaceAll("\\\\", "/");
// Retrieve the current tagging information for the S3 key.
getObjectTaggingRequest.setKey(s3Key);
GetObjectTaggingResult getObjectTaggingResult = s3Operations.getObjectTagging(getObjectTaggingRequest, s3Client);
// Update the list of tags to include the specified S3 object tag.
List<Tag> updatedTags = new ArrayList<>();
updatedTags.add(tag);
if (CollectionUtils.isNotEmpty(getObjectTaggingResult.getTagSet())) {
for (Tag currentTag : getObjectTaggingResult.getTagSet()) {
if (!StringUtils.equals(tag.getKey(), currentTag.getKey())) {
updatedTags.add(currentTag);
}
}
}
// Update the tagging information.
setObjectTaggingRequest.setKey(s3Key);
setObjectTaggingRequest.setTagging(new ObjectTagging(updatedTags));
s3Operations.setObjectTagging(setObjectTaggingRequest, s3ObjectTaggerClient);
}
} catch (Exception e) {
throw new IllegalStateException(String.format("Failed to tag S3 object with \"%s\" key in \"%s\" bucket. Reason: %s", s3Key, s3FileTransferRequestParamsDto.getS3BucketName(), e.getMessage()), e);
} finally {
if (s3Client != null) {
s3Client.shutdown();
}
if (s3ObjectTaggerClient != null) {
s3ObjectTaggerClient.shutdown();
}
}
}
}
use of software.amazon.awssdk.services.ec2.model.Tag in project herd by FINRAOS.
the class S3DaoTest method testTagObjectsOtherTagKeyAlreadyExists.
@Test
public void testTagObjectsOtherTagKeyAlreadyExists() {
// Create two S3 object tags having different tag keys.
List<Tag> tags = Arrays.asList(new Tag(S3_OBJECT_TAG_KEY, S3_OBJECT_TAG_VALUE), new Tag(S3_OBJECT_TAG_KEY_2, S3_OBJECT_TAG_VALUE_2));
// Put a file in S3 that is already tagged with the first S3 object tag.
PutObjectRequest putObjectRequest = new PutObjectRequest(S3_BUCKET_NAME, TARGET_S3_KEY, new ByteArrayInputStream(new byte[1]), new ObjectMetadata());
putObjectRequest.setTagging(new ObjectTagging(Arrays.asList(tags.get(0))));
s3Operations.putObject(putObjectRequest, null);
// Validate that the S3 object is tagged with the first tag only.
GetObjectTaggingResult getObjectTaggingResult = s3Operations.getObjectTagging(new GetObjectTaggingRequest(S3_BUCKET_NAME, TARGET_S3_KEY), null);
assertEquals(Arrays.asList(tags.get(0)), getObjectTaggingResult.getTagSet());
// Tag the S3 file with the second S3 object tag.
S3FileTransferRequestParamsDto params = new S3FileTransferRequestParamsDto();
params.setS3BucketName(S3_BUCKET_NAME);
params.setFiles(Arrays.asList(new File(TARGET_S3_KEY)));
s3Dao.tagObjects(params, new S3FileTransferRequestParamsDto(), tags.get(1));
// Validate that the S3 object is now tagged with both tags.
getObjectTaggingResult = s3Operations.getObjectTagging(new GetObjectTaggingRequest(S3_BUCKET_NAME, TARGET_S3_KEY), null);
assertEquals(tags.size(), getObjectTaggingResult.getTagSet().size());
assertTrue(getObjectTaggingResult.getTagSet().containsAll(tags));
}
use of software.amazon.awssdk.services.ec2.model.Tag in project herd by FINRAOS.
the class S3DaoTest method testTagObjectsAmazonServiceException.
@Test
public void testTagObjectsAmazonServiceException() {
// Try to retrieve S3 object metadata when AmazonServiceException is expected tpo be thrown..
try {
S3FileTransferRequestParamsDto params = new S3FileTransferRequestParamsDto();
params.setS3BucketName(MockS3OperationsImpl.MOCK_S3_BUCKET_NAME_INTERNAL_ERROR);
params.setFiles(Arrays.asList(new File(TARGET_S3_KEY)));
s3Dao.tagObjects(params, new S3FileTransferRequestParamsDto(), new Tag(S3_OBJECT_TAG_KEY, S3_OBJECT_TAG_VALUE));
fail();
} catch (IllegalStateException e) {
assertEquals(String.format("Failed to tag S3 object with \"%s\" key in \"%s\" bucket. " + "Reason: InternalError (Service: null; Status Code: 0; Error Code: InternalError; Request ID: null)", TARGET_S3_KEY, MockS3OperationsImpl.MOCK_S3_BUCKET_NAME_INTERNAL_ERROR), e.getMessage());
}
}
use of software.amazon.awssdk.services.ec2.model.Tag in project photon-model by vmware.
the class AWSComputeStateCreationAdapterService method updateTagLinks.
/**
* Updates tag links of existing computes using TagsUtil.
*/
private DeferredResult<AWSComputeStateCreationContext> updateTagLinks(AWSComputeStateCreationContext context) {
if (context.request.instancesToBeUpdated == null || context.request.instancesToBeUpdated.size() == 0) {
logFine(() -> "No local compute states to be updated so there are no tags to update.");
return DeferredResult.completed(context);
} else {
List<DeferredResult<Set<String>>> updateCSTagLinksOps = new ArrayList<>();
for (String instanceId : context.request.instancesToBeUpdated.keySet()) {
Instance instance = context.request.instancesToBeUpdated.get(instanceId);
ComputeState existingComputeState = context.request.computeStatesToBeUpdated.get(instanceId);
Map<String, String> remoteTags = new HashMap<>();
for (Tag awsInstanceTag : instance.getTags()) {
if (!awsInstanceTag.getKey().equals(AWSConstants.AWS_TAG_NAME)) {
remoteTags.put(awsInstanceTag.getKey(), awsInstanceTag.getValue());
}
}
updateCSTagLinksOps.add(updateLocalTagStates(this, existingComputeState, remoteTags, null));
}
return DeferredResult.allOf(updateCSTagLinksOps).thenApply(gnore -> context);
}
}
Aggregations