public ProgressEvent handleRequest()

in aws-iot-securityprofile/src/main/java/com/amazonaws/iot/securityprofile/CreateHandler.java [35:88]


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

        CreateSecurityProfileRequest createRequest = translateToCreateRequest(request, logger);

        ResourceModel model = request.getDesiredResourceState();
        if (!StringUtils.isEmpty(model.getSecurityProfileArn())) {
            logger.log(String.format("Arn is read-only, but the caller passed %s.", model.getSecurityProfileArn()));
            // Note: this is necessary even though Arn is marked readOnly in the schema.
            return ProgressEvent.failed(model, callbackContext, HandlerErrorCode.InvalidRequest,
                    "Arn is a read-only property and cannot be set.");
        }

        CreateSecurityProfileResponse createResponse;
        try {
            createResponse = proxy.injectCredentialsAndInvokeV2(
                    createRequest, iotClient::createSecurityProfile);
        } catch (ResourceAlreadyExistsException e) {
            logger.log(String.format("Resource already exists %s.", model.getSecurityProfileName()));
            throw new CfnAlreadyExistsException(e);
        } catch (RuntimeException e) {
            return Translator.translateExceptionToProgressEvent(model, e, logger);
        }

        model.setSecurityProfileArn(createResponse.securityProfileArn());
        logger.log("Created " + createResponse.securityProfileArn());

        // We're letting customers manage Security Profile attachments in the same CFN template,
        // using the TargetArns field. Thus, we need to make an AttachSecurityProfile call for every target.
        Set<String> targetArns = model.getTargetArns();
        if (targetArns != null) {
            // The number of targets can be large, we need to avoid getting throttled.
            RateLimiter rateLimiter = RateLimiter.create(MAX_CALLS_PER_SECOND_LIMIT);
            for (String targetArn : targetArns) {
                rateLimiter.acquire();

                AttachSecurityProfileRequest attachRequest = AttachSecurityProfileRequest.builder()
                        .securityProfileName(model.getSecurityProfileName())
                        .securityProfileTargetArn(targetArn)
                        .build();
                try {
                    proxy.injectCredentialsAndInvokeV2(attachRequest, iotClient::attachSecurityProfile);
                } catch (RuntimeException e) {
                    return Translator.translateExceptionToProgressEvent(model, e, logger);
                }
                logger.log("Attached the security profile to " + targetArn);
            }
        }

        return ProgressEvent.defaultSuccessHandler(model);
    }