Skip to content

Commit 04cf26f

Browse files
omkhegdexiwhuang
andauthored
Add getPreviousResourceTags method (#300)
* Issue #, if available: #292 Description of changes: This change adds getPreviousResourceTags method which returns previous tags that were applied to a resource. The tags come from previousStackTags and previousResourceDefinedTags By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. * Fix comments * Add null check for getPreviousResourceProperties * Handle backward compatability * Add null check * Fix indent Co-authored-by: xiwhuang <[email protected]>
1 parent ee42201 commit 04cf26f

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

src/main/java/software/amazon/cloudformation/LambdaWrapper.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ public void handleRequest(final InputStream inputStream, final OutputStream outp
259259
// transform the request object to pass to caller
260260
ResourceHandlerRequest<ResourceT> resourceHandlerRequest = transform(request);
261261

262+
if (resourceHandlerRequest != null) {
263+
resourceHandlerRequest.setPreviousResourceTags(getPreviousResourceTags(request));
264+
}
265+
262266
this.metricsPublisherProxy.publishInvocationMetric(Instant.now(), request.getAction());
263267

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

528+
/**
529+
* Combines the previous tags supplied by the caller (e.g; CloudFormation) into
530+
* a single Map which represents the desired final set of tags that were applied
531+
* to this resource in the previous state.
532+
*
533+
* @param request The request object contains the new set of tags to be applied
534+
* at a Stack level. These will be overridden with any resource-level
535+
* tags which are specified as a direct resource property.
536+
* @return a Map of Tag names to Tag values
537+
*/
538+
@VisibleForTesting
539+
protected Map<String, String> getPreviousResourceTags(final HandlerRequest<ResourceT, CallbackT> request) {
540+
Map<String, String> previousResourceTags = new HashMap<>();
541+
542+
if (request != null && request.getRequestData() != null) {
543+
replaceInMap(previousResourceTags, request.getRequestData().getPreviousStackTags());
544+
if (request.getRequestData().getPreviousResourceProperties() != null) {
545+
replaceInMap(previousResourceTags,
546+
provideResourceDefinedTags(request.getRequestData().getPreviousResourceProperties()));
547+
}
548+
}
549+
550+
return previousResourceTags;
551+
}
552+
524553
private void replaceInMap(final Map<String, String> targetMap, final Map<String, String> sourceMap) {
525554
if (targetMap == null) {
526555
return;

src/main/java/software/amazon/cloudformation/proxy/RequestData.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ public class RequestData<ResourceT> {
2929
private ResourceT previousResourceProperties;
3030
private Map<String, String> systemTags;
3131
private Map<String, String> stackTags;
32+
private Map<String, String> previousStackTags;
3233
}

src/main/java/software/amazon/cloudformation/proxy/ResourceHandlerRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class ResourceHandlerRequest<T> {
3636
private T desiredResourceState;
3737
private T previousResourceState;
3838
private Map<String, String> desiredResourceTags;
39+
private Map<String, String> previousResourceTags;
3940
private Map<String, String> systemTags;
4041
private String awsAccountId;
4142
private String awsPartition;

src/test/java/software/amazon/cloudformation/LambdaWrapperTest.java

100644100755
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,4 +956,47 @@ public void getDesiredResourceTags_resourceTagOverridesStackTag() {
956956
assertThat(tags.size()).isEqualTo(1);
957957
assertThat(tags.get("Tag1")).isEqualTo("Value2");
958958
}
959+
960+
@Test
961+
public void getPreviousResourceTags_oneStackTagAndOneResourceTag() {
962+
final Map<String, String> stackTags = new HashMap<>();
963+
stackTags.put("Tag1", "Value1");
964+
965+
final Map<String, String> resourceTags = new HashMap<>();
966+
resourceTags.put("Tag2", "Value2");
967+
final TestModel model = TestModel.builder().tags(resourceTags).build();
968+
969+
final HandlerRequest<TestModel, TestContext> request = new HandlerRequest<>();
970+
final RequestData<TestModel> requestData = new RequestData<>();
971+
requestData.setPreviousResourceProperties(model);
972+
requestData.setPreviousStackTags(stackTags);
973+
request.setRequestData(requestData);
974+
975+
final Map<String, String> tags = wrapper.getPreviousResourceTags(request);
976+
assertThat(tags).isNotNull();
977+
assertThat(tags.size()).isEqualTo(2);
978+
assertThat(tags.get("Tag1")).isEqualTo("Value1");
979+
assertThat(tags.get("Tag2")).isEqualTo("Value2");
980+
}
981+
982+
@Test
983+
public void getPreviousResourceTags_resourceTagOverridesStackTag() {
984+
final Map<String, String> stackTags = new HashMap<>();
985+
stackTags.put("Tag1", "Value1");
986+
987+
final Map<String, String> resourceTags = new HashMap<>();
988+
resourceTags.put("Tag1", "Value2");
989+
final TestModel model = TestModel.builder().tags(resourceTags).build();
990+
991+
final HandlerRequest<TestModel, TestContext> request = new HandlerRequest<>();
992+
final RequestData<TestModel> requestData = new RequestData<>();
993+
requestData.setPreviousResourceProperties(model);
994+
requestData.setPreviousStackTags(stackTags);
995+
request.setRequestData(requestData);
996+
997+
final Map<String, String> tags = wrapper.getPreviousResourceTags(request);
998+
assertThat(tags).isNotNull();
999+
assertThat(tags.size()).isEqualTo(1);
1000+
assertThat(tags.get("Tag1")).isEqualTo("Value2");
1001+
}
9591002
}

0 commit comments

Comments
 (0)