public ProgressEvent handleRequest()

in aws-iot-certificate/src/main/java/com/amazonaws/iot/certificate/CreateHandler.java [95:188]


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

        final ResourceModel model = request.getDesiredResourceState();
        if (model.getCertificateMode() == null) {
            model.setCertificateMode(CERTIFICATE_MODE_DEFAULT);
        }

        // Determine the creation mode we are in based on which request fields are present and create accordingly
        IotRequest currentRequest = null;
        String currentOperation = null;
        try {
            if (isMuliAccountRequest(model)) {
                final RegisterCertificateWithoutCaRequest registerRequest = RegisterCertificateWithoutCaRequest.builder()
                        .certificatePem(model.getCertificatePem())
                        .status(model.getStatus())
                        .build();

                currentOperation = REGISTER_WITHOUT_CA_OPERATION;
                currentRequest = registerRequest;

                final RegisterCertificateWithoutCaResponse registerResponse = proxy.injectCredentialsAndInvokeV2(
                        registerRequest,
                        iotClient::registerCertificateWithoutCA);

                model.setArn(registerResponse.certificateArn());
                model.setId(registerResponse.certificateId());

            } else if (isCsrRequest(model)) {
                final CreateCertificateFromCsrRequest signingRequest = CreateCertificateFromCsrRequest.builder()
                        .certificateSigningRequest(model.getCertificateSigningRequest())
                        .build();

                currentOperation = SIGNING_OPERATION;
                currentRequest = signingRequest;

                final CreateCertificateFromCsrResponse signingResponse = proxy.injectCredentialsAndInvokeV2(
                        signingRequest,
                        iotClient::createCertificateFromCsr);

                model.setArn(signingResponse.certificateArn());
                model.setId(signingResponse.certificateId());

                // Update the status to the desired state
                final UpdateCertificateRequest updateRequest = UpdateCertificateRequest.builder()
                        .certificateId(signingResponse.certificateId())
                        .newStatus(model.getStatus())
                        .build();
                proxy.injectCredentialsAndInvokeV2(updateRequest, iotClient::updateCertificate);

            } else if (isCertificatePemRequest(model)) {
                final RegisterCertificateRequest registerRequest = RegisterCertificateRequest.builder()
                        .certificatePem(model.getCertificatePem())
                        .caCertificatePem(model.getCACertificatePem())
                        .status(model.getStatus())
                        .build();

                currentOperation = REGISTER_OPERATION;
                currentRequest = registerRequest;

                final RegisterCertificateResponse registerResponse = proxy.injectCredentialsAndInvokeV2(
                        registerRequest,
                        iotClient::registerCertificate);

                model.setArn(registerResponse.certificateArn());
                model.setId(registerResponse.certificateId());

            } else {
                // Invalid configuration, throw a CFN exception
                throw new CfnGeneralServiceException("Invalid certificate resource configuration");
            }

            logger.log(String.format("%s [%s] registered successfully", ResourceModel.TYPE_NAME, model.getId()));
            return ProgressEvent.defaultSuccessHandler(model);

        } catch (final ResourceAlreadyExistsException e) {
            throw new CfnAlreadyExistsException(e);
        } catch (final InternalFailureException|InternalException e) {
            throw new CfnServiceInternalErrorException(currentOperation, e);
        } catch (final InvalidRequestException e) {
            throw new CfnInvalidRequestException(e.getMessage(), e);
        } catch (final LimitExceededException e) {
            throw new CfnServiceLimitExceededException(ResourceModel.TYPE_NAME, e.getMessage());
        } catch (final ServiceUnavailableException e) {
            throw new CfnGeneralServiceException(currentOperation, e);
        } catch (final ThrottlingException e) {
            throw new CfnThrottlingException(currentOperation, e);
        } catch (final UnauthorizedException e) {
            throw new CfnAccessDeniedException(currentOperation, e);
        }
    }