package software.amazon.rds.dbsubnetgroup; import java.util.List; import software.amazon.awssdk.services.rds.RdsClient; import software.amazon.awssdk.services.rds.model.DBSubnetGroup; import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy; 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; public class ReadHandler extends BaseHandlerStd { public ReadHandler() { this(HandlerConfig.builder().build()); } public ReadHandler(final HandlerConfig config) { super(config); } protected ProgressEvent handleRequest( final AmazonWebServicesClientProxy proxy, final ResourceHandlerRequest request, final CallbackContext callbackContext, final ProxyClient proxyClient, final Logger logger ) { return ProgressEvent.progress(request.getDesiredResourceState(), callbackContext) .then(progress -> describeDbSubnetGroup(proxy, request, callbackContext, proxyClient)) .then(progress -> readTags(proxyClient, progress)); } private ProgressEvent describeDbSubnetGroup(final AmazonWebServicesClientProxy proxy, final ResourceHandlerRequest request, final CallbackContext callbackContext, final ProxyClient proxyClient ) { return proxy.initiate("rds::read-dbsubnet-group", proxyClient, request.getDesiredResourceState(), callbackContext) .translateToServiceRequest(Translator::describeDbSubnetGroupsRequest) .backoffDelay(config.getBackoff()) .makeServiceCall((describeDbSubnetGroupRequest, proxyInvocation) -> proxyInvocation.injectCredentialsAndInvokeV2(describeDbSubnetGroupRequest, proxyInvocation.client()::describeDBSubnetGroups)) .handleError((awsRequest, exception, client, resourceModel, context) -> Commons.handleException( ProgressEvent.progress(resourceModel, context), exception, DEFAULT_DB_SUBNET_GROUP_ERROR_RULE_SET)) .done((describeDbSubnetGroupsRequest, describeDbSubnetGroupsResponse, proxyInvocation, model, context) -> { final DBSubnetGroup dbSubnetGroup = describeDbSubnetGroupsResponse.dbSubnetGroups().stream().findFirst().get(); context.setDbSubnetGroupArn(dbSubnetGroup.dbSubnetGroupArn()); return ProgressEvent.progress(Translator.translateToModel(dbSubnetGroup), context); }); } protected ProgressEvent readTags( final ProxyClient proxyClient, final ProgressEvent progress ) { ResourceModel model = progress.getResourceModel(); CallbackContext context = progress.getCallbackContext(); try { String arn = progress.getCallbackContext().getDbSubnetGroupArn(); List resourceTags = Translator .translateTags(Tagging.listTagsForResource(proxyClient, arn)); model.setTags(resourceTags); } catch (Exception exception) { return Commons.handleException( ProgressEvent.progress(model, context), exception, DEFAULT_DB_SUBNET_GROUP_ERROR_RULE_SET.extendWith(Tagging.SOFT_FAIL_TAG_ERROR_RULE_SET) ); } return ProgressEvent.success(model, context); } }