diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java index 79c3530822..d4824cf1ff 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java @@ -11,6 +11,7 @@ import software.amazon.awssdk.services.redshiftserverless.RedshiftServerlessClie import software.amazon.awssdk.services.redshiftserverless.model.ConflictException; import software.amazon.awssdk.services.redshiftserverless.model.EndpointAccess; import software.amazon.awssdk.services.redshiftserverless.model.GetCredentialsResponse; +import software.amazon.awssdk.services.redshiftserverless.model.InvalidPaginationException; import software.amazon.awssdk.services.redshiftserverless.model.Namespace; import software.amazon.awssdk.services.redshiftserverless.model.NamespaceStatus; import software.amazon.awssdk.services.redshiftserverless.model.ResourceNotFoundException; @@ -373,6 +374,39 @@ class RedshiftServerlessTest { } } + @Test + void snapshotCopyDestinationCanChangeOnlyAfterDeletion() { + assumeFalse(TestFixtures.isRealAws(), "Asserts metadata-only snapshot copying"); + try (RedshiftServerlessClient client = TestFixtures.redshiftServerlessClient()) { + String namespaceName = "floci-compat-copy-ns"; + try { + client.createNamespace(request -> request.namespaceName(namespaceName).adminUsername("admin")); + String copyId = client.createSnapshotCopyConfiguration(request -> request + .namespaceName(namespaceName).destinationRegion("us-west-2")) + .snapshotCopyConfiguration().snapshotCopyConfigurationId(); + assertThatThrownBy(() -> client.createSnapshotCopyConfiguration(request -> request + .namespaceName(namespaceName).destinationRegion("eu-west-1"))) + .isInstanceOf(ConflictException.class); + client.deleteSnapshotCopyConfiguration(request -> request.snapshotCopyConfigurationId(copyId)); + String replacementId = client.createSnapshotCopyConfiguration(request -> request + .namespaceName(namespaceName).destinationRegion("eu-west-1")) + .snapshotCopyConfiguration().snapshotCopyConfigurationId(); + assertThat(client.listSnapshotCopyConfigurations(request -> request.namespaceName(namespaceName)) + .snapshotCopyConfigurations()).extracting(configuration -> configuration.destinationRegion()) + .containsExactly("eu-west-1"); + assertThatThrownBy(() -> client.listSnapshotCopyConfigurations(request -> request + .namespaceName(namespaceName).maxResults(0))) + .isInstanceOf(ValidationException.class); + assertThatThrownBy(() -> client.listSnapshotCopyConfigurations(request -> request + .namespaceName(namespaceName).nextToken("bad-token"))) + .isInstanceOf(InvalidPaginationException.class); + client.deleteSnapshotCopyConfiguration(request -> request.snapshotCopyConfigurationId(replacementId)); + } finally { + deleteBestEffort(client, namespaceName); + } + } + } + private static void deleteSnapshotBestEffort(RedshiftServerlessClient client, String snapshotName) { try { client.deleteSnapshot(request -> request.snapshotName(snapshotName)); diff --git a/docs/services/index.md b/docs/services/index.md index a415832786..fcfd62ebd6 100644 --- a/docs/services/index.md +++ b/docs/services/index.md @@ -77,7 +77,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac | [DMS](dms.md) | `POST /` + `X-Amz-Target: AmazonDMSv20160101.*` | JSON 1.1 | 20 | | [Redshift](redshift.md) | `POST /` with `Action=` param + PostgreSQL container | Query + PostgreSQL wire (+ CFN) | 43 | | [Redshift Data API](redshift-data.md) | `POST /` + `X-Amz-Target: RedshiftData.*` | JSON 1.1 | 11 | -| [Redshift Serverless](redshift-serverless.md) | `POST /` + `X-Amz-Target: RedshiftServerless.*` | JSON 1.1 | 46 | +| [Redshift Serverless](redshift-serverless.md) | `POST /` + `X-Amz-Target: RedshiftServerless.*` | JSON 1.1 | 50 | | [EMR](emr.md) | `POST /` + `X-Amz-Target: ElasticMapReduce.*` | JSON 1.1 | 37 | | [EMR Serverless](emr-serverless.md) | `/applications/*`, `/tags/*` | REST JSON | 10 | | [Data Firehose](firehose.md) | `POST /` + `X-Amz-Target: Firehose_20150804.*` | JSON 1.1 | 12 | diff --git a/docs/services/redshift-serverless.md b/docs/services/redshift-serverless.md index 41b6ef771e..92fe42f315 100644 --- a/docs/services/redshift-serverless.md +++ b/docs/services/redshift-serverless.md @@ -58,6 +58,10 @@ For the upstream API shape, see the [Amazon Redshift Serverless API Reference](h | `ListCustomDomainAssociations` | Page through the associations, filtered by `customDomainName` and `customDomainCertificateArn` | | `UpdateCustomDomainAssociation` | Point an association at another certificate ARN | | `DeleteCustomDomainAssociation` | Remove a custom domain association | +| `CreateSnapshotCopyConfiguration` | Record a snapshot copy configuration to another Region; nothing is copied | +| `ListSnapshotCopyConfigurations` | Page through the snapshot copy configurations, filtered by `namespaceName` | +| `UpdateSnapshotCopyConfiguration` | Change the `snapshotRetentionPeriod` of a snapshot copy configuration | +| `DeleteSnapshotCopyConfiguration` | Remove a snapshot copy configuration and return it | Namespace and workgroup state is account and Region scoped and persisted through `StorageFactory`. @@ -135,3 +139,4 @@ aws redshift-serverless delete-namespace --namespace-name analytics - **Resource policies are recorded, not acted on.** A policy is stored per account and Region, echoed in the shape of the API model, and dropped on read once the snapshot it belongs to is gone. A snapshot recreated under the same name is a different snapshot: the record keeps the snapshot's creation time, so the policy is not inherited. `PutResourcePolicy` accepts a JSON object for a snapshot ARN of this account and Region; no principal in it gains access. A foreign or non-snapshot ARN is a `ValidationException`. - **Endpoint access is recorded, not acted on.** An endpoint is stored per account and Region, echoed in the shape of the API model, and dropped on read once its workgroup is gone; a workgroup recreated under the same name is a different workgroup and does not inherit it. `CreateEndpointAccess` creates no VPC endpoint. `address` and `port` are the workgroup's own, so a client can connect; `vpcEndpoint` carries a generated id and one network interface per subnet, without a `vpcId`, and `ListEndpointAccess` with a `vpcId` returns nothing. `vpcSecurityGroupIds` is optional. Endpoint names are 1 to 30 letters, digits or hyphens, start with a letter, and contain no consecutive or trailing hyphens. Security groups are the only member `UpdateEndpointAccess` changes. - **Custom domain associations are recorded, not acted on.** An association is stored per account and Region and dropped on read once its workgroup is gone; a workgroup recreated under the same name does not inherit it. A workgroup has at most one custom domain association. The certificate expiry time comes from ACM when the certificate exists in the emulator. +- **Snapshot copy configurations are recorded, not acted on.** `CreateSnapshotCopyConfiguration` copies no snapshot. A configuration is stored per account and Region and dropped on read once its namespace is gone; a namespace recreated under the same name does not inherit it. The destination Region must differ from the namespace's, and a namespace has only one configuration at a time (`ConflictException`). Delete the existing configuration before choosing another destination Region. diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminJsonHandler.java index e8428f91c7..cbb02542d4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminJsonHandler.java @@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.services.redshiftserverless.model.CustomDomainAssociation; import io.github.hectorvent.floci.services.redshiftserverless.model.EndpointAccess; import io.github.hectorvent.floci.services.redshiftserverless.model.ResourcePolicy; +import io.github.hectorvent.floci.services.redshiftserverless.model.SnapshotCopyConfiguration; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.ws.rs.core.Response; @@ -31,16 +32,19 @@ public class RedshiftServerlessAdminJsonHandler { private final RedshiftServerlessCustomDomainService customDomains; private final RedshiftServerlessEndpointAccessService endpointAccess; private final RedshiftServerlessResourcePolicyService resourcePolicies; + private final RedshiftServerlessSnapshotCopyService snapshotCopies; @Inject public RedshiftServerlessAdminJsonHandler(ObjectMapper objectMapper, RedshiftServerlessResourcePolicyService resourcePolicies, RedshiftServerlessEndpointAccessService endpointAccess, - RedshiftServerlessCustomDomainService customDomains) { + RedshiftServerlessCustomDomainService customDomains, + RedshiftServerlessSnapshotCopyService snapshotCopies) { this.objectMapper = objectMapper; this.resourcePolicies = resourcePolicies; this.endpointAccess = endpointAccess; this.customDomains = customDomains; + this.snapshotCopies = snapshotCopies; } Supplier operation(String action, JsonNode request, String region) { @@ -94,6 +98,20 @@ public class RedshiftServerlessAdminJsonHandler { ServerlessSupport.text(request, "customDomainName"), region); return emptyResponse(); }; + case "CreateSnapshotCopyConfiguration" -> () -> snapshotCopyResponse( + snapshotCopies.createSnapshotCopyConfiguration( + ServerlessSupport.text(request, "namespaceName"), + ServerlessSupport.text(request, "destinationRegion"), + ServerlessSupport.text(request, "destinationKmsKeyId"), + ServerlessSupport.integer(request, "snapshotRetentionPeriod"), region)); + case "ListSnapshotCopyConfigurations" -> () -> listSnapshotCopies(request, region); + case "UpdateSnapshotCopyConfiguration" -> () -> snapshotCopyResponse( + snapshotCopies.updateSnapshotCopyConfiguration( + ServerlessSupport.text(request, "snapshotCopyConfigurationId"), + ServerlessSupport.integer(request, "snapshotRetentionPeriod"), region)); + case "DeleteSnapshotCopyConfiguration" -> () -> snapshotCopyResponse( + snapshotCopies.deleteSnapshotCopyConfiguration( + ServerlessSupport.text(request, "snapshotCopyConfigurationId"), region)); default -> null; }; } @@ -202,6 +220,39 @@ public class RedshiftServerlessAdminJsonHandler { return node; } + // ── Snapshot copy configuration ──────────────────────────────────────── + + private Response listSnapshotCopies(JsonNode request, String region) { + PaginatedResult page = snapshotCopies.listSnapshotCopyConfigurations( + ServerlessSupport.text(request, "namespaceName"), region, ServerlessSupport.maxResults(request), + ServerlessSupport.text(request, "nextToken")); + ObjectNode response = objectMapper.createObjectNode(); + ArrayNode items = response.putArray("snapshotCopyConfigurations"); + page.items().forEach(configuration -> items.add(snapshotCopyNode(configuration))); + putNextToken(response, page); + return Response.ok(response).build(); + } + + private Response snapshotCopyResponse(SnapshotCopyConfiguration configuration) { + ObjectNode response = objectMapper.createObjectNode(); + response.set("snapshotCopyConfiguration", snapshotCopyNode(configuration)); + return Response.ok(response).build(); + } + + private ObjectNode snapshotCopyNode(SnapshotCopyConfiguration configuration) { + ObjectNode node = objectMapper.createObjectNode(); + if (configuration.getDestinationKmsKeyId() != null) { + node.put("destinationKmsKeyId", configuration.getDestinationKmsKeyId()); + } + node.put("destinationRegion", configuration.getDestinationRegion()); + node.put("namespaceName", configuration.getNamespaceName()); + node.put("snapshotCopyConfigurationArn", configuration.getSnapshotCopyConfigurationArn()); + node.put("snapshotCopyConfigurationId", configuration.getSnapshotCopyConfigurationId()); + if (configuration.getSnapshotRetentionPeriod() != null) { + node.put("snapshotRetentionPeriod", configuration.getSnapshotRetentionPeriod()); + } + return node; + } // ── Tracks, managed workgroups and reservations ──────────────────────── /** diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessCustomDomainService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessCustomDomainService.java index ae176d56c5..54c5443b47 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessCustomDomainService.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessCustomDomainService.java @@ -33,8 +33,6 @@ import java.util.Optional; @ApplicationScoped public class RedshiftServerlessCustomDomainService implements Resettable { - private static final int PAGE_SIZE = 100; - private final AccountAwareStorageBackend associations; private final RedshiftServerlessService serverless; private final AcmService acmService; @@ -91,10 +89,7 @@ public class RedshiftServerlessCustomDomainService implements Resettable { public synchronized PaginatedResult listCustomDomainAssociations( String customDomainName, String certificateArn, String region, Integer maxResults, String nextToken) { - if (maxResults != null && (maxResults < 1 || maxResults > PAGE_SIZE)) { - throw ServerlessSupport.validation("maxResults must be between 1 and " + PAGE_SIZE); - } - int limit = maxResults != null ? maxResults : PAGE_SIZE; + int limit = ServerlessSupport.pageSize(maxResults); List matching = live(region).stream() .filter(association -> customDomainName == null || customDomainName.equals(association.getCustomDomainName())) @@ -107,7 +102,7 @@ public class RedshiftServerlessCustomDomainService implements Resettable { return Pagination.paginate(matching, association -> association.getWorkgroupName() + "|" + association.getCustomDomainName(), limit, nextToken, "custom-domain", - token -> new AwsException("InvalidPaginationException", "Invalid nextToken.", 400)); + token -> ServerlessSupport.invalidPagination("Invalid nextToken.")); } /** Points the association at another certificate, the only member AWS lets an update change. */ diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessEndpointAccessService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessEndpointAccessService.java index f70e3ff9f1..5d46c58ece 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessEndpointAccessService.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessEndpointAccessService.java @@ -33,7 +33,6 @@ import java.util.regex.Pattern; public class RedshiftServerlessEndpointAccessService implements Resettable { private static final Pattern ENDPOINT_NAME = Pattern.compile("^[A-Za-z][A-Za-z0-9-]{0,29}$"); - private static final int PAGE_SIZE = 100; private final AccountAwareStorageBackend endpoints; private final RegionResolver regionResolver; @@ -103,10 +102,7 @@ public class RedshiftServerlessEndpointAccessService implements Resettable { .sorted(Comparator.comparing(EndpointAccess::getEndpointName)) .map(endpoint -> withCurrentConnection(endpoint, region)) .toList(); - if (maxResults != null && (maxResults < 1 || maxResults > PAGE_SIZE)) { - throw ServerlessSupport.validation("maxResults must be between 1 and " + PAGE_SIZE); - } - int limit = maxResults != null ? maxResults : PAGE_SIZE; + int limit = ServerlessSupport.pageSize(maxResults); return Pagination.paginate(matching, EndpointAccess::getEndpointName, limit, nextToken, "endpoint-access", token -> ServerlessSupport.validation("Invalid nextToken.")); } diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java index 4384d4f1d1..4103907b9c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java @@ -480,6 +480,11 @@ public class RedshiftServerlessService implements Resettable { return workgroupName == null ? Optional.empty() : workgroups.get(storageKey(region, workgroupName)); } + /** The namespace called {@code namespaceName} in {@code region}, empty when there is none. */ + public Optional findNamespace(String namespaceName, String region) { + return namespaceName == null ? Optional.empty() : namespaces.get(storageKey(region, namespaceName)); + } + /** The snapshot called {@code snapshotName} in {@code region}, empty when there is none. */ public Optional findSnapshot(String snapshotName, String region) { return snapshotName == null ? Optional.empty() : snapshots.get(storageKey(region, snapshotName)); diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessSnapshotCopyService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessSnapshotCopyService.java new file mode 100644 index 0000000000..7925e9d096 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessSnapshotCopyService.java @@ -0,0 +1,159 @@ +package io.github.hectorvent.floci.services.redshiftserverless; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.core.common.PaginatedResult; +import io.github.hectorvent.floci.core.common.Pagination; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.Resettable; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace; +import io.github.hectorvent.floci.services.redshiftserverless.model.SnapshotCopyConfiguration; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; + +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; +import java.util.regex.Pattern; + +/** + * Redshift Serverless snapshot copy configurations. They are stored and echoed, not run: Floci + * copies no snapshot to the destination Region. + * + *

A configuration belongs to a namespace but lives in its own store, so deleting the namespace + * needs no hook here. Each read checks that the namespace is still there and drops the + * configuration otherwise. + */ +@ApplicationScoped +public class RedshiftServerlessSnapshotCopyService implements Resettable { + + /** The model gives {@code destinationRegion} no pattern, so the shape of a Region name is checked here. */ + private static final Pattern REGION_NAME = Pattern.compile("^[a-z0-9-]+$"); + private static final int MAX_RETENTION_DAYS = 3653; + + private final AccountAwareStorageBackend configurations; + private final RegionResolver regionResolver; + private final RedshiftServerlessService serverless; + + @Inject + public RedshiftServerlessSnapshotCopyService(StorageFactory storageFactory, RegionResolver regionResolver, + RedshiftServerlessService serverless) { + this.configurations = storageFactory.create("redshiftserverless", + "redshiftserverless-snapshotcopyconfigurations.json", + new TypeReference>() {}); + this.regionResolver = regionResolver; + this.serverless = serverless; + } + + public synchronized SnapshotCopyConfiguration createSnapshotCopyConfiguration( + String namespaceName, String destinationRegion, String destinationKmsKeyId, + Integer snapshotRetentionPeriod, String region) { + if (!REGION_NAME.matcher(destinationRegion).matches()) { + throw ServerlessSupport.validation("destinationRegion must be lower-case letters, digits and hyphens."); + } + if (destinationRegion.equals(region)) { + throw ServerlessSupport.validation("destinationRegion must differ from the namespace's Region."); + } + validateRetention(snapshotRetentionPeriod); + Namespace namespace = serverless.findNamespace(namespaceName, region) + .orElseThrow(() -> ServerlessSupport.notFound("The namespace " + namespaceName + " was not found.")); + boolean duplicate = live(region).stream().anyMatch(existing -> + existing.getNamespaceName().equals(namespaceName)); + if (duplicate) { + throw ServerlessSupport.conflict("The namespace " + namespaceName + + " already has a snapshot copy configuration."); + } + + SnapshotCopyConfiguration configuration = new SnapshotCopyConfiguration(); + configuration.setSnapshotCopyConfigurationId(UUID.randomUUID().toString()); + configuration.setSnapshotCopyConfigurationArn(regionResolver.buildArn("redshift-serverless", region, + "snapshotcopyconfiguration/" + configuration.getSnapshotCopyConfigurationId())); + configuration.setNamespaceName(namespaceName); + configuration.setOwnerId(namespace.getNamespaceId()); + configuration.setDestinationRegion(destinationRegion); + configuration.setDestinationKmsKeyId(destinationKmsKeyId); + configuration.setSnapshotRetentionPeriod(snapshotRetentionPeriod); + configurations.put(storageKey(region, configuration.getSnapshotCopyConfigurationId()), configuration); + return new SnapshotCopyConfiguration(configuration); + } + + public synchronized PaginatedResult listSnapshotCopyConfigurations( + String namespaceName, String region, Integer maxResults, String nextToken) { + int limit = ServerlessSupport.pageSize(maxResults); + List matching = live(region).stream() + .filter(configuration -> namespaceName == null + || namespaceName.equals(configuration.getNamespaceName())) + .sorted(Comparator.comparing(SnapshotCopyConfiguration::getSnapshotCopyConfigurationId)) + .map(SnapshotCopyConfiguration::new) + .toList(); + return Pagination.paginate(matching, SnapshotCopyConfiguration::getSnapshotCopyConfigurationId, + limit, nextToken, "snapshot-copy", + token -> ServerlessSupport.invalidPagination("Invalid nextToken.")); + } + + /** Changes the retention of copied snapshots, the only member AWS lets an update change. */ + public synchronized SnapshotCopyConfiguration updateSnapshotCopyConfiguration( + String snapshotCopyConfigurationId, Integer snapshotRetentionPeriod, String region) { + SnapshotCopyConfiguration updated = new SnapshotCopyConfiguration(require(snapshotCopyConfigurationId, region)); + if (snapshotRetentionPeriod != null) { + validateRetention(snapshotRetentionPeriod); + updated.setSnapshotRetentionPeriod(snapshotRetentionPeriod); + configurations.put(storageKey(region, snapshotCopyConfigurationId), updated); + } + return new SnapshotCopyConfiguration(updated); + } + + public synchronized SnapshotCopyConfiguration deleteSnapshotCopyConfiguration( + String snapshotCopyConfigurationId, String region) { + SnapshotCopyConfiguration existing = new SnapshotCopyConfiguration( + require(snapshotCopyConfigurationId, region)); + configurations.delete(storageKey(region, snapshotCopyConfigurationId)); + return existing; + } + + @Override + public void clear() { + configurations.clear(); + } + + private SnapshotCopyConfiguration require(String id, String region) { + SnapshotCopyConfiguration configuration = configurations.get(storageKey(region, id)).orElse(null); + if (configuration != null && !isOnSameNamespace(configuration, region)) { + configurations.delete(storageKey(region, id)); + configuration = null; + } + if (configuration == null) { + throw ServerlessSupport.notFound("The snapshot copy configuration " + id + " was not found."); + } + return configuration; + } + + /** The region's configurations whose namespace is still there; any other is deleted as it is found. */ + private List live(String region) { + return ServerlessSupport.live(configurations, region, + configuration -> isOnSameNamespace(configuration, region), + configuration -> storageKey(region, configuration.getSnapshotCopyConfigurationId())); + } + + /** A namespace recreated under the same name has a new id, so it never inherits its predecessor's configurations. */ + private boolean isOnSameNamespace(SnapshotCopyConfiguration configuration, String region) { + return serverless.findNamespace(configuration.getNamespaceName(), region) + .map(namespace -> Objects.equals(namespace.getNamespaceId(), configuration.getOwnerId())) + .orElse(false); + } + + private static void validateRetention(Integer retentionPeriod) { + if (retentionPeriod != null && retentionPeriod != -1 + && (retentionPeriod < 1 || retentionPeriod > MAX_RETENTION_DAYS)) { + throw ServerlessSupport.validation( + "snapshotRetentionPeriod must be -1 (indefinite) or between 1 and " + MAX_RETENTION_DAYS + "."); + } + } + + private static String storageKey(String region, String id) { + return region + "::" + id; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/ServerlessSupport.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/ServerlessSupport.java index 9169875078..867e194785 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/ServerlessSupport.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/ServerlessSupport.java @@ -21,6 +21,8 @@ import java.util.function.Predicate; */ final class ServerlessSupport { + static final int PAGE_SIZE = 100; + /** Use only for members whose model overrides the epoch-seconds default with ISO 8601. */ static final DateTimeFormatter ISO_8601 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneOffset.UTC); @@ -49,6 +51,10 @@ final class ServerlessSupport { return new AwsException("ConflictException", message, 400); } + static AwsException invalidPagination(String message) { + return new AwsException("InvalidPaginationException", message, 400); + } + static void requireText(String value, String field) { if (value == null || value.isBlank()) { throw validation(field + " is required."); @@ -133,6 +139,13 @@ final class ServerlessSupport { return integer(request, "maxResults"); } + static int pageSize(Integer maxResults) { + if (maxResults != null && (maxResults < 1 || maxResults > PAGE_SIZE)) { + throw validation("maxResults must be between 1 and " + PAGE_SIZE); + } + return maxResults != null ? maxResults : PAGE_SIZE; + } + /** Return the Region's surviving records and remove records whose original parent is gone. */ static List live(AccountAwareStorageBackend storage, String region, Predicate hasParent, Function storageKey) { diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/SnapshotCopyConfiguration.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/SnapshotCopyConfiguration.java new file mode 100644 index 0000000000..d4f507894f --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/SnapshotCopyConfiguration.java @@ -0,0 +1,90 @@ +package io.github.hectorvent.floci.services.redshiftserverless.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import io.quarkus.runtime.annotations.RegisterForReflection; + +/** + * A Redshift Serverless snapshot copy configuration. Floci records it but copies nothing: a snapshot + * that should appear in another Region is created there directly. + */ +@RegisterForReflection +@JsonIgnoreProperties(ignoreUnknown = true) +public class SnapshotCopyConfiguration { + + private String snapshotCopyConfigurationId; + private String snapshotCopyConfigurationArn; + private String namespaceName; + private String destinationRegion; + private String destinationKmsKeyId; + private Integer snapshotRetentionPeriod; + private String ownerId; + + public SnapshotCopyConfiguration() { + } + + public SnapshotCopyConfiguration(SnapshotCopyConfiguration other) { + this.ownerId = other.ownerId; + this.snapshotCopyConfigurationId = other.snapshotCopyConfigurationId; + this.snapshotCopyConfigurationArn = other.snapshotCopyConfigurationArn; + this.namespaceName = other.namespaceName; + this.destinationRegion = other.destinationRegion; + this.destinationKmsKeyId = other.destinationKmsKeyId; + this.snapshotRetentionPeriod = other.snapshotRetentionPeriod; + } + + public String getSnapshotCopyConfigurationId() { + return snapshotCopyConfigurationId; + } + + public void setSnapshotCopyConfigurationId(String snapshotCopyConfigurationId) { + this.snapshotCopyConfigurationId = snapshotCopyConfigurationId; + } + + public String getSnapshotCopyConfigurationArn() { + return snapshotCopyConfigurationArn; + } + + public void setSnapshotCopyConfigurationArn(String snapshotCopyConfigurationArn) { + this.snapshotCopyConfigurationArn = snapshotCopyConfigurationArn; + } + + public String getNamespaceName() { + return namespaceName; + } + + public void setNamespaceName(String namespaceName) { + this.namespaceName = namespaceName; + } + + public String getDestinationRegion() { + return destinationRegion; + } + + public void setDestinationRegion(String destinationRegion) { + this.destinationRegion = destinationRegion; + } + + public String getDestinationKmsKeyId() { + return destinationKmsKeyId; + } + + public void setDestinationKmsKeyId(String destinationKmsKeyId) { + this.destinationKmsKeyId = destinationKmsKeyId; + } + + public Integer getSnapshotRetentionPeriod() { + return snapshotRetentionPeriod; + } + + public void setSnapshotRetentionPeriod(Integer snapshotRetentionPeriod) { + this.snapshotRetentionPeriod = snapshotRetentionPeriod; + } + + public String getOwnerId() { + return ownerId; + } + + public void setOwnerId(String ownerId) { + this.ownerId = ownerId; + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdministrationHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdministrationHandlerTest.java index 05af8416e2..e5b4061fda 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdministrationHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdministrationHandlerTest.java @@ -51,7 +51,7 @@ class RedshiftServerlessAdministrationHandlerTest { void setUp() { StorageFactory storageFactory = mock(StorageFactory.class); for (String file : List.of("namespaces", "workgroups", "snapshots", "usagelimits", "resourcepolicies", - "endpointaccess", "customdomains")) { + "endpointaccess", "customdomains", "snapshotcopyconfigurations")) { when(storageFactory.create(eq("redshiftserverless"), eq("redshiftserverless-" + file + ".json"), any(TypeReference.class))).thenReturn((AccountAwareStorageBackend) AccountAwareStorageBackend.inMemory(ACCOUNT_ID)); @@ -91,7 +91,8 @@ class RedshiftServerlessAdministrationHandlerTest { new RedshiftServerlessAdminJsonHandler(mapper, new RedshiftServerlessResourcePolicyService(storageFactory, serverlessService, mapper), new RedshiftServerlessEndpointAccessService(storageFactory, regionResolver, serverlessService), - customDomains), + customDomains, + new RedshiftServerlessSnapshotCopyService(storageFactory, regionResolver, serverlessService)), customDomains); } @@ -455,6 +456,72 @@ class RedshiftServerlessAdministrationHandlerTest { } } + @Test + void snapshotCopyConfigurationHasOneDestinationPerNamespace() { + namespace("copy-ns"); + + JsonNode created = ok("CreateSnapshotCopyConfiguration", "{\"namespaceName\":\"copy-ns\"," + + "\"destinationRegion\":\"us-west-2\",\"snapshotRetentionPeriod\":14," + + "\"destinationKmsKeyId\":\"key-1\"}").get("snapshotCopyConfiguration"); + String id = created.get("snapshotCopyConfigurationId").textValue(); + assertEquals("copy-ns", created.get("namespaceName").textValue()); + assertEquals("us-west-2", created.get("destinationRegion").textValue()); + assertEquals("key-1", created.get("destinationKmsKeyId").textValue()); + assertEquals(14, created.get("snapshotRetentionPeriod").intValue()); + assertTrue(created.get("snapshotCopyConfigurationArn").textValue().contains(":snapshotcopyconfiguration/")); + + assertEquals("ConflictException", errorType("CreateSnapshotCopyConfiguration", + "{\"namespaceName\":\"copy-ns\",\"destinationRegion\":\"us-west-2\"}")); + assertEquals("ConflictException", errorType("CreateSnapshotCopyConfiguration", + "{\"namespaceName\":\"copy-ns\",\"destinationRegion\":\"eu-west-1\"}")); + assertEquals("ValidationException", errorType("CreateSnapshotCopyConfiguration", + "{\"namespaceName\":\"copy-ns\",\"destinationRegion\":\"us-east-1\"}")); + assertEquals("ValidationException", errorType("CreateSnapshotCopyConfiguration", + "{\"namespaceName\":\"copy-ns\",\"destinationRegion\":\"US-WEST-2\"}")); + assertEquals("ValidationException", errorType("CreateSnapshotCopyConfiguration", + "{\"namespaceName\":\"copy-ns\",\"destinationRegion\":\"invalid_region!\"}")); + assertEquals("ResourceNotFoundException", errorType("CreateSnapshotCopyConfiguration", + "{\"namespaceName\":\"nope-ns\",\"destinationRegion\":\"us-west-2\"}")); + + assertEquals(30, ok("UpdateSnapshotCopyConfiguration", "{\"snapshotCopyConfigurationId\":\"" + id + + "\",\"snapshotRetentionPeriod\":30}").get("snapshotCopyConfiguration") + .get("snapshotRetentionPeriod").intValue()); + assertEquals("ValidationException", errorType("UpdateSnapshotCopyConfiguration", + "{\"snapshotCopyConfigurationId\":\"" + id + "\",\"snapshotRetentionPeriod\":0}")); + assertEquals(1, ok("ListSnapshotCopyConfigurations", "{\"namespaceName\":\"copy-ns\"}") + .get("snapshotCopyConfigurations").size()); + + assertEquals(id, ok("DeleteSnapshotCopyConfiguration", "{\"snapshotCopyConfigurationId\":\"" + id + "\"}") + .get("snapshotCopyConfiguration").get("snapshotCopyConfigurationId").textValue()); + assertEquals(0, ok("ListSnapshotCopyConfigurations", "{}").get("snapshotCopyConfigurations").size()); + assertEquals("eu-west-1", ok("CreateSnapshotCopyConfiguration", + "{\"namespaceName\":\"copy-ns\",\"destinationRegion\":\"eu-west-1\"}") + .get("snapshotCopyConfiguration").get("destinationRegion").textValue()); + } + + @Test + void snapshotCopyPagingWalksPagesAndValidatesInputs() { + for (String name : List.of("page-copy-a", "page-copy-b", "page-copy-c")) { + namespace(name); + ok("CreateSnapshotCopyConfiguration", "{\"namespaceName\":\"" + name + + "\",\"destinationRegion\":\"us-west-2\"}"); + } + + JsonNode firstPage = ok("ListSnapshotCopyConfigurations", "{\"maxResults\":2}"); + assertEquals(2, firstPage.get("snapshotCopyConfigurations").size()); + assertTrue(firstPage.has("nextToken")); + + String token = firstPage.get("nextToken").textValue(); + JsonNode secondPage = ok("ListSnapshotCopyConfigurations", "{\"nextToken\":\"" + token + "\"}"); + assertEquals(1, secondPage.get("snapshotCopyConfigurations").size()); + assertFalse(secondPage.has("nextToken")); + + assertEquals("ValidationException", errorType("ListSnapshotCopyConfigurations", "{\"maxResults\":0}")); + assertEquals("ValidationException", errorType("ListSnapshotCopyConfigurations", "{\"maxResults\":101}")); + assertEquals("InvalidPaginationException", + errorType("ListSnapshotCopyConfigurations", "{\"nextToken\":\"bad-token\"}")); + } + @Test void tracksAreTheTwoAwsReleaseTracks() { JsonNode track = ok("GetTrack", "{\"trackName\":\"trailing\"}").get("track"); @@ -538,6 +605,19 @@ class RedshiftServerlessAdministrationHandlerTest { + "\"customDomainCertificateArn\":\"" + CERTIFICATE_ARN + "\"}"); } + @Test + void aNamespaceRecreatedUnderTheSameNameDoesNotInheritCopyConfigurations() { + namespace("recreate-ns"); + ok("CreateSnapshotCopyConfiguration", "{\"namespaceName\":\"recreate-ns\",\"destinationRegion\":\"us-west-2\"}"); + + ok("DeleteNamespace", "{\"namespaceName\":\"recreate-ns\"}"); + namespace("recreate-ns"); + + assertEquals(0, ok("ListSnapshotCopyConfigurations", "{\"namespaceName\":\"recreate-ns\"}") + .get("snapshotCopyConfigurations").size()); + ok("CreateSnapshotCopyConfiguration", "{\"namespaceName\":\"recreate-ns\",\"destinationRegion\":\"us-west-2\"}"); + } + @Test void aSnapshotRecreatedUnderTheSameNameDoesNotInheritItsPolicy() { String arn = snapshot("recreate-snap-ns", "recreate-snap"); diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java index 475af7cd61..184b1e0e95 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java @@ -707,6 +707,35 @@ class RedshiftServerlessIntegrationTest { .body("workgroup.customDomainName", nullValue()); } + @Test + void snapshotCopyConfigurationRoundTripsOverTheWire() { + call("CreateNamespace", "{\"namespaceName\":\"copy-ns\",\"adminUsername\":\"admin\"}").statusCode(200); + + String copyId = call("CreateSnapshotCopyConfiguration", + "{\"namespaceName\":\"copy-ns\",\"destinationRegion\":\"us-west-2\",\"snapshotRetentionPeriod\":10}") + .statusCode(200) + .body("snapshotCopyConfiguration.destinationRegion", equalTo("us-west-2")) + .extract().path("snapshotCopyConfiguration.snapshotCopyConfigurationId"); + call("CreateSnapshotCopyConfiguration", + "{\"namespaceName\":\"copy-ns\",\"destinationRegion\":\"eu-west-1\"}") + .statusCode(400) + .body("__type", equalTo("ConflictException")); + call("DeleteSnapshotCopyConfiguration", "{\"snapshotCopyConfigurationId\":\"" + copyId + "\"}").statusCode(200); + String replacementCopyId = call("CreateSnapshotCopyConfiguration", + "{\"namespaceName\":\"copy-ns\",\"destinationRegion\":\"eu-west-1\"}") + .statusCode(200) + .body("snapshotCopyConfiguration.destinationRegion", equalTo("eu-west-1")) + .extract().path("snapshotCopyConfiguration.snapshotCopyConfigurationId"); + call("ListSnapshotCopyConfigurations", "{\"maxResults\":0}") + .statusCode(400) + .body("__type", equalTo("ValidationException")); + call("ListSnapshotCopyConfigurations", "{\"nextToken\":\"bad-token\"}") + .statusCode(400) + .body("__type", equalTo("InvalidPaginationException")); + call("DeleteSnapshotCopyConfiguration", "{\"snapshotCopyConfigurationId\":\"" + replacementCopyId + "\"}") + .statusCode(200); + } + private static io.restassured.response.ValidatableResponse call(String action, String body) { return given() .header("X-Amz-Target", TARGET_PREFIX + action) diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java index e54a75677e..456f22b45f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java @@ -95,13 +95,14 @@ class RedshiftServerlessJsonHandlerTest { new RedshiftServerlessAdminJsonHandler(mapper, new RedshiftServerlessResourcePolicyService(storageFactory, service, mapper), new RedshiftServerlessEndpointAccessService(storageFactory, regionResolver, service), - customDomains), + customDomains, + new RedshiftServerlessSnapshotCopyService(storageFactory, regionResolver, service)), customDomains); } @SuppressWarnings({"unchecked", "rawtypes"}) private static void stubAdministrationStores(StorageFactory storageFactory) { - for (String file : List.of("resourcepolicies", "endpointaccess", "customdomains")) { + for (String file : List.of("resourcepolicies", "endpointaccess", "customdomains", "snapshotcopyconfigurations")) { when(storageFactory.create(eq("redshiftserverless"), eq("redshiftserverless-" + file + ".json"), any(TypeReference.class))).thenReturn((AccountAwareStorageBackend) AccountAwareStorageBackend.inMemory(ACCOUNT_ID));