in aws-iot-fleetmetric/src/main/java/com/amazonaws/iot/fleetmetric/CreateHandler.java [36:92]
public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
AmazonWebServicesClientProxy proxy,
ResourceHandlerRequest<ResourceModel> request,
CallbackContext callbackContext,
Logger logger) {
CreateFleetMetricRequest createFleetMetricRequest = translateToCreateRequest(request, logger);
ResourceModel model = request.getDesiredResourceState();
if (!StringUtils.isEmpty(model.getMetricArn())) {
logger.log(String.format("MetricArn is read-only, but the caller passed %s.", model.getMetricArn()));
// Note: this is necessary even though MetricArn is marked readOnly in the schema.
return ProgressEvent.failed(model, callbackContext, HandlerErrorCode.InvalidRequest,
"MetricArn is a read-only property and cannot be set.");
}
DescribeFleetMetricResponse describeFleetMetricResponse = null;
try {
DescribeFleetMetricRequest describeFleetMetricRequest = DescribeFleetMetricRequest.builder()
.metricName(model.getMetricName())
.build();
describeFleetMetricResponse = proxy.injectCredentialsAndInvokeV2(
describeFleetMetricRequest, iotClient::describeFleetMetric);
} catch (ResourceNotFoundException e) {
// expected
} catch (RuntimeException e) {
return Translator.translateExceptionToProgressEvent(model, e, logger);
}
if (describeFleetMetricResponse != null) {
// According to CFN's expectation, if the FleetMetric already exists, createHandler should report a failure.
// https://github.com/aws-cloudformation/cloudformation-cli/blob/653024cfaab7ecfb8ba7c70212f2fecbabb4b095/src/rpdk/core/contract/suite/handler_create.py#L39
logger.log(String.format("Resource already exists %s.", model.getMetricName()));
throw new CfnAlreadyExistsException(ResourceAlreadyExistsException.builder()
.resourceArn(describeFleetMetricResponse.metricArn()).build());
}
CreateFleetMetricResponse createFleetMetricResponse;
try {
createFleetMetricResponse = proxy.injectCredentialsAndInvokeV2(
createFleetMetricRequest, iotClient::createFleetMetric);
} catch (ResourceAlreadyExistsException e) {
logger.log(String.format("Resource already exists %s.", model.getMetricName()));
throw new CfnAlreadyExistsException(e);
} catch (ResourceNotFoundException e) {
logger.log(String.format("Indexing is not enabled when creating %s. Message: %s, stack trace: %s",
model.getMetricName(), e.getMessage(), ExceptionUtils.getStackTrace(e)));
return ProgressEvent.failed(model, callbackContext, HandlerErrorCode.NotFound, e.getMessage());
} catch (RuntimeException e) {
return Translator.translateExceptionToProgressEvent(model, e, logger);
}
model.setMetricArn(createFleetMetricResponse.metricArn());
logger.log(String.format("Created %s.", createFleetMetricResponse.metricArn()));
return ProgressEvent.defaultSuccessHandler(model);
}