package software.amazon.sns.topicpolicy; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.SetTopicAttributesResponse; import software.amazon.awssdk.utils.CollectionUtils; import software.amazon.cloudformation.proxy.ProgressEvent; import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy; import software.amazon.cloudformation.proxy.ProxyClient; import software.amazon.cloudformation.proxy.ResourceHandlerRequest; import software.amazon.cloudformation.proxy.Logger; import software.amazon.cloudformation.proxy.HandlerErrorCode; import java.util.HashSet; import java.util.Set; public class DeleteHandler extends BaseHandlerStd { private software.amazon.cloudformation.proxy.Logger logger; protected ProgressEvent handleRequest( final AmazonWebServicesClientProxy proxy, final ResourceHandlerRequest request, final CallbackContext callbackContext, final ProxyClient proxyClient, final Logger logger) { this.logger = logger; ResourceModel resourceModel = request.getDesiredResourceState(); if (resourceModel == null) { return ProgressEvent.failed(resourceModel, callbackContext, HandlerErrorCode.InvalidRequest, "Invalid request"); } else if(CollectionUtils.isNullOrEmpty(resourceModel.getTopics())){ return ProgressEvent.failed(resourceModel, callbackContext, null, "Value of property Topics must be of type List of String"); } logger.log(String.format("[StackId: %s, ClientRequestToken: %s] Calling Delete TopicPolicy", request.getStackId(), request.getClientRequestToken())); return ProgressEvent.progress(resourceModel, callbackContext) .then(progress -> Delete(proxy, proxyClient, request, progress, logger)) .then(progress -> ProgressEvent.defaultSuccessHandler(null)); } protected ProgressEvent Delete( final AmazonWebServicesClientProxy proxy, final ProxyClient proxyClient, final ResourceHandlerRequest request, ProgressEvent progress, final Logger logger) { final ResourceModel model = request.getDesiredResourceState(); final CallbackContext callbackContext = progress.getCallbackContext(); callbackContext.setIgnoreNotFound(true); final Set topics = new HashSet<>(model.getTopics()); for (final String topicArn : topics) { String policy = getDefaultPolicy(request,topicArn); final ProgressEvent progressEvent = proxy .initiate("AWS-SNS-TopicPolicy::Delete", proxyClient, model, callbackContext) .translateToServiceRequest((resourceModel) -> Translator.translateToRequest(topicArn, policy)) .makeServiceCall((awsRequest, client) -> { SetTopicAttributesResponse response = proxyClient.injectCredentialsAndInvokeV2(awsRequest, client.client()::setTopicAttributes); logger.log(String.format("Resource Deleted in StackId: %s", request.getStackId())); return response; }) .handleError((awsRequest, exception, client, rModel, context) -> handleError(awsRequest, exception, client, rModel, context)) .success(); if (!progressEvent.isSuccess()) { return progressEvent; } } return ProgressEvent.progress(model, callbackContext); } }