private ProgressEvent createTargetGroupAndUpdateProgress()

in aws-rds-dbproxytargetgroup/src/main/java/software/amazon/rds/dbproxytargetgroup/CreateHandler.java [62:151]


    private ProgressEvent<ResourceModel, CallbackContext> createTargetGroupAndUpdateProgress(ResourceModel model,
                                                                                             CallbackContext callbackContext) {
        // This Lambda will continually be re-invoked with the current state of the proxy, finally succeeding when state stabilizes.
        final DBProxy proxyStateSoFar = callbackContext.getProxy();

        if (callbackContext.getStabilizationRetriesRemaining() == 0) {
            throw new RuntimeException(TIMED_OUT_MESSAGE);
        }

        if (callbackContext.getTargetGroupStatus() == null) {
            DBProxyTargetGroup targetGroupSettings = modifyProxyTargetGroup(model);
            model.setTargetGroupArn(targetGroupSettings.getTargetGroupArn());
            return ProgressEvent.<ResourceModel, CallbackContext>builder()
                           .resourceModel(model)
                           .status(OperationStatus.IN_PROGRESS)
                           .callbackContext(CallbackContext.builder()
                                                           .proxy(proxyStateSoFar)
                                                           .targetGroupStatus(targetGroupSettings)
                                                           .stabilizationRetriesRemaining(Constants.NUMBER_OF_STATE_POLL_RETRIES)
                                                           .build())
                           .build();

        } else {
            if (callbackContext.getTargets() == null) {
                //If targets have not been setup, register them
                List<DBProxyTarget> targets = registerDefaultTarget(model);
                return ProgressEvent.<ResourceModel, CallbackContext>builder()
                               .resourceModel(model)
                               .status(OperationStatus.IN_PROGRESS)
                               .callbackContext(CallbackContext.builder()
                                                               .proxy(proxyStateSoFar)
                                                               .targetGroupStatus(callbackContext.getTargetGroupStatus())
                                                               .targets(targets)
                                                               .stabilizationRetriesRemaining(Constants.NUMBER_OF_STATE_POLL_RETRIES)
                                                               .build())
                               .build();
            } else {
                if (!callbackContext.isAllTargetsHealthy()) {
                    boolean allTargetsHealthy = checkTargetHealth(model);

                    try {
                        Thread.sleep(Constants.POLL_RETRY_DELAY_IN_MS);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                    return ProgressEvent.<ResourceModel, CallbackContext>builder()
                                   .resourceModel(model)
                                   .status(OperationStatus.IN_PROGRESS)
                                   .callbackContext(CallbackContext.builder()
                                       .proxy(proxyStateSoFar)
                                       .targetGroupStatus(callbackContext.getTargetGroupStatus())
                                       .targets(callbackContext.getTargets())
                                       .stabilizationRetriesRemaining(callbackContext.getStabilizationRetriesRemaining() - 1)
                                       .allTargetsHealthy(allTargetsHealthy)
                                       .build())
                                   .build();
                } else {
                    if (!callbackContext.isAllTargetsHealthy()) {
                        boolean allTargetsHealthy = checkTargetHealth(model);

                        try {
                            Thread.sleep(Constants.POLL_RETRY_DELAY_IN_MS);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }

                        return ProgressEvent.<ResourceModel, CallbackContext>builder()
                                       .resourceModel(model)
                                       .status(OperationStatus.IN_PROGRESS)
                                       .callbackContext(CallbackContext.builder()
                                           .proxy(proxyStateSoFar)
                                           .targetGroupStatus(callbackContext.getTargetGroupStatus())
                                           .targets(callbackContext.getTargets())
                                           .stabilizationRetriesRemaining(callbackContext.getStabilizationRetriesRemaining() - 1)
                                           .allTargetsHealthy(allTargetsHealthy)
                                           .build())
                                       .build();
                    } else {
                        //All setup has been completed
                        return ProgressEvent.<ResourceModel, CallbackContext>builder()
                                       .resourceModel(model)
                                       .status(OperationStatus.SUCCESS)
                                       .build();
                    }
                }
            }
        }

    }