Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions python/rpdk/java/templates/generate/HandlerWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public Map<String, String> provideResourceDefinedTags(final {{ pojo_name}} resou
.desiredResourceState(requestData.getResourceProperties())
.previousResourceState(requestData.getPreviousResourceProperties())
.desiredResourceTags(getDesiredResourceTags(request))
.previousResourceTags(getPreviousResourceTags(request))
.systemTags(request.getRequestData().getSystemTags())
.awsAccountId(request.getAwsAccountId())
.logicalResourceIdentifier(request.getRequestData().getLogicalResourceId())
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/software/amazon/cloudformation/LambdaWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,29 @@ 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());
replaceInMap(previousResourceTags,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getPreviousResourceProperties() is null for CREATE/DELETE/READ, and not all provideResourceDefinedTags handles the null resource model. let's have a null check here.

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

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");
}
}