package software.amazon.sagemaker.pipeline; import software.amazon.awssdk.awscore.exception.AwsServiceException; import software.amazon.awssdk.services.sagemaker.SageMakerClient; import software.amazon.awssdk.services.sagemaker.model.DeletePipelineRequest; import software.amazon.awssdk.services.sagemaker.model.DeletePipelineResponse; import software.amazon.awssdk.services.sagemaker.model.ResourceNotFoundException; 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.OperationStatus; import software.amazon.cloudformation.proxy.ProgressEvent; import software.amazon.cloudformation.proxy.ProxyClient; import software.amazon.cloudformation.proxy.ResourceHandlerRequest; public class DeleteHandler extends BaseHandlerStd { private static final String OPERATION = "AWS-SageMaker-Pipeline::Delete"; private Logger logger; @Override protected ProgressEvent handleRequest( AmazonWebServicesClientProxy proxy, ResourceHandlerRequest request, CallbackContext callbackContext, ProxyClient proxyClient, Logger logger ) { this.logger = logger; final ResourceModel model = request.getDesiredResourceState(); return ProgressEvent.progress(model, callbackContext) .then(progress -> proxy.initiate(OPERATION, proxyClient, model, callbackContext) .translateToServiceRequest(TranslatorForRequest::translateToDeleteRequest) .makeServiceCall(this::deleteResource) .done(awsResponse -> ProgressEvent.builder() .status(OperationStatus.SUCCESS) .build())); } /** * Implement client invocation of the delete request through the proxyClient. * @param awsRequest the aws service request to delete a resource * @param proxyClient the aws service client to make the call * @return delete resource response */ private DeletePipelineResponse deleteResource( final DeletePipelineRequest awsRequest, final ProxyClient proxyClient ) { DeletePipelineResponse response = null; try { response = proxyClient.injectCredentialsAndInvokeV2(awsRequest, proxyClient.client()::deletePipeline); } catch (ResourceNotFoundException e) { // NotFound responded from Delete handler will be considered as success by CFN backend service. throw new CfnNotFoundException(ResourceModel.TYPE_NAME, awsRequest.pipelineName()); } catch (final AwsServiceException e) { Translator.throwCfnException(Action.DELETE.toString(), ResourceModel.TYPE_NAME, awsRequest.pipelineName(), e); } return response; } }