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 9e6e22d950..dc17e99275 100644 --- a/src/main/java/io/floci/aws/services/redshift/RedshiftQueryHandler.java +++ b/src/main/java/io/floci/aws/services/redshift/RedshiftQueryHandler.java @@ -185,7 +185,7 @@ public class RedshiftQueryHandler { case "CreateClusterSnapshot" -> { String snapshotIdentifier = params.getFirst("SnapshotIdentifier"); String clusterIdentifier = params.getFirst("ClusterIdentifier"); - Snapshot snapshot = service.createSnapshot(snapshotIdentifier, clusterIdentifier); + Snapshot snapshot = service.createSnapshot(snapshotIdentifier, clusterIdentifier, parseTags(params)); String xml = new XmlBuilder() .start("CreateClusterSnapshotResponse") .start("CreateClusterSnapshotResult") @@ -304,7 +304,8 @@ public class RedshiftQueryHandler { String parameterGroupName = params.getFirst("ParameterGroupName"); String parameterGroupFamily = params.getFirst("ParameterGroupFamily"); String description = params.getFirst("Description"); - ClusterParameterGroup group = service.createClusterParameterGroup(parameterGroupName, parameterGroupFamily, description); + ClusterParameterGroup group = service.createClusterParameterGroup(parameterGroupName, parameterGroupFamily, + description, parseTags(params)); String xml = new XmlBuilder() .start("CreateClusterParameterGroupResponse") .start("CreateClusterParameterGroupResult") @@ -1327,8 +1328,12 @@ 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("Snapshot").build(); + return builder.end("Tags").end("Snapshot").build(); } private Response clusterResponse(String operation, Cluster cluster) { @@ -1460,9 +1465,12 @@ public class RedshiftQueryHandler { .start("ClusterParameterGroup") .elem("ParameterGroupName", group.getParameterGroupName()) .elem("ParameterGroupFamily", group.getParameterGroupFamily()) - .elem("Description", group.getDescription()); - - return builder.end("ClusterParameterGroup").build(); + .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(); } private String buildClusterSubnetGroupXml(ClusterSubnetGroup group) { diff --git a/src/main/java/io/floci/aws/services/redshift/RedshiftService.java b/src/main/java/io/floci/aws/services/redshift/RedshiftService.java index 7e9a182bac..359ecdf916 100644 --- a/src/main/java/io/floci/aws/services/redshift/RedshiftService.java +++ b/src/main/java/io/floci/aws/services/redshift/RedshiftService.java @@ -1196,7 +1196,7 @@ public class RedshiftService implements ManagedSecretRotator { if (!"available".equals(cluster.getClusterStatus())) { throw new AwsException("InvalidClusterState", "Cluster must be available to create a final snapshot", 400); } - createSnapshot(finalSnapshotIdentifier, identifier, retentionPeriod); + createSnapshot(finalSnapshotIdentifier, identifier, retentionPeriod, null); } Cluster removed = mutate(() -> { @@ -1950,22 +1950,28 @@ public class RedshiftService implements ManagedSecretRotator { } public Snapshot createSnapshot(String snapshotIdentifier, String clusterIdentifier) { - return withResourceLock("cluster", clusterIdentifier, () -> withResourceLock("snapshot", snapshotIdentifier, - () -> createSnapshot(snapshotIdentifier, clusterIdentifier, null))); + return createSnapshot(snapshotIdentifier, clusterIdentifier, null); } - private Snapshot createSnapshot(String snapshotIdentifier, String clusterIdentifier, Integer retentionPeriod) { + public Snapshot createSnapshot(String snapshotIdentifier, String clusterIdentifier, Map tags) { + return withResourceLock("cluster", clusterIdentifier, () -> withResourceLock("snapshot", snapshotIdentifier, + () -> createSnapshot(snapshotIdentifier, clusterIdentifier, null, tags))); + } + + private Snapshot createSnapshot(String snapshotIdentifier, String clusterIdentifier, Integer retentionPeriod, + Map tags) { validateSnapshotIdentifier(snapshotIdentifier); Optional clusterOpt = clusters.get(clusterIdentifier); if (clusterOpt.isEmpty()) { throw new AwsException("ClusterNotFound", "Cluster " + clusterIdentifier + " not found", 404); } return createSnapshot(clusters.accountId(), snapshotIdentifier, clusterOpt.get(), retentionPeriod, - Instant.now(), null); + Instant.now(), null, tags); } private Snapshot createSnapshot(String accountId, String snapshotIdentifier, Cluster cluster, - Integer retentionPeriod, Instant now, Instant scheduledTime) { + Integer retentionPeriod, Instant now, Instant scheduledTime, + Map tags) { rejectPausedCluster(cluster); validateSnapshotIdentifier(snapshotIdentifier); String clusterIdentifier = cluster.getClusterIdentifier(); @@ -1990,6 +1996,9 @@ public class RedshiftService implements ManagedSecretRotator { snapshot.setSnapshotType(scheduledTime == null ? "manual" : "automated"); snapshot.setScheduledTime(scheduledTime); snapshot.setClusterGeneration(cluster.getSnapshotGeneration()); + if (tags != null) { + snapshot.setTags(new LinkedHashMap<>(tags)); + } if (scheduledTime != null) { snapshot.setAutomatedSnapshotRetentionPeriod(cluster.getAutomatedSnapshotRetentionPeriod()); } @@ -2444,6 +2453,11 @@ public class RedshiftService implements ManagedSecretRotator { // ── Parameter Group Operations ─────────────────────────────────────────── public ClusterParameterGroup createClusterParameterGroup(String parameterGroupName, String parameterGroupFamily, String description) { + return createClusterParameterGroup(parameterGroupName, parameterGroupFamily, description, null); + } + + public ClusterParameterGroup createClusterParameterGroup(String parameterGroupName, String parameterGroupFamily, + String description, Map tags) { if (DEFAULT_PARAMETER_GROUP_NAME.equals(parameterGroupName)) { throw new AwsException("InvalidParameterValue", "The default parameter group name is reserved.", 400); } @@ -2452,6 +2466,9 @@ public class RedshiftService implements ManagedSecretRotator { } ClusterParameterGroup group = new ClusterParameterGroup(parameterGroupName, parameterGroupFamily, description); + if (tags != null) { + group.setTags(new LinkedHashMap<>(tags)); + } parameterGroups.put(parameterGroupName, group); parameterGroups.flush(); emitEvent(RedshiftEventCatalog.PARAMETER_GROUP_CREATED, parameterGroupName, @@ -3176,7 +3193,7 @@ public class RedshiftService implements ManagedSecretRotator { if (tooSoon) { return false; } - createSnapshot(accountId, snapshotId, cluster, null, now, scheduled); + createSnapshot(accountId, snapshotId, cluster, null, now, scheduled, null); } else if (!"automated".equals(existing.getSnapshotType()) || !clusterId.equals(existing.getClusterIdentifier()) || !scheduled.equals(existing.getScheduledTime())) { throw new AwsException("ClusterSnapshotAlreadyExists", "Snapshot identifier is already in use", 400); diff --git a/src/test/java/io/floci/aws/services/redshift/RedshiftOperationsTest.java b/src/test/java/io/floci/aws/services/redshift/RedshiftOperationsTest.java index db1938855c..209c3ba50b 100644 --- a/src/test/java/io/floci/aws/services/redshift/RedshiftOperationsTest.java +++ b/src/test/java/io/floci/aws/services/redshift/RedshiftOperationsTest.java @@ -1037,6 +1037,39 @@ public class RedshiftOperationsTest { .statusCode(200); } + @Test + @Order(3) + void parameterGroupAndSnapshotKeepTheirCreateTags() { + when(containerManager.start(any(), eq("cluster-create-tags"), any(), any(), any(), any())) + .thenReturn(new RedshiftContainerHandle("c4", "cluster-create-tags", "localhost", 5442)); + assertEquals(200, redshift("CreateCluster", "ClusterIdentifier", "cluster-create-tags", "NodeType", "dc2.large", + "MasterUsername", "admin", "MasterUserPassword", "password123").statusCode()); + String tag = "teamred"; + + redshift("CreateClusterParameterGroup", "ParameterGroupName", "pg-create-tags", + "ParameterGroupFamily", "redshift-1.0", "Description", "d", + "Tags.Tag.1.Key", "team", "Tags.Tag.1.Value", "red") + .then().statusCode(200).body(containsString("" + tag + "")); + redshift("DescribeClusterParameterGroups", "ParameterGroupName", "pg-create-tags") + .then().statusCode(200).body(containsString(tag)); + redshift("DescribeTags", "ResourceName", "arn:aws:redshift:us-east-1:000000000000:parametergroup:pg-create-tags") + .then().statusCode(200).body(containsString(tag)); + + redshift("CreateClusterSnapshot", "SnapshotIdentifier", "snap-create-tags", + "ClusterIdentifier", "cluster-create-tags", "Tags.Tag.1.Key", "team", "Tags.Tag.1.Value", "red") + .then().statusCode(200).body(containsString("" + tag + "")); + redshift("DescribeClusterSnapshots", "SnapshotIdentifier", "snap-create-tags") + .then().statusCode(200).body(containsString(tag)); + redshift("DescribeTags", "ResourceName", + "arn:aws:redshift:us-east-1:000000000000:snapshot:cluster-create-tags/snap-create-tags") + .then().statusCode(200).body(containsString(tag)); + + assertEquals(200, redshift("DeleteClusterSnapshot", "SnapshotIdentifier", "snap-create-tags").statusCode()); + assertEquals(200, redshift("DeleteClusterParameterGroup", "ParameterGroupName", "pg-create-tags").statusCode()); + assertEquals(200, redshift("DeleteCluster", "ClusterIdentifier", "cluster-create-tags", + "SkipFinalClusterSnapshot", "true").statusCode()); + } + /** * CreateSnapshotCopyGrant checks the name is free and then writes. Both steps run under the * service lock, so of two concurrent creates of one name exactly one wins and the other sees 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 ddb2eda06e..52b62ba4d2 100644 --- a/src/test/java/io/floci/aws/services/redshift/RedshiftQueryHandlerTest.java +++ b/src/test/java/io/floci/aws/services/redshift/RedshiftQueryHandlerTest.java @@ -535,7 +535,7 @@ class RedshiftQueryHandlerTest { params.putSingle("ClusterIdentifier", "test-cluster"); Snapshot snapshot = new Snapshot("test-snapshot", "test-cluster", "available", 5439, "admin"); - when(service.createSnapshot("test-snapshot", "test-cluster")).thenReturn(snapshot); + when(service.createSnapshot("test-snapshot", "test-cluster", Map.of())).thenReturn(snapshot); Response response = handler.handle("CreateClusterSnapshot", params); assertEquals(200, response.getStatus()); @@ -762,7 +762,8 @@ class RedshiftQueryHandlerTest { params.putSingle("Description", "custom redshift param group"); ClusterParameterGroup group = new ClusterParameterGroup("test-pg", "redshift-1.0", "custom redshift param group"); - when(service.createClusterParameterGroup("test-pg", "redshift-1.0", "custom redshift param group")).thenReturn(group); + when(service.createClusterParameterGroup("test-pg", "redshift-1.0", "custom redshift param group", Map.of())) + .thenReturn(group); Response response = handler.handle("CreateClusterParameterGroup", params); assertEquals(200, response.getStatus());