private void updateTags()

in aws-logs-loggroup/src/main/java/software/amazon/logs/loggroup/UpdateHandler.java [168:213]


    private void updateTags(final AmazonWebServicesClientProxy proxy,
                            final ResourceModel model,
                            final Map<String, String> previousTags,
                            final Map<String, String> tags,
                            final Logger logger) {
        MapDifference<String, String> tagsDifference = Maps.difference(Optional.ofNullable(previousTags).orElse(new HashMap<>()),
                Optional.ofNullable(tags).orElse(new HashMap<>()));
        final Map<String, String> tagsToRemove = tagsDifference.entriesOnlyOnLeft();
        final Map<String, String> tagsToAdd = tagsDifference.entriesOnlyOnRight();
        final Map<String, String> tagsToDiffer = tagsDifference.entriesDiffering().entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey, tag -> tag.getValue().rightValue()));
        final Map<String, String> tagsToUpdate = Stream.concat(tagsToAdd.entrySet().stream(), tagsToDiffer.entrySet().stream())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        try {
            if (!tagsToRemove.isEmpty()) {
                final List<String> tagKeys = new ArrayList<>(tagsToRemove.keySet());
                proxy.injectCredentialsAndInvokeV2(Translator.translateToUntagLogGroupRequest(model.getLogGroupName(), tagKeys),
                        ClientBuilder.getClient()::untagLogGroup);

                final String message =
                        String.format("%s [%s] successfully removed tags: [%s]",
                                ResourceModel.TYPE_NAME, model.getLogGroupName(), tagKeys);
                logger.log(message);
            }
            if(!tagsToUpdate.isEmpty()) {
                proxy.injectCredentialsAndInvokeV2(Translator.translateToTagLogGroupRequest(model.getLogGroupName(), tagsToUpdate),
                        ClientBuilder.getClient()::tagLogGroup);

                final String message =
                        String.format("%s [%s] successfully added tags: [%s]",
                                ResourceModel.TYPE_NAME, model.getLogGroupName(), tagsToUpdate);
                logger.log(message);
            }
        } catch (final ResourceNotFoundException e) {
            throwNotFoundException(model);
        } catch (final InvalidParameterException e) {
            throw new CfnInternalFailureException(e);
        } catch (final CloudWatchLogsException e) {
            if (Translator.ACCESS_DENIED_ERROR_CODE.equals(e.awsErrorDetails().errorCode())) {
                // fail silently, if there is no permission to list tags
                logger.log(e.getMessage());
            } else {
                throw e;
            }
        }
    }