package software.amazon.rds.dbclusterendpoint; import software.amazon.awssdk.services.rds.RdsClient; import software.amazon.awssdk.services.rds.model.DBClusterEndpoint; import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy; import software.amazon.cloudformation.proxy.HandlerErrorCode; import software.amazon.cloudformation.proxy.Logger; import software.amazon.cloudformation.proxy.ProgressEvent; import software.amazon.cloudformation.proxy.ProxyClient; import software.amazon.cloudformation.proxy.ResourceHandlerRequest; import software.amazon.rds.common.handler.Commons; import software.amazon.rds.common.handler.HandlerConfig; import software.amazon.rds.common.handler.Tagging; import java.util.Optional; import java.util.Set; public class ReadHandler extends BaseHandlerStd { public ReadHandler() { this(HandlerConfig.builder() .backoff(BACKOFF_DELAY) .build()); } public ReadHandler(HandlerConfig config) { super(config); } @Override protected ProgressEvent handleRequest( final AmazonWebServicesClientProxy proxy, final ResourceHandlerRequest request, final CallbackContext callbackContext, final ProxyClient proxyClient, final Logger logger ) { return proxy.initiate("rds::describe-db-cluster-endpoint", proxyClient, request.getDesiredResourceState(), callbackContext) .translateToServiceRequest(Translator::describeDbClustersEndpointRequest) .backoffDelay(config.getBackoff()) .makeServiceCall((describeRequest, proxyInvocation) -> proxyInvocation.injectCredentialsAndInvokeV2( describeRequest, proxyInvocation.client()::describeDBClusterEndpoints )) .handleError((describeRequest, exception, client, resourceModel, ctx) -> Commons.handleException( ProgressEvent.progress(resourceModel, ctx), exception, DEFAULT_DB_CLUSTER_ENDPOINT_ERROR_RULE_SET)) .done((describeRequest, describeResponse, proxyInvocation, model, context) -> { final Optional dbClusterEndpoint = describeResponse.dbClusterEndpoints().stream().findFirst(); if (!dbClusterEndpoint.isPresent()) { return ProgressEvent.failed(model, context, HandlerErrorCode.NotFound, "DBClusterEndpoint " + model.getDBClusterEndpointIdentifier() + " not found"); } return ProgressEvent.progress(Translator.translateDbClusterEndpointFromSdk(dbClusterEndpoint.get()), context); }) .then(progress -> readTags(proxyClient, progress)); } protected ProgressEvent readTags( final ProxyClient proxyClient, final ProgressEvent progress) { ResourceModel model = progress.getResourceModel(); CallbackContext context = progress.getCallbackContext(); try { String arn = model.getDBClusterEndpointArn(); Set resourceTags = Translator.translateTagsFromSdk(Tagging.listTagsForResource(proxyClient, arn)); model.setTags(resourceTags); } catch (Exception exception) { return Commons.handleException( ProgressEvent.progress(model, context), exception, DEFAULT_DB_CLUSTER_ENDPOINT_ERROR_RULE_SET.extendWith(Tagging.SOFT_FAIL_TAG_ERROR_RULE_SET) ); } return ProgressEvent.success(model, context); } }