Skip to content

Commit af543cc

Browse files
authored
Fixing tagging edge case bug in namespace and workgroup resource. The bug is getting trigerred if customer doesn't pass any tags during Update operation (#76)
1 parent 5de3d8e commit af543cc

File tree

2 files changed

+26
-4
lines changed
  • aws-redshiftserverless-namespace/src/main/java/software/amazon/redshiftserverless/namespace
  • aws-redshiftserverless-workgroup/src/main/java/software/amazon/redshiftserverless/workgroup

2 files changed

+26
-4
lines changed

aws-redshiftserverless-namespace/src/main/java/software/amazon/redshiftserverless/namespace/Translator.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,14 +299,25 @@ static UpdateTagsRequest translateToUpdateTagsRequest(final ResourceModel desire
299299
final ResourceModel currentResourceState) {
300300
String resourceArn = currentResourceState.getNamespace().getNamespaceArn();
301301

302-
List<Tag> toBeCreatedTags = desiredResourceState.getTags() == null ? Collections.emptyList() : desiredResourceState.getTags()
302+
// If desiredResourceState.getTags() is null, we should preserve existing tags
303+
// This ensures that when CloudFormation doesn't specify tags in the update,
304+
// we don't remove existing tags
305+
final List<Tag> effectiveDesiredTags;
306+
if (desiredResourceState.getTags() == null && currentResourceState.getTags() != null) {
307+
// Preserve existing tags by using them as the desired tags
308+
effectiveDesiredTags = currentResourceState.getTags();
309+
} else {
310+
effectiveDesiredTags = desiredResourceState.getTags();
311+
}
312+
313+
List<Tag> toBeCreatedTags = effectiveDesiredTags == null ? Collections.emptyList() : effectiveDesiredTags
303314
.stream()
304315
.filter(tag -> currentResourceState.getTags() == null || !currentResourceState.getTags().contains(tag))
305316
.collect(Collectors.toList());
306317

307318
List<Tag> toBeDeletedTags = currentResourceState.getTags() == null ? Collections.emptyList() : currentResourceState.getTags()
308319
.stream()
309-
.filter(tag -> desiredResourceState.getTags() == null || !desiredResourceState.getTags().contains(tag))
320+
.filter(tag -> effectiveDesiredTags == null || !effectiveDesiredTags.contains(tag))
310321
.collect(Collectors.toList());
311322

312323
return UpdateTagsRequest.builder()

aws-redshiftserverless-workgroup/src/main/java/software/amazon/redshiftserverless/workgroup/Translator.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,25 @@ static UpdateTagsRequest translateToUpdateTagsRequest(final ResourceModel desire
287287
final ResourceModel currentResourceState) {
288288
String resourceArn = currentResourceState.getWorkgroup().getWorkgroupArn();
289289

290-
List<Tag> toBeCreatedTags = desiredResourceState.getTags() == null ? Collections.emptyList() : desiredResourceState.getTags()
290+
// If desiredResourceState.getTags() is null, we should preserve existing tags
291+
// This ensures that when CloudFormation doesn't specify tags in the update,
292+
// we don't remove existing tags
293+
final List<Tag> effectiveDesiredTags;
294+
if (desiredResourceState.getTags() == null && currentResourceState.getTags() != null) {
295+
// Preserve existing tags by using them as the desired tags
296+
effectiveDesiredTags = currentResourceState.getTags();
297+
} else {
298+
effectiveDesiredTags = desiredResourceState.getTags();
299+
}
300+
301+
List<Tag> toBeCreatedTags = effectiveDesiredTags == null ? Collections.emptyList() : effectiveDesiredTags
291302
.stream()
292303
.filter(tag -> currentResourceState.getTags() == null || !currentResourceState.getTags().contains(tag))
293304
.collect(Collectors.toList());
294305

295306
List<Tag> toBeDeletedTags = currentResourceState.getTags() == null ? Collections.emptyList() : currentResourceState.getTags()
296307
.stream()
297-
.filter(tag -> desiredResourceState.getTags() == null || !desiredResourceState.getTags().contains(tag))
308+
.filter(tag -> effectiveDesiredTags == null || !effectiveDesiredTags.contains(tag))
298309
.collect(Collectors.toList());
299310

300311
return UpdateTagsRequest.builder()

0 commit comments

Comments
 (0)