feat(redshift): support CopyClusterSnapshot API action (#7748) (#7750)

* feat(redshift): support CopyClusterSnapshot API action (#7748)

* fix(redshift): address review findings and sync docs for CopyClusterSnapshot (#7748)

* refactor(redshift): simplify CopyClusterSnapshot using existing helpers (#7748)

Drop Tags, KmsKeyId and the ARN branch, which CopyClusterSnapshotMessage does not carry.
Reuse validateManualRetention (InvalidRetentionPeriodFault), takenFrom and SnapshotDumpCopier.copy,
and share the automated-snapshot setup across the new tests.
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-11-08 17:56:25 -04:00
1 parent b4b80cf039
commit e0692092b5
7 files changed
+358 -1

No files matched your search

@@ -241,6 +241,27 @@ public class RedshiftQueryHandler {
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
case "CopyClusterSnapshot" -> {
String sourceSnapshotId = requireParam(params, "SourceSnapshotIdentifier");
String targetSnapshotId = requireParam(params, "TargetSnapshotIdentifier");
String sourceClusterId = params.getFirst("SourceSnapshotClusterIdentifier");
Integer manualRetention = parseOptionalInteger(params, "ManualSnapshotRetentionPeriod");
Snapshot snapshot = service.copyClusterSnapshot(sourceSnapshotId, targetSnapshotId,
sourceClusterId, manualRetention);
String xml = new XmlBuilder()
.start("CopyClusterSnapshotResponse")
.start("CopyClusterSnapshotResult")
.raw(buildSnapshotXml(snapshot))
.end("CopyClusterSnapshotResult")
.start("ResponseMetadata")
.elem("RequestId", "test-req-id")
.end("ResponseMetadata")
.end("CopyClusterSnapshotResponse")
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
case "RestoreFromClusterSnapshot" -> {
String clusterIdentifier = params.getFirst("ClusterIdentifier");
String snapshotIdentifier = params.getFirst("SnapshotIdentifier");
@@ -2052,6 +2052,88 @@ public class RedshiftService implements ManagedSecretRotator {
|| clusterIdentifier.equals(snapshot.getClusterIdentifier());
}
public Snapshot copyClusterSnapshot(String sourceSnapshotIdentifier, String targetSnapshotIdentifier,
String sourceSnapshotClusterIdentifier, Integer manualRetention) {
validateSnapshotIdentifier(targetSnapshotIdentifier);
if (manualRetention != null) {
validateManualRetention(manualRetention);
}
String accountId = clusters.accountId();
String region = regionResolver.getRegion();
return withResourceLock("snapshot", targetSnapshotIdentifier, () -> {
boolean shadowedByCopy = snapshotCopies.get(copyKey(region, targetSnapshotIdentifier)).isPresent();
if (shadowedByCopy || snapshots.get(targetSnapshotIdentifier).isPresent()) {
throw new AwsException("ClusterSnapshotAlreadyExists",
"Snapshot " + targetSnapshotIdentifier + " already exists", 400);
}
Snapshot source = findSnapshot(sourceSnapshotIdentifier)
.filter(found -> takenFrom(found, sourceSnapshotClusterIdentifier))
.orElseThrow(() -> new AwsException("ClusterSnapshotNotFound",
"Snapshot " + sourceSnapshotIdentifier + " not found", 404));
if (!"available".equals(source.getStatus())) {
throw new AwsException("InvalidClusterSnapshotState",
"Snapshot " + sourceSnapshotIdentifier + " is not available", 400);
}
if (!"automated".equals(source.getSnapshotType())) {
throw new AwsException("InvalidClusterSnapshotState",
"Cannot copy a manual snapshot: " + sourceSnapshotIdentifier
+ ". Only automated snapshots can be copied.", 400);
}
Snapshot copy = new Snapshot(source);
copy.setSnapshotIdentifier(targetSnapshotIdentifier);
copy.setSnapshotType("manual");
copy.setSnapshotCreateTime(Instant.now());
copy.setManualSnapshotRetentionPeriod(
manualRetention != null ? manualRetention : RETAIN_MANUAL_SNAPSHOTS_INDEFINITELY);
copy.setAutomatedSnapshotRetentionPeriod(null);
copy.setScheduledTime(null);
copy.setSnapshotArn(snapshotArn(source.getClusterIdentifier(), targetSnapshotIdentifier));
copy.setCopiedToRegions(new LinkedHashSet<>());
copy.setSqlDump(null);
Path copiedDump = null;
if (source.getSqlDump() != null && !source.getSqlDump().isBlank()) {
Path dumpDir = accountDumpDir(accountId);
try {
copiedDump = SnapshotDumpCopier.copy(Paths.get(source.getSqlDump()), dumpDir, dumpDir);
} catch (IOException e) {
LOG.warnv(e, "Could not copy the dump of snapshot {0}", sourceSnapshotIdentifier);
throw new AwsException("InternalFailure", "Failed to copy snapshot dump", 500);
}
copy.setSqlDump(copiedDump.toString());
}
try {
snapshots.putAllAtomically(Map.of(targetSnapshotIdentifier, copy));
snapshots.flush();
} catch (RuntimeException e) {
deleteQuietly(copiedDump, e);
throw e;
}
emitSnapshotEvent(accountId, region, RedshiftEventCatalog.SNAPSHOT_STARTED,
targetSnapshotIdentifier, "A user snapshot '" + targetSnapshotIdentifier
+ "' for Amazon Redshift cluster '" + source.getClusterIdentifier()
+ "' was copied from snapshot '" + sourceSnapshotIdentifier + "'.");
return copy;
});
}
private static void deleteQuietly(Path file, Exception cause) {
if (file == null) {
return;
}
try {
Files.deleteIfExists(file);
} catch (IOException cleanupFailure) {
cause.addSuppressed(cleanupFailure);
}
}
public Snapshot deleteSnapshot(String snapshotIdentifier) {
return deleteSnapshot(snapshotIdentifier, null);
}