private void updateTagsOnDeliveryStream()

in deliverystream/src/main/java/com/amazonaws/kinesisfirehose/deliverystream/UpdateHandler.java [247:299]


    private void updateTagsOnDeliveryStream(final FirehoseAPIWrapper firehoseAPIWrapper,
                                            final ResourceModel model,
                                            final ResourceModel previousModel,
                                            final Logger logger,
                                            final List<Tag> previousResourceAndStackTags,
                                            final List<Tag> currentResourceAndStackTags) {
        val tagKeysToRemove = HandlerUtils.tagsInFirstListButNotInSecond(previousResourceAndStackTags, currentResourceAndStackTags);
        if (tagKeysToRemove != null && !tagKeysToRemove.isEmpty()) {
            boolean wasExceptionThrown = false;
            try {
                firehoseAPIWrapper.untagDeliveryStream(model.getDeliveryStreamName(), tagKeysToRemove);
            } catch(Exception e){
                wasExceptionThrown = true;
                // If previous model didn't had any resource tags, and we tried to remove tags only because of stack level tags, this might be unexpected from
                // a customer standpoint and might look like a breaking API change. We need to do a soft fail.
                if (customerDidNotSpecifiedModelTags(previousModel, model) && (e instanceof FirehoseException
                    && ((FirehoseException) e).awsErrorDetails() != null
                    && (HandlerUtils.ACCESS_DENIED_ERROR_CODE
                    .equals(((FirehoseException) e).awsErrorDetails().errorCode())))) {
                    logger.log(String.format(ACCESS_DENIED_FOR_SPECIFIED_API_FORMAT, "UntagDeliveryStream",  model.getDeliveryStreamName()));
                } else {
                    // Surface the error to the customer if they explicitly wanted to use tags, or if we ran into a different error while talking to the backend API.
                    throw e;
                }
            }
            if (!wasExceptionThrown) {
                logger.log(String.format("Removed %d existing tags for the delivery stream name:%s",tagKeysToRemove.size(),
                        model.getDeliveryStreamName()));
            }
        }

        if (currentResourceAndStackTags != null && !currentResourceAndStackTags.isEmpty()) {
            boolean wasExceptionThrown = false;
            try {
                firehoseAPIWrapper.tagDeliveryStream(model.getDeliveryStreamName(), HandlerUtils.translateCFNModelTagsToFirehoseSDKTags(currentResourceAndStackTags));
            } catch(Exception e){
                wasExceptionThrown = true;
                // If current model didn't had any resource tags, and we tried to add tags during update only because of stack level tags, this might be unexpected from
                // a customer standpoint and might look like a breaking API change if the customer didn't had permissions. We need to do a soft fail.
                if (customerDidNotSpecifiedModelTags(previousModel, model) && (e instanceof FirehoseException
                    && ((FirehoseException)e).awsErrorDetails().errorCode().equals(HandlerUtils.ACCESS_DENIED_ERROR_CODE))){
                    logger.log(String.format(ACCESS_DENIED_FOR_SPECIFIED_API_FORMAT, "TagDeliveryStream", model.getDeliveryStreamName()));
                } else {
                    // Surface the error to the customer if they explicitly wanted to use tags, or if we ran into a different error while talking to the backend API.
                    throw e;
                }
            }
            if (!wasExceptionThrown) {
                logger.log(String.format("Added/Replaced %d tags for the delivery stream name:%s", currentResourceAndStackTags.size(),
                        model.getDeliveryStreamName()));
            }
        }
    }