Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated transformer create handler to allow for creation of active transformer #22

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions aws-b2bi-transformer/aws-b2bi-transformer.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
],
"readOnlyProperties": [
"/properties/CreatedAt",
"/properties/ModifiedAt",
"/properties/TransformerArn",
"/properties/TransformerId"
],
Expand All @@ -177,6 +178,7 @@
"permissions": [
"b2bi:CreateTransformer",
"b2bi:TagResource",
"b2bi:UpdateTransformer",
"logs:CreateLogDelivery",
"logs:CreateLogGroup",
"logs:CreateLogStream",
Expand All @@ -190,8 +192,8 @@
},
"read": {
"permissions": [
"b2bi:ListTagsForResource",
"b2bi:GetTransformer"
"b2bi:GetTransformer",
"b2bi:ListTagsForResource"
]
},
"update": {
Expand Down
14 changes: 4 additions & 10 deletions aws-b2bi-transformer/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ To declare this entity in your AWS CloudFormation template, use the following sy
"<a href="#editype" title="EdiType">EdiType</a>" : <i><a href="editype.md">EdiType</a></i>,
"<a href="#fileformat" title="FileFormat">FileFormat</a>" : <i>String</i>,
"<a href="#mappingtemplate" title="MappingTemplate">MappingTemplate</a>" : <i>String</i>,
"<a href="#modifiedat" title="ModifiedAt">ModifiedAt</a>" : <i>String</i>,
"<a href="#name" title="Name">Name</a>" : <i>String</i>,
"<a href="#sampledocument" title="SampleDocument">SampleDocument</a>" : <i>String</i>,
"<a href="#status" title="Status">Status</a>" : <i>String</i>,
Expand All @@ -32,7 +31,6 @@ Properties:
<a href="#editype" title="EdiType">EdiType</a>: <i><a href="editype.md">EdiType</a></i>
<a href="#fileformat" title="FileFormat">FileFormat</a>: <i>String</i>
<a href="#mappingtemplate" title="MappingTemplate">MappingTemplate</a>: <i>String</i>
<a href="#modifiedat" title="ModifiedAt">ModifiedAt</a>: <i>String</i>
<a href="#name" title="Name">Name</a>: <i>String</i>
<a href="#sampledocument" title="SampleDocument">SampleDocument</a>: <i>String</i>
<a href="#status" title="Status">Status</a>: <i>String</i>
Expand Down Expand Up @@ -70,14 +68,6 @@ _Maximum Length_: <code>350000</code>

_Update requires_: [No interruption](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html#update-no-interrupt)

#### ModifiedAt

_Required_: No

_Type_: String

_Update requires_: [No interruption](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html#update-no-interrupt)

#### Name

_Required_: Yes
Expand Down Expand Up @@ -134,6 +124,10 @@ For more information about using the `Fn::GetAtt` intrinsic function, see [Fn::G

Returns the <code>CreatedAt</code> value.

#### ModifiedAt

Returns the <code>ModifiedAt</code> value.

#### TransformerArn

Returns the <code>TransformerArn</code> value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import software.amazon.cloudformation.proxy.*
import software.amazon.awssdk.services.b2bi.B2BiClient
import software.amazon.awssdk.services.b2bi.model.CreateTransformerRequest
import software.amazon.awssdk.services.b2bi.model.CreateTransformerResponse
import software.amazon.awssdk.services.b2bi.model.TransformerStatus
import software.amazon.awssdk.services.b2bi.model.UpdateTransformerRequest
import software.amazon.b2bi.transformer.TagHelper.convertToList
import software.amazon.b2bi.transformer.TagHelper.getNewDesiredTags
import software.amazon.b2bi.transformer.Translator.toCfnException
Expand All @@ -30,10 +32,12 @@ class CreateHandler : BaseHandlerStd() {
proxy.initiate(OPERATION, proxyClient, progress.resourceModel, progress.callbackContext)
.translateToServiceRequest(Translator::translateToCreateRequest)
.makeServiceCall { awsRequest, client -> createTransformer(awsRequest, client, resourceModel) }
.stabilize { _, _, client, resourceModel, _ -> stabilizeTransformer(resourceModel, client) }
.progress()
}
.then { progress -> ProgressEvent.defaultSuccessHandler(progress.resourceModel) }
}

private fun createTransformer(
request: CreateTransformerRequest,
proxyClient: ProxyClient<B2BiClient>,
Expand All @@ -48,12 +52,35 @@ class CreateHandler : BaseHandlerStd() {
transformerId = response.transformerId()
transformerArn = response.transformerArn()
createdAt = response.createdAt().toString()
status = response.status().toString()
}
logger.log("Successfully created ${ResourceModel.TYPE_NAME} ${resourceModel.transformerId}")
return response
}

private fun stabilizeTransformer(
resourceModel: ResourceModel,
proxyClient: ProxyClient<B2BiClient>,
): Boolean {
val requestedTransformerStatus = resourceModel.status
if (requestedTransformerStatus != TransformerStatus.ACTIVE.toString()) {
resourceModel.apply {
status = TransformerStatus.INACTIVE.toString()
}
return true
}

val updateTransformerRequest = Translator.translateToUpdateRequest(resourceModel)
val updateTransformerResponse = try {
proxyClient.injectCredentialsAndInvokeV2(updateTransformerRequest, proxyClient.client()::updateTransformer)
} catch (e: AwsServiceException) {
throw e.toCfnException()
}
resourceModel.apply {
status = updateTransformerResponse.statusAsString()
}
return true
}

companion object {
private const val OPERATION = "AWS-B2BI-Transformer::Create"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
package software.amazon.b2bi.transformer

import com.google.common.collect.Lists
import software.amazon.awssdk.awscore.AwsRequest
import software.amazon.awssdk.awscore.AwsResponse
import software.amazon.awssdk.awscore.exception.AwsServiceException
import software.amazon.awssdk.services.b2bi.model.*
import software.amazon.awssdk.services.b2bi.model.AccessDeniedException
import software.amazon.awssdk.services.b2bi.model.ConflictException
import software.amazon.awssdk.services.b2bi.model.CreateTransformerRequest
import software.amazon.awssdk.services.b2bi.model.DeleteTransformerRequest
import software.amazon.awssdk.services.b2bi.model.GetTransformerRequest
import software.amazon.awssdk.services.b2bi.model.GetTransformerResponse
import software.amazon.awssdk.services.b2bi.model.InternalServerException
import software.amazon.awssdk.services.b2bi.model.ListTransformersRequest
import software.amazon.awssdk.services.b2bi.model.ListTransformersResponse
import software.amazon.awssdk.services.b2bi.model.ResourceNotFoundException
import software.amazon.awssdk.services.b2bi.model.X12Details
import software.amazon.awssdk.services.b2bi.model.ServiceQuotaExceededException
import software.amazon.awssdk.services.b2bi.model.TagResourceRequest
import software.amazon.awssdk.services.b2bi.model.ThrottlingException
import software.amazon.awssdk.services.b2bi.model.UntagResourceRequest
import software.amazon.awssdk.services.b2bi.model.UpdateTransformerRequest
import software.amazon.awssdk.services.b2bi.model.ValidationException
import software.amazon.b2bi.transformer.TagHelper.toSdkTag
import software.amazon.cloudformation.exceptions.*
import java.util.*
import java.util.stream.Collectors
import java.util.stream.Stream
import software.amazon.b2bi.transformer.EdiType as ResourceEdi
import software.amazon.cloudformation.exceptions.BaseHandlerException
import software.amazon.cloudformation.exceptions.CfnAccessDeniedException
import software.amazon.cloudformation.exceptions.CfnAlreadyExistsException
import software.amazon.cloudformation.exceptions.CfnGeneralServiceException
import software.amazon.cloudformation.exceptions.CfnInvalidRequestException
import software.amazon.cloudformation.exceptions.CfnNotFoundException
import software.amazon.cloudformation.exceptions.CfnServiceInternalErrorException
import software.amazon.cloudformation.exceptions.CfnServiceLimitExceededException
import software.amazon.cloudformation.exceptions.CfnThrottlingException
import software.amazon.awssdk.services.b2bi.model.EdiType as SdkEdi
import software.amazon.b2bi.transformer.X12Details as ResourceX12
import software.amazon.awssdk.services.b2bi.model.X12Details as SdkX12
import software.amazon.b2bi.transformer.EdiType as ResourceEdi
import software.amazon.b2bi.transformer.X12Details as ResourceX12

/**
* This class is a centralized placeholder for
Expand Down Expand Up @@ -53,7 +68,7 @@ object Translator {

/**
* Translates resource object from sdk into a resource model
* @param awsResponse the aws service describe resource response
* @param response the aws service describe resource response
* @return model resource model
*/
fun translateFromReadResponse(response: GetTransformerResponse): ResourceModel {
Expand Down Expand Up @@ -114,7 +129,7 @@ object Translator {

/**
* Translates resource objects from sdk into a resource model (primary identifier only)
* @param awsResponse the aws service describe resource response
* @param response the aws service describe resource response
* @return list of resource models
*/
fun translateFromListResponse(response: ListTransformersResponse): List<ResourceModel> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package software.amazon.b2bi.transformer

import software.amazon.awssdk.awscore.AwsResponse
import software.amazon.awssdk.awscore.exception.AwsServiceException
import software.amazon.awssdk.core.SdkClient
import software.amazon.awssdk.services.b2bi.B2BiClient
import software.amazon.awssdk.services.b2bi.model.UpdateTransformerRequest
import software.amazon.awssdk.services.b2bi.model.UpdateTransformerResponse
import software.amazon.b2bi.transformer.Translator.translateToResourceEdi
import software.amazon.b2bi.transformer.TagHelper.convertToList
import software.amazon.b2bi.transformer.TagHelper.generateTagsToAdd
import software.amazon.b2bi.transformer.TagHelper.generateTagsToRemove
Expand All @@ -16,8 +13,12 @@ import software.amazon.b2bi.transformer.TagHelper.shouldUpdateTags
import software.amazon.b2bi.transformer.TagHelper.tagResource
import software.amazon.b2bi.transformer.TagHelper.untagResource
import software.amazon.b2bi.transformer.Translator.toCfnException
import software.amazon.cloudformation.exceptions.CfnGeneralServiceException
import software.amazon.cloudformation.proxy.*
import software.amazon.b2bi.transformer.Translator.translateToResourceEdi
import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy
import software.amazon.cloudformation.proxy.Logger
import software.amazon.cloudformation.proxy.ProgressEvent
import software.amazon.cloudformation.proxy.ProxyClient
import software.amazon.cloudformation.proxy.ResourceHandlerRequest

class UpdateHandler : BaseHandlerStd() {
private lateinit var logger: Logger
Expand Down Expand Up @@ -94,5 +95,4 @@ class UpdateHandler : BaseHandlerStd() {
companion object {
private const val OPERATION = "AWS-B2BI-Transformer::Update"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class CreateHandlerTest : AbstractTestBase() {
proxyClient = mockProxy(proxy, b2BiClient)
}

@AfterEach
fun reset() {
clearAllMocks()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import software.amazon.awssdk.services.b2bi.model.GetTransformerResponse
import software.amazon.awssdk.services.b2bi.model.ListTransformersResponse
import software.amazon.awssdk.services.b2bi.model.ListTagsForResourceResponse
import software.amazon.awssdk.services.b2bi.model.Logging
import software.amazon.awssdk.services.b2bi.model.TransformerStatus
import software.amazon.awssdk.services.b2bi.model.TransformerSummary
import software.amazon.awssdk.services.b2bi.model.UpdateTransformerResponse
import software.amazon.b2bi.transformer.TagHelper.toSdkTag
Expand All @@ -22,7 +23,7 @@ val TEST_X12: X12Details = X12Details.builder().transactionSet(TEST_TRANSACTION_
const val TEST_NAME = "Test transformer name"
const val TEST_FILE_FORMAT = "JSON"
const val TEST_MAPPING_TEMPLATE = "test mapping template"
const val TEST_TRANSFORMER_STATUS = "active"
val TEST_TRANSFORMER_STATUS = TransformerStatus.INACTIVE.toString()
val TEST_RESOURCE_EDI_TYPE: EdiType = EdiType.builder().x12Details(TEST_X12).build()
val TEST_SDK_EDI_TYPE = TEST_RESOURCE_EDI_TYPE.translateToSdkEdi()
const val TEST_TRANSFORMER_ID = "t-12345678901234567"
Expand All @@ -48,6 +49,7 @@ val TEST_CREATE_TRANSFORMER_REQUEST_RESOURCE_MODEL_WITH_REQUIRED_FIELDS = Resour
.fileFormat(TEST_FILE_FORMAT)
.mappingTemplate(TEST_MAPPING_TEMPLATE)
.ediType(TEST_RESOURCE_EDI_TYPE)
.status(TEST_TRANSFORMER_STATUS)
.tags(emptyList())
.createdAt(TEST_INSTANT.toString())
.modifiedAt(TEST_INSTANT.toString())
Expand Down Expand Up @@ -177,4 +179,4 @@ val TEST_UPDATE_TRANSFORMER_RESPONSE_RESOURCE_MODEL_WITH_ALL_FIELDS = ResourceMo
.fileFormat(TEST_FILE_FORMAT)
.status(TEST_TRANSFORMER_STATUS)
.tags(emptyList())
.build()
.build()
Loading