public ProgressEvent handleRequest()

in aws-nimblestudio-studiocomponent/src/main/java/software/amazon/nimblestudio/studiocomponent/CreateHandler.java [31:101]


    public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
        final AmazonWebServicesClientProxy proxy,
        final ResourceHandlerRequest<ResourceModel> request,
        final CallbackContext callbackContext,
        final ProxyClient<NimbleClient> proxyClient,
        final Logger logger) {

        return ProgressEvent.progress(request.getDesiredResourceState(), callbackContext)
            .then(progress -> proxy
                .initiate(
                    "AWS-NimbleStudio-StudioComponent::Create",
                    proxyClient,
                    progress.getResourceModel(),
                    progress.getCallbackContext()
                )
                .translateToServiceRequest(model -> fromResourceModel(model, request.getClientRequestToken()))
                .makeServiceCall((awsRequest, client) -> {
                    try {
                        final CreateStudioComponentResponse createStudioComponentResponse = client
                            .injectCredentialsAndInvokeV2(awsRequest, client.client()::createStudioComponent);

                        logger.log(String.format("%s [%s] create requested successfully", ResourceModel.TYPE_NAME,
                            createStudioComponentResponse.studioComponent().studioComponentId()));

                        return createStudioComponentResponse;
                    } catch (final NimbleException e) {
                        logger.log(String.format("Exception during CREATE: %s.", ResourceModel.TYPE_NAME));
                        throw ExceptionTranslator.translateToCfnException(e);
                    }
                })
                .stabilize((awsRequest, awsResponse, client, model, context) -> {
                    final String studioComponentId = awsResponse.studioComponent().studioComponentId();
                    model.setStudioComponentId(studioComponentId);

                    GetStudioComponentRequest getStudioComponentRequest = GetStudioComponentRequest.builder()
                            .studioId(awsRequest.studioId())
                            .studioComponentId(studioComponentId)
                            .build();
                    GetStudioComponentResponse getStudioComponentResponse;

                    try {
                        getStudioComponentResponse = client.injectCredentialsAndInvokeV2(
                                getStudioComponentRequest, client.client()::getStudioComponent);
                    } catch (final NimbleException e) {
                        logger.log(String.format("Exception during creation: %s for id: %s.", ResourceModel.TYPE_NAME,
                                studioComponentId));
                        throw ExceptionTranslator.translateToCfnException(e);
                    }

                    if (StudioComponentState.CREATE_IN_PROGRESS.equals(getStudioComponentResponse.studioComponent().state())) {
                        logger.log(String.format("%s [%s] is in state CREATE_IN_PROGRESS, creation in progress",
                                ResourceModel.TYPE_NAME, studioComponentId));
                        return false;
                    }
                    if (StudioComponentState.READY.equals(getStudioComponentResponse.studioComponent().state())) {
                        logger.log(String.format("%s [%s] is in state READY, creation succeeded",
                                ResourceModel.TYPE_NAME, studioComponentId));
                        return true;
                    }

                    logger.log(String.format("%s [%s] is in error state %s, creation failed", ResourceModel.TYPE_NAME,
                            studioComponentId, getStudioComponentResponse.studioComponent().state()));
                    throw new CfnGeneralServiceException(String.format("Unexpected state %s: %s - %s",
                            getStudioComponentResponse.studioComponent().stateAsString(),
                            getStudioComponentResponse.studioComponent().statusCodeAsString(),
                            getStudioComponentResponse.studioComponent().statusMessage()));
                })
                .progress()
            )
            .then((r) -> new ReadHandler().handleRequest(proxy, request, callbackContext, proxyClient, logger));
    }