package software.amazon.sagemaker.userprofile; import software.amazon.awssdk.awscore.exception.AwsServiceException; import software.amazon.awssdk.services.sagemaker.SageMakerClient; import software.amazon.awssdk.services.sagemaker.model.CreateUserProfileRequest; import software.amazon.awssdk.services.sagemaker.model.CreateUserProfileResponse; import software.amazon.awssdk.services.sagemaker.model.ResourceNotFoundException; import software.amazon.awssdk.services.sagemaker.model.UserProfileStatus; import software.amazon.cloudformation.Action; import software.amazon.cloudformation.exceptions.CfnInvalidRequestException; import software.amazon.cloudformation.exceptions.CfnNotStabilizedException; import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy; import software.amazon.cloudformation.proxy.Logger; import software.amazon.cloudformation.proxy.ProgressEvent; import software.amazon.cloudformation.proxy.ProxyClient; import software.amazon.cloudformation.proxy.ResourceHandlerRequest; public class CreateHandler extends BaseHandlerStd { private static final String OPERATION = "AWS-SageMaker-UserProfile::Create"; private static final String READ_ONLY_PROPERTY_ERROR_MESSAGE = "The following property '%s' is not allowed to configured."; private Logger logger; protected ProgressEvent handleRequest( final AmazonWebServicesClientProxy proxy, final ResourceHandlerRequest request, final CallbackContext callbackContext, final ProxyClient proxyClient, final Logger logger) { this.logger = logger; final ResourceModel model = request.getDesiredResourceState(); // read only properties are not allowed to be set by the user during creation. // https://github.com/aws-cloudformation/aws-cloudformation-resource-schema/issues/102 if (callbackContext.callGraphs().isEmpty()) { if (model.getUserProfileArn() != null) { throw new CfnInvalidRequestException(String.format(READ_ONLY_PROPERTY_ERROR_MESSAGE, "UserProfileArn")); } } return ProgressEvent.progress(model, callbackContext) .then(progress -> proxy.initiate(OPERATION, proxyClient, model, callbackContext) .translateToServiceRequest(TranslatorForRequest::translateToCreateRequest) .makeServiceCall(this::createResource) .stabilize(this::stabilizedOnCreate) .done(createResponse -> constructResourceModelFromResponse(model, createResponse)) ); } /** * Client invocation of the create request through the proxyClient * * @param createRequest aws service create resource request * @param proxyClient aws service client to make the call * @return create resource response */ private CreateUserProfileResponse createResource( final CreateUserProfileRequest createRequest, final ProxyClient proxyClient) { CreateUserProfileResponse response = null; try { response = proxyClient.injectCredentialsAndInvokeV2( createRequest, proxyClient.client()::createUserProfile); } catch (final AwsServiceException e) { Translator.throwCfnException(Action.CREATE.toString(), ResourceModel.TYPE_NAME, createRequest.userProfileName(), e); } return response; } /** * Ensure resource has moved from pending to terminal state. * * @param awsRequest the aws service request to create a resource * @param awsResponse the aws service response to create a resource * @param proxyClient the aws service client to make the call * @param model Resource Model * @param callbackContext call back context * @return boolean flag indicate if the creation is stabilized */ private boolean stabilizedOnCreate( final CreateUserProfileRequest awsRequest, final CreateUserProfileResponse awsResponse, final ProxyClient proxyClient, final ResourceModel model, final CallbackContext callbackContext) { if (model.getUserProfileName() == null) { model.setUserProfileName(awsRequest.userProfileName()); } if (model.getDomainId() == null) { model.setDomainId(awsRequest.domainId()); } final UserProfileStatus UserProfileStatus; try { UserProfileStatus = proxyClient.injectCredentialsAndInvokeV2( TranslatorForRequest.translateToReadRequest(model), proxyClient.client()::describeUserProfile).status(); } catch (ResourceNotFoundException rnfe) { logger.log(String.format("Resource not found for %s, stabilizing.", model.getPrimaryIdentifier())); return false; } switch (UserProfileStatus) { case IN_SERVICE: logger.log(String.format("%s [%s] has been stabilized with status %s.", ResourceModel.TYPE_NAME, model.getPrimaryIdentifier(), UserProfileStatus)); return true; case PENDING: logger.log(String.format("%s [%s] is stabilizing.", ResourceModel.TYPE_NAME, model.getPrimaryIdentifier())); return false; default: logger.log(String.format("%s [%s] failed to stabilize with status: %s.", ResourceModel.TYPE_NAME, model.getPrimaryIdentifier(), UserProfileStatus)); throw new CfnNotStabilizedException(ResourceModel.TYPE_NAME, model.getUserProfileName()); } } /** * Build the Progress Event object from the create response. * * @param model resource model * @param awsResponse aws service create resource response * @return progressEvent indicating success */ private ProgressEvent constructResourceModelFromResponse( final ResourceModel model, final CreateUserProfileResponse awsResponse) { model.setUserProfileArn(awsResponse.userProfileArn()); return ProgressEvent.defaultSuccessHandler(model); } }