From 8bdf86420b5dcc3be729902f2b4d41331d725cc3 Mon Sep 17 00:00:00 2001 From: soreavis <263610811+soreavis@users.noreply.github.com> Date: Thu, 12 Nov 2026 13:07:03 +0100 Subject: [PATCH] refactor(redshift): share one Tags writer across the query builders (#8086) RedshiftQueryHandler wrote the same Tags/Tag loop in eight resource builders: clusters, snapshots, cluster parameter groups, cluster subnet groups, snapshot schedules, event subscriptions, snapshot copy grants and integrations. They now call one tagsXml helper. The XML is unchanged. Clusters, snapshot copy grants and integrations still leave out an empty Tags list, and the other five still write one. A new RedshiftQueryHandlerTest case pins both shapes for all eight; it passes before and after the change. --- .../redshift/RedshiftQueryHandler.java | 76 +++++-------------- .../redshift/RedshiftQueryHandlerTest.java | 58 ++++++++++++++ 2 files changed, 78 insertions(+), 56 deletions(-) diff --git a/src/main/java/io/floci/aws/services/redshift/RedshiftQueryHandler.java b/src/main/java/io/floci/aws/services/redshift/RedshiftQueryHandler.java index dc17e99275..4be611ac7e 100644 --- a/src/main/java/io/floci/aws/services/redshift/RedshiftQueryHandler.java +++ b/src/main/java/io/floci/aws/services/redshift/RedshiftQueryHandler.java @@ -1211,14 +1211,7 @@ public class RedshiftQueryHandler { .end("ClusterParameterGroups"); if (cluster.getTags() != null && !cluster.getTags().isEmpty()) { - builder.start("Tags"); - for (Map.Entry tag : cluster.getTags().entrySet()) { - builder.start("Tag") - .elem("Key", tag.getKey()) - .elem("Value", tag.getValue()) - .end("Tag"); - } - builder.end("Tags"); + builder.raw(tagsXml(cluster.getTags())); } if (cluster.getSnapshotCopyDestinationRegion() != null) { @@ -1328,12 +1321,7 @@ public class RedshiftQueryHandler { if (snapshot.getKmsKeyId() != null) { builder.elem("Encrypted", "true").elem("KmsKeyId", snapshot.getKmsKeyId()); } - builder.start("Tags"); - for (Map.Entry tag : snapshot.getTags().entrySet()) { - builder.start("Tag").elem("Key", tag.getKey()).elem("Value", tag.getValue()).end("Tag"); - } - - return builder.end("Tags").end("Snapshot").build(); + return builder.raw(tagsXml(snapshot.getTags())).end("Snapshot").build(); } private Response clusterResponse(String operation, Cluster cluster) { @@ -1385,15 +1373,8 @@ public class RedshiftQueryHandler { } builder.end("ScheduleDefinitions") .elem("ScheduleIdentifier", schedule.getScheduleIdentifier()) - .elem("ScheduleDescription", schedule.getScheduleDescription()); - builder.start("Tags"); - for (Map.Entry tag : schedule.getTags().entrySet()) { - builder.start("Tag") - .elem("Key", tag.getKey()) - .elem("Value", tag.getValue()) - .end("Tag"); - } - builder.end("Tags"); + .elem("ScheduleDescription", schedule.getScheduleDescription()) + .raw(tagsXml(schedule.getTags())); List associated = service.clustersUsingSnapshotSchedule(schedule.getScheduleIdentifier()); builder.elem("AssociatedClusterCount", associated.size()).start("AssociatedClusters"); for (String clusterIdentifier : associated) { @@ -1461,16 +1442,13 @@ public class RedshiftQueryHandler { } private String buildClusterParameterGroupXml(ClusterParameterGroup group) { - XmlBuilder builder = new XmlBuilder() + return new XmlBuilder() .start("ClusterParameterGroup") .elem("ParameterGroupName", group.getParameterGroupName()) .elem("ParameterGroupFamily", group.getParameterGroupFamily()) .elem("Description", group.getDescription()) - .start("Tags"); - for (Map.Entry tag : group.getTags().entrySet()) { - builder.start("Tag").elem("Key", tag.getKey()).elem("Value", tag.getValue()).end("Tag"); - } - return builder.end("Tags").end("ClusterParameterGroup").build(); + .raw(tagsXml(group.getTags())) + .end("ClusterParameterGroup").build(); } private String buildClusterSubnetGroupXml(ClusterSubnetGroup group) { @@ -1489,11 +1467,7 @@ public class RedshiftQueryHandler { } builder.elem("SubnetStatus", "Active").end("Subnet"); } - builder.end("Subnets").start("Tags"); - for (Map.Entry tag : group.getTags().entrySet()) { - builder.start("Tag").elem("Key", tag.getKey()).elem("Value", tag.getValue()).end("Tag"); - } - return builder.end("Tags").end("ClusterSubnetGroup").build(); + return builder.end("Subnets").raw(tagsXml(group.getTags())).end("ClusterSubnetGroup").build(); } private Response eventSubscriptionResponse(String operation, EventSubscription subscription) { @@ -1527,14 +1501,10 @@ public class RedshiftQueryHandler { for (String category : subscription.getEventCategoriesList()) { builder.elem("EventCategory", category); } - builder.end("EventCategoriesList") + return builder.end("EventCategoriesList") .elem("SubscriptionCreationTime", EVENT_TIMESTAMP.format(subscription.getSubscriptionCreationTime())) .elem("Enabled", String.valueOf(subscription.isEnabled())) - .start("Tags"); - for (Map.Entry tag : subscription.getTags().entrySet()) { - builder.start("Tag").elem("Key", tag.getKey()).elem("Value", tag.getValue()).end("Tag"); - } - return builder.end("Tags") + .raw(tagsXml(subscription.getTags())) .elem("Status", subscription.getStatus()) .elem("CustomerAwsId", subscription.getCustomerAwsId()) .end("EventSubscription").build(); @@ -1598,14 +1568,7 @@ public class RedshiftQueryHandler { .elem("KmsKeyId", grant.getKmsKeyId()); if (grant.getTags() != null && !grant.getTags().isEmpty()) { - builder.start("Tags"); - for (Map.Entry tag : grant.getTags().entrySet()) { - builder.start("Tag") - .elem("Key", tag.getKey()) - .elem("Value", tag.getValue()) - .end("Tag"); - } - builder.end("Tags"); + builder.raw(tagsXml(grant.getTags())); } return builder.end("SnapshotCopyGrant").build(); @@ -1664,18 +1627,19 @@ public class RedshiftQueryHandler { builder.start("Errors").end("Errors"); } if (integration.getTags() != null && !integration.getTags().isEmpty()) { - builder.start("Tags"); - for (Map.Entry tag : integration.getTags().entrySet()) { - builder.start("Tag") - .elem("Key", tag.getKey()) - .elem("Value", tag.getValue()) - .end("Tag"); - } - builder.end("Tags"); + builder.raw(tagsXml(integration.getTags())); } return builder.build(); } + private static String tagsXml(Map tags) { + XmlBuilder builder = new XmlBuilder().start("Tags"); + for (Map.Entry tag : tags.entrySet()) { + builder.start("Tag").elem("Key", tag.getKey()).elem("Value", tag.getValue()).end("Tag"); + } + return builder.end("Tags").build(); + } + /** * Reads the {@code TagList.Tag.N.Key} / {@code .Value} pairs of a Query request. * diff --git a/src/test/java/io/floci/aws/services/redshift/RedshiftQueryHandlerTest.java b/src/test/java/io/floci/aws/services/redshift/RedshiftQueryHandlerTest.java index 52b62ba4d2..aec3677f3f 100644 --- a/src/test/java/io/floci/aws/services/redshift/RedshiftQueryHandlerTest.java +++ b/src/test/java/io/floci/aws/services/redshift/RedshiftQueryHandlerTest.java @@ -11,6 +11,7 @@ import io.floci.aws.services.redshift.model.ClusterParameterGroup; import io.floci.aws.services.redshift.model.ClusterSubnetGroup; import io.floci.aws.services.redshift.model.Endpoint; import io.floci.aws.services.redshift.model.EventSubscription; +import io.floci.aws.services.redshift.model.Integration; import io.floci.aws.services.redshift.model.Parameter; import io.floci.aws.services.redshift.model.Snapshot; import io.floci.aws.services.redshift.model.SnapshotCopyGrant; @@ -23,6 +24,7 @@ import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import java.time.Instant; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -1309,6 +1311,62 @@ class RedshiftQueryHandlerTest { assertTrue(xml.contains("prod")); } + @Test + void describeResponsesListTagsAndClustersGrantsAndIntegrationsOmitAnEmptyList() { + List describes = List.of("DescribeClusters", "DescribeClusterSnapshots", + "DescribeClusterParameterGroups", "DescribeClusterSubnetGroups", "DescribeSnapshotSchedules", + "DescribeEventSubscriptions", "DescribeSnapshotCopyGrants", "DescribeIntegrations"); + Map tags = new LinkedHashMap<>(); + tags.put("team", "red"); + tags.put("env", "dev"); + stubOneOfEachTaggedResource(tags); + for (String action : describes) { + String xml = (String) handler.handle(action, new MultivaluedHashMap<>()).getEntity(); + assertTrue(xml.contains("teamred" + + "envdev"), action); + } + + List omitEmpty = List.of("DescribeClusters", "DescribeSnapshotCopyGrants", "DescribeIntegrations"); + stubOneOfEachTaggedResource(Map.of()); + for (String action : describes) { + String xml = (String) handler.handle(action, new MultivaluedHashMap<>()).getEntity(); + assertEquals(!omitEmpty.contains(action), xml.contains(""), action); + assertEquals(!omitEmpty.contains(action), xml.contains(""), action); + } + } + + private void stubOneOfEachTaggedResource(Map tags) { + Cluster cluster = new Cluster(); + cluster.setTags(tags); + when(service.describeClusters(any())).thenReturn(List.of(cluster)); + Snapshot snapshot = new Snapshot(); + snapshot.setTags(tags); + when(service.describeSnapshots(any(), any())).thenReturn(List.of(snapshot)); + ClusterParameterGroup parameterGroup = new ClusterParameterGroup(); + parameterGroup.setTags(tags); + when(service.describeClusterParameterGroups(any())).thenReturn(List.of(parameterGroup)); + ClusterSubnetGroup subnetGroup = new ClusterSubnetGroup(); + subnetGroup.setTags(tags); + when(service.describeClusterSubnetGroups(any())).thenReturn(List.of(subnetGroup)); + SnapshotSchedule schedule = new SnapshotSchedule(); + schedule.setTags(tags); + when(service.describeSnapshotSchedules(any(), any(), any(), any(), any(), any())) + .thenReturn(new PaginatedResult<>(List.of(schedule), null)); + EventSubscription subscription = new EventSubscription(); + subscription.setSubscriptionCreationTime(Instant.parse("2026-11-12T00:00:00Z")); + subscription.setTags(tags); + when(service.describeEventSubscriptions(any(), any(), any(), any(), any())) + .thenReturn(new PaginatedResult<>(List.of(subscription), null)); + SnapshotCopyGrant grant = new SnapshotCopyGrant(); + grant.setTags(tags); + when(service.describeSnapshotCopyGrants(any(), any(), any())) + .thenReturn(new PaginatedResult<>(List.of(grant), null)); + Integration integration = new Integration(); + integration.setTags(tags); + when(service.describeIntegrations(any(), any(), any(), any())) + .thenReturn(new RedshiftService.IntegrationPage(List.of(integration), null)); + } + // ── GetClusterCredentials ──────────────────────────────────────────────── @Test