package software.amazon.datasync.locationefs; import software.amazon.awssdk.services.datasync.DataSyncClient; import software.amazon.awssdk.services.datasync.model.DataSyncException; import software.amazon.awssdk.services.datasync.model.DescribeLocationEfsRequest; import software.amazon.awssdk.services.datasync.model.DescribeLocationEfsResponse; import software.amazon.awssdk.services.datasync.model.InternalException; import software.amazon.awssdk.services.datasync.model.InvalidRequestException; import software.amazon.cloudformation.exceptions.CfnNotFoundException; import software.amazon.cloudformation.exceptions.CfnServiceInternalErrorException; import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy; import software.amazon.cloudformation.proxy.Logger; import software.amazon.cloudformation.proxy.ProgressEvent; import software.amazon.cloudformation.proxy.OperationStatus; import software.amazon.cloudformation.proxy.ResourceHandlerRequest; import java.util.Set; import java.util.stream.Collectors; public class ReadHandler extends BaseHandler { private static final String AWS_CFN_TAG_PREFIX = "aws:cloudformation:"; @Override public ProgressEvent handleRequest( final AmazonWebServicesClientProxy proxy, final ResourceHandlerRequest request, final CallbackContext callbackContext, final Logger logger) { final ResourceModel model = request.getDesiredResourceState(); final DataSyncClient client = ClientBuilder.getClient(); final DescribeLocationEfsRequest describeLocationEfsRequest; describeLocationEfsRequest = Translator.translateToReadRequest(model.getLocationArn()); DescribeLocationEfsResponse response; try { response = proxy.injectCredentialsAndInvokeV2(describeLocationEfsRequest, client::describeLocationEfs); } catch (InvalidRequestException e) { throw new CfnNotFoundException(ResourceModel.TYPE_NAME, model.getLocationArn()); } catch (InternalException e) { throw new CfnServiceInternalErrorException(e.getMessage(), e.getCause()); } catch (DataSyncException e) { throw Translator.translateDataSyncExceptionToCfnException(e); } // Current tags are not supplied by the Describe call and must be retrieved separately final Set allTags = TagRequestMaker.listTagsForResource(proxy, client, model.getLocationArn()); final Set userTags = allTags.stream() .filter(tag -> !tag.getKey().startsWith(AWS_CFN_TAG_PREFIX)) // Filter our system tags on the user tags .collect(Collectors.toSet()); ResourceModel returnModel = ResourceModel.builder() .locationArn(response.locationArn()) .locationUri(response.locationUri()) .ec2Config(Translator.translateToResourceModelEc2Config(response.ec2Config())) .tags(userTags) .build(); return ProgressEvent.builder() .resourceModel(returnModel) .status(OperationStatus.SUCCESS) .build(); } }