mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(redshift): keep tags on new cluster parameter groups and snapshots (#8068)
CreateClusterParameterGroup and CreateClusterSnapshot both take Tags, but the handler never parsed them, so the new parameter group or snapshot came back untagged: in the create response, in DescribeClusterParameterGroups and DescribeClusterSnapshots, and in DescribeTags on its ARN. Tags are now parsed (Tags.Tag.N and Tags.member.N) and stored on the resource, as CreateClusterSubnetGroup already does, and the ClusterParameterGroup and Snapshot elements carry a Tags list, as AWS returns them. DeleteCluster's final snapshot and automated snapshots are still created without tags.
This commit is contained in:
1 parent
35bdfdcb3f
commit
07d6674f79
4 files changed
+74
-15
No files matched your search
@@ -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<String, String> 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<String, String> 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) {
|
||||
|
||||
@@ -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<String, String> tags) {
|
||||
return withResourceLock("cluster", clusterIdentifier, () -> withResourceLock("snapshot", snapshotIdentifier,
|
||||
() -> createSnapshot(snapshotIdentifier, clusterIdentifier, null, tags)));
|
||||
}
|
||||
|
||||
private Snapshot createSnapshot(String snapshotIdentifier, String clusterIdentifier, Integer retentionPeriod,
|
||||
Map<String, String> tags) {
|
||||
validateSnapshotIdentifier(snapshotIdentifier);
|
||||
Optional<Cluster> 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<String, String> 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<String, String> 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);
|
||||
|
||||
@@ -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 = "<Tag><Key>team</Key><Value>red</Value></Tag>";
|
||||
|
||||
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("<Tags>" + tag + "</Tags>"));
|
||||
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("<Tags>" + tag + "</Tags>"));
|
||||
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
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in new issue
Block a user