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

@@ -5,6 +5,8 @@ import io.github.hectorvent.floci.services.redshift.container.RedshiftContainerM
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.xml.XmlPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import jakarta.inject.Inject;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
@@ -940,4 +942,215 @@ public class RedshiftOperationsTest {
.then()
.statusCode(200);
}
private static Response redshift(String action, String... keyValues) {
RequestSpecification request = given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", AUTH_HEADER)
.formParam("Action", action);
for (int i = 0; i < keyValues.length; i += 2) {
request.formParam(keyValues[i], keyValues[i + 1]);
}
return request.when().post("/");
}
private static void assertFault(Response response, int status, String code) {
assertEquals(status, response.statusCode(), response.asString());
assertEquals(code, XmlPath.from(response.asString()).getString("ErrorResponse.Error.Code"));
}
private static XmlPath describeCluster(String clusterIdentifier) {
Response response = redshift("DescribeClusters", "ClusterIdentifier", clusterIdentifier);
assertEquals(200, response.statusCode(), response.asString());
return XmlPath.from(response.asString())
.setRootPath("DescribeClustersResponse.DescribeClustersResult.Clusters.Cluster");
}
private static XmlPath describeSchedules(String... filters) {
Response response = redshift("DescribeSnapshotSchedules", filters);
assertEquals(200, response.statusCode(), response.asString());
return XmlPath.from(response.asString())
.setRootPath("DescribeSnapshotSchedulesResponse.DescribeSnapshotSchedulesResult.SnapshotSchedules");
}
/**
* aws_redshift_snapshot_copy reads ClusterSnapshotCopyStatus back through DescribeClusters,
* so every assertion here reads the state through that separate call.
*/
@Test
@Order(9)
void snapshotCopyLifecycle() {
when(containerManager.start(any(), eq("cluster-copy"), any(), any()))
.thenReturn(new RedshiftContainerHandle("c9", "cluster-copy", "localhost", 5449));
assertEquals(200, redshift("CreateCluster", "ClusterIdentifier", "cluster-copy", "NodeType", "dc2.large",
"MasterUsername", "admin", "MasterUserPassword", "password123").statusCode());
assertFalse(redshift("DescribeClusters", "ClusterIdentifier", "cluster-copy").asString()
.contains("ClusterSnapshotCopyStatus"));
assertFault(redshift("DisableSnapshotCopy", "ClusterIdentifier", "cluster-copy"),
400, "SnapshotCopyAlreadyDisabledFault");
assertFault(redshift("ModifySnapshotCopyRetentionPeriod", "ClusterIdentifier", "cluster-copy",
"RetentionPeriod", "10"), 400, "SnapshotCopyDisabledFault");
assertFault(redshift("EnableSnapshotCopy", "ClusterIdentifier", "no-such-cluster",
"DestinationRegion", "us-west-2"), 404, "ClusterNotFound");
assertFault(redshift("EnableSnapshotCopy", "ClusterIdentifier", "cluster-copy",
"DestinationRegion", "us-west-2", "RetentionPeriod", "0"), 400, "InvalidRetentionPeriodFault");
assertFault(redshift("EnableSnapshotCopy", "ClusterIdentifier", "cluster-copy",
"DestinationRegion", "us-west-2", "SnapshotCopyGrantName", "no-such-grant"),
400, "SnapshotCopyGrantNotFoundFault");
for (String bad : List.of("us-nowhere-1", "not a region")) {
assertFault(redshift("EnableSnapshotCopy", "ClusterIdentifier", "cluster-copy",
"DestinationRegion", bad), 404, "UnknownSnapshotCopyRegionFault");
}
assertFalse(redshift("DescribeClusters", "ClusterIdentifier", "cluster-copy").asString()
.contains("ClusterSnapshotCopyStatus"));
Response enabled = redshift("EnableSnapshotCopy",
"ClusterIdentifier", "cluster-copy", "DestinationRegion", "us-west-2");
assertEquals(200, enabled.statusCode(), enabled.asString());
assertEquals("us-west-2", XmlPath.from(enabled.asString()).getString(
"EnableSnapshotCopyResponse.EnableSnapshotCopyResult.Cluster.ClusterSnapshotCopyStatus.DestinationRegion"));
XmlPath copy = describeCluster("cluster-copy");
assertEquals("us-west-2", copy.getString("ClusterSnapshotCopyStatus.DestinationRegion"));
assertEquals(7, copy.getInt("ClusterSnapshotCopyStatus.RetentionPeriod"));
assertEquals(-1, copy.getInt("ClusterSnapshotCopyStatus.ManualSnapshotRetentionPeriod"));
assertFault(redshift("EnableSnapshotCopy", "ClusterIdentifier", "cluster-copy",
"DestinationRegion", "eu-west-1"), 400, "SnapshotCopyAlreadyEnabledFault");
assertEquals(200, redshift("ModifySnapshotCopyRetentionPeriod", "ClusterIdentifier", "cluster-copy",
"RetentionPeriod", "14").statusCode());
assertEquals(200, redshift("ModifySnapshotCopyRetentionPeriod", "ClusterIdentifier", "cluster-copy",
"RetentionPeriod", "30", "Manual", "true").statusCode());
copy = describeCluster("cluster-copy");
assertEquals(14, copy.getInt("ClusterSnapshotCopyStatus.RetentionPeriod"));
assertEquals(30, copy.getInt("ClusterSnapshotCopyStatus.ManualSnapshotRetentionPeriod"));
assertEquals("us-west-2", copy.getString("ClusterSnapshotCopyStatus.DestinationRegion"));
assertEquals(200, redshift("DisableSnapshotCopy", "ClusterIdentifier", "cluster-copy").statusCode());
assertFalse(redshift("DescribeClusters", "ClusterIdentifier", "cluster-copy").asString()
.contains("ClusterSnapshotCopyStatus"));
// A grant named on enable is reported back.
assertEquals(200, redshift("CreateSnapshotCopyGrant", "SnapshotCopyGrantName", "copy-lifecycle-grant").statusCode());
assertEquals(200, redshift("EnableSnapshotCopy", "ClusterIdentifier", "cluster-copy",
"DestinationRegion", "us-west-2", "RetentionPeriod", "3",
"ManualSnapshotRetentionPeriod", "5", "SnapshotCopyGrantName", "copy-lifecycle-grant").statusCode());
copy = describeCluster("cluster-copy");
assertEquals("copy-lifecycle-grant", copy.getString("ClusterSnapshotCopyStatus.SnapshotCopyGrantName"));
assertEquals(3, copy.getInt("ClusterSnapshotCopyStatus.RetentionPeriod"));
assertEquals(5, copy.getInt("ClusterSnapshotCopyStatus.ManualSnapshotRetentionPeriod"));
// A grant a cluster's enabled copy still names cannot be deleted (InvalidSnapshotCopyGrantStateFault).
assertFault(redshift("DeleteSnapshotCopyGrant", "SnapshotCopyGrantName", "copy-lifecycle-grant"),
400, "InvalidSnapshotCopyGrantStateFault");
assertEquals(200, redshift("DisableSnapshotCopy", "ClusterIdentifier", "cluster-copy").statusCode());
assertEquals(200, redshift("DeleteCluster", "ClusterIdentifier", "cluster-copy").statusCode());
assertEquals(200, redshift("DeleteSnapshotCopyGrant", "SnapshotCopyGrantName", "copy-lifecycle-grant").statusCode());
}
/**
* aws_redshift_snapshot_schedule_association reads DescribeSnapshotSchedules by ClusterIdentifier
* and ScheduleIdentifier and looks for the cluster in AssociatedClusters with state ACTIVE.
*/
@Test
@Order(10)
void snapshotScheduleLifecycle() {
when(containerManager.start(any(), eq("cluster-sched"), any(), any()))
.thenReturn(new RedshiftContainerHandle("c10", "cluster-sched", "localhost", 5450));
assertEquals(200, redshift("CreateCluster", "ClusterIdentifier", "cluster-sched", "NodeType", "dc2.large",
"MasterUsername", "admin", "MasterUserPassword", "password123").statusCode());
Response created = redshift("CreateSnapshotSchedule",
"ScheduleIdentifier", "sched-1",
"ScheduleDescription", "twice a day",
"ScheduleDefinitions.ScheduleDefinition.1", "rate(12 hours)",
"Tags.Tag.1.Key", "env", "Tags.Tag.1.Value", "test");
assertEquals(200, created.statusCode(), created.asString());
assertEquals("sched-1", XmlPath.from(created.asString())
.getString("CreateSnapshotScheduleResponse.CreateSnapshotScheduleResult.ScheduleIdentifier"));
assertFault(redshift("CreateSnapshotSchedule", "ScheduleIdentifier", "sched-1",
"ScheduleDefinitions.ScheduleDefinition.1", "rate(1 day)"), 400, "SnapshotScheduleAlreadyExists");
assertFault(redshift("CreateSnapshotSchedule", "ScheduleIdentifier", "sched-bad",
"ScheduleDefinitions.ScheduleDefinition.1", "every noon"), 400, "InvalidSchedule");
for (String bad : List.of("rate(foo)", "rate(0 hours)", "rate(25 hours)", "rate(2 days)",
"cron(not-a-cron-expression)", "cron(0 12)")) {
assertFault(redshift("CreateSnapshotSchedule", "ScheduleIdentifier", "sched-bad",
"ScheduleDefinitions.ScheduleDefinition.1", bad), 400, "InvalidSchedule");
}
for (String badId : List.of("sched bad", "sched_bad", "sched!")) {
assertFault(redshift("CreateSnapshotSchedule", "ScheduleIdentifier", badId,
"ScheduleDefinitions.ScheduleDefinition.1", "rate(1 day)"), 400, "InvalidParameterValue");
assertEquals(0, describeSchedules("ScheduleIdentifier", badId).getList("SnapshotSchedule").size());
}
for (String goodId : List.of("tf-redshift-snapshot-schedule", "Sched2")) {
assertEquals(200, redshift("CreateSnapshotSchedule", "ScheduleIdentifier", goodId,
"ScheduleDefinitions.ScheduleDefinition.1", "rate(1 day)").statusCode());
assertEquals(List.of(goodId), describeSchedules("ScheduleIdentifier", goodId)
.getList("SnapshotSchedule.ScheduleIdentifier"));
assertEquals(200, redshift("DeleteSnapshotSchedule", "ScheduleIdentifier", goodId).statusCode());
}
XmlPath schedules = describeSchedules("ScheduleIdentifier", "sched-1");
assertEquals(1, schedules.getList("SnapshotSchedule.ScheduleIdentifier").size());
assertEquals("rate(12 hours)", schedules.getString("SnapshotSchedule.ScheduleDefinitions.ScheduleDefinition"));
assertEquals("twice a day", schedules.getString("SnapshotSchedule.ScheduleDescription"));
assertEquals("env", schedules.getString("SnapshotSchedule.Tags.Tag.Key"));
assertEquals(0, schedules.getInt("SnapshotSchedule.AssociatedClusterCount"));
assertEquals(0, describeSchedules("ScheduleIdentifier", "no-such-schedule")
.getList("SnapshotSchedule").size());
assertFault(redshift("ModifyClusterSnapshotSchedule", "ClusterIdentifier", "cluster-sched",
"ScheduleIdentifier", "no-such-schedule"), 400, "SnapshotScheduleNotFound");
assertFault(redshift("ModifyClusterSnapshotSchedule", "ClusterIdentifier", "no-such-cluster",
"ScheduleIdentifier", "sched-1"), 404, "ClusterNotFound");
assertEquals(200, redshift("ModifyClusterSnapshotSchedule", "ClusterIdentifier", "cluster-sched",
"ScheduleIdentifier", "sched-1").statusCode());
for (XmlPath associated : List.of(describeSchedules("ClusterIdentifier", "cluster-sched"),
describeSchedules("ScheduleIdentifier", "sched-1"))) {
assertEquals("sched-1", associated.getString("SnapshotSchedule.ScheduleIdentifier"));
assertEquals(1, associated.getInt("SnapshotSchedule.AssociatedClusterCount"));
assertEquals("cluster-sched", associated.getString(
"SnapshotSchedule.AssociatedClusters.ClusterAssociatedToSchedule.ClusterIdentifier"));
assertEquals("ACTIVE", associated.getString(
"SnapshotSchedule.AssociatedClusters.ClusterAssociatedToSchedule.ScheduleAssociationState"));
}
assertEquals("sched-1", describeCluster("cluster-sched").getString("SnapshotScheduleIdentifier"));
assertFault(redshift("DeleteSnapshotSchedule", "ScheduleIdentifier", "sched-1"),
400, "InvalidClusterSnapshotScheduleState");
assertFault(redshift("ModifySnapshotSchedule", "ScheduleIdentifier", "sched-1",
"ScheduleDefinitions.ScheduleDefinition.1", "rate(foo)"), 400, "InvalidSchedule");
assertEquals(200, redshift("ModifySnapshotSchedule", "ScheduleIdentifier", "sched-1",
"ScheduleDefinitions.ScheduleDefinition.1", "cron(0 12 * * ? *)").statusCode());
assertEquals("cron(0 12 * * ? *)", describeSchedules("ScheduleIdentifier", "sched-1")
.getString("SnapshotSchedule.ScheduleDefinitions.ScheduleDefinition"));
String scheduleArn = "arn:aws:redshift:us-east-1:000000000000:snapshotschedule:sched-1";
assertEquals(200, redshift("CreateTags", "ResourceName", scheduleArn,
"Tags.Tag.1.Key", "team", "Tags.Tag.1.Value", "data").statusCode());
XmlPath tags = XmlPath.from(redshift("DescribeTags", "ResourceName", scheduleArn).asString())
.setRootPath("DescribeTagsResponse.DescribeTagsResult.TaggedResources");
assertEquals(List.of("env", "team"), tags.getList("TaggedResource.Tag.Key"));
assertEquals("snapshotschedule", tags.getString("TaggedResource[0].ResourceType"));
assertEquals(List.of("sched-1"), describeSchedules("TagValues.TagValue.1", "data")
.getList("SnapshotSchedule.ScheduleIdentifier"));
assertEquals(200, redshift("ModifyClusterSnapshotSchedule", "ClusterIdentifier", "cluster-sched",
"DisassociateSchedule", "true").statusCode());
assertEquals(0, describeSchedules("ClusterIdentifier", "cluster-sched").getList("SnapshotSchedule").size());
assertEquals(0, describeSchedules("ScheduleIdentifier", "sched-1").getInt("SnapshotSchedule.AssociatedClusterCount"));
assertEquals(200, redshift("DeleteSnapshotSchedule", "ScheduleIdentifier", "sched-1").statusCode());
assertEquals(0, describeSchedules("ScheduleIdentifier", "sched-1").getList("SnapshotSchedule").size());
assertFault(redshift("DeleteSnapshotSchedule", "ScheduleIdentifier", "sched-1"), 400, "SnapshotScheduleNotFound");
assertEquals(200, redshift("DeleteCluster", "ClusterIdentifier", "cluster-sched").statusCode());
}
}
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.redshift.model.Endpoint;
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.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
@@ -513,6 +514,26 @@ class RedshiftQueryHandlerTest {
assertEquals("InvalidParameterValue", ex.getErrorCode());
}
@Test
void createSnapshotScheduleReadsScheduleDefinitionLocationNameInNumericOrder() {
// The SDK serialises ScheduleDefinitions as "ScheduleDefinitions.ScheduleDefinition.N".
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
params.putSingle("ScheduleIdentifier", "sched");
params.putSingle("ScheduleDefinitions.ScheduleDefinition.10", "cron(0 1 * * ? *)");
params.putSingle("ScheduleDefinitions.ScheduleDefinition.2", "rate(12 hours)");
when(service.createSnapshotSchedule(any(), any(), any(), any()))
.thenAnswer(inv -> new SnapshotSchedule(inv.getArgument(0), null, inv.getArgument(2)));
Response response = handler.handle("CreateSnapshotSchedule", params);
assertEquals(200, response.getStatus());
verify(service).createSnapshotSchedule(eq("sched"), isNull(),
eq(List.of("rate(12 hours)", "cron(0 1 * * ? *)")), eq(Map.of()));
String xml = (String) response.getEntity();
assertTrue(xml.contains("<CreateSnapshotScheduleResult><ScheduleDefinitions>"
+ "<ScheduleDefinition>rate(12 hours)</ScheduleDefinition>"), xml);
}
@Test
void createsSnapshotCopyGrant() {
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();