feat(dms): add replication subnet group lifecycle and tagging (#3630)

* feat(dms): add replication subnet group lifecycle

AWS DMS was entirely absent, so every terraform-aws-data-storage example
using aws_dms_replication_subnet_group failed at plan with
UnknownOperationException.

Adds a minimal dms service on AWS JSON 1.1 (X-Amz-Target prefix
AmazonDMSv20160101.) implementing CreateReplicationSubnetGroup,
DescribeReplicationSubnetGroups and DeleteReplicationSubnetGroup.
Subnets are resolved against the emulated EC2 service, so the VpcId and
Availability Zones a group reports are the ones its subnets actually
have, and AWS's own rules are enforced: the identifier is stored
lowercase, "default" is reserved, subnets must share a VPC, and a group
must cover at least two Availability Zones.

Tagging actions and replication instances, endpoints and tasks are out
of scope and still report UnknownOperationException.

* feat(dms): add resource tagging for replication subnet groups

Terraform's aws_dms_replication_subnet_group reads tags back through
ListTagsForResource, so without the tagging trio the resource could be
created but never read cleanly.

Adds ListTagsForResource, AddTagsToResource and RemoveTagsFromResource,
and stores the Tags member of CreateReplicationSubnetGroup with the
group. DMS does not return a subnet group ARN from describe, so the
provider builds arn:aws:dms:<region>:<account>:subgrp:<id> client side;
Floci parses that form back to the stored group and rejects anything
else, including another account's ARN, with ResourceNotFoundFault.

* fix(dms): address review findings

Emit IsReadOnly on ReplicationSubnetGroup, the one member of the model's
seven the response was missing. It is always false: a read-only group is
one DMS manages for a zero-ETL integration, which Floci does not emulate.

Fault a cross-Region ARN the way a cross-account one already faulted.
Storage is keyed by the caller's Region, so honouring an eu-west-1 ARN
from a us-east-1 caller silently reached the caller's own group of that
name rather than the one named.

Reject a subnet with no Availability Zone as InvalidSubnet. Set.copyOf
rejects nulls, so such a subnet raised a NullPointerException and
surfaced as a 500 instead of a client error.

Adds the user-realistic minimum-AZ case: two subnets sharing one AZ,
which the previous tests did not cover because they passed one subnet.

* fix(dms): reject wrongly-typed tag, filter and arn-list members

A present member of the wrong JSON type was coerced rather than
rejected. asText() on an object yields the empty string, so a tag whose
Value was an object stored an empty value and reported success; a
Filters or ResourceArnList member that was not a list was ignored
outright, which turned a filtered describe into an unfiltered one.

Every string member now answers SerializationException when it is
present with another JSON type, matching what AWS returns when a
json-1.1 member will not deserialize, and matching how Kinesis, Cognito
and KMS already report the same class of error here. An absent member
stays a parameter error: missing and mistyped are different faults.

* fix(dms): paginate DescribeReplicationSubnetGroups and validate the description

DescribeReplicationSubnetGroups returned every matching group in one response and
ignored both MaxRecords and Marker, so an SDK or Terraform paginator diverged from a
live account as soon as more than one page of groups existed.

Page through the shared opaque-cursor helper, keyed on the group identifier, so a
Marker stays resumable across groups created or deleted between requests. MaxRecords
defaults to 100 and is rejected outside the documented 20 to 100 range rather than
clamped, with the InvalidParameterValueException a live account answers. Marker is
emitted only while a further page remains: an SDK paginator treats any Marker as "ask
again", so one on the final page loops forever.

ReplicationSubnetGroupDescription now also enforces the model's printable-character
constraint, so a value carrying a control character such as 0x01 is rejected instead
of persisted.

* docs(dms): describe the Marker as the opaque cursor it is

* fix(test): DmsTest must compile at the sdk-test-java module's Java 17 target

List.getFirst() is a Java 21 SequencedCollection method. The main project
compiles at 21, but compatibility-tests/sdk-test-java pins source/target 17, and
DmsTest was the only file in that module using getFirst() -- so the native arm64
sdk-test-java image failed to build and every test in it was skipped.

This was not caught earlier because the previous head of this PR had no CI
rollup at all; the merge that made the branch mergeable is what first ran the
job. Replaced with get(0), which is what the rest of the module uses.

* style(test): import Objects and Collectors instead of naming them inline

AGENTS.md asks for imports rather than inline fully-qualified names.
DmsServiceTest had two: java.util.Objects::nonNull and
java.util.stream.Collectors.toMap. Neither was imported; both now are.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-09-21 19:45:01 -04:00
1 parent 09b1dc0803
commit 532bae5525
19 files changed
+1945 -3

No files matched your search

+1 -1
View File
@@ -297,7 +297,7 @@ Floci supports local emulation for application services, data services, eventing
| API and identity | API Gateway REST, API Gateway v2, AppSync, Cognito, Cognito Identity, ACM, Route53, Route 53 Resolver, Cloud Map, Global Accelerator |
| Containers and compute | ECS, EC2, Lightsail, EKS, MWAA, ECR, EFS, CodeBuild, CodeDeploy, CodePipeline, CodeGuru Reviewer, CodeArtifact, AWS Batch, Auto Scaling, Application Auto Scaling, Elastic Beanstalk, ELB v2, ELB Classic |
| Data, analytics, and AI | Athena, Glue, Lake Formation, EMR, EMR Serverless, Redshift, Redshift Data API, Firehose, Managed Service for Apache Flink, OpenSearch, S3 Tables, S3 Vectors, Textract, Transcribe, Comprehend, Rekognition, Translate, Bedrock, Bedrock Runtime, Bedrock AgentCore, Bedrock AgentCore Control, SageMaker |
| Databases and caching | RDS, RDS Data API, Neptune, DocumentDB, MemoryDB, ElastiCache, Timestream for InfluxDB |
| Databases and caching | RDS, RDS Data API, Neptune, DocumentDB, DMS, MemoryDB, ElastiCache, Timestream for InfluxDB |
| Messaging and transfer | SES, Kinesis, MSK, Amazon MQ, Transfer Family, DataSync, IoT Core, Amazon Connect, Amazon AppIntegrations |
| Security and governance | AWS Network Firewall, AWS RAM, Service Quotas, WAF v2, GuardDuty, Amazon Inspector, CloudTrail, CloudFront, Resource Groups Tagging API, Resource Explorer 2, CloudHSM v2, Organizations, AWS Account Management, IAM Access Analyzer, IAM Identity Center (SSO Admin, OIDC, Access Portal, SCIM), Identity Store, Amazon Macie, Amazon Detective, Security Hub, Amazon Verified Permissions, Control Catalog, Control Tower, Service Catalog, AWS Marketplace |
| Cost and billing | AWS Budgets, Pricing, Cost Explorer, Cost and Usage Reports, BCM Pricing Calculator, BCM Data Exports |
@@ -223,6 +223,11 @@
<groupId>software.amazon.awssdk</groupId>
<artifactId>neptune</artifactId>
</dependency>
<!-- DMS management client -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>databasemigration</artifactId>
</dependency>
<!-- EventBridge client -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
@@ -54,6 +54,7 @@ import software.amazon.awssdk.services.marketplacemetering.MarketplaceMeteringCl
import software.amazon.awssdk.services.marketplacediscovery.MarketplaceDiscoveryClient;
import software.amazon.awssdk.services.inspector2.Inspector2Client;
import software.amazon.awssdk.services.securityhub.SecurityHubClient;
import software.amazon.awssdk.services.databasemigration.DatabaseMigrationClient;
import software.amazon.awssdk.services.detective.DetectiveClient;
import software.amazon.awssdk.services.globalaccelerator.GlobalAcceleratorClient;
import software.amazon.awssdk.services.rum.RumClient;
@@ -1021,6 +1022,14 @@ public final class TestFixtures {
.build();
}
public static DatabaseMigrationClient databaseMigrationClient() {
return DatabaseMigrationClient.builder()
.endpointOverride(ENDPOINT)
.region(REGION)
.credentialsProvider(CREDENTIALS)
.build();
}
public static RumClient rumClient() {
return RumClient.builder()
.endpointOverride(ENDPOINT)
@@ -0,0 +1,198 @@
package com.floci.test;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.databasemigration.DatabaseMigrationClient;
import software.amazon.awssdk.services.databasemigration.model.DatabaseMigrationException;
import software.amazon.awssdk.services.databasemigration.model.DescribeReplicationSubnetGroupsResponse;
import software.amazon.awssdk.services.databasemigration.model.ReplicationSubnetGroup;
import software.amazon.awssdk.services.databasemigration.model.Tag;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.Subnet;
import software.amazon.awssdk.services.sts.StsClient;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class DmsTest {
private static final String GROUP_ID = "sdk-compat-subnet-group";
private static final String PAGED_GROUP_PREFIX = "sdk-compat-page-";
private static final String CONTROL_CHAR_GROUP_ID = "sdk-compat-control-char";
@Test
void replicationSubnetGroupLifecycleUsesAwsSdkWireContract() {
List<Subnet> subnets = twoSubnetsInOneVpcAcrossZones();
List<String> subnetIds = subnets.stream().map(Subnet::subnetId).toList();
try (DatabaseMigrationClient dms = TestFixtures.databaseMigrationClient()) {
dms.createReplicationSubnetGroup(request -> request
.replicationSubnetGroupIdentifier(GROUP_ID)
.replicationSubnetGroupDescription("sdk compatibility suite")
.subnetIds(subnetIds)
.tags(tag -> tag.key("env").value("test")));
try {
List<ReplicationSubnetGroup> described = dms.describeReplicationSubnetGroups(request -> request
.filters(filter -> filter.name("replication-subnet-group-id").values(GROUP_ID)))
.replicationSubnetGroups();
assertEquals(1, described.size());
ReplicationSubnetGroup group = described.get(0);
assertEquals(GROUP_ID, group.replicationSubnetGroupIdentifier());
assertEquals("Complete", group.subnetGroupStatus());
assertEquals(subnets.get(0).vpcId(), group.vpcId());
assertFalse(group.subnets().isEmpty());
assertTrue(group.subnets().stream()
.allMatch(subnet -> "Active".equals(subnet.subnetStatus())));
String arn = "arn:aws:dms:us-east-1:" + callerAccountId() + ":subgrp:" + GROUP_ID;
assertEquals(Map.of("env", "test"), tagsOf(dms, arn));
dms.addTagsToResource(request -> request.resourceArn(arn)
.tags(tag -> tag.key("owner").value("data")));
assertEquals(Map.of("env", "test", "owner", "data"), tagsOf(dms, arn));
dms.removeTagsFromResource(request -> request.resourceArn(arn).tagKeys("env"));
assertEquals(Map.of("owner", "data"), tagsOf(dms, arn));
} finally {
dms.deleteReplicationSubnetGroup(request -> request.replicationSubnetGroupIdentifier(GROUP_ID));
}
DatabaseMigrationException deleted = assertThrows(DatabaseMigrationException.class,
() -> dms.describeReplicationSubnetGroups(request -> request
.filters(filter -> filter.name("replication-subnet-group-id").values(GROUP_ID))));
assertEquals("ResourceNotFoundFault", deleted.awsErrorDetails().errorCode());
}
}
/**
* DMS pages this describe at a minimum MaxRecords of 20, so a second page needs more than 20
* groups to exist. Walk the pages by Marker and check the walk is a partition of what was
* created: every identifier once, none missing, non-final pages full, and no Marker handed back
* on the last page (an SDK paginator treats any Marker as "ask again" and would loop).
*/
@Test
void describeReplicationSubnetGroupsPagesByMarker() {
List<String> subnetIds = twoSubnetsInOneVpcAcrossZones().stream().map(Subnet::subnetId).toList();
List<String> created = new ArrayList<>();
for (int i = 0; i < 25; i++) {
created.add(PAGED_GROUP_PREFIX + String.format("%02d", i));
}
try (DatabaseMigrationClient dms = TestFixtures.databaseMigrationClient()) {
for (String identifier : created) {
dms.createReplicationSubnetGroup(request -> request
.replicationSubnetGroupIdentifier(identifier)
.replicationSubnetGroupDescription("sdk pagination suite")
.subnetIds(subnetIds));
}
try {
List<String> walked = new ArrayList<>();
String marker = null;
int pages = 0;
do {
String resume = marker;
DescribeReplicationSubnetGroupsResponse page = dms.describeReplicationSubnetGroups(
request -> request.maxRecords(20).marker(resume));
pages++;
marker = page.marker();
if (marker != null) {
assertEquals(20, page.replicationSubnetGroups().size(),
"a non-final page must be full");
}
page.replicationSubnetGroups()
.forEach(g -> walked.add(g.replicationSubnetGroupIdentifier()));
} while (marker != null);
assertTrue(pages > 1, "25 groups at MaxRecords=20 must span more than one page");
assertEquals(walked.size(), walked.stream().distinct().count(), "a group was returned twice");
assertEquals(created, walked.stream()
.filter(id -> id.startsWith(PAGED_GROUP_PREFIX))
.sorted()
.toList());
} finally {
for (String identifier : created) {
dms.deleteReplicationSubnetGroup(request -> request
.replicationSubnetGroupIdentifier(identifier));
}
}
}
}
@Test
void describeReplicationSubnetGroupsRejectsMaxRecordsOutsideTheDocumentedRange() {
try (DatabaseMigrationClient dms = TestFixtures.databaseMigrationClient()) {
for (int outOfRange : new int[] {19, 101}) {
DatabaseMigrationException rejected = assertThrows(DatabaseMigrationException.class,
() -> dms.describeReplicationSubnetGroups(request -> request.maxRecords(outOfRange)));
assertEquals("InvalidParameterValueException", rejected.awsErrorDetails().errorCode());
}
}
}
/**
* The DMS request model allows only printable characters in the description, so a control
* character is rejected rather than persisted.
*/
@Test
void createReplicationSubnetGroupRejectsANonPrintableDescription() {
List<String> subnetIds = twoSubnetsInOneVpcAcrossZones().stream().map(Subnet::subnetId).toList();
try (DatabaseMigrationClient dms = TestFixtures.databaseMigrationClient()) {
DatabaseMigrationException rejected = assertThrows(DatabaseMigrationException.class,
() -> dms.createReplicationSubnetGroup(request -> request
.replicationSubnetGroupIdentifier(CONTROL_CHAR_GROUP_ID)
.replicationSubnetGroupDescription("sdk\u0001suite")
.subnetIds(subnetIds)));
assertEquals("InvalidParameterValueException", rejected.awsErrorDetails().errorCode());
DatabaseMigrationException absent = assertThrows(DatabaseMigrationException.class,
() -> dms.describeReplicationSubnetGroups(request -> request
.filters(filter -> filter.name("replication-subnet-group-id")
.values(CONTROL_CHAR_GROUP_ID))));
assertEquals("ResourceNotFoundFault", absent.awsErrorDetails().errorCode());
}
}
/**
* DMS does not return the subnet group's ARN, so the tagging calls need it built the way the
* Terraform provider builds it. Read the account from STS rather than hardcoding the default.
*/
private static String callerAccountId() {
try (StsClient sts = TestFixtures.stsClient()) {
return sts.getCallerIdentity().account();
}
}
private static Map<String, String> tagsOf(DatabaseMigrationClient dms, String arn) {
return dms.listTagsForResource(request -> request.resourceArn(arn)).tagList().stream()
.collect(Collectors.toMap(Tag::key, Tag::value));
}
/**
* DMS requires the group's subnets to share a VPC and to cover at least two Availability
* Zones, so pick a VPC that satisfies both rather than the first two subnets returned.
*/
private static List<Subnet> twoSubnetsInOneVpcAcrossZones() {
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
Map<String, Map<String, Subnet>> byVpcAndZone = new LinkedHashMap<>();
for (Subnet subnet : ec2.describeSubnets().subnets()) {
byVpcAndZone
.computeIfAbsent(subnet.vpcId(), vpc -> new LinkedHashMap<>())
.putIfAbsent(subnet.availabilityZone(), subnet);
}
return byVpcAndZone.values().stream()
.filter(byZone -> byZone.size() >= 2)
.map(byZone -> byZone.values().stream().limit(2).toList())
.findFirst()
.orElseThrow(() -> new IllegalStateException(
"no VPC has subnets in two Availability Zones"));
}
}
}
+91
View File
@@ -0,0 +1,91 @@
# AWS Database Migration Service (DMS)
**Protocol:** AWS JSON 1.1
**Signing name:** `dms`
Floci emulates the replication subnet group lifecycle, which is what
`aws_dms_replication_subnet_group` needs to plan and apply. Subnets are resolved against
the emulated EC2 service, so the VPC and Availability Zones a group reports are the ones
those subnets actually have.
## Supported Actions
<!-- floci:actions:start -->
| Action | Description |
| --- | --- |
| `CreateReplicationSubnetGroup` | Creates a replication subnet group from existing EC2 subnets. |
| `DescribeReplicationSubnetGroups` | Lists replication subnet groups, optionally filtered by identifier. |
| `DeleteReplicationSubnetGroup` | Deletes the specified replication subnet group. |
| `ListTagsForResource` | Lists the tags on one or more DMS resource ARNs. |
| `AddTagsToResource` | Merges tags into the resource, overwriting by key. |
| `RemoveTagsFromResource` | Removes the named tag keys from the resource. |
<!-- floci:actions:end -->
## Behaviour
- The identifier is stored as a lowercase string, as AWS does, so a group created as
`MyGroup` is described and deleted as `mygroup`.
- `ReplicationSubnetGroupIdentifier` must not be `default` and is limited to 255
alphanumeric characters, periods, underscores, or hyphens.
- A group must cover at least two Availability Zones. Fewer returns
`ReplicationSubnetGroupDoesNotCoverEnoughAZs`, as AWS does.
- Subnets must exist and must all belong to one VPC. Otherwise `InvalidSubnet`.
- `SubnetGroupStatus` is immediately `Complete`, every subnet reports `Active`,
`SupportedNetworkTypes` is `["IPV4"]`, and `IsReadOnly` is always `false`: a read-only
group is one DMS manages for a zero-ETL integration, which Floci does not emulate.
- `DescribeReplicationSubnetGroups` supports the `replication-subnet-group-id` filter.
A filter naming a group that does not exist returns `ResourceNotFoundFault`, which is
how Terraform detects a group deleted outside its state.
- `DescribeReplicationSubnetGroups` paginates on `MaxRecords` and `Marker`. `MaxRecords`
defaults to 100 and must be between 20 and 100; a value outside that range is rejected
with `InvalidParameterValueException` rather than clamped, as AWS does. Results are
ordered by identifier and `Marker` is an opaque cursor that resumes after the last item
of the page, so a page stays resumable across groups created or deleted between
requests. `Marker` is returned
only when a further page exists, never on the last one.
- `ReplicationSubnetGroupDescription` is required, must not be blank, and must contain only
printable characters. A control character such as `0x01` returns
`InvalidParameterValueException`.
- A member of the wrong JSON type returns `SerializationException`, as AWS does, rather than
being coerced. That covers a `Tags` entry whose `Value` is an object, a filter whose
`Values` is not a list, a non-string element in `ResourceArnList`, `SubnetIds` or
`TagKeys`, and a non-string identifier. An absent member is still a parameter error,
not a serialization one.
- Groups are scoped per account and Region and persist through `StorageFactory`.
### Tagging
`Tags` on `CreateReplicationSubnetGroup` are stored with the group, and the tagging trio
works against the group's ARN, which is
`arn:aws:dms:<region>:<account>:subgrp:<identifier>`. DMS does not return that ARN from
`DescribeReplicationSubnetGroups`, so Terraform builds it client side and Floci parses it
back the same way.
- `AddTagsToResource` merges by key, so re-tagging an existing key overwrites its value.
- `RemoveTagsFromResource` removes the named keys and ignores keys that are not present.
- `ListTagsForResource` accepts either `ResourceArn` or `ResourceArnList`. Each returned
tag carries its `ResourceArn` only for the `ResourceArnList` form, which is the form
AWS documents it on.
- Tag keys are 1 to 128 characters, values at most 256, and neither may start with `aws:`
or `dms:`.
- An ARN that is unparseable, names a different DMS resource type, names another account
or Region, or names a group that does not exist returns `ResourceNotFoundFault`. Tagging
is not a cross-account or cross-Region operation on AWS and is not one here either.
- Deleting a group deletes its tags with it.
## Limitations
- **Only the subnet group lifecycle is implemented.** Replication instances, endpoints,
and replication tasks return `UnknownOperationException`.
- **Tagging covers replication subnet groups only.** The tagging trio is implemented, but
a `ResourceArn` naming a replication instance, endpoint, or task returns
`ResourceNotFoundFault` because Floci holds no such resource.
- **No `ModifyReplicationSubnetGroup`.** A subnet change has to be a delete and recreate.
See the [AWS DMS API Reference](https://docs.aws.amazon.com/dms/latest/APIReference/Welcome.html).
## Configuration
| Variable | Default | Description |
|---|---|---|
| `FLOCI_SERVICES_DMS_ENABLED` | `true` | Enable or disable DMS |
+1
View File
@@ -72,6 +72,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
| [Lake Formation](lakeformation.md) | `POST /<Action>` | REST JSON | 16 |
| [Neptune](neptune.md) | `POST /` with `Action=` param + Gremlin TCP proxy | Query + WebSocket | 14 |
| [DocumentDB](docdb.md) | `POST /` with `Action=` param + MongoDB container | Query + MongoDB wire | 8 |
| [DMS](dms.md) | `POST /` + `X-Amz-Target: AmazonDMSv20160101.*` | JSON 1.1 | 6 |
| [Redshift](redshift.md) | `POST /` with `Action=` param + PostgreSQL container | Query + PostgreSQL wire (+ CFN) | 29 |
| [Redshift Data API](redshift-data.md) | `POST /` + `X-Amz-Target: RedshiftData.*` | JSON 1.1 | 11 |
| [EMR](emr.md) | `POST /` + `X-Amz-Target: ElasticMapReduce.*` | JSON 1.1 | 24 |
+1
View File
@@ -147,6 +147,7 @@ nav:
- Lake Formation: services/lakeformation.md
- Neptune: services/neptune.md
- DocumentDB: services/docdb.md
- DMS: services/dms.md
- Athena: services/athena.md
- Redshift: services/redshift.md
- Redshift Data API: services/redshift-data.md
@@ -792,6 +792,12 @@ public interface EmulatorConfig {
CodeGuruReviewerServiceConfig codegurureviewer();
CodeArtifactServiceConfig codeartifact();
MarketplaceServiceConfig marketplace();
DmsServiceConfig dms();
}
interface DmsServiceConfig {
@WithDefault("true")
boolean enabled();
}
interface CodeArtifactServiceConfig {
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.cloudhsmv2.CloudHsmV2JsonHandler;
import io.github.hectorvent.floci.services.codebuild.CodeBuildJsonHandler;
import io.github.hectorvent.floci.services.codedeploy.CodeDeployJsonHandler;
import io.github.hectorvent.floci.services.codepipeline.CodePipelineJsonHandler;
import io.github.hectorvent.floci.services.dms.DmsJsonHandler;
import io.github.hectorvent.floci.services.ecr.EcrJsonHandler;
import io.github.hectorvent.floci.services.transfer.TransferHandler;
import io.github.hectorvent.floci.services.ecs.EcsJsonHandler;
@@ -141,6 +142,7 @@ public class AwsJson11Controller {
private final MarketplaceEntitlementController marketplaceEntitlementController;
private final MarketplaceMeteringController marketplaceMeteringController;
private final SageMakerJsonHandler sageMakerJsonHandler;
private final DmsJsonHandler dmsJsonHandler;
@Inject
public AwsJson11Controller(ObjectMapper objectMapper, ResolvedServiceCatalog catalog,
@@ -195,7 +197,8 @@ public class AwsJson11Controller {
ServiceQuotasJsonHandler serviceQuotasJsonHandler,
MarketplaceEntitlementController marketplaceEntitlementController,
MarketplaceMeteringController marketplaceMeteringController,
SageMakerJsonHandler sageMakerJsonHandler) {
SageMakerJsonHandler sageMakerJsonHandler,
DmsJsonHandler dmsJsonHandler) {
this.objectMapper = objectMapper;
this.strictBodyReader = objectMapper.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
this.catalog = catalog;
@@ -255,6 +258,7 @@ public class AwsJson11Controller {
this.marketplaceEntitlementController = marketplaceEntitlementController;
this.marketplaceMeteringController = marketplaceMeteringController;
this.sageMakerJsonHandler = sageMakerJsonHandler;
this.dmsJsonHandler = dmsJsonHandler;
}
@POST
@@ -367,6 +371,7 @@ public class AwsJson11Controller {
yield marketplaceMeteringController.handle(action, request, region);
}
case "sagemaker" -> sageMakerJsonHandler.handle(action, request, region);
case "dms" -> dmsJsonHandler.handle(action, request, region);
default -> null;
};
// catalog.matchTarget is protocol-agnostic: a JSON 1.0 target
@@ -733,7 +733,11 @@ public class ResolvedServiceCatalog {
"marketplace", config.storage().mode(), 5000L, null, ServiceProtocol.REST_JSON,
protocols(ServiceProtocol.REST_JSON, ServiceProtocol.JSON, ServiceProtocol.CBOR),
Set.of("AWSMPCommerceService_v20200301.", "AWSMPEntitlementService.", "AWSMPMeteringService."), Set.of("aws-marketplace"), Set.of("AWS Marketplace Entitlement Service"),
Set.of(MarketplaceCatalogController.class, MarketplaceDeploymentController.class, MarketplaceReportingController.class, MarketplaceDiscoveryController.class))
Set.of(MarketplaceCatalogController.class, MarketplaceDeploymentController.class, MarketplaceReportingController.class, MarketplaceDiscoveryController.class)),
descriptor("dms", "dms", config.services().dms().enabled(), true,
"dms", config.storage().mode(), 5000L, null, ServiceProtocol.JSON,
protocols(ServiceProtocol.JSON),
Set.of("AmazonDMSv20160101."), Set.of("dms"), Set.of(), Set.of())
));
}
@@ -0,0 +1,100 @@
package io.github.hectorvent.floci.services.dms;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.services.dms.model.ReplicationSubnetGroup;
import io.github.hectorvent.floci.services.dms.model.ResourceTag;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
@ApplicationScoped
public class DmsJsonHandler {
private final DmsService service;
private final ObjectMapper objectMapper;
@Inject
public DmsJsonHandler(DmsService service, ObjectMapper objectMapper) {
this.service = service;
this.objectMapper = objectMapper;
}
public Response handle(String action, JsonNode request, String region) {
return switch (action) {
case "CreateReplicationSubnetGroup" -> {
ObjectNode response = objectMapper.createObjectNode();
response.set("ReplicationSubnetGroup",
subnetGroup(service.createReplicationSubnetGroup(request, region)));
yield Response.ok(response).build();
}
case "DescribeReplicationSubnetGroups" -> {
PaginatedResult<ReplicationSubnetGroup> page =
service.describeReplicationSubnetGroups(request, region);
ObjectNode response = objectMapper.createObjectNode();
ArrayNode items = response.putArray("ReplicationSubnetGroups");
page.items().forEach(group -> items.add(subnetGroup(group)));
// Marker is present only while another page remains: an SDK paginator treats any
// Marker at all as "ask again", so emitting one on the final page loops forever.
if (page.nextToken() != null) {
response.put("Marker", page.nextToken());
}
yield Response.ok(response).build();
}
case "DeleteReplicationSubnetGroup" -> {
service.deleteReplicationSubnetGroup(request, region);
yield Response.ok(objectMapper.createObjectNode()).build();
}
case "ListTagsForResource" -> {
ObjectNode response = objectMapper.createObjectNode();
ArrayNode tagList = response.putArray("TagList");
service.listTagsForResource(request, region).forEach(tag -> tagList.add(tagNode(tag)));
yield Response.ok(response).build();
}
case "AddTagsToResource" -> {
service.addTagsToResource(request, region);
yield Response.ok(objectMapper.createObjectNode()).build();
}
case "RemoveTagsFromResource" -> {
service.removeTagsFromResource(request, region);
yield Response.ok(objectMapper.createObjectNode()).build();
}
default -> null;
};
}
private ObjectNode tagNode(ResourceTag tag) {
ObjectNode node = objectMapper.createObjectNode();
node.put("Key", tag.key());
node.put("Value", tag.value());
if (tag.resourceArn() != null) {
node.put("ResourceArn", tag.resourceArn());
}
return node;
}
private ObjectNode subnetGroup(ReplicationSubnetGroup group) {
ObjectNode node = objectMapper.createObjectNode();
node.put("ReplicationSubnetGroupIdentifier", group.getReplicationSubnetGroupIdentifier());
node.put("ReplicationSubnetGroupDescription", group.getReplicationSubnetGroupDescription());
node.put("VpcId", group.getVpcId());
node.put("SubnetGroupStatus", group.getSubnetGroupStatus());
ArrayNode subnets = node.putArray("Subnets");
group.getSubnetIds().forEach(subnetId -> {
ObjectNode subnet = subnets.addObject();
subnet.put("SubnetIdentifier", subnetId);
subnet.putObject("SubnetAvailabilityZone")
.put("Name", group.getSubnetAvailabilityZones().get(subnetId));
subnet.put("SubnetStatus", "Active");
});
ArrayNode networkTypes = node.putArray("SupportedNetworkTypes");
group.getSupportedNetworkTypes().forEach(networkTypes::add);
// Always false: a read-only group is one DMS manages for a zero-ETL integration, which
// Floci does not emulate, so every group here is caller-owned and modifiable.
node.put("IsReadOnly", false);
return node;
}
}
@@ -0,0 +1,456 @@
package io.github.hectorvent.floci.services.dms;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
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.dms.model.ReplicationSubnetGroup;
import io.github.hectorvent.floci.services.dms.model.ResourceTag;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Subnet;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.regex.Pattern;
@ApplicationScoped
public class DmsService implements Resettable {
private static final Pattern SUBNET_GROUP_IDENTIFIER = Pattern.compile("[A-Za-z0-9._-]+");
private static final String SUBNET_GROUP_ID_FILTER = "replication-subnet-group-id";
private static final int MINIMUM_AVAILABILITY_ZONES = 2;
private static final String SUBNET_GROUP_ARN_RESOURCE_TYPE = "subgrp";
private static final int DEFAULT_MAX_RECORDS = 100;
private static final int MINIMUM_MAX_RECORDS = 20;
private static final int MAXIMUM_MAX_RECORDS = 100;
private static final Set<String> RESERVED_TAG_PREFIXES = Set.of("aws:", "dms:");
private final AccountAwareStorageBackend<ReplicationSubnetGroup> subnetGroups;
private final Ec2Service ec2Service;
private final RegionResolver regionResolver;
@Inject
public DmsService(StorageFactory storageFactory, Ec2Service ec2Service, RegionResolver regionResolver) {
this.subnetGroups = storageFactory.create("dms", "dms-replication-subnet-groups.json",
new TypeReference<Map<String, ReplicationSubnetGroup>>() {});
this.ec2Service = ec2Service;
this.regionResolver = regionResolver;
}
public synchronized ReplicationSubnetGroup createReplicationSubnetGroup(JsonNode request, String region) {
String identifier = requireIdentifier(request);
String description = requireDescription(request);
List<String> subnetIds = requireSubnetIds(request);
if (subnetGroups.get(storageKey(region, identifier)).isPresent()) {
throw new AwsException("ResourceAlreadyExistsFault",
"The resource you are attempting to create already exists.", 400);
}
ReplicationSubnetGroup group = buildSubnetGroup(identifier, description, subnetIds, region);
group.setTags(readTags(request.get("Tags")));
subnetGroups.put(storageKey(region, identifier), group);
return group;
}
/**
* Pages by identifier through the shared opaque-cursor helper, so {@code Marker} stays
* resumable when a group is created or deleted between requests. DMS documents a default
* {@code MaxRecords} of 100 and a valid range of 20 to 100, and rejects a value outside that
* range rather than clamping it.
*/
public PaginatedResult<ReplicationSubnetGroup> describeReplicationSubnetGroups(JsonNode request, String region) {
Integer maxRecords = maxRecords(request);
String marker = text(request, "Marker");
List<String> requestedIdentifiers = identifierFilters(request);
List<ReplicationSubnetGroup> matching;
if (!requestedIdentifiers.isEmpty()) {
matching = requestedIdentifiers.stream()
.map(identifier -> subnetGroups.get(storageKey(region, identifier))
.orElseThrow(() -> notFound(identifier)))
.toList();
} else {
matching = subnetGroups.scan(key -> key.startsWith(region + "::"));
}
return Pagination.paginate(matching, ReplicationSubnetGroup::getReplicationSubnetGroupIdentifier,
maxRecords, marker, DEFAULT_MAX_RECORDS, MAXIMUM_MAX_RECORDS,
"InvalidParameterValueException");
}
public synchronized void deleteReplicationSubnetGroup(JsonNode request, String region) {
String identifier = requireIdentifier(request);
String key = storageKey(region, identifier);
if (subnetGroups.get(key).isEmpty()) {
throw notFound(identifier);
}
subnetGroups.delete(key);
}
public List<ResourceTag> listTagsForResource(JsonNode request, String region) {
List<String> arnList = arnList(request);
if (!arnList.isEmpty()) {
List<ResourceTag> tags = new ArrayList<>();
for (String arn : arnList) {
resolveByArn(arn, region).getTags()
.forEach((key, value) -> tags.add(new ResourceTag(arn, key, value)));
}
return tags;
}
String resourceArn = requireResourceArn(request);
return resolveByArn(resourceArn, region).getTags().entrySet().stream()
.map(entry -> new ResourceTag(null, entry.getKey(), entry.getValue()))
.toList();
}
public synchronized void addTagsToResource(JsonNode request, String region) {
String resourceArn = requireResourceArn(request);
JsonNode tagsNode = request == null ? null : request.get("Tags");
if (tagsNode == null || tagsNode.isNull()) {
throw invalidParameter("The parameter Tags must be provided.");
}
Map<String, String> added = readTags(tagsNode);
updateTags(resourceArn, region, tags -> tags.putAll(added));
}
public synchronized void removeTagsFromResource(JsonNode request, String region) {
String resourceArn = requireResourceArn(request);
JsonNode keysNode = request == null ? null : request.get("TagKeys");
if (keysNode == null || keysNode.isNull()) {
throw invalidParameter("The parameter TagKeys must be provided.");
}
List<String> keys = stringList(keysNode, "TagKeys");
updateTags(resourceArn, region, tags -> keys.forEach(tags::remove));
}
@Override
public void clear() {
subnetGroups.clear();
}
private ReplicationSubnetGroup buildSubnetGroup(String identifier, String description,
List<String> subnetIds, String region) {
Map<String, Subnet> resolved = new LinkedHashMap<>();
ec2Service.describeSubnets(region, subnetIds, Map.of())
.forEach(subnet -> resolved.put(subnet.getSubnetId(), subnet));
List<String> requested = subnetIds.stream().distinct().toList();
List<String> missing = requested.stream().filter(id -> !resolved.containsKey(id)).toList();
if (!missing.isEmpty()) {
throw new AwsException("InvalidSubnet",
"The subnet provided is invalid: " + missing + ".", 400);
}
// Response order follows the request rather than storage iteration order, so a describe
// that follows a create returns the same subnet ordering every time.
Map<String, String> availabilityZones = new LinkedHashMap<>();
for (String subnetId : requested) {
String availabilityZone = resolved.get(subnetId).getAvailabilityZone();
if (availabilityZone == null) {
throw new AwsException("InvalidSubnet",
"Subnet " + subnetId + " has no Availability Zone.", 400);
}
availabilityZones.put(subnetId, availabilityZone);
}
String vpcId = resolved.get(requested.getFirst()).getVpcId();
boolean sameVpc = resolved.values().stream()
.map(Subnet::getVpcId)
.filter(Objects::nonNull)
.allMatch(vpcId::equals);
if (!sameVpc) {
throw new AwsException("InvalidSubnet",
"The subnets provided for replication subnet group " + identifier
+ " belong to more than one VPC.", 400);
}
if (Set.copyOf(availabilityZones.values()).size() < MINIMUM_AVAILABILITY_ZONES) {
throw new AwsException("ReplicationSubnetGroupDoesNotCoverEnoughAZs",
"The replication subnet group does not cover enough Availability Zones (AZs)."
+ " Edit the replication subnet group and add more AZs.", 400);
}
ReplicationSubnetGroup group = new ReplicationSubnetGroup();
group.setReplicationSubnetGroupIdentifier(identifier);
group.setReplicationSubnetGroupDescription(description);
group.setVpcId(vpcId);
group.setSubnetGroupStatus("Complete");
group.setSubnetIds(requested);
group.setSubnetAvailabilityZones(availabilityZones);
group.setSupportedNetworkTypes(List.of("IPV4"));
return group;
}
/**
* AWS stores the identifier as a lowercase string, so every lookup normalises the same way:
* a group created as "MyGroup" is described and deleted as "mygroup".
*/
private static String requireIdentifier(JsonNode request) {
String value = text(request, "ReplicationSubnetGroupIdentifier");
if (value == null || value.isBlank()) {
throw invalidParameter("The parameter ReplicationSubnetGroupIdentifier must be provided"
+ " and must not be blank.");
}
String identifier = value.toLowerCase(Locale.ROOT);
if (identifier.length() > 255 || !SUBNET_GROUP_IDENTIFIER.matcher(identifier).matches()) {
throw invalidParameter("ReplicationSubnetGroupIdentifier must contain no more than 255"
+ " alphanumeric characters, periods, underscores, or hyphens.");
}
if ("default".equals(identifier)) {
throw invalidParameter("ReplicationSubnetGroupIdentifier must not be \"default\".");
}
return identifier;
}
/**
* A description carrying a control character such as 0x01 is rejected here rather than
* persisted, per the review of the upstream PR; AWS rejects non-printable control characters
* in this member. Blank and absent stay the same parameter error they were.
*/
private static String requireDescription(JsonNode request) {
String description = text(request, "ReplicationSubnetGroupDescription");
if (description == null || description.isBlank()) {
throw invalidParameter("The parameter ReplicationSubnetGroupDescription must be provided"
+ " and must not be blank.");
}
if (description.chars().anyMatch(Character::isISOControl)) {
throw invalidParameter("The parameter ReplicationSubnetGroupDescription must contain only"
+ " printable characters.");
}
return description;
}
/**
* {@code MaxRecords} is modelled as an integer, so a non-integer value is a serialization error
* rather than a parameter one. A value outside 20 to 100 is rejected, not clamped, which is
* what a live account does.
*/
private static Integer maxRecords(JsonNode request) {
JsonNode node = request == null ? null : request.get("MaxRecords");
if (node == null || node.isNull()) {
return null;
}
if (!node.isIntegralNumber() || !node.canConvertToInt()) {
throw serialization("MaxRecords must be an integer.");
}
int value = node.intValue();
if (value < MINIMUM_MAX_RECORDS || value > MAXIMUM_MAX_RECORDS) {
throw invalidParameter("Invalid value " + value + " for MaxRecords. Must be between "
+ MINIMUM_MAX_RECORDS + " and " + MAXIMUM_MAX_RECORDS + ".");
}
return value;
}
private static List<String> requireSubnetIds(JsonNode request) {
JsonNode node = request == null ? null : request.get("SubnetIds");
if (node == null || node.isNull()) {
throw invalidParameter("The parameter SubnetIds must be provided and must not be empty.");
}
if (!node.isArray()) {
throw serialization("SubnetIds must be a list of strings.");
}
if (node.isEmpty()) {
throw invalidParameter("The parameter SubnetIds must be provided and must not be empty.");
}
List<String> subnetIds = stringList(node, "SubnetIds");
if (subnetIds.stream().anyMatch(String::isBlank)) {
throw invalidParameter("The parameter SubnetIds must contain subnet identifiers.");
}
return subnetIds;
}
private static List<String> identifierFilters(JsonNode request) {
JsonNode filters = request == null ? null : request.get("Filters");
if (filters == null || filters.isNull()) {
return List.of();
}
if (!filters.isArray()) {
throw serialization("Filters must be a list of Name and Values pairs.");
}
List<String> identifiers = new ArrayList<>();
for (JsonNode filter : filters) {
if (!filter.isObject()) {
throw serialization("Filters must be a list of Name and Values pairs.");
}
String name = text(filter, "Name");
if (!SUBNET_GROUP_ID_FILTER.equals(name)) {
throw invalidParameter("Invalid filter: " + name + ".");
}
JsonNode values = filter.get("Values");
if (values == null || values.isNull()) {
throw invalidParameter("The filter " + SUBNET_GROUP_ID_FILTER + " must have values.");
}
if (!values.isArray()) {
throw serialization("Filter Values must be a list of strings.");
}
if (values.isEmpty()) {
throw invalidParameter("The filter " + SUBNET_GROUP_ID_FILTER + " must have values.");
}
stringList(values, "Filter Values")
.forEach(value -> identifiers.add(value.toLowerCase(Locale.ROOT)));
}
return identifiers;
}
private static AwsException notFound(String identifier) {
return new AwsException("ResourceNotFoundFault",
"Replication subnet group " + identifier + " not found.", 400);
}
private static AwsException invalidParameter(String message) {
return new AwsException("InvalidParameterValueException", message, 400);
}
/**
* Reads a string member. Absent or explicitly null yields null; a member of any other JSON type
* is a SerializationException, which is what AWS returns when a json-1.1 member will not
* deserialize to its modelled type. Coercing it instead (asText on an object yields "") would
* silently store a wrong value.
*/
private static String text(JsonNode request, String field) {
JsonNode node = request == null ? null : request.get(field);
if (node == null || node.isNull()) {
return null;
}
if (!node.isTextual()) {
throw serialization(field + " must be a string.");
}
return node.textValue();
}
private static List<String> stringList(JsonNode array, String field) {
if (!array.isArray()) {
throw serialization(field + " must be a list of strings.");
}
List<String> values = new ArrayList<>();
for (JsonNode element : array) {
if (!element.isTextual()) {
throw serialization(field + " must be a list of strings.");
}
values.add(element.textValue());
}
return values;
}
private static AwsException serialization(String message) {
return new AwsException("SerializationException", message, 400);
}
private static String storageKey(String region, String identifier) {
return region + "::" + identifier;
}
private void updateTags(String resourceArn, String region,
Consumer<Map<String, String>> mutation) {
String key = subnetGroupKeyForArn(resourceArn, region);
ReplicationSubnetGroup group = subnetGroups.get(key).orElseThrow(() -> notFoundForArn(resourceArn));
Map<String, String> tags = new LinkedHashMap<>(group.getTags());
mutation.accept(tags);
group.setTags(tags);
subnetGroups.put(key, group);
}
private ReplicationSubnetGroup resolveByArn(String resourceArn, String region) {
return subnetGroups.get(subnetGroupKeyForArn(resourceArn, region))
.orElseThrow(() -> notFoundForArn(resourceArn));
}
/**
* DescribeReplicationSubnetGroups does not return an ARN, so Terraform builds
* {@code arn:aws:dms:<region>:<account>:subgrp:<id>} itself and tags against that. Anything
* that is not such an ARN names no DMS resource Floci holds, which is a ResourceNotFoundFault
* rather than a parameter error.
*
* <p>An ARN naming another account or another Region is treated the same way. Tagging is not a
* cross-account or cross-Region operation on AWS, and here it cannot be one either: storage is
* scoped to the caller's account and keyed by the caller's Region, so honouring a foreign ARN
* would silently reach the caller's own group of that name instead of the one named.
*/
private String subnetGroupKeyForArn(String resourceArn, String callerRegion) {
AwsArnUtils.Arn arn;
try {
arn = AwsArnUtils.parse(resourceArn);
} catch (IllegalArgumentException e) {
throw notFoundForArn(resourceArn);
}
String[] resource = arn.resource().split(":", 2);
if (!"dms".equals(arn.service()) || resource.length != 2
|| !SUBNET_GROUP_ARN_RESOURCE_TYPE.equals(resource[0]) || resource[1].isBlank()) {
throw notFoundForArn(resourceArn);
}
if (!arn.accountId().isBlank() && !arn.accountId().equals(regionResolver.getAccountId())) {
throw notFoundForArn(resourceArn);
}
if (arn.region() != null && !arn.region().isBlank() && !arn.region().equals(callerRegion)) {
throw notFoundForArn(resourceArn);
}
return storageKey(callerRegion, resource[1].toLowerCase(Locale.ROOT));
}
private static List<String> arnList(JsonNode request) {
JsonNode node = request == null ? null : request.get("ResourceArnList");
if (node == null || node.isNull()) {
return List.of();
}
if (!node.isArray()) {
throw serialization("ResourceArnList must be a list of strings.");
}
return stringList(node, "ResourceArnList");
}
private static String requireResourceArn(JsonNode request) {
String resourceArn = text(request, "ResourceArn");
if (resourceArn == null || resourceArn.isBlank()) {
throw invalidParameter("The parameter ResourceArn must be provided and must not be blank.");
}
return resourceArn;
}
private static Map<String, String> readTags(JsonNode node) {
Map<String, String> tags = new LinkedHashMap<>();
if (node == null || node.isNull()) {
return tags;
}
if (!node.isArray()) {
throw serialization("Tags must be a list of Key and Value pairs.");
}
for (JsonNode element : node) {
if (!element.isObject()) {
throw serialization("Tags must be a list of Key and Value pairs.");
}
String key = text(element, "Key");
String value = text(element, "Value");
if (key == null || key.isEmpty() || key.length() > 128 || isReserved(key)) {
throw invalidParameter("Tag keys must be 1-128 characters and must not start with"
+ " \"aws:\" or \"dms:\".");
}
String tagValue = value == null ? "" : value;
if (tagValue.length() > 256 || isReserved(tagValue)) {
throw invalidParameter("Tag values must be at most 256 characters and must not start"
+ " with \"aws:\" or \"dms:\".");
}
tags.put(key, tagValue);
}
return tags;
}
private static boolean isReserved(String value) {
return RESERVED_TAG_PREFIXES.stream().anyMatch(value::startsWith);
}
private static AwsException notFoundForArn(String resourceArn) {
return new AwsException("ResourceNotFoundFault",
"Resource " + resourceArn + " not found.", 400);
}
}
@@ -0,0 +1,95 @@
package io.github.hectorvent.floci.services.dms.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public class ReplicationSubnetGroup {
private String replicationSubnetGroupIdentifier;
private String replicationSubnetGroupDescription;
private String vpcId;
private String subnetGroupStatus = "Complete";
private List<String> subnetIds = new ArrayList<>();
private Map<String, String> subnetAvailabilityZones = new LinkedHashMap<>();
private List<String> supportedNetworkTypes = new ArrayList<>();
private Map<String, String> tags = new LinkedHashMap<>();
public ReplicationSubnetGroup() {
}
public String getReplicationSubnetGroupIdentifier() {
return replicationSubnetGroupIdentifier;
}
public void setReplicationSubnetGroupIdentifier(String replicationSubnetGroupIdentifier) {
this.replicationSubnetGroupIdentifier = replicationSubnetGroupIdentifier;
}
public String getReplicationSubnetGroupDescription() {
return replicationSubnetGroupDescription;
}
public void setReplicationSubnetGroupDescription(String replicationSubnetGroupDescription) {
this.replicationSubnetGroupDescription = replicationSubnetGroupDescription;
}
public String getVpcId() {
return vpcId;
}
public void setVpcId(String vpcId) {
this.vpcId = vpcId;
}
public String getSubnetGroupStatus() {
return subnetGroupStatus;
}
public void setSubnetGroupStatus(String subnetGroupStatus) {
this.subnetGroupStatus = subnetGroupStatus;
}
public List<String> getSubnetIds() {
return List.copyOf(subnetIds);
}
public void setSubnetIds(List<String> subnetIds) {
this.subnetIds = subnetIds != null ? new ArrayList<>(subnetIds) : new ArrayList<>();
}
public Map<String, String> getSubnetAvailabilityZones() {
return Map.copyOf(subnetAvailabilityZones);
}
public void setSubnetAvailabilityZones(Map<String, String> subnetAvailabilityZones) {
this.subnetAvailabilityZones = subnetAvailabilityZones != null
? new LinkedHashMap<>(subnetAvailabilityZones)
: new LinkedHashMap<>();
}
public List<String> getSupportedNetworkTypes() {
return List.copyOf(supportedNetworkTypes);
}
public void setSupportedNetworkTypes(List<String> supportedNetworkTypes) {
this.supportedNetworkTypes = supportedNetworkTypes != null
? new ArrayList<>(supportedNetworkTypes)
: new ArrayList<>();
}
public Map<String, String> getTags() {
return Collections.unmodifiableMap(new LinkedHashMap<>(tags));
}
public void setTags(Map<String, String> tags) {
this.tags = tags != null ? new LinkedHashMap<>(tags) : new LinkedHashMap<>();
}
}
@@ -0,0 +1,9 @@
package io.github.hectorvent.floci.services.dms.model;
/**
* One tag as DMS returns it from {@code ListTagsForResource}. {@code resourceArn} is null for a
* single-ARN request and set when the caller passed {@code ResourceArnList}, which is the only
* form where AWS documents the field as part of the response.
*/
public record ResourceTag(String resourceArn, String key, String value) {
}
+2
View File
@@ -832,3 +832,5 @@ floci:
enabled: true
marketplace:
enabled: true
dms:
enabled: true
@@ -0,0 +1,381 @@
package io.github.hectorvent.floci.services.dms;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class DmsIntegrationTest {
private static final String CONTENT_TYPE = "application/x-amz-json-1.1";
private static final String TARGET_PREFIX = "AmazonDMSv20160101.";
private static final String ACCOUNT_ID = "723679240095";
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=" + ACCOUNT_ID + "/20260101/us-east-1/dms/aws4_request";
private static final String SUBNET_A = "subnet-default-us-east-1-a";
private static final String SUBNET_B = "subnet-default-us-east-1-b";
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void subnetGroupLifecycleIsReadableThroughDescribe() {
dms("CreateReplicationSubnetGroup")
.body("{\"ReplicationSubnetGroupIdentifier\":\"Tf-Lifecycle\","
+ "\"ReplicationSubnetGroupDescription\":\"terraform managed\","
+ "\"SubnetIds\":[\"" + SUBNET_A + "\",\"" + SUBNET_B + "\"]}")
.when()
.post("/")
.then()
.statusCode(200)
.body("ReplicationSubnetGroup.ReplicationSubnetGroupIdentifier", equalTo("tf-lifecycle"));
dms("DescribeReplicationSubnetGroups")
.body("{\"Filters\":[{\"Name\":\"replication-subnet-group-id\","
+ "\"Values\":[\"tf-lifecycle\"]}]}")
.when()
.post("/")
.then()
.statusCode(200)
.body("ReplicationSubnetGroups", hasSize(1))
.body("ReplicationSubnetGroups[0].ReplicationSubnetGroupDescription",
equalTo("terraform managed"))
.body("ReplicationSubnetGroups[0].VpcId", equalTo("vpc-default-us-east-1"))
.body("ReplicationSubnetGroups[0].SubnetGroupStatus", equalTo("Complete"))
.body("ReplicationSubnetGroups[0].Subnets.SubnetIdentifier", contains(SUBNET_A, SUBNET_B))
.body("ReplicationSubnetGroups[0].Subnets.find { it.SubnetIdentifier == '"
+ SUBNET_A + "' }.SubnetAvailabilityZone.Name", equalTo("us-east-1a"))
.body("ReplicationSubnetGroups[0].Subnets.find { it.SubnetIdentifier == '"
+ SUBNET_B + "' }.SubnetAvailabilityZone.Name", equalTo("us-east-1b"))
.body("ReplicationSubnetGroups[0].Subnets.SubnetStatus", everyItem(equalTo("Active")))
.body("ReplicationSubnetGroups[0].SupportedNetworkTypes", contains("IPV4"))
.body("ReplicationSubnetGroups[0].IsReadOnly", equalTo(false));
dms("DeleteReplicationSubnetGroup")
.body("{\"ReplicationSubnetGroupIdentifier\":\"tf-lifecycle\"}")
.when()
.post("/")
.then()
.statusCode(200);
dms("DescribeReplicationSubnetGroups")
.body("{\"Filters\":[{\"Name\":\"replication-subnet-group-id\","
+ "\"Values\":[\"tf-lifecycle\"]}]}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ResourceNotFoundFault"));
}
/**
* DMS pages at a minimum MaxRecords of 20, so a second page needs more than 20 groups. Walk the
* pages on the wire and check Marker resumes where the previous page stopped and is absent from
* the final response.
*/
@Test
void describePagesOnMarkerAndOmitsItFromTheFinalPage() {
List<String> created = new ArrayList<>();
for (int i = 0; i < 25; i++) {
String identifier = "tf-page-" + String.format("%02d", i);
created.add(identifier);
dms("CreateReplicationSubnetGroup")
.body(createBody(identifier))
.when()
.post("/")
.then()
.statusCode(200);
}
try {
List<String> walked = new ArrayList<>();
String marker = null;
int pages = 0;
do {
String body = marker == null
? "{\"MaxRecords\":20}"
: "{\"MaxRecords\":20,\"Marker\":\"" + marker + "\"}";
Response page = dms("DescribeReplicationSubnetGroups")
.body(body)
.when()
.post("/")
.then()
.statusCode(200)
.extract().response();
pages++;
marker = page.path("Marker");
List<String> identifiers =
page.path("ReplicationSubnetGroups.ReplicationSubnetGroupIdentifier");
if (marker != null) {
assertEquals(20, identifiers.size(), "a non-final page must be full");
}
walked.addAll(identifiers);
} while (marker != null);
assertNull(marker, "the final page must not carry a Marker");
assertTrue(pages > 1, "25 groups at MaxRecords=20 must span more than one page");
assertEquals(walked.size(), walked.stream().distinct().count(), "a group was returned twice");
assertEquals(created, walked.stream().filter(id -> id.startsWith("tf-page-")).sorted().toList());
} finally {
created.forEach(identifier -> dms("DeleteReplicationSubnetGroup")
.body("{\"ReplicationSubnetGroupIdentifier\":\"" + identifier + "\"}")
.when()
.post("/")
.then()
.statusCode(200));
}
}
@Test
void describeRejectsMaxRecordsOutsideTheDocumentedRange() {
for (int outOfRange : new int[] {19, 101}) {
dms("DescribeReplicationSubnetGroups")
.body("{\"MaxRecords\":" + outOfRange + "}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidParameterValueException"));
}
}
@Test
void createRejectsADescriptionCarryingAControlCharacter() {
dms("CreateReplicationSubnetGroup")
.body("{\"ReplicationSubnetGroupIdentifier\":\"tf-control-char\","
+ "\"ReplicationSubnetGroupDescription\":\"terraform\\u0001managed\","
+ "\"SubnetIds\":[\"" + SUBNET_A + "\",\"" + SUBNET_B + "\"]}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidParameterValueException"));
dms("DescribeReplicationSubnetGroups")
.body("{\"Filters\":[{\"Name\":\"replication-subnet-group-id\","
+ "\"Values\":[\"tf-control-char\"]}]}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ResourceNotFoundFault"));
}
@Test
void createRejectsADuplicateIdentifier() {
dms("CreateReplicationSubnetGroup")
.body(createBody("tf-duplicate"))
.when()
.post("/")
.then()
.statusCode(200);
dms("CreateReplicationSubnetGroup")
.body(createBody("tf-duplicate"))
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ResourceAlreadyExistsFault"));
dms("DeleteReplicationSubnetGroup")
.body("{\"ReplicationSubnetGroupIdentifier\":\"tf-duplicate\"}")
.when()
.post("/")
.then()
.statusCode(200);
}
@Test
void createRejectsSubnetsInASingleAvailabilityZone() {
dms("CreateReplicationSubnetGroup")
.body("{\"ReplicationSubnetGroupIdentifier\":\"tf-one-az\","
+ "\"ReplicationSubnetGroupDescription\":\"terraform managed\","
+ "\"SubnetIds\":[\"" + SUBNET_A + "\"]}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ReplicationSubnetGroupDoesNotCoverEnoughAZs"));
}
@Test
void createRejectsAnUnknownSubnet() {
dms("CreateReplicationSubnetGroup")
.body("{\"ReplicationSubnetGroupIdentifier\":\"tf-bad-subnet\","
+ "\"ReplicationSubnetGroupDescription\":\"terraform managed\","
+ "\"SubnetIds\":[\"" + SUBNET_A + "\",\"subnet-does-not-exist\"]}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidSubnet"));
}
@Test
void createRejectsAMissingDescription() {
dms("CreateReplicationSubnetGroup")
.body("{\"ReplicationSubnetGroupIdentifier\":\"tf-no-description\","
+ "\"SubnetIds\":[\"" + SUBNET_A + "\",\"" + SUBNET_B + "\"]}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidParameterValueException"));
}
@Test
void deleteOfAMissingSubnetGroupFaults() {
dms("DeleteReplicationSubnetGroup")
.body("{\"ReplicationSubnetGroupIdentifier\":\"tf-absent\"}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ResourceNotFoundFault"));
}
@Test
void aTagValueOfTheWrongTypeIsASerializationErrorOnTheWire() {
dms("CreateReplicationSubnetGroup")
.body("{\"ReplicationSubnetGroupIdentifier\":\"tf-object-tag\","
+ "\"ReplicationSubnetGroupDescription\":\"terraform managed\","
+ "\"SubnetIds\":[\"" + SUBNET_A + "\",\"" + SUBNET_B + "\"],"
+ "\"Tags\":[{\"Key\":\"env\",\"Value\":{\"nested\":1}}]}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("SerializationException"));
dms("DescribeReplicationSubnetGroups")
.body("{\"Filters\":[{\"Name\":\"replication-subnet-group-id\","
+ "\"Values\":[\"tf-object-tag\"]}]}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ResourceNotFoundFault"));
}
@Test
void unsupportedDmsActionReportsUnknownOperation() {
dms("CreateReplicationInstance")
.body("{}")
.when()
.post("/")
.then()
.statusCode(404)
.body("__type", equalTo("UnknownOperationException"));
}
@Test
void tagsSurviveCreateAndAreReadableThroughListTagsForResource() {
dms("CreateReplicationSubnetGroup")
.body("{\"ReplicationSubnetGroupIdentifier\":\"tf-tagged\","
+ "\"ReplicationSubnetGroupDescription\":\"terraform managed\","
+ "\"SubnetIds\":[\"" + SUBNET_A + "\",\"" + SUBNET_B + "\"],"
+ "\"Tags\":[{\"Key\":\"env\",\"Value\":\"test\"}]}")
.when()
.post("/")
.then()
.statusCode(200);
dms("ListTagsForResource")
.body("{\"ResourceArn\":\"" + arn("tf-tagged") + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("TagList", hasSize(1))
.body("TagList[0].Key", equalTo("env"))
.body("TagList[0].Value", equalTo("test"))
.body("TagList[0].ResourceArn", nullValue());
dms("AddTagsToResource")
.body("{\"ResourceArn\":\"" + arn("tf-tagged") + "\","
+ "\"Tags\":[{\"Key\":\"owner\",\"Value\":\"data\"}]}")
.when()
.post("/")
.then()
.statusCode(200);
dms("ListTagsForResource")
.body("{\"ResourceArn\":\"" + arn("tf-tagged") + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("TagList.Key", containsInAnyOrder("env", "owner"));
dms("RemoveTagsFromResource")
.body("{\"ResourceArn\":\"" + arn("tf-tagged") + "\",\"TagKeys\":[\"env\"]}")
.when()
.post("/")
.then()
.statusCode(200);
dms("ListTagsForResource")
.body("{\"ResourceArnList\":[\"" + arn("tf-tagged") + "\"]}")
.when()
.post("/")
.then()
.statusCode(200)
.body("TagList", hasSize(1))
.body("TagList[0].Key", equalTo("owner"))
.body("TagList[0].ResourceArn", equalTo(arn("tf-tagged")));
dms("DeleteReplicationSubnetGroup")
.body("{\"ReplicationSubnetGroupIdentifier\":\"tf-tagged\"}")
.when()
.post("/")
.then()
.statusCode(200);
}
@Test
void listTagsForAnUnknownArnFaults() {
dms("ListTagsForResource")
.body("{\"ResourceArn\":\"" + arn("tf-never-created") + "\"}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ResourceNotFoundFault"));
}
private static String arn(String identifier) {
return "arn:aws:dms:us-east-1:" + ACCOUNT_ID + ":subgrp:" + identifier;
}
private static String createBody(String identifier) {
return "{\"ReplicationSubnetGroupIdentifier\":\"" + identifier + "\","
+ "\"ReplicationSubnetGroupDescription\":\"terraform managed\","
+ "\"SubnetIds\":[\"" + SUBNET_A + "\",\"" + SUBNET_B + "\"]}";
}
private static RequestSpecification dms(String action) {
return given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", TARGET_PREFIX + action)
.header("Authorization", AUTH_HEADER);
}
}
@@ -0,0 +1,573 @@
package io.github.hectorvent.floci.services.dms;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.dms.model.ReplicationSubnetGroup;
import io.github.hectorvent.floci.services.dms.model.ResourceTag;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Subnet;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class DmsServiceTest {
private static final String REGION = "us-east-1";
private static final String ACCOUNT_ID = "123456789012";
private static final String VPC_ID = "vpc-0dms";
private final ObjectMapper mapper = new ObjectMapper();
private DmsService service;
@BeforeEach
@SuppressWarnings({"unchecked", "rawtypes"})
void setUp() {
StorageFactory storageFactory = mock(StorageFactory.class);
AccountAwareStorageBackend<ReplicationSubnetGroup> store =
AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
when(storageFactory.create(eq("dms"), eq("dms-replication-subnet-groups.json"), any(TypeReference.class)))
.thenReturn((AccountAwareStorageBackend) store);
Ec2Service ec2Service = mock(Ec2Service.class);
when(ec2Service.describeSubnets(eq(REGION), anyList(), anyMap())).thenAnswer(invocation -> {
List<String> requested = invocation.getArgument(1);
return requested.stream().map(DmsServiceTest::subnet).filter(Objects::nonNull).toList();
});
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn(ACCOUNT_ID);
service = new DmsService(storageFactory, ec2Service, regionResolver);
}
@Test
void createStoresGroupUnderLowercasedIdentifier() {
service.createReplicationSubnetGroup(
createRequest("Tf-Example", "example", "subnet-a", "subnet-b"), REGION);
List<ReplicationSubnetGroup> described =
service.describeReplicationSubnetGroups(filterRequest("tf-example"), REGION).items();
assertEquals(1, described.size());
ReplicationSubnetGroup group = described.getFirst();
assertEquals("tf-example", group.getReplicationSubnetGroupIdentifier());
assertEquals("example", group.getReplicationSubnetGroupDescription());
assertEquals(VPC_ID, group.getVpcId());
assertEquals("Complete", group.getSubnetGroupStatus());
assertEquals(List.of("subnet-a", "subnet-b"), group.getSubnetIds());
assertEquals(Map.of("subnet-a", "us-east-1a", "subnet-b", "us-east-1b"),
group.getSubnetAvailabilityZones());
assertEquals(List.of("IPV4"), group.getSupportedNetworkTypes());
}
@Test
void createRejectsDuplicateIdentifierRegardlessOfCase() {
service.createReplicationSubnetGroup(createRequest("dup", "first", "subnet-a", "subnet-b"), REGION);
AwsException duplicate = assertThrows(AwsException.class, () -> service.createReplicationSubnetGroup(
createRequest("DUP", "second", "subnet-a", "subnet-b"), REGION));
assertEquals("ResourceAlreadyExistsFault", duplicate.getErrorCode());
}
@Test
void createRejectsUnknownSubnets() {
AwsException invalid = assertThrows(AwsException.class, () -> service.createReplicationSubnetGroup(
createRequest("bad-subnets", "example", "subnet-a", "subnet-missing"), REGION));
assertEquals("InvalidSubnet", invalid.getErrorCode());
}
@Test
void createRejectsSubnetsInASingleAvailabilityZone() {
AwsException tooFewZones = assertThrows(AwsException.class, () -> service.createReplicationSubnetGroup(
createRequest("one-az", "example", "subnet-a"), REGION));
assertEquals("ReplicationSubnetGroupDoesNotCoverEnoughAZs", tooFewZones.getErrorCode());
}
@Test
void createRejectsTwoSubnetsSharingOneAvailabilityZone() {
AwsException tooFewZones = assertThrows(AwsException.class, () -> service.createReplicationSubnetGroup(
createRequest("same-az", "example", "subnet-a", "subnet-a2"), REGION));
assertEquals("ReplicationSubnetGroupDoesNotCoverEnoughAZs", tooFewZones.getErrorCode());
}
@Test
void createRejectsASubnetWithNoAvailabilityZone() {
AwsException noZone = assertThrows(AwsException.class, () -> service.createReplicationSubnetGroup(
createRequest("no-az", "example", "subnet-a", "subnet-no-az"), REGION));
assertEquals("InvalidSubnet", noZone.getErrorCode());
}
@Test
void createRejectsSubnetsSpanningVpcs() {
AwsException crossVpc = assertThrows(AwsException.class, () -> service.createReplicationSubnetGroup(
createRequest("cross-vpc", "example", "subnet-a", "subnet-other-vpc"), REGION));
assertEquals("InvalidSubnet", crossVpc.getErrorCode());
}
@Test
void createRejectsMissingDescription() {
ObjectNode request = createRequest("no-description", null, "subnet-a", "subnet-b");
AwsException missing = assertThrows(AwsException.class,
() -> service.createReplicationSubnetGroup(request, REGION));
assertEquals("InvalidParameterValueException", missing.getErrorCode());
}
@Test
void createRejectsReservedDefaultIdentifier() {
AwsException reserved = assertThrows(AwsException.class, () -> service.createReplicationSubnetGroup(
createRequest("Default", "example", "subnet-a", "subnet-b"), REGION));
assertEquals("InvalidParameterValueException", reserved.getErrorCode());
}
@Test
void describeIsScopedToTheRequestRegion() {
service.createReplicationSubnetGroup(createRequest("scoped", "example", "subnet-a", "subnet-b"), REGION);
assertTrue(service.describeReplicationSubnetGroups(mapper.createObjectNode(), "eu-west-1")
.items().isEmpty());
}
@Test
void describeOfMissingGroupFaults() {
AwsException notFound = assertThrows(AwsException.class,
() -> service.describeReplicationSubnetGroups(filterRequest("absent"), REGION));
assertEquals("ResourceNotFoundFault", notFound.getErrorCode());
}
@Test
void describeWalksEveryGroupAcrossPagesWithoutDuplicatesOrOmissions() {
List<String> created = createGroups(25);
PaginatedResult<ReplicationSubnetGroup> first =
service.describeReplicationSubnetGroups(pageRequest(20, null), REGION);
assertEquals(20, first.items().size());
assertNotNull(first.nextToken());
PaginatedResult<ReplicationSubnetGroup> second =
service.describeReplicationSubnetGroups(pageRequest(20, first.nextToken()), REGION);
assertEquals(5, second.items().size());
assertNull(second.nextToken(), "the final page must not carry a Marker");
List<String> walked = new ArrayList<>();
first.items().forEach(group -> walked.add(group.getReplicationSubnetGroupIdentifier()));
second.items().forEach(group -> walked.add(group.getReplicationSubnetGroupIdentifier()));
assertEquals(created, walked);
}
@Test
void describeResumesAfterTheMarkerEvenWhenAnEarlierGroupIsDeleted() {
createGroups(25);
PaginatedResult<ReplicationSubnetGroup> first =
service.describeReplicationSubnetGroups(pageRequest(20, null), REGION);
service.deleteReplicationSubnetGroup(identifierRequest("page-00"), REGION);
PaginatedResult<ReplicationSubnetGroup> second =
service.describeReplicationSubnetGroups(pageRequest(20, first.nextToken()), REGION);
assertEquals(List.of("page-20", "page-21", "page-22", "page-23", "page-24"),
second.items().stream().map(ReplicationSubnetGroup::getReplicationSubnetGroupIdentifier).toList());
}
@Test
void describeDefaultsToOneHundredRecordsPerPage() {
createGroups(25);
PaginatedResult<ReplicationSubnetGroup> page =
service.describeReplicationSubnetGroups(mapper.createObjectNode(), REGION);
assertEquals(25, page.items().size());
assertNull(page.nextToken());
}
@Test
void describeRejectsMaxRecordsBelowTwenty() {
AwsException tooSmall = assertThrows(AwsException.class,
() -> service.describeReplicationSubnetGroups(pageRequest(19, null), REGION));
assertEquals("InvalidParameterValueException", tooSmall.getErrorCode());
}
@Test
void describeRejectsMaxRecordsAboveOneHundred() {
AwsException tooLarge = assertThrows(AwsException.class,
() -> service.describeReplicationSubnetGroups(pageRequest(101, null), REGION));
assertEquals("InvalidParameterValueException", tooLarge.getErrorCode());
}
@Test
void describeRejectsMaxRecordsThatIsNotAnInteger() {
ObjectNode request = mapper.createObjectNode();
request.put("MaxRecords", "20");
AwsException wrongType = assertThrows(AwsException.class,
() -> service.describeReplicationSubnetGroups(request, REGION));
assertEquals("SerializationException", wrongType.getErrorCode());
}
@Test
void createRejectsADescriptionCarryingAControlCharacter() {
ObjectNode request = createRequest("control-char", "terraform\u0001managed", "subnet-a", "subnet-b");
AwsException nonPrintable = assertThrows(AwsException.class,
() -> service.createReplicationSubnetGroup(request, REGION));
assertEquals("InvalidParameterValueException", nonPrintable.getErrorCode());
AwsException notStored = assertThrows(AwsException.class,
() -> service.describeReplicationSubnetGroups(filterRequest("control-char"), REGION));
assertEquals("ResourceNotFoundFault", notStored.getErrorCode());
}
@Test
void deleteRemovesTheGroup() {
service.createReplicationSubnetGroup(createRequest("doomed", "example", "subnet-a", "subnet-b"), REGION);
service.deleteReplicationSubnetGroup(identifierRequest("doomed"), REGION);
AwsException notFound = assertThrows(AwsException.class,
() -> service.describeReplicationSubnetGroups(filterRequest("doomed"), REGION));
assertEquals("ResourceNotFoundFault", notFound.getErrorCode());
}
@Test
void deleteOfMissingGroupFaults() {
AwsException notFound = assertThrows(AwsException.class,
() -> service.deleteReplicationSubnetGroup(identifierRequest("absent"), REGION));
assertEquals("ResourceNotFoundFault", notFound.getErrorCode());
}
@Test
void clearRemovesPersistedState() {
service.createReplicationSubnetGroup(createRequest("reset-me", "example", "subnet-a", "subnet-b"), REGION);
service.clear();
assertTrue(service.describeReplicationSubnetGroups(mapper.createObjectNode(), REGION).items().isEmpty());
}
@Test
void createStoresTagsReadableThroughListTagsForResource() {
ObjectNode request = createRequest("tagged", "example", "subnet-a", "subnet-b");
tagList(request.putArray("Tags"), "env", "test", "team", "platform");
service.createReplicationSubnetGroup(request, REGION);
assertEquals(Map.of("env", "test", "team", "platform"), tagsOf("tagged"));
}
@Test
void addTagsMergesWithExistingTagsAndOverwritesByKey() {
ObjectNode request = createRequest("merge-me", "example", "subnet-a", "subnet-b");
tagList(request.putArray("Tags"), "env", "test");
service.createReplicationSubnetGroup(request, REGION);
ObjectNode addRequest = mapper.createObjectNode();
addRequest.put("ResourceArn", arn("merge-me"));
tagList(addRequest.putArray("Tags"), "env", "prod", "owner", "data");
service.addTagsToResource(addRequest, REGION);
assertEquals(Map.of("env", "prod", "owner", "data"), tagsOf("merge-me"));
}
@Test
void removeTagsDropsOnlyTheNamedKeys() {
ObjectNode request = createRequest("trim-me", "example", "subnet-a", "subnet-b");
tagList(request.putArray("Tags"), "env", "test", "owner", "data");
service.createReplicationSubnetGroup(request, REGION);
ObjectNode removeRequest = mapper.createObjectNode();
removeRequest.put("ResourceArn", arn("trim-me"));
removeRequest.putArray("TagKeys").add("env").add("absent-key");
service.removeTagsFromResource(removeRequest, REGION);
assertEquals(Map.of("owner", "data"), tagsOf("trim-me"));
}
@Test
void listTagsByArnListCarriesTheOwningArnOnEachTag() {
ObjectNode request = createRequest("arn-list", "example", "subnet-a", "subnet-b");
tagList(request.putArray("Tags"), "env", "test");
service.createReplicationSubnetGroup(request, REGION);
ObjectNode listRequest = mapper.createObjectNode();
listRequest.putArray("ResourceArnList").add(arn("arn-list"));
List<ResourceTag> tags = service.listTagsForResource(listRequest, REGION);
assertEquals(List.of(new ResourceTag(arn("arn-list"), "env", "test")), tags);
}
@Test
void listTagsBySingleArnOmitsTheArnOnEachTag() {
ObjectNode request = createRequest("single-arn", "example", "subnet-a", "subnet-b");
tagList(request.putArray("Tags"), "env", "test");
service.createReplicationSubnetGroup(request, REGION);
assertEquals(List.of(new ResourceTag(null, "env", "test")),
service.listTagsForResource(arnRequest("single-arn"), REGION));
}
@Test
void tagOperationsOnAnUnknownArnFault() {
String missing = "arn:aws:dms:" + REGION + ":" + ACCOUNT_ID + ":subgrp:not-there";
ObjectNode listRequest = mapper.createObjectNode();
listRequest.put("ResourceArn", missing);
AwsException notFound = assertThrows(AwsException.class,
() -> service.listTagsForResource(listRequest, REGION));
assertEquals("ResourceNotFoundFault", notFound.getErrorCode());
}
@Test
void tagOperationsOnAnotherAccountsArnFault() {
ObjectNode request = createRequest("other-account", "example", "subnet-a", "subnet-b");
tagList(request.putArray("Tags"), "env", "test");
service.createReplicationSubnetGroup(request, REGION);
ObjectNode listRequest = mapper.createObjectNode();
listRequest.put("ResourceArn", "arn:aws:dms:" + REGION + ":999999999999:subgrp:other-account");
AwsException notFound = assertThrows(AwsException.class,
() -> service.listTagsForResource(listRequest, REGION));
assertEquals("ResourceNotFoundFault", notFound.getErrorCode());
}
@Test
void tagOperationsOnAnotherRegionsArnFault() {
ObjectNode request = createRequest("other-region", "example", "subnet-a", "subnet-b");
tagList(request.putArray("Tags"), "env", "test");
service.createReplicationSubnetGroup(request, REGION);
ObjectNode listRequest = mapper.createObjectNode();
listRequest.put("ResourceArn", "arn:aws:dms:eu-west-1:" + ACCOUNT_ID + ":subgrp:other-region");
AwsException notFound = assertThrows(AwsException.class,
() -> service.listTagsForResource(listRequest, REGION));
assertEquals("ResourceNotFoundFault", notFound.getErrorCode());
}
@Test
void tagOperationsOnANonSubnetGroupArnFault() {
ObjectNode listRequest = mapper.createObjectNode();
listRequest.put("ResourceArn", "arn:aws:dms:" + REGION + ":" + ACCOUNT_ID + ":rep:Example");
AwsException notFound = assertThrows(AwsException.class,
() -> service.listTagsForResource(listRequest, REGION));
assertEquals("ResourceNotFoundFault", notFound.getErrorCode());
}
@Test
void reservedTagKeyPrefixesAreRejected() {
ObjectNode request = createRequest("reserved-tag", "example", "subnet-a", "subnet-b");
tagList(request.putArray("Tags"), "aws:created-by", "someone");
AwsException reserved = assertThrows(AwsException.class,
() -> service.createReplicationSubnetGroup(request, REGION));
assertEquals("InvalidParameterValueException", reserved.getErrorCode());
}
@Test
void deletingAGroupDropsItsTags() {
ObjectNode request = createRequest("tags-die", "example", "subnet-a", "subnet-b");
tagList(request.putArray("Tags"), "env", "test");
service.createReplicationSubnetGroup(request, REGION);
service.deleteReplicationSubnetGroup(identifierRequest("tags-die"), REGION);
AwsException notFound = assertThrows(AwsException.class,
() -> service.listTagsForResource(arnRequest("tags-die"), REGION));
assertEquals("ResourceNotFoundFault", notFound.getErrorCode());
}
@Test
void createRejectsATagValueThatIsNotAString() {
ObjectNode request = createRequest("object-tag-value", "example", "subnet-a", "subnet-b");
request.putArray("Tags").addObject().put("Key", "env").putObject("Value").put("nested", 1);
AwsException wrongType = assertThrows(AwsException.class,
() -> service.createReplicationSubnetGroup(request, REGION));
assertEquals("SerializationException", wrongType.getErrorCode());
}
@Test
void createRejectsATagsMemberThatIsNotAList() {
ObjectNode request = createRequest("scalar-tags", "example", "subnet-a", "subnet-b");
request.put("Tags", "env=test");
AwsException wrongType = assertThrows(AwsException.class,
() -> service.createReplicationSubnetGroup(request, REGION));
assertEquals("SerializationException", wrongType.getErrorCode());
}
@Test
void createRejectsASubnetIdThatIsNotAString() {
ObjectNode request = identifierRequest("numeric-subnet");
request.put("ReplicationSubnetGroupDescription", "example");
request.putArray("SubnetIds").add("subnet-a").add(42);
AwsException wrongType = assertThrows(AwsException.class,
() -> service.createReplicationSubnetGroup(request, REGION));
assertEquals("SerializationException", wrongType.getErrorCode());
}
@Test
void describeRejectsFilterValuesThatAreNotAList() {
ObjectNode request = mapper.createObjectNode();
ObjectNode filter = request.putArray("Filters").addObject();
filter.put("Name", "replication-subnet-group-id");
filter.put("Values", "tf-example");
AwsException wrongType = assertThrows(AwsException.class,
() -> service.describeReplicationSubnetGroups(request, REGION));
assertEquals("SerializationException", wrongType.getErrorCode());
}
@Test
void describeRejectsAFilterValueThatIsNotAString() {
ObjectNode request = mapper.createObjectNode();
ObjectNode filter = request.putArray("Filters").addObject();
filter.put("Name", "replication-subnet-group-id");
filter.putArray("Values").addObject().put("Value", "tf-example");
AwsException wrongType = assertThrows(AwsException.class,
() -> service.describeReplicationSubnetGroups(request, REGION));
assertEquals("SerializationException", wrongType.getErrorCode());
}
@Test
void listTagsRejectsAnArnListElementThatIsNotAString() {
ObjectNode request = mapper.createObjectNode();
request.putArray("ResourceArnList").addObject().put("ResourceArn", arn("whatever"));
AwsException wrongType = assertThrows(AwsException.class,
() -> service.listTagsForResource(request, REGION));
assertEquals("SerializationException", wrongType.getErrorCode());
}
@Test
void removeTagsRejectsATagKeyThatIsNotAString() {
ObjectNode request = createRequest("bad-tag-keys", "example", "subnet-a", "subnet-b");
service.createReplicationSubnetGroup(request, REGION);
ObjectNode removeRequest = mapper.createObjectNode();
removeRequest.put("ResourceArn", arn("bad-tag-keys"));
removeRequest.putArray("TagKeys").add(7);
AwsException wrongType = assertThrows(AwsException.class,
() -> service.removeTagsFromResource(removeRequest, REGION));
assertEquals("SerializationException", wrongType.getErrorCode());
}
@Test
void createRejectsAnIdentifierThatIsNotAString() {
ObjectNode request = mapper.createObjectNode();
request.put("ReplicationSubnetGroupIdentifier", 12345);
request.put("ReplicationSubnetGroupDescription", "example");
request.putArray("SubnetIds").add("subnet-a").add("subnet-b");
AwsException wrongType = assertThrows(AwsException.class,
() -> service.createReplicationSubnetGroup(request, REGION));
assertEquals("SerializationException", wrongType.getErrorCode());
}
private Map<String, String> tagsOf(String identifier) {
return service.listTagsForResource(arnRequest(identifier), REGION).stream()
.collect(Collectors.toMap(ResourceTag::key, ResourceTag::value));
}
private ObjectNode arnRequest(String identifier) {
ObjectNode request = mapper.createObjectNode();
request.put("ResourceArn", arn(identifier));
return request;
}
private String arn(String identifier) {
return "arn:aws:dms:" + REGION + ":" + ACCOUNT_ID + ":subgrp:" + identifier;
}
private static void tagList(ArrayNode tags, String... keysAndValues) {
for (int i = 0; i < keysAndValues.length; i += 2) {
tags.addObject().put("Key", keysAndValues[i]).put("Value", keysAndValues[i + 1]);
}
}
private ObjectNode createRequest(String identifier, String description, String... subnetIds) {
ObjectNode request = identifierRequest(identifier);
if (description != null) {
request.put("ReplicationSubnetGroupDescription", description);
}
ArrayNode subnets = request.putArray("SubnetIds");
for (String subnetId : subnetIds) {
subnets.add(subnetId);
}
return request;
}
private ObjectNode identifierRequest(String identifier) {
ObjectNode request = mapper.createObjectNode();
request.put("ReplicationSubnetGroupIdentifier", identifier);
return request;
}
private List<String> createGroups(int count) {
List<String> identifiers = new ArrayList<>();
for (int i = 0; i < count; i++) {
String identifier = String.format("page-%02d", i);
service.createReplicationSubnetGroup(
createRequest(identifier, "example", "subnet-a", "subnet-b"), REGION);
identifiers.add(identifier);
}
return identifiers;
}
private ObjectNode pageRequest(int maxRecords, String marker) {
ObjectNode request = mapper.createObjectNode();
request.put("MaxRecords", maxRecords);
if (marker != null) {
request.put("Marker", marker);
}
return request;
}
private ObjectNode filterRequest(String identifier) {
ObjectNode request = mapper.createObjectNode();
ObjectNode filter = request.putArray("Filters").addObject();
filter.put("Name", "replication-subnet-group-id");
filter.putArray("Values").add(identifier);
return request;
}
private static Subnet subnet(String subnetId) {
return switch (subnetId) {
case "subnet-a" -> subnet(subnetId, VPC_ID, "us-east-1a");
case "subnet-b" -> subnet(subnetId, VPC_ID, "us-east-1b");
case "subnet-a2" -> subnet(subnetId, VPC_ID, "us-east-1a");
case "subnet-no-az" -> subnet(subnetId, VPC_ID, null);
case "subnet-other-vpc" -> subnet(subnetId, "vpc-0other", "us-east-1b");
default -> null;
};
}
private static Subnet subnet(String subnetId, String vpcId, String availabilityZone) {
Subnet subnet = new Subnet();
subnet.setSubnetId(subnetId);
subnet.setVpcId(vpcId);
subnet.setAvailabilityZone(availabilityZone);
subnet.setRegion(REGION);
return subnet;
}
}
+2
View File
@@ -510,3 +510,5 @@ floci:
enabled: true
marketplace:
enabled: true
dms:
enabled: true
+4
View File
@@ -179,6 +179,10 @@ services:
doc: docs/services/detective.md
sources:
- { path: src/main/java/io/github/hectorvent/floci/services/detective/DetectiveController.java, mode: rest }
- service: dms
doc: docs/services/dms.md
sources:
- { path: src/main/java/io/github/hectorvent/floci/services/dms/DmsJsonHandler.java, mode: switch }
- service: docdb
doc: docs/services/docdb.md
sources: