|
| 1 | +package software.amazon.rds.dbinstance.common.create; |
| 2 | + |
| 3 | +import lombok.AllArgsConstructor; |
| 4 | +import software.amazon.awssdk.services.rds.RdsClient; |
| 5 | +import software.amazon.awssdk.services.rds.model.DBInstance; |
| 6 | +import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy; |
| 7 | +import software.amazon.cloudformation.proxy.ProgressEvent; |
| 8 | +import software.amazon.cloudformation.proxy.ProxyClient; |
| 9 | +import software.amazon.rds.common.handler.Commons; |
| 10 | +import software.amazon.rds.common.handler.HandlerConfig; |
| 11 | +import software.amazon.rds.common.handler.HandlerMethod; |
| 12 | +import software.amazon.rds.common.handler.Tagging; |
| 13 | +import software.amazon.rds.common.logging.RequestLogger; |
| 14 | +import software.amazon.rds.dbinstance.CallbackContext; |
| 15 | +import software.amazon.rds.dbinstance.DBInstancePredicates; |
| 16 | +import software.amazon.rds.dbinstance.ResourceModel; |
| 17 | +import software.amazon.rds.dbinstance.Translator; |
| 18 | +import software.amazon.rds.dbinstance.client.ApiVersion; |
| 19 | +import software.amazon.rds.dbinstance.client.ApiVersionDispatcher; |
| 20 | +import software.amazon.rds.dbinstance.client.VersionedProxyClient; |
| 21 | +import software.amazon.rds.dbinstance.common.ErrorRuleSets; |
| 22 | +import software.amazon.rds.dbinstance.common.Fetch; |
| 23 | + |
| 24 | +@AllArgsConstructor |
| 25 | +public class FreshInstance implements DBInstanceFactory { |
| 26 | + |
| 27 | + private final AmazonWebServicesClientProxy proxy; |
| 28 | + private final VersionedProxyClient<RdsClient> rdsProxyClient; |
| 29 | + private final Tagging.TagSet allTags; |
| 30 | + private final RequestLogger requestLogger; |
| 31 | + private final HandlerConfig config; |
| 32 | + private final ApiVersionDispatcher<ResourceModel, CallbackContext> apiVersionDispatcher; |
| 33 | + |
| 34 | + @Override |
| 35 | + public ProgressEvent<ResourceModel, CallbackContext> create(ProgressEvent<ResourceModel, CallbackContext> progress) { |
| 36 | + final ResourceModel model = progress.getResourceModel(); |
| 37 | + final CallbackContext callbackContext = progress.getCallbackContext(); |
| 38 | + final ApiVersion apiVersion = selectVersion(model, callbackContext); |
| 39 | + |
| 40 | + if (apiVersion == ApiVersion.V12) { |
| 41 | + return createDbInstanceV12(proxy, rdsProxyClient.forVersion(apiVersion), progress, allTags); |
| 42 | + } |
| 43 | + |
| 44 | + return safeAddTags(this::createDbInstance).invoke(proxy, rdsProxyClient.forVersion(apiVersion), progress, allTags); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean modelSatisfiesConstructor(ResourceModel model) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + private ProgressEvent<ResourceModel, CallbackContext> createDbInstance( |
| 53 | + final AmazonWebServicesClientProxy proxy, |
| 54 | + final ProxyClient<RdsClient> rdsProxyClient, |
| 55 | + final ProgressEvent<ResourceModel, CallbackContext> progress, |
| 56 | + final Tagging.TagSet tagSet |
| 57 | + ) { |
| 58 | + final Fetch fetch = new Fetch(rdsProxyClient); |
| 59 | + return proxy.initiate( |
| 60 | + "rds::create-db-instance", |
| 61 | + rdsProxyClient, |
| 62 | + progress.getResourceModel(), |
| 63 | + progress.getCallbackContext() |
| 64 | + ).translateToServiceRequest(model -> Translator.createDbInstanceRequest(model, tagSet)) |
| 65 | + .backoffDelay(config.getBackoff()) |
| 66 | + .makeServiceCall((createRequest, proxyInvocation) -> proxyInvocation.injectCredentialsAndInvokeV2( |
| 67 | + createRequest, |
| 68 | + proxyInvocation.client()::createDBInstance |
| 69 | + )) |
| 70 | + .stabilize((request, response, proxyInvocation, model, context) -> { |
| 71 | + final DBInstance dbInstance = fetch.dbInstance(model); |
| 72 | + return DBInstancePredicates.isDBInstanceStabilizedAfterMutate(dbInstance, model, context, requestLogger); |
| 73 | + }) |
| 74 | + .handleError((request, exception, client, model, context) -> Commons.handleException( |
| 75 | + ProgressEvent.progress(model, context), |
| 76 | + exception, |
| 77 | + software.amazon.rds.dbinstance.common.ErrorRuleSets.CREATE_DB_INSTANCE, |
| 78 | + requestLogger |
| 79 | + )) |
| 80 | + .progress(); |
| 81 | + } |
| 82 | + |
| 83 | + private ProgressEvent<ResourceModel, CallbackContext> createDbInstanceV12( |
| 84 | + final AmazonWebServicesClientProxy proxy, |
| 85 | + final ProxyClient<RdsClient> rdsProxyClient, |
| 86 | + final ProgressEvent<ResourceModel, CallbackContext> progress, |
| 87 | + final Tagging.TagSet tagSet |
| 88 | + ) { |
| 89 | + requestLogger.log("CreateDbInstanceAPIv12Invoked"); |
| 90 | + requestLogger.log("API version 12 create detected", |
| 91 | + "This indicates that the customer is using DBSecurityGroup, which may result in certain features not" + |
| 92 | + " functioning properly. Please refer to the API model for supported parameters"); |
| 93 | + final Fetch fetch = new Fetch(rdsProxyClient); |
| 94 | + return proxy.initiate( |
| 95 | + "rds::create-db-instance-v12", |
| 96 | + rdsProxyClient, |
| 97 | + progress.getResourceModel(), |
| 98 | + progress.getCallbackContext() |
| 99 | + ).translateToServiceRequest(Translator::createDbInstanceRequestV12) |
| 100 | + .backoffDelay(config.getBackoff()) |
| 101 | + .makeServiceCall((createRequest, proxyInvocation) -> proxyInvocation.injectCredentialsAndInvokeV2( |
| 102 | + createRequest, |
| 103 | + proxyInvocation.client()::createDBInstance |
| 104 | + )) |
| 105 | + .stabilize((request, response, proxyInvocation, model, context) -> { |
| 106 | + final DBInstance dbInstance = fetch.dbInstance(model); |
| 107 | + return DBInstancePredicates.isDBInstanceStabilizedAfterMutate(dbInstance, model, context, requestLogger); |
| 108 | + }) |
| 109 | + .handleError((request, exception, client, model, context) -> Commons.handleException( |
| 110 | + ProgressEvent.progress(model, context), |
| 111 | + exception, |
| 112 | + ErrorRuleSets.CREATE_DB_INSTANCE, |
| 113 | + requestLogger |
| 114 | + )) |
| 115 | + .progress(); |
| 116 | + } |
| 117 | + |
| 118 | + private ApiVersion selectVersion(ResourceModel model, CallbackContext callbackContext) { |
| 119 | + return apiVersionDispatcher.dispatch(model, callbackContext); |
| 120 | + } |
| 121 | + |
| 122 | + private HandlerMethod<ResourceModel, CallbackContext> safeAddTags(final HandlerMethod<ResourceModel, CallbackContext> handlerMethod) { |
| 123 | + return (proxy, rdsProxyClient, progress, tagSet) -> progress.then(p -> Tagging.createWithTaggingFallback(proxy, rdsProxyClient, handlerMethod, progress, tagSet)); |
| 124 | + } |
| 125 | +} |
0 commit comments