public static void updateTagsForResource()

in aws-datasync-locationnfs/src/main/java/software/amazon/datasync/locationnfs/TagRequestMaker.java [69:134]


    public static void updateTagsForResource(
            final AmazonWebServicesClientProxy proxy,
            final DataSyncClient client,
            final String resourceArn,
            final ResourceHandlerRequest<ResourceModel> request,
            final Logger logger) {

        Map<String, String> tagList = request.getDesiredResourceTags();
        if (tagList == null) {
            tagList = new HashMap<String, String>();
        }

        Map<String, String> prevTagList = new HashMap<String, String>();
        if (request.getPreviousResourceTags() != null) {
            prevTagList = request.getPreviousResourceTags();
        }

        final Set<String> keysToRemove = Sets.difference(
                prevTagList.keySet(),
                tagList.keySet()
        );

        if (!keysToRemove.isEmpty()) {
            UntagResourceRequest untagResourceRequest = TagTranslator.translateToUntagResourceRequest(
                    keysToRemove, resourceArn);
            try {
                proxy.injectCredentialsAndInvokeV2(untagResourceRequest, client::untagResource);
                logger.log(String.format("%s %s old tags removed successfully", ResourceModel.TYPE_NAME,
                        resourceArn));
            } catch (InvalidRequestException e) {
                throw new CfnNotFoundException(ResourceModel.TYPE_NAME, resourceArn);
            } catch (InternalException e) {
                throw new CfnServiceInternalErrorException(e.getMessage(), e.getCause());
            }
        }

        MapDifference<String, String> mapDifference = Maps.difference(tagList, prevTagList);
        final Set<Tag> tagsToAdd = mapDifference.entriesDiffering().entrySet().stream().map(entry -> {
            return Tag.builder().key(entry.getKey()).value(entry.getValue().leftValue()).build();
        }).collect(Collectors.toSet());
        tagsToAdd.addAll(TagTranslator.translateMapToTags(mapDifference.entriesOnlyOnLeft()));

        for (Tag tag : tagsToAdd) {
            if (tag.getKey().trim().toLowerCase().startsWith(AWS_TAG_PREFIX)) {
                throw new CfnInvalidRequestException(tag.getKey() + " is an invalid key. aws: prefixed tag key names cannot be requested.");
            }
        }

        if (request.getPreviousSystemTags() == null && request.getSystemTags() != null) {
            tagsToAdd.addAll(TagTranslator.translateMapToTags(request.getSystemTags()));
        }

        if (!tagsToAdd.isEmpty()) {
            TagResourceRequest tagResourceRequest = TagTranslator.translateToTagResourceRequest(
                    tagsToAdd, resourceArn);
            try {
                proxy.injectCredentialsAndInvokeV2(tagResourceRequest, client::tagResource);
                logger.log(String.format("%s %s tags updated successfully", ResourceModel.TYPE_NAME,
                        resourceArn));
            } catch (InvalidRequestException e) {
                throw new CfnNotFoundException(ResourceModel.TYPE_NAME, resourceArn);
            } catch (InternalException e) {
                throw new CfnServiceInternalErrorException(e.getMessage(), e.getCause());
            }
        }
    }