package com.amazonaws.iotanalytics.channel; import software.amazon.awssdk.services.iotanalytics.IoTAnalyticsClient; import software.amazon.awssdk.services.iotanalytics.model.CreateChannelRequest; import software.amazon.awssdk.services.iotanalytics.model.CreateChannelResponse; import software.amazon.awssdk.services.iotanalytics.model.IoTAnalyticsException; import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy; import software.amazon.cloudformation.proxy.HandlerErrorCode; import software.amazon.cloudformation.proxy.Logger; import software.amazon.cloudformation.proxy.ProgressEvent; import software.amazon.cloudformation.proxy.ProxyClient; import software.amazon.cloudformation.proxy.ResourceHandlerRequest; import org.apache.commons.lang3.StringUtils; import software.amazon.cloudformation.resource.IdentifierUtils; public class CreateHandler extends BaseIoTAnalyticsHandler { private static final String CALL_GRAPH = "AWS-IoTAnalytics-Channel::Create"; private static final String OPERATION = "CreateChannel"; private static final int MAX_NAME_LENGTH = 128; private Logger logger; @Override protected 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(); if (!StringUtils.isEmpty(model.getId())) { logger.log(String.format("%s [%s] id is read-only", ResourceModel.TYPE_NAME, model.getId())); return ProgressEvent.failed(model, null, HandlerErrorCode.InvalidRequest, "Id is a read-only property and cannot be set."); } if (StringUtils.isBlank(model.getChannelName())) { final String channelName = IdentifierUtils.generateResourceIdentifier( request.getLogicalResourceIdentifier(), request.getClientRequestToken(), MAX_NAME_LENGTH ).replaceAll("-", "_"); logger.log(String.format("Missing channelName. Generated channelName for %s: %s", ResourceModel.TYPE_NAME, channelName)); model.setChannelName(channelName); } return ProgressEvent.progress(model, callbackContext) .then(progress -> proxy.initiate(CALL_GRAPH, proxyClient, model, callbackContext) .translateToServiceRequest(Translator::translateToCreateChannelRequest) .backoffDelay(DELAY_CONSTANT) .makeServiceCall(this::createChannel) .progress()) .then(progress -> new ReadHandler().handleRequest(proxy, request, callbackContext, proxyClient, logger)); } private CreateChannelResponse createChannel(final CreateChannelRequest request, final ProxyClient proxyClient) { try { final CreateChannelResponse response = proxyClient.injectCredentialsAndInvokeV2(request, proxyClient.client()::createChannel); logger.log(String.format("%s [%s] successfully created", ResourceModel.TYPE_NAME, request.channelName())); return response; } catch (final IoTAnalyticsException e) { logger.log(String.format("ERROR %s [%s] fail to be created: %s", ResourceModel.TYPE_NAME, request.channelName(), e.toString())); throw Translator.translateExceptionToHandlerException( e, OPERATION, request.channelName() ); } } }