diff --git a/docs/services/dms.md b/docs/services/dms.md index c817223354..09270bbcea 100644 --- a/docs/services/dms.md +++ b/docs/services/dms.md @@ -51,7 +51,9 @@ those subnets actually have. No data is migrated: the resources are control-plan group is one DMS manages for a zero-ETL integration, which Floci does not emulate. - The `default` group is synthesised from the region's default-VPC subnets, and only exists once `CreateReplicationInstance` has put an instance in it (an instance created - without `ReplicationSubnetGroupIdentifier`), as AWS creates it the first time it is needed. + without `ReplicationSubnetGroupIdentifier`, or naming `default` explicitly, which is + accepted whenever the region has default subnets), as AWS creates it the first time it + is needed. Until then it is neither listed nor found by the filter. Unlike AWS, it disappears again once no instance in the region uses it. - `DescribeReplicationSubnetGroups` supports the `replication-subnet-group-id` filter. diff --git a/src/main/java/io/github/hectorvent/floci/services/dms/DmsService.java b/src/main/java/io/github/hectorvent/floci/services/dms/DmsService.java index 831e9e8479..7d65aa597c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dms/DmsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dms/DmsService.java @@ -205,19 +205,30 @@ public class DmsService implements Resettable { putText(attributes, request, "KmsKeyId"); putText(attributes, request, "DnsNameServers"); + applyInstanceMembers(attributes, request); + // Resolved only once every other check has passed: resolving "default" reads the region's + // subnets, which seeds its default VPC, and a rejected request must not leave that behind. + return create(instances, region, identifier, attributes, request, + () -> attributes.putObject("ReplicationSubnetGroup") + .put("ReplicationSubnetGroupIdentifier", resolveInstanceSubnetGroup(request, region))); + } + + private String resolveInstanceSubnetGroup(JsonNode request, String region) { String subnetGroup = text(request, "ReplicationSubnetGroupIdentifier"); if (subnetGroup == null) { // DMS falls back to the account's "default" group, which is why that name is reserved. - subnetGroup = "default"; - } else { - subnetGroup = subnetGroup.toLowerCase(Locale.ROOT); - if (subnetGroups.get(storageKey(region, subnetGroup)).isEmpty()) { - throw notFound(subnetGroup); - } + return "default"; } - attributes.putObject("ReplicationSubnetGroup").put("ReplicationSubnetGroupIdentifier", subnetGroup); - applyInstanceMembers(attributes, request); - return create(instances, region, identifier, attributes, request); + subnetGroup = subnetGroup.toLowerCase(Locale.ROOT); + // Naming "default" explicitly is how it comes into existence: AWS creates it from the + // default-VPC subnets on first use, so it is not stored and is not yet listed here. + boolean exists = "default".equals(subnetGroup) + ? defaultSubnetGroup(region, subnetGroup).isPresent() + : subnetGroups.get(storageKey(region, subnetGroup)).isPresent(); + if (!exists) { + throw notFound(subnetGroup); + } + return subnetGroup; } public PaginatedResult describeReplicationInstances(JsonNode request, String region) { @@ -261,6 +272,10 @@ public class DmsService implements Resettable { if (!subnetGroupInUse(region, identifier)) { return Optional.empty(); } + return defaultSubnetGroup(region, identifier); + } + + private Optional defaultSubnetGroup(String region, String identifier) { List defaultSubnets = ec2Service.describeSubnets(region, List.of(), Map.of()).stream() .filter(Subnet::isDefaultForAz) .map(Subnet::getSubnetId) @@ -459,6 +474,12 @@ public class DmsService implements Resettable { private DmsResource create(Kind kind, String region, String identifier, ObjectNode attributes, JsonNode request) { + return create(kind, region, identifier, attributes, request, () -> { }); + } + + /** {@code beforeStore} runs after every create-time check, so its side effects never follow a rejection. */ + private DmsResource create(Kind kind, String region, String identifier, ObjectNode attributes, + JsonNode request, Runnable beforeStore) { if (findByIdentifier(kind, region, identifier).isPresent()) { throw new AwsException("ResourceAlreadyExistsFault", kind.label() + " " + identifier + " already exists.", 400); @@ -476,11 +497,13 @@ public class DmsService implements Resettable { throw new AwsException("ResourceAlreadyExistsFault", "ResourceIdentifier " + resourceId + " is already in use.", 400); } + Map tags = readTags(request.get("Tags")); + beforeStore.run(); attributes.put(kind.idMember(), identifier); attributes.put(kind.arnMember(), regionResolver.buildArn("dms", region, kind.arnType() + ":" + resourceId)); DmsResource resource = new DmsResource(); resource.setAttributes(attributes); - resource.setTags(readTags(request.get("Tags"))); + resource.setTags(tags); kind.store().put(key, resource); return resource; } diff --git a/src/test/java/io/github/hectorvent/floci/services/dms/DmsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dms/DmsIntegrationTest.java index 0b436c38da..b3e84e4b14 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dms/DmsIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dms/DmsIntegrationTest.java @@ -205,6 +205,34 @@ class DmsIntegrationTest { .body("ReplicationSubnetGroups[0].VpcId", equalTo("vpc-default-ap-southeast-2")); } + /** + * Naming {@code default} explicitly is how an instance brings it into use, so it is accepted + * before it is listed, and listed afterwards. Own region for the same reason as above. + */ + @Test + void createInstanceNamingDefaultExplicitlyBringsTheDefaultGroupIntoUse() { + String region = "ca-central-1"; + dms("CreateReplicationInstance", region) + .body("{\"ReplicationInstanceIdentifier\":\"tf-explicit-default\"," + + "\"ReplicationInstanceClass\":\"dms.t3.micro\"," + + "\"ReplicationSubnetGroupIdentifier\":\"Default\"}") + .when() + .post("/") + .then() + .statusCode(200) + .body("ReplicationInstance.ReplicationSubnetGroup.ReplicationSubnetGroupIdentifier", + equalTo("default")); + + dms("DescribeReplicationSubnetGroups", region) + .body("{\"Filters\":[{\"Name\":\"replication-subnet-group-id\",\"Values\":[\"default\"]}]}") + .when() + .post("/") + .then() + .statusCode(200) + .body("ReplicationSubnetGroups", hasSize(1)) + .body("ReplicationSubnetGroups[0].VpcId", equalTo("vpc-default-ca-central-1")); + } + @Test void describeRejectsMaxRecordsOutsideTheDocumentedRange() { for (int outOfRange : new int[] {19, 101}) { diff --git a/src/test/java/io/github/hectorvent/floci/services/dms/DmsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dms/DmsServiceTest.java index 7c9acfc8d2..5ffde55b44 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dms/DmsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dms/DmsServiceTest.java @@ -9,6 +9,7 @@ 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.DmsResource; 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; @@ -210,6 +211,53 @@ class DmsServiceTest { assertEquals("ResourceNotFoundFault", notFound.getErrorCode()); } + @Test + void createInstanceNamingDefaultExplicitlyBringsTheDefaultGroupIntoUse() { + useDefaultVpc(); + + DmsResource instance = service.createReplicationInstance(instanceRequest("Default"), REGION); + + assertEquals("default", instance.getAttributes() + .path("ReplicationSubnetGroup").path("ReplicationSubnetGroupIdentifier").asText()); + List described = + service.describeReplicationSubnetGroups(filterRequest("default"), REGION).items(); + assertEquals(1, described.size()); + assertEquals(List.of("subnet-a", "subnet-b"), described.getFirst().getSubnetIds()); + } + + @Test + void createInstanceNamingDefaultFaultsWhenTheRegionHasNoDefaultSubnets() { + AwsException notFound = assertThrows(AwsException.class, + () -> service.createReplicationInstance(instanceRequest("default"), REGION)); + assertEquals("ResourceNotFoundFault", notFound.getErrorCode()); + assertTrue(service.describeReplicationSubnetGroups(mapper.createObjectNode(), REGION).items().isEmpty()); + } + + @Test + void rejectedCreateNamingDefaultNeverReadsTheRegionsSubnets() { + useDefaultVpc(); + ObjectNode first = instanceRequest("default"); + first.remove("ReplicationSubnetGroupIdentifier"); + service.createReplicationInstance(first, REGION); + + ObjectNode duplicate = instanceRequest("default"); + ObjectNode badResourceId = instanceRequest("default"); + badResourceId.put("ReplicationInstanceIdentifier", "bad-resource-id"); + badResourceId.put("ResourceIdentifier", "1-starts-with-a-digit"); + ObjectNode badTag = instanceRequest("default"); + badTag.put("ReplicationInstanceIdentifier", "bad-tag"); + badTag.putArray("Tags").addObject().put("Key", "aws:reserved").put("Value", "v"); + + assertEquals("ResourceAlreadyExistsFault", assertThrows(AwsException.class, + () -> service.createReplicationInstance(duplicate, REGION)).getErrorCode()); + assertEquals("InvalidParameterValueException", assertThrows(AwsException.class, + () -> service.createReplicationInstance(badResourceId, REGION)).getErrorCode()); + assertEquals("InvalidParameterValueException", assertThrows(AwsException.class, + () -> service.createReplicationInstance(badTag, REGION)).getErrorCode()); + // Reading the region's subnets is what seeds its default VPC in the real Ec2Service. + verify(ec2Service, never()).describeSubnets(eq(REGION), eq(List.of()), anyMap()); + } + @Test void describeOfMissingGroupFaults() { AwsException notFound = assertThrows(AwsException.class, @@ -616,6 +664,14 @@ class DmsServiceTest { service.createReplicationInstance(request, REGION); } + private ObjectNode instanceRequest(String subnetGroup) { + ObjectNode request = mapper.createObjectNode(); + request.put("ReplicationInstanceIdentifier", "names-default"); + request.put("ReplicationInstanceClass", "dms.t3.micro"); + request.put("ReplicationSubnetGroupIdentifier", subnetGroup); + return request; + } + private void useDefaultVpc() { Subnet a = subnet("subnet-a"); Subnet b = subnet("subnet-b");