package software.amazon.opensearchserverless.securityconfig; import software.amazon.awssdk.services.opensearchserverless.OpenSearchServerlessClient; import software.amazon.awssdk.services.opensearchserverless.model.ConflictException; import software.amazon.awssdk.services.opensearchserverless.model.CreateSecurityConfigRequest; import software.amazon.awssdk.services.opensearchserverless.model.CreateSecurityConfigResponse; import software.amazon.awssdk.services.opensearchserverless.model.InternalServerException; import software.amazon.awssdk.services.opensearchserverless.model.ValidationException; import software.amazon.cloudformation.exceptions.CfnAlreadyExistsException; import software.amazon.cloudformation.exceptions.CfnInvalidRequestException; import software.amazon.cloudformation.exceptions.CfnServiceInternalErrorException; 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 com.amazonaws.util.StringUtils; public class CreateHandler extends BaseHandlerStd { public CreateHandler() { super(); } public CreateHandler(OpenSearchServerlessClient openSearchServerlessClient) { super(openSearchServerlessClient); } protected ProgressEvent handleRequest( final AmazonWebServicesClientProxy proxy, final ResourceHandlerRequest request, final CallbackContext callbackContext, final ProxyClient proxyClient, final Logger logger) { ResourceModel model = request.getDesiredResourceState(); if (callbackContext == null && model.getId() != null) { throw new CfnInvalidRequestException("Id should not be set"); } if (StringUtils.isNullOrEmpty(model.getName())) { return ProgressEvent.failed(model, callbackContext, HandlerErrorCode.InvalidRequest, "Name cannot be empty"); } if (StringUtils.isNullOrEmpty(model.getType())) { return ProgressEvent.failed(model, callbackContext, HandlerErrorCode.InvalidRequest, "Type cannot be empty"); } return ProgressEvent.progress(request.getDesiredResourceState(), callbackContext) .then(progress -> proxy.initiate("AWS-OpenSearchServerless-SecurityConfig::Create", proxyClient, progress.getResourceModel(), progress.getCallbackContext()) .translateToServiceRequest(Translator::translateToCreateRequest) .makeServiceCall((awsRequest, client) -> createSecurityConfig(awsRequest, client, logger)) .done(awsResponse -> ProgressEvent.defaultSuccessHandler(Translator.translateFromCreateResponse(awsResponse)))); } private CreateSecurityConfigResponse createSecurityConfig( final CreateSecurityConfigRequest createSecurityConfigRequest, final ProxyClient proxyClient, final Logger logger) { CreateSecurityConfigResponse createSecurityConfigResponse; try { createSecurityConfigResponse = proxyClient.injectCredentialsAndInvokeV2(createSecurityConfigRequest, proxyClient.client()::createSecurityConfig); } catch (ConflictException e) { throw new CfnAlreadyExistsException(ResourceModel.TYPE_NAME, String.format("Name:%s, Type:%s", createSecurityConfigRequest.name(), createSecurityConfigRequest.typeAsString()), e); } catch (ValidationException e) { throw new CfnInvalidRequestException(createSecurityConfigRequest.toString() + ", " + e.getMessage(), e); } catch (InternalServerException e) { throw new CfnServiceInternalErrorException("CreateSecurityConfig", e); } logger.log(String.format("%s successfully created for %s", ResourceModel.TYPE_NAME, createSecurityConfigRequest)); return createSecurityConfigResponse; } }