Skip to content
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
29 changes: 29 additions & 0 deletions src/main/java/software/amazon/cloudformation/LambdaWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ public void handleRequest(final InputStream inputStream, final OutputStream outp
// transform the request object to pass to caller
ResourceHandlerRequest<ResourceT> resourceHandlerRequest = transform(request);

if (resourceHandlerRequest != null) {
resourceHandlerRequest.setPreviousResourceTags(getPreviousResourceTags(request));
}

this.metricsPublisherProxy.publishInvocationMetric(Instant.now(), request.getAction());

// for CUD actions, validate incoming model - any error is a terminal failure on
Expand Down Expand Up @@ -521,6 +525,31 @@ protected Map<String, String> getDesiredResourceTags(final HandlerRequest<Resour
return desiredResourceTags;
}

/**
* Combines the previous tags supplied by the caller (e.g; CloudFormation) into
* a single Map which represents the desired final set of tags that were applied
* to this resource in the previous state.
*
* @param request The request object contains the new set of tags to be applied
* at a Stack level. These will be overridden with any resource-level
* tags which are specified as a direct resource property.
* @return a Map of Tag names to Tag values
*/
@VisibleForTesting
protected Map<String, String> getPreviousResourceTags(final HandlerRequest<ResourceT, CallbackT> request) {
Map<String, String> previousResourceTags = new HashMap<>();

if (request != null && request.getRequestData() != null) {
replaceInMap(previousResourceTags, request.getRequestData().getPreviousStackTags());
if (request.getRequestData().getPreviousResourceProperties() != null) {
replaceInMap(previousResourceTags,
provideResourceDefinedTags(request.getRequestData().getPreviousResourceProperties()));
}
}

return previousResourceTags;
}

private void replaceInMap(final Map<String, String> targetMap, final Map<String, String> sourceMap) {
if (targetMap == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ public class RequestData<ResourceT> {
private ResourceT previousResourceProperties;
private Map<String, String> systemTags;
private Map<String, String> stackTags;
private Map<String, String> previousStackTags;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class ResourceHandlerRequest<T> {
private T desiredResourceState;
private T previousResourceState;
private Map<String, String> desiredResourceTags;
private Map<String, String> previousResourceTags;
private Map<String, String> systemTags;
private String awsAccountId;
private String awsPartition;
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/software/amazon/cloudformation/LambdaWrapperTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -956,4 +956,47 @@ public void getDesiredResourceTags_resourceTagOverridesStackTag() {
assertThat(tags.size()).isEqualTo(1);
assertThat(tags.get("Tag1")).isEqualTo("Value2");
}

@Test
public void getPreviousResourceTags_oneStackTagAndOneResourceTag() {
final Map<String, String> stackTags = new HashMap<>();
stackTags.put("Tag1", "Value1");

final Map<String, String> resourceTags = new HashMap<>();
resourceTags.put("Tag2", "Value2");
final TestModel model = TestModel.builder().tags(resourceTags).build();

final HandlerRequest<TestModel, TestContext> request = new HandlerRequest<>();
final RequestData<TestModel> requestData = new RequestData<>();
requestData.setPreviousResourceProperties(model);
requestData.setPreviousStackTags(stackTags);
request.setRequestData(requestData);

final Map<String, String> tags = wrapper.getPreviousResourceTags(request);
assertThat(tags).isNotNull();
assertThat(tags.size()).isEqualTo(2);
assertThat(tags.get("Tag1")).isEqualTo("Value1");
assertThat(tags.get("Tag2")).isEqualTo("Value2");
}

@Test
public void getPreviousResourceTags_resourceTagOverridesStackTag() {
final Map<String, String> stackTags = new HashMap<>();
stackTags.put("Tag1", "Value1");

final Map<String, String> resourceTags = new HashMap<>();
resourceTags.put("Tag1", "Value2");
final TestModel model = TestModel.builder().tags(resourceTags).build();

final HandlerRequest<TestModel, TestContext> request = new HandlerRequest<>();
final RequestData<TestModel> requestData = new RequestData<>();
requestData.setPreviousResourceProperties(model);
requestData.setPreviousStackTags(stackTags);
request.setRequestData(requestData);

final Map<String, String> tags = wrapper.getPreviousResourceTags(request);
assertThat(tags).isNotNull();
assertThat(tags.size()).isEqualTo(1);
assertThat(tags.get("Tag1")).isEqualTo("Value2");
}
}