void updateTags()

in aws-iotfleethub-application/src/main/java/software/amazon/iotfleethub/application/UpdateHandler.java [108:160]


    void updateTags(AmazonWebServicesClientProxy proxy,
                    ResourceHandlerRequest<ResourceModel> request,
                    String applicationArn,
                    Map<String, String> currentTags,
                    Logger logger) {

        Map<String, String> desiredTags = new HashMap<>();
        ResourceModel model = request.getDesiredResourceState();
        if (model.getTags() != null) {
            for (Tag t : model.getTags()) {
                desiredTags.put(t.getKey(), t.getValue());
            }
        }
        if (request.getDesiredResourceTags() != null) {
            desiredTags.putAll(request.getDesiredResourceTags());
        }

        // Add Tags
        Map<String, String> tagsToAdd = new HashMap<>();
        for (Map.Entry<String,String> tagEntry : desiredTags.entrySet()) {
            String currentTagValue = currentTags.get(tagEntry.getKey());
            if (currentTagValue == null || !currentTagValue.equals(tagEntry.getValue())) {
                tagsToAdd.put(tagEntry.getKey(), tagEntry.getValue());
            }
        }

        if (!tagsToAdd.isEmpty()) {
            TagResourceRequest tagRequest = TagResourceRequest.builder()
                    .resourceArn(applicationArn)
                    .tags(tagsToAdd)
                    .build();
            proxy.injectCredentialsAndInvokeV2(tagRequest, iotFleetHubClient::tagResource);
            logger.log(String.format("Called TagResource for %s.", applicationArn));
        }

        // Remove Tags
        Collection<String> tagKeysToRemove = new HashSet<>();
        for (Map.Entry<String,String> tagEntry : currentTags.entrySet()) {
            String currentKey = tagEntry.getKey();
            if (desiredTags.get(currentKey) == null) {
                tagKeysToRemove.add(currentKey);
            }
        }

        if (!tagKeysToRemove.isEmpty()) {
            UntagResourceRequest untagRequest = UntagResourceRequest.builder()
                    .resourceArn(applicationArn)
                    .tagKeys(tagKeysToRemove)
                    .build();
            proxy.injectCredentialsAndInvokeV2(untagRequest, iotFleetHubClient::untagResource);
            logger.log(String.format("Called UntagResource for %s.", applicationArn));
        }
    }