diff --git a/aws-logs-loggroup/src/main/java/software/amazon/logs/loggroup/CreateHandler.java b/aws-logs-loggroup/src/main/java/software/amazon/logs/loggroup/CreateHandler.java index c31b033..5fcbc94 100644 --- a/aws-logs-loggroup/src/main/java/software/amazon/logs/loggroup/CreateHandler.java +++ b/aws-logs-loggroup/src/main/java/software/amazon/logs/loggroup/CreateHandler.java @@ -1,5 +1,6 @@ package software.amazon.logs.loggroup; +import org.apache.commons.collections.MapUtils; import software.amazon.cloudformation.exceptions.CfnAlreadyExistsException; import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy; import software.amazon.cloudformation.proxy.Logger; @@ -21,9 +22,7 @@ public ProgressEvent handleRequest( final ResourceHandlerRequest request, final CallbackContext callbackContext, final Logger logger) { - prepareResourceModel(request); - final ResourceModel model = request.getDesiredResourceState(); try { @@ -33,7 +32,6 @@ public ProgressEvent handleRequest( throw new CfnAlreadyExistsException(ResourceModel.TYPE_NAME, Objects.toString(model.getPrimaryIdentifier())); } - final String createMessage = String.format("%s [%s] successfully created.", ResourceModel.TYPE_NAME, model.getLogGroupName()); logger.log(createMessage); @@ -41,7 +39,6 @@ public ProgressEvent handleRequest( if (model.getRetentionInDays() != null) { updateRetentionInDays(proxy, request, logger); } - return ProgressEvent.defaultSuccessHandler(model); } @@ -58,23 +55,28 @@ private void prepareResourceModel(final ResourceHandlerRequest re if (request.getDesiredResourceState() == null) { request.setDesiredResourceState(new ResourceModel()); } - final ResourceModel model = request.getDesiredResourceState(); - final String identifierPrefix = request.getLogicalResourceIdentifier() == null ? - DEFAULT_LOG_GROUP_NAME_PREFIX : - request.getLogicalResourceIdentifier(); if (StringUtils.isNullOrEmpty(model.getLogGroupName())) { - model.setLogGroupName( - IdentifierUtils.generateResourceIdentifier( - identifierPrefix, - request.getClientRequestToken(), - MAX_LENGTH_LOG_GROUP_NAME - ) - ); + model.setLogGroupName(generateName(request)); } } + private String generateName(final ResourceHandlerRequest request) { + final StringBuilder identifierPrefix = new StringBuilder(); + identifierPrefix.append((request.getSystemTags() != null && + MapUtils.isNotEmpty(request.getSystemTags())) ? + request.getSystemTags().get("aws:cloudformation:stack-name") + "-" : ""); + identifierPrefix.append(request.getLogicalResourceIdentifier() == null ? + DEFAULT_LOG_GROUP_NAME_PREFIX : + request.getLogicalResourceIdentifier()); + + return IdentifierUtils.generateResourceIdentifier( + identifierPrefix.toString(), + request.getClientRequestToken(), + MAX_LENGTH_LOG_GROUP_NAME); + } + private void updateRetentionInDays(final AmazonWebServicesClientProxy proxy, final ResourceHandlerRequest request, final Logger logger) { diff --git a/aws-logs-loggroup/src/test/java/software/amazon/logs/loggroup/CreateHandlerTest.java b/aws-logs-loggroup/src/test/java/software/amazon/logs/loggroup/CreateHandlerTest.java index b8889e1..78bbe1d 100644 --- a/aws-logs-loggroup/src/test/java/software/amazon/logs/loggroup/CreateHandlerTest.java +++ b/aws-logs-loggroup/src/test/java/software/amazon/logs/loggroup/CreateHandlerTest.java @@ -19,6 +19,8 @@ import software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -137,12 +139,19 @@ public void handleRequest_SuccessGeneratedLogGroupName() { .desiredResourceState(model) .build(); + final Map systemTags = new HashMap<>(); + systemTags.put("aws:cloudformation:stack-name", "unit_test_Stack"); + request.setSystemTags(systemTags); + request.setClientRequestToken("4b90a7e4-b790-456b-a937-0cfdfa212fed"); + request.setLogicalResourceIdentifier("taskDefinition"); + final ProgressEvent response = handler.handleRequest(proxy, request, null, logger); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS); assertThat(response.getCallbackContext()).isNull(); assertThat(response.getCallbackDelaySeconds()).isEqualTo(0); + assertThat(response.getResourceModel().getLogGroupName()).startsWith("unit_test_Stack"); assertThat(response.getResourceModels()).isNull(); // There isn't an easy way to check the generated value of the name assertThat(response.getResourceModel()).isNotNull();