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

@@ -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"));
}
}
}