protected ProgressEvent handleRequest()

in replicakey/src/main/java/software/amazon/kms/replicakey/CreateHandler.java [42:98]


    protected ProgressEvent<ResourceModel, CallbackContext> handleRequest(
        final AmazonWebServicesClientProxy proxy,
        final ResourceHandlerRequest<ResourceModel> request,
        final CallbackContext callbackContext,
        final ProxyClient<KmsClient> proxyClient,
        final Logger logger) {
        final ResourceModel model = setDefaults(request.getDesiredResourceState());

        return ProgressEvent.progress(model, callbackContext)
            .then(progress -> {
                    // We need to make the replicate request to the primary region
                    final ProxyClient<KmsClient> primaryRegionClient = proxy.newProxy(clientBuilder
                        .getClientForArnRegion(model.getPrimaryKeyArn()));

                    return proxy
                        .initiate("kms::replicate-key", primaryRegionClient, model, callbackContext)
                        .translateToServiceRequest(
                            m -> translator.replicateKeyRequest(m, request.getRegion(),
                                request.getDesiredResourceTags()))
                        .makeServiceCall(keyApiHelper::replicateKey)
                        .done(replicateKeyResponse -> {
                            // Continue along if we have already saved the replica key Id
                            if (!StringUtils.isNullOrEmpty(model.getKeyId())) {
                                return ProgressEvent.progress(model, callbackContext);
                            }

                            // Update our resource model with the replica key's Id
                            model.setKeyId(replicateKeyResponse.replicaKeyMetadata().keyId());
                            model.setArn(replicateKeyResponse.replicaKeyMetadata().arn());

                            // Wait for key state to propagate to other hosts
                            return ProgressEvent.defaultInProgressHandler(callbackContext,
                                EventualConsistencyHandlerHelper.EVENTUAL_CONSISTENCY_DELAY_SECONDS,
                                model);
                        });
                }
            )
            /*
             * Wait until after kms::replicate-key to stabilize the replica.
             * This allows us to do the eventual consistency wait before stabilization,
             * which makes it more likely that the replica will be stabilized on the first
             * stabilization check.
             */
            .then(progress -> proxy
                .initiate("kms::replicate-key-is-done-creating", proxyClient, model,
                    callbackContext)
                .translateToServiceRequest(Function.identity())
                .makeServiceCall(EMPTY_CALL)
                .stabilize((replicateRequest, replicateResponse, client, m, ctx) ->
                    isDoneCreating(model, proxyClient))
                .progress())
            .then(progress -> keyHandlerHelper
                .disableKeyIfNecessary(proxy, proxyClient, null, model, callbackContext))
            // Final propagation to make sure all updates are reflected
            .then(eventualConsistencyHandlerHelper::waitForChangesToPropagate)
            .then(progress -> ProgressEvent.defaultSuccessHandler(unsetWriteOnly(model)));
    }