feat(redshift): add snapshot copy and snapshot schedule operations (#5574)

* feat(redshift): add snapshot copy and snapshot schedule operations

Terraform's aws_redshift_snapshot_copy, aws_redshift_snapshot_schedule and
aws_redshift_snapshot_schedule_association failed with InvalidAction because
the Query handler had no arms for these actions.

- EnableSnapshotCopy / DisableSnapshotCopy / ModifySnapshotCopyRetentionPeriod
  store the copy settings on the cluster, and DescribeClusters reports
  ClusterSnapshotCopyStatus while copy is enabled. RetentionPeriod defaults
  to 7 and ManualSnapshotRetentionPeriod to -1. Faults follow the Redshift
  model: SnapshotCopyAlreadyEnabledFault, SnapshotCopyAlreadyDisabledFault,
  SnapshotCopyDisabledFault (modify), InvalidRetentionPeriodFault and
  SnapshotCopyGrantNotFoundFault.
- Create/Describe/Modify/DeleteSnapshotSchedule and
  ModifyClusterSnapshotSchedule. The association lives on the cluster, so
  DescribeSnapshotSchedules derives AssociatedClusters (state ACTIVE) and
  filters by ClusterIdentifier from it, and a deleted cluster cannot leave a
  stale association. Deleting a schedule in use is refused with
  InvalidClusterSnapshotScheduleState.
- Snapshot schedules take part in CreateTags/DeleteTags/DescribeTags as
  resource type snapshotschedule.

No snapshot is copied to another region or taken on a schedule; the docs
say so.

* fix(redshift): reject malformed snapshot schedules and in-use copy grant deletion

* fix(redshift): validate snapshot copy region and snapshot schedule identifier

EnableSnapshotCopy now refuses a DestinationRegion that is not a published
AWS region with UnknownSnapshotCopyRegionFault (404), as the model lists.

CreateSnapshotSchedule now refuses a ScheduleIdentifier containing anything
but letters, digits and hyphens with InvalidParameterValue (400). The API
reference allows alphanumerics; the management guide's example uses a hyphen.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-13 13:12:34 -04:00
1 parent 43eacd524c
commit 63b48342d6
10 files changed
+727 -8

No files matched your search

@@ -644,6 +644,9 @@ public class AwsQueryController {
"ModifyClusterParameterGroup",
"CreateClusterSubnetGroup", "DescribeClusterSubnetGroups", "ModifyClusterSubnetGroup", "DeleteClusterSubnetGroup",
"CreateSnapshotCopyGrant", "DescribeSnapshotCopyGrants", "DeleteSnapshotCopyGrant",
"EnableSnapshotCopy", "DisableSnapshotCopy", "ModifySnapshotCopyRetentionPeriod",
"CreateSnapshotSchedule", "DescribeSnapshotSchedules", "ModifySnapshotSchedule",
"DeleteSnapshotSchedule", "ModifyClusterSnapshotSchedule",
"CreateTags", "DeleteTags", "DescribeTags",
"GetClusterCredentials", "GetClusterCredentialsWithIAM"
);
@@ -13,6 +13,7 @@ import io.github.hectorvent.floci.services.redshift.model.Integration;
import io.github.hectorvent.floci.services.redshift.model.Parameter;
import io.github.hectorvent.floci.services.redshift.model.Snapshot;
import io.github.hectorvent.floci.services.redshift.model.SnapshotCopyGrant;
import io.github.hectorvent.floci.services.redshift.model.SnapshotSchedule;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.MediaType;
@@ -564,6 +565,72 @@ public class RedshiftQueryHandler {
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
case "EnableSnapshotCopy" -> {
Cluster cluster = service.enableSnapshotCopy(requireParam(params, "ClusterIdentifier"),
requireParam(params, "DestinationRegion"),
intParam(params, "RetentionPeriod"),
intParam(params, "ManualSnapshotRetentionPeriod"),
params.getFirst("SnapshotCopyGrantName"));
return clusterResponse(action, cluster);
}
case "DisableSnapshotCopy" -> {
return clusterResponse(action, service.disableSnapshotCopy(requireParam(params, "ClusterIdentifier")));
}
case "ModifySnapshotCopyRetentionPeriod" -> {
String clusterIdentifier = requireParam(params, "ClusterIdentifier");
requireParam(params, "RetentionPeriod");
Cluster cluster = service.modifySnapshotCopyRetentionPeriod(clusterIdentifier,
intParam(params, "RetentionPeriod"), Boolean.parseBoolean(params.getFirst("Manual")));
return clusterResponse(action, cluster);
}
case "CreateSnapshotSchedule" -> {
SnapshotSchedule schedule = service.createSnapshotSchedule(params.getFirst("ScheduleIdentifier"),
params.getFirst("ScheduleDescription"), memberList(params, "ScheduleDefinitions"),
parseTags(params));
return snapshotScheduleResponse(action, schedule);
}
case "ModifySnapshotSchedule" -> {
SnapshotSchedule schedule = service.modifySnapshotSchedule(requireParam(params, "ScheduleIdentifier"),
memberList(params, "ScheduleDefinitions"));
return snapshotScheduleResponse(action, schedule);
}
case "DescribeSnapshotSchedules" -> {
PaginatedResult<SnapshotSchedule> page = service.describeSnapshotSchedules(
params.getFirst("ScheduleIdentifier"), params.getFirst("ClusterIdentifier"),
memberList(params, "TagKeys"), memberList(params, "TagValues"),
intParam(params, "MaxRecords"), params.getFirst("Marker"));
XmlBuilder xmlBuilder = new XmlBuilder()
.start("DescribeSnapshotSchedulesResponse")
.start("DescribeSnapshotSchedulesResult")
.start("SnapshotSchedules");
for (SnapshotSchedule schedule : page.items()) {
xmlBuilder.start("SnapshotSchedule")
.raw(buildSnapshotScheduleFields(schedule))
.end("SnapshotSchedule");
}
xmlBuilder.end("SnapshotSchedules");
if (page.nextToken() != null) {
xmlBuilder.elem("Marker", page.nextToken());
}
String xml = xmlBuilder
.end("DescribeSnapshotSchedulesResult")
.start("ResponseMetadata")
.elem("RequestId", "test-req-id")
.end("ResponseMetadata")
.end("DescribeSnapshotSchedulesResponse")
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
case "DeleteSnapshotSchedule" -> {
service.deleteSnapshotSchedule(requireParam(params, "ScheduleIdentifier"));
return emptyResponse(action);
}
case "ModifyClusterSnapshotSchedule" -> {
service.modifyClusterSnapshotSchedule(requireParam(params, "ClusterIdentifier"),
params.getFirst("ScheduleIdentifier"),
Boolean.parseBoolean(params.getFirst("DisassociateSchedule")));
return emptyResponse(action);
}
case "ModifyCluster" -> {
String clusterIdentifier = params.getFirst("ClusterIdentifier");
if (clusterIdentifier == null || clusterIdentifier.isBlank()) {
@@ -846,6 +913,20 @@ public class RedshiftQueryHandler {
builder.end("Tags");
}
if (cluster.getSnapshotCopyDestinationRegion() != null) {
builder.start("ClusterSnapshotCopyStatus")
.elem("DestinationRegion", cluster.getSnapshotCopyDestinationRegion())
.elem("RetentionPeriod", String.valueOf(cluster.getSnapshotCopyRetentionPeriod()))
.elem("ManualSnapshotRetentionPeriod", String.valueOf(cluster.getSnapshotCopyManualRetentionPeriod()))
.elem("SnapshotCopyGrantName", cluster.getSnapshotCopyGrantName())
.end("ClusterSnapshotCopyStatus");
}
if (cluster.getSnapshotScheduleIdentifier() != null) {
builder.elem("SnapshotScheduleIdentifier", cluster.getSnapshotScheduleIdentifier())
.elem("SnapshotScheduleState", "ACTIVE");
}
if (cluster.getEndpoint() != null) {
builder.start("Endpoint")
.elem("Address", cluster.getEndpoint().getAddress())
@@ -924,6 +1005,75 @@ public class RedshiftQueryHandler {
return builder.end("Snapshot").build();
}
private Response clusterResponse(String operation, Cluster cluster) {
String xml = new XmlBuilder()
.start(operation + "Response")
.start(operation + "Result")
.raw(buildClusterXml(cluster))
.end(operation + "Result")
.start("ResponseMetadata")
.elem("RequestId", "test-req-id")
.end("ResponseMetadata")
.end(operation + "Response")
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
// Create/ModifySnapshotSchedule return the SnapshotSchedule members directly inside the Result.
private Response snapshotScheduleResponse(String operation, SnapshotSchedule schedule) {
String xml = new XmlBuilder()
.start(operation + "Response")
.start(operation + "Result")
.raw(buildSnapshotScheduleFields(schedule))
.end(operation + "Result")
.start("ResponseMetadata")
.elem("RequestId", "test-req-id")
.end("ResponseMetadata")
.end(operation + "Response")
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
private static Response emptyResponse(String operation) {
String xml = new XmlBuilder()
.start(operation + "Response")
.start("ResponseMetadata")
.elem("RequestId", "test-req-id")
.end("ResponseMetadata")
.end(operation + "Response")
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
// No snapshots are taken on a schedule, so NextInvocations is omitted. An association
// takes effect immediately, hence ACTIVE.
private String buildSnapshotScheduleFields(SnapshotSchedule schedule) {
XmlBuilder builder = new XmlBuilder().start("ScheduleDefinitions");
for (String definition : schedule.getScheduleDefinitions()) {
builder.elem("ScheduleDefinition", definition);
}
builder.end("ScheduleDefinitions")
.elem("ScheduleIdentifier", schedule.getScheduleIdentifier())
.elem("ScheduleDescription", schedule.getScheduleDescription());
builder.start("Tags");
for (Map.Entry<String, String> tag : schedule.getTags().entrySet()) {
builder.start("Tag")
.elem("Key", tag.getKey())
.elem("Value", tag.getValue())
.end("Tag");
}
builder.end("Tags");
List<String> associated = service.clustersUsingSnapshotSchedule(schedule.getScheduleIdentifier());
builder.elem("AssociatedClusterCount", associated.size()).start("AssociatedClusters");
for (String clusterIdentifier : associated) {
builder.start("ClusterAssociatedToSchedule")
.elem("ClusterIdentifier", clusterIdentifier)
.elem("ScheduleAssociationState", "ACTIVE")
.end("ClusterAssociatedToSchedule");
}
return builder.end("AssociatedClusters").build();
}
private Response loggingStatusResponse(String operation, Cluster cluster) {
String xml = new XmlBuilder()
.start(operation + "Response")
@@ -1162,6 +1312,8 @@ public class RedshiftQueryHandler {
case "VpcSecurityGroupIds" -> quoted + "(\\.member|\\.VpcSecurityGroupId)?\\.\\d+";
case "IamRoles", "AddIamRoles", "RemoveIamRoles" -> quoted + "(\\.member|\\.IamRoleArn)?\\.\\d+";
case "TagKeys" -> quoted + "(\\.member|\\.TagKey)?\\.\\d+";
case "TagValues" -> quoted + "(\\.member|\\.TagValue)?\\.\\d+";
case "ScheduleDefinitions" -> quoted + "(\\.member|\\.ScheduleDefinition)?\\.\\d+";
case "DbGroups" -> quoted + "(\\.member|\\.DbGroup)?\\.\\d+";
default -> quoted + "(\\.member)?\\.\\d+";
};
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -23,6 +24,7 @@ import io.github.hectorvent.floci.services.redshift.model.Integration;
import io.github.hectorvent.floci.services.redshift.model.Parameter;
import io.github.hectorvent.floci.services.redshift.model.Snapshot;
import io.github.hectorvent.floci.services.redshift.model.SnapshotCopyGrant;
import io.github.hectorvent.floci.services.redshift.model.SnapshotSchedule;
import io.github.hectorvent.floci.services.redshift.proxy.RedshiftProxyManager;
import io.github.hectorvent.floci.services.secretsmanager.RandomPasswordGenerator;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
@@ -51,6 +53,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ApplicationScoped
@@ -75,6 +78,7 @@ public class RedshiftService {
private final AccountAwareStorageBackend<Integration> integrations;
private final AccountAwareStorageBackend<SnapshotCopyGrant> snapshotCopyGrants;
private final AccountAwareStorageBackend<SnapshotSchedule> snapshotSchedules;
private final RedshiftContainerManager containerManager;
private final EmulatorConfig config;
private final RegionResolver regionResolver;
@@ -100,6 +104,7 @@ public class RedshiftService {
this.subnetGroups = storageFactory.create("redshift", "redshift-subnet-groups.json", new TypeReference<Map<String, ClusterSubnetGroup>>() {});
this.integrations = storageFactory.create("redshift", "redshift-integrations.json", new TypeReference<Map<String, Integration>>() {});
this.snapshotCopyGrants = storageFactory.create("redshift", "redshift-snapshot-copy-grants.json", new TypeReference<Map<String, SnapshotCopyGrant>>() {});
this.snapshotSchedules = storageFactory.create("redshift", "redshift-snapshot-schedules.json", new TypeReference<Map<String, SnapshotSchedule>>() {});
this.containerManager = containerManager;
this.config = config;
this.regionResolver = regionResolver;
@@ -1317,11 +1322,253 @@ public class RedshiftService {
SnapshotCopyGrant grant = snapshotCopyGrants.get(name)
.orElseThrow(() -> new AwsException("SnapshotCopyGrantNotFoundFault",
"Snapshot copy grant " + name + " not found", 400));
boolean inUse = clusters.scan(k -> true).stream()
.anyMatch(c -> name.equals(c.getSnapshotCopyGrantName()));
if (inUse) {
throw new AwsException("InvalidSnapshotCopyGrantStateFault",
"Snapshot copy grant " + name + " is used by one or more clusters", 400);
}
snapshotCopyGrants.delete(name);
snapshotCopyGrants.flush();
return grant;
}
// ── Snapshot Copy Operations ─────────────────────────────────────────────
private static final int DEFAULT_SNAPSHOT_COPY_RETENTION_DAYS = 7;
// AWS reports -1 ("retain indefinitely") when ManualSnapshotRetentionPeriod is not given.
private static final int RETAIN_MANUAL_SNAPSHOTS_INDEFINITELY = -1;
private Cluster requireCluster(String clusterIdentifier) {
return clusters.get(clusterIdentifier)
.orElseThrow(() -> new AwsException("ClusterNotFound", "Cluster " + clusterIdentifier + " not found", 404));
}
// Automated snapshots: 1 to 35 days.
private static void validateAutomatedRetention(int days) {
if (days < 1 || days > 35) {
throw new AwsException("InvalidRetentionPeriodFault",
"RetentionPeriod must be between 1 and 35 days.", 400);
}
}
// Manual snapshots: -1 (indefinitely) or 1 to 3653 days.
private static void validateManualRetention(int days) {
if (days != RETAIN_MANUAL_SNAPSHOTS_INDEFINITELY && (days < 1 || days > 3653)) {
throw new AwsException("InvalidRetentionPeriodFault",
"ManualSnapshotRetentionPeriod must be -1 or between 1 and 3653 days.", 400);
}
}
/** Copy is recorded on the cluster only; no snapshot is actually copied to another region. */
public synchronized Cluster enableSnapshotCopy(String clusterIdentifier, String destinationRegion,
Integer retentionPeriod, Integer manualRetentionPeriod,
String snapshotCopyGrantName) {
Cluster cluster = requireCluster(clusterIdentifier);
if (cluster.getSnapshotCopyDestinationRegion() != null) {
throw new AwsException("SnapshotCopyAlreadyEnabledFault",
"Snapshot copy is already enabled on cluster " + clusterIdentifier + ".", 400);
}
if (!AwsRegions.isRegionId(destinationRegion)) {
throw new AwsException("UnknownSnapshotCopyRegionFault",
"Unknown destination region: " + destinationRegion, 404);
}
int retention = retentionPeriod == null ? DEFAULT_SNAPSHOT_COPY_RETENTION_DAYS : retentionPeriod;
int manualRetention = manualRetentionPeriod == null
? RETAIN_MANUAL_SNAPSHOTS_INDEFINITELY : manualRetentionPeriod;
validateAutomatedRetention(retention);
validateManualRetention(manualRetention);
boolean hasGrant = snapshotCopyGrantName != null && !snapshotCopyGrantName.isBlank();
if (hasGrant && snapshotCopyGrants.get(snapshotCopyGrantName).isEmpty()) {
throw new AwsException("SnapshotCopyGrantNotFoundFault",
"Snapshot copy grant " + snapshotCopyGrantName + " not found", 400);
}
cluster.setSnapshotCopyDestinationRegion(destinationRegion);
cluster.setSnapshotCopyRetentionPeriod(retention);
cluster.setSnapshotCopyManualRetentionPeriod(manualRetention);
cluster.setSnapshotCopyGrantName(hasGrant ? snapshotCopyGrantName : null);
clusters.put(clusterIdentifier, cluster);
clusters.flush();
return cluster;
}
public synchronized Cluster disableSnapshotCopy(String clusterIdentifier) {
Cluster cluster = requireCluster(clusterIdentifier);
if (cluster.getSnapshotCopyDestinationRegion() == null) {
throw new AwsException("SnapshotCopyAlreadyDisabledFault",
"Snapshot copy is not enabled on cluster " + clusterIdentifier + ".", 400);
}
cluster.setSnapshotCopyDestinationRegion(null);
cluster.setSnapshotCopyRetentionPeriod(null);
cluster.setSnapshotCopyManualRetentionPeriod(null);
cluster.setSnapshotCopyGrantName(null);
clusters.put(clusterIdentifier, cluster);
clusters.flush();
return cluster;
}
public synchronized Cluster modifySnapshotCopyRetentionPeriod(String clusterIdentifier, int retentionPeriod,
boolean manual) {
Cluster cluster = requireCluster(clusterIdentifier);
// SnapshotCopyDisabledFault, not ...AlreadyDisabledFault: the model lists only this one here.
if (cluster.getSnapshotCopyDestinationRegion() == null) {
throw new AwsException("SnapshotCopyDisabledFault",
"Snapshot copy is not enabled on cluster " + clusterIdentifier + ".", 400);
}
if (manual) {
validateManualRetention(retentionPeriod);
cluster.setSnapshotCopyManualRetentionPeriod(retentionPeriod);
} else {
validateAutomatedRetention(retentionPeriod);
cluster.setSnapshotCopyRetentionPeriod(retentionPeriod);
}
clusters.put(clusterIdentifier, cluster);
clusters.flush();
return cluster;
}
// ── Snapshot Schedule Operations ─────────────────────────────────────────
private static final int SNAPSHOT_SCHEDULE_PAGE_MAX = 100;
private static final Pattern RATE_DEFINITION =
Pattern.compile("rate\\(\\s*(\\d+)\\s+(hours?|days?)\\s*\\)");
private static final Pattern CRON_DEFINITION =
Pattern.compile("cron\\(\\s*([A-Za-z0-9,\\-*/?#]+(?:\\s+[A-Za-z0-9,\\-*/?#]+){2,5})\\s*\\)");
// The API reference allows only alphanumerics; the management guide's own example uses a hyphen.
private static final Pattern SCHEDULE_IDENTIFIER = Pattern.compile("[A-Za-z0-9-]+");
/**
* Shape check against the Redshift snapshot-schedule documentation: {@code rate(<n> hours|days)}
* with a frequency between once an hour and once a day, or {@code cron(...)} with three to six
* whitespace-separated fields drawn from the documented cron alphabet. Per-field value ranges
* are not checked.
*/
private static boolean isValidScheduleDefinition(String definition) {
if (definition == null) {
return false;
}
Matcher rate = RATE_DEFINITION.matcher(definition);
if (rate.matches()) {
long n = Long.parseLong(rate.group(1).length() > 9 ? "0" : rate.group(1));
return rate.group(2).startsWith("hour") ? n >= 1 && n <= 24 : n == 1;
}
return CRON_DEFINITION.matcher(definition).matches();
}
private static void validateScheduleDefinitions(List<String> definitions) {
if (definitions == null || definitions.isEmpty()) {
throw new AwsException("InvalidSchedule", "ScheduleDefinitions must contain at least one definition.", 400);
}
for (String definition : definitions) {
if (!isValidScheduleDefinition(definition)) {
throw new AwsException("InvalidSchedule", "Invalid schedule definition: " + definition, 400);
}
}
}
private SnapshotSchedule requireSnapshotSchedule(String scheduleIdentifier) {
return snapshotSchedules.get(scheduleIdentifier)
.orElseThrow(() -> new AwsException("SnapshotScheduleNotFound",
"Snapshot schedule " + scheduleIdentifier + " not found", 400));
}
public synchronized SnapshotSchedule createSnapshotSchedule(String scheduleIdentifier, String description,
List<String> definitions, Map<String, String> tags) {
String id = (scheduleIdentifier == null || scheduleIdentifier.isBlank())
? UUID.randomUUID().toString().replace("-", "")
: scheduleIdentifier;
if (!SCHEDULE_IDENTIFIER.matcher(id).matches()) {
throw new AwsException("InvalidParameterValue",
"ScheduleIdentifier may contain only letters, digits and hyphens: " + id, 400);
}
if (snapshotSchedules.get(id).isPresent()) {
throw new AwsException("SnapshotScheduleAlreadyExists", "Snapshot schedule " + id + " already exists", 400);
}
validateScheduleDefinitions(definitions);
SnapshotSchedule schedule = new SnapshotSchedule(id, description, definitions);
if (tags != null && !tags.isEmpty()) {
schedule.setTags(new LinkedHashMap<>(tags));
}
snapshotSchedules.put(id, schedule);
snapshotSchedules.flush();
return schedule;
}
/**
* Every filter given must match. An unknown schedule or cluster yields an empty list rather
* than a fault: the model lists no errors for this operation.
*/
public PaginatedResult<SnapshotSchedule> describeSnapshotSchedules(String scheduleIdentifier,
String clusterIdentifier,
List<String> tagKeys, List<String> tagValues,
Integer maxRecords, String marker) {
String clusterSchedule = null;
if (clusterIdentifier != null && !clusterIdentifier.isBlank()) {
clusterSchedule = clusters.get(clusterIdentifier).map(Cluster::getSnapshotScheduleIdentifier).orElse(null);
if (clusterSchedule == null) {
return new PaginatedResult<>(List.of(), null);
}
}
String byCluster = clusterSchedule;
List<SnapshotSchedule> matching = snapshotSchedules.scan(k -> true).stream()
.filter(s -> scheduleIdentifier == null || scheduleIdentifier.isBlank()
|| scheduleIdentifier.equals(s.getScheduleIdentifier()))
.filter(s -> byCluster == null || byCluster.equals(s.getScheduleIdentifier()))
.filter(s -> tagKeys == null || tagKeys.isEmpty()
|| tagKeys.stream().anyMatch(s.getTags()::containsKey))
.filter(s -> tagValues == null || tagValues.isEmpty()
|| tagValues.stream().anyMatch(s.getTags()::containsValue))
.toList();
return Pagination.paginate(matching, SnapshotSchedule::getScheduleIdentifier, maxRecords, marker,
SNAPSHOT_SCHEDULE_PAGE_MAX, "InvalidParameterValue");
}
/** Identifiers of the clusters currently using the schedule, sorted for a stable response. */
public List<String> clustersUsingSnapshotSchedule(String scheduleIdentifier) {
return clusters.scan(k -> true).stream()
.filter(c -> scheduleIdentifier.equals(c.getSnapshotScheduleIdentifier()))
.map(Cluster::getClusterIdentifier)
.sorted()
.toList();
}
public synchronized SnapshotSchedule modifySnapshotSchedule(String scheduleIdentifier, List<String> definitions) {
SnapshotSchedule schedule = requireSnapshotSchedule(scheduleIdentifier);
validateScheduleDefinitions(definitions);
schedule.setScheduleDefinitions(new ArrayList<>(definitions));
snapshotSchedules.put(scheduleIdentifier, schedule);
snapshotSchedules.flush();
return schedule;
}
public synchronized void deleteSnapshotSchedule(String scheduleIdentifier) {
requireSnapshotSchedule(scheduleIdentifier);
if (!clustersUsingSnapshotSchedule(scheduleIdentifier).isEmpty()) {
throw new AwsException("InvalidClusterSnapshotScheduleState",
"Snapshot schedule " + scheduleIdentifier + " is associated with one or more clusters.", 400);
}
snapshotSchedules.delete(scheduleIdentifier);
snapshotSchedules.flush();
}
public synchronized Cluster modifyClusterSnapshotSchedule(String clusterIdentifier, String scheduleIdentifier,
boolean disassociate) {
Cluster cluster = requireCluster(clusterIdentifier);
if (disassociate) {
cluster.setSnapshotScheduleIdentifier(null);
} else {
if (scheduleIdentifier == null || scheduleIdentifier.isBlank()) {
throw new AwsException("InvalidParameterValue",
"ScheduleIdentifier is required unless DisassociateSchedule is true", 400);
}
requireSnapshotSchedule(scheduleIdentifier);
cluster.setSnapshotScheduleIdentifier(scheduleIdentifier);
}
clusters.put(clusterIdentifier, cluster);
clusters.flush();
return cluster;
}
// ── Tagging Operations ───────────────────────────────────────────────────
/** A resolved tag target: its current tags plus a sink that persists an updated map. */
@@ -1384,6 +1631,12 @@ public class RedshiftService {
"snapshotcopygrant", g.getTags(), tagKeysFilter);
}
}
if (resourceType == null || "snapshotschedule".equalsIgnoreCase(resourceType)) {
for (SnapshotSchedule s : snapshotSchedules.scan(k -> true)) {
addTaggedResources(result, snapshotScheduleArn(s.getScheduleIdentifier()),
"snapshotschedule", s.getTags(), tagKeysFilter);
}
}
return result;
}
@@ -1425,13 +1678,17 @@ public class RedshiftService {
return regionResolver.buildArn("redshift", regionResolver.getRegion(), "snapshotcopygrant:" + name);
}
private String snapshotScheduleArn(String scheduleIdentifier) {
return regionResolver.buildArn("redshift", regionResolver.getRegion(), "snapshotschedule:" + scheduleIdentifier);
}
/**
* Resolves a tagging ResourceName to its backing resource.
*
* Redshift ARNs have the shape {@code arn:aws:redshift:<region>:<account>:<type>:<id>},
* where {@code <type>} is one of {@code cluster}, {@code snapshot} (id shape
* {@code <clusterId>/<snapshotId>}), {@code parametergroup}, {@code subnetgroup} or
* {@code snapshotcopygrant}. Unlike RDS's tag
* {@code snapshotcopygrant} or {@code snapshotschedule}. Unlike RDS's tag
* resolution, there is no bare-name fallback: Redshift tagging is new, so there is no
* existing caller to stay backward compatible with.
*/
@@ -1514,6 +1771,16 @@ public class RedshiftService {
snapshotCopyGrants.flush();
});
}
case "snapshotschedule" -> {
// ResourceNotFoundFault for the same reason as snapshotcopygrant above.
SnapshotSchedule schedule = snapshotSchedules.get(id)
.orElseThrow(() -> new AwsException("ResourceNotFoundFault", "Snapshot schedule " + id + " not found", 404));
yield new TagHandle(schedule.getTags(), updated -> {
schedule.setTags(updated);
snapshotSchedules.put(id, schedule);
snapshotSchedules.flush();
});
}
default -> throw new AwsException("InvalidParameterValue",
"Tagging for resource type '" + type + "' is not supported: " + resourceName, 400);
};
@@ -73,6 +73,24 @@ public class Cluster {
public String getLoggingS3TableGranularity() { return loggingS3TableGranularity; }
public void setLoggingS3TableGranularity(String loggingS3TableGranularity) { this.loggingS3TableGranularity = loggingS3TableGranularity; }
// Cross-region snapshot copy (EnableSnapshotCopy). Enabled exactly when the destination is set.
private String snapshotCopyDestinationRegion;
private Integer snapshotCopyRetentionPeriod;
private Integer snapshotCopyManualRetentionPeriod;
private String snapshotCopyGrantName;
private String snapshotScheduleIdentifier;
public String getSnapshotCopyDestinationRegion() { return snapshotCopyDestinationRegion; }
public void setSnapshotCopyDestinationRegion(String snapshotCopyDestinationRegion) { this.snapshotCopyDestinationRegion = snapshotCopyDestinationRegion; }
public Integer getSnapshotCopyRetentionPeriod() { return snapshotCopyRetentionPeriod; }
public void setSnapshotCopyRetentionPeriod(Integer snapshotCopyRetentionPeriod) { this.snapshotCopyRetentionPeriod = snapshotCopyRetentionPeriod; }
public Integer getSnapshotCopyManualRetentionPeriod() { return snapshotCopyManualRetentionPeriod; }
public void setSnapshotCopyManualRetentionPeriod(Integer snapshotCopyManualRetentionPeriod) { this.snapshotCopyManualRetentionPeriod = snapshotCopyManualRetentionPeriod; }
public String getSnapshotCopyGrantName() { return snapshotCopyGrantName; }
public void setSnapshotCopyGrantName(String snapshotCopyGrantName) { this.snapshotCopyGrantName = snapshotCopyGrantName; }
public String getSnapshotScheduleIdentifier() { return snapshotScheduleIdentifier; }
public void setSnapshotScheduleIdentifier(String snapshotScheduleIdentifier) { this.snapshotScheduleIdentifier = snapshotScheduleIdentifier; }
private boolean multiAZ = false;
public boolean isMultiAZ() { return multiAZ; }
@@ -0,0 +1,37 @@
package io.github.hectorvent.floci.services.redshift.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* A snapshot schedule. Associated clusters are not stored here: each cluster records the
* schedule it uses, so deleting a cluster cannot leave a stale association behind.
*/
@RegisterForReflection
public class SnapshotSchedule {
private String scheduleIdentifier;
private String scheduleDescription;
private List<String> scheduleDefinitions = new ArrayList<>();
private Map<String, String> tags = new LinkedHashMap<>();
public SnapshotSchedule() {}
public SnapshotSchedule(String scheduleIdentifier, String scheduleDescription, List<String> scheduleDefinitions) {
this.scheduleIdentifier = scheduleIdentifier;
this.scheduleDescription = scheduleDescription;
this.scheduleDefinitions = new ArrayList<>(scheduleDefinitions);
}
public String getScheduleIdentifier() { return scheduleIdentifier; }
public void setScheduleIdentifier(String scheduleIdentifier) { this.scheduleIdentifier = scheduleIdentifier; }
public String getScheduleDescription() { return scheduleDescription; }
public void setScheduleDescription(String scheduleDescription) { this.scheduleDescription = scheduleDescription; }
public List<String> getScheduleDefinitions() { return scheduleDefinitions; }
public void setScheduleDefinitions(List<String> scheduleDefinitions) { this.scheduleDefinitions = scheduleDefinitions; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
}