package software.amazon.sagemaker.app; import software.amazon.awssdk.awscore.exception.AwsServiceException; import software.amazon.awssdk.services.sagemaker.SageMakerClient; import software.amazon.awssdk.services.sagemaker.model.AppStatus; import software.amazon.awssdk.services.sagemaker.model.DescribeAppRequest; import software.amazon.awssdk.services.sagemaker.model.DescribeAppResponse; import software.amazon.cloudformation.Action; import software.amazon.cloudformation.exceptions.CfnNotFoundException; 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 ReadHandler extends BaseHandlerStd { private static final String OPERATION = "AWS-SageMaker-App::Read"; private Logger logger; @Override public 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(); return proxy.initiate(OPERATION, proxyClient, model, callbackContext) .translateToServiceRequest(TranslatorForRequest::translateToReadRequest) .makeServiceCall((awsRequest, sdkProxyClient) -> readResource(awsRequest, sdkProxyClient, model)) .done(this::constructResourceModelFromResponse); } /** * Client invocation of the read request through the proxyClient. * * @param awsRequest the aws service describe resource request * @param proxyClient the aws service client to make the call * @param model Resource Model * @return describe resource response */ private DescribeAppResponse readResource( final DescribeAppRequest awsRequest, final ProxyClient proxyClient, final ResourceModel model) { DescribeAppResponse response = null; try { response = proxyClient.injectCredentialsAndInvokeV2(awsRequest, proxyClient.client()::describeApp); } catch (final AwsServiceException e) { Translator.throwCfnException(Action.READ.toString(), ResourceModel.TYPE_NAME, awsRequest.appName(), e); } // Deleted Apps stay present for 24 hours with a Deleted status. // Deleted resources are expected to throw CfnNotFoundException. if (response.status().equals(AppStatus.DELETED)) { throw new CfnNotFoundException(ResourceModel.TYPE_NAME, awsRequest.appName()); } return response; } /** * Implement client invocation of the read request through the proxyClient. * * @param awsResponse the aws service describe resource response * @return progressEvent indicating success, in progress, delay, callback or failed state */ private ProgressEvent constructResourceModelFromResponse( final DescribeAppResponse awsResponse) { return ProgressEvent.defaultSuccessHandler(TranslatorForResponse.translateFromReadResponse(awsResponse)); } }