public static void updateTags()

in aws-codeguruprofiler-profilinggroup/src/main/java/software/amazon/codeguruprofiler/profilinggroup/TagHelper.java [34:128]


    public static void updateTags(AmazonWebServicesClientProxy proxy,
                                  ResourceModel desiredModel,
                                  String awsAccountId,
                                  String resourceArn,
                                  Logger logger) {
        Set<Tag> existingTags = convertTagMapIntoSet(listTagsForResource(proxy, resourceArn).tags());

        Set<Tag> desiredTags = tagsFromModel(desiredModel);

        if (existingTags.equals(desiredTags)) {
            logger.log(
                String.format("No tag change detected for [%s] for accountId [%s]: Old: %s, New: %s",
                    resourceArn,
                    awsAccountId,
                    existingTags,
                    desiredTags
                )
            );
            return;
        }

        logger.log(
            String.format("Tag change detected for [%s] for accountId [%s]: Old: %s, New: %s",
                resourceArn,
                awsAccountId,
                existingTags,
                desiredTags
            )
        );

        final Set<Tag> tagsToRemove = new HashSet<>(existingTags);
        tagsToRemove.removeIf(desiredTags::contains);
        desiredTags.removeIf(existingTags::contains);

        if (!tagsToRemove.isEmpty()) {
            logger.log(
                String.format("Untagging tags from [%s] for accountId [%s]: %s",
                    resourceArn,
                    awsAccountId,
                    tagsToRemove
                )
            );
            untagResource(proxy, resourceArn, tagsToRemove.stream().map(Tag::getKey).collect(Collectors.toSet()));
            logger.log(
                String.format("Successfully untagged tags from [%s] for accountId [%s]",
                    resourceArn,
                    awsAccountId
                )
            );
        }

        if (!desiredTags.isEmpty()) {
            logger.log(
                String.format("Adding new tags to [%s] for accountId [%s]: %s",
                    resourceArn,
                    awsAccountId,
                    desiredTags
                )
            );
            try {
                tagResource(proxy, resourceArn, desiredTags.stream().collect(Collectors.toMap(Tag::getKey, Tag::getValue)));
            } catch(CodeGuruProfilerException e) {
                logger.log(
                    String.format("Failed to add new tags to [%s] for accountId [%s]",
                        resourceArn,
                        awsAccountId
                    )
                );

                if (!tagsToRemove.isEmpty()) {
                    logger.log(
                        String.format("Adding back old tags to [%s] for accountId [%s]: %s",
                            resourceArn,
                            awsAccountId,
                            desiredTags
                        )
                    );
                    tagResource(proxy, resourceArn, tagsToRemove.stream().collect(Collectors.toMap(Tag::getKey, Tag::getValue)));
                    logger.log(
                        String.format("Successfully added back old tags to [%s] for accountId [%s]",
                            resourceArn,
                            awsAccountId
                        )
                    );
                }
                throw e;
            }
            logger.log(
                String.format("Successfully added new tags to [%s] for accountId [%s]",
                    resourceArn,
                    awsAccountId
                )
            );
        }
    }