in aws-customerprofiles-objecttype/src/main/java/software/amazon/customerprofiles/objecttype/CreateHandler.java [35:122]
public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
final AmazonWebServicesClientProxy proxy,
final ResourceHandlerRequest<ResourceModel> request,
final CallbackContext callbackContext,
final Logger logger) {
if (this.client == null) {
this.client = ClientBuilder.getClient();
}
final ResourceModel model = request.getDesiredResourceState();
final GetProfileObjectTypeRequest getProfileObjectTypeRequest = GetProfileObjectTypeRequest.builder()
.domainName(model.getDomainName())
.objectTypeName(model.getObjectTypeName())
.build();
final GetProfileObjectTypeResponse getProfileObjectTypeResponse;
try {
getProfileObjectTypeResponse = proxy.injectCredentialsAndInvokeV2(getProfileObjectTypeRequest, client::getProfileObjectType);
} catch (Exception exc) {
// 1. BadRequestException will also handled by PutProfileObjectType
// 2. ResourceNotFoundException is the exact exception we want before calling PutProfileObjectType
// 3. Whatever 5xx error GetProfileObjectType call meet, it should not affect the performance of Create Action
Map<String, String> resourceTag;
if (request.getDesiredResourceTags() == null) {
resourceTag = null;
} else if (request.getDesiredResourceTags().isEmpty()) {
resourceTag = null;
} else {
resourceTag = request.getDesiredResourceTags();
}
final PutProfileObjectTypeRequest putProfileObjectTypeRequest = PutProfileObjectTypeRequest.builder()
.domainName(model.getDomainName())
.objectTypeName(model.getObjectTypeName())
.allowProfileCreation(model.getAllowProfileCreation())
.description(model.getDescription())
.encryptionKey(model.getEncryptionKey())
.expirationDays(model.getExpirationDays())
.fields(Translator.listFieldsToMap(model.getFields()))
.keys(Translator.listKeysToMap(model.getKeys()))
.tags(resourceTag)
.templateId(model.getTemplateId())
.build();
final PutProfileObjectTypeResponse putProfileObjectTypeResponse;
try {
putProfileObjectTypeResponse = proxy.injectCredentialsAndInvokeV2(putProfileObjectTypeRequest, client::putProfileObjectType);
logger.log(String.format("ProfileObjectType Created with domainName = %s, objectTypeName = %s",
model.getDomainName(), model.getObjectTypeName()));
} catch (BadRequestException e) {
throw new CfnInvalidRequestException(e);
} catch (ResourceNotFoundException e) {
throw new CfnNotFoundException(e);
} catch (InternalServerException e) {
throw new CfnServiceInternalErrorException(e);
} catch (Exception e) {
throw new CfnGeneralServiceException(e);
}
final ResourceModel responseModel = ResourceModel.builder()
.domainName(model.getDomainName())
.allowProfileCreation(putProfileObjectTypeResponse.allowProfileCreation())
.createdAt(putProfileObjectTypeResponse.createdAt().toString())
.description(putProfileObjectTypeResponse.description())
.encryptionKey(putProfileObjectTypeResponse.encryptionKey())
.expirationDays(putProfileObjectTypeResponse.expirationDays())
.fields(Translator.mapFieldsToList(putProfileObjectTypeResponse.fields()))
.keys(Translator.mapKeysToList(putProfileObjectTypeResponse.keys()))
.lastUpdatedAt(putProfileObjectTypeResponse.lastUpdatedAt().toString())
.objectTypeName(putProfileObjectTypeResponse.objectTypeName())
.tags(Translator.mapTagsToList(putProfileObjectTypeResponse.tags()))
.templateId(putProfileObjectTypeResponse.templateId())
.build();
return ProgressEvent.defaultSuccessHandler(responseModel);
}
// If getProfileObjectType call succeed
// Return a Bad Request Exception as ObjectType already existed
final String errorMessage = String.format("ObjectType %s already exists with domainName = %s",
getProfileObjectTypeResponse.objectTypeName(), model.getDomainName());
logger.log(errorMessage);
BadRequestException e = BadRequestException.builder()
.statusCode(BAD_REQUEST_ERROR_CODE)
.message(errorMessage)
.build();
throw new CfnAlreadyExistsException(e);
}