mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dms): list the default replication subnet group (#5771)
* fix(dms): list the default replication subnet group DescribeReplicationSubnetGroups read stored groups only, so filtering on "default" faulted with ResourceNotFoundFault and the unfiltered list omitted the group that replication instances created without one report. Resolve both paths through findReplicationSubnetGroup, which synthesises "default" from the region's default-VPC subnets. * fix(dms): list the default subnet group only once an instance uses it AWS creates the default replication subnet group the first time an instance needs it, so a region with default subnets but no such instance has no default group. DescribeReplicationSubnetGroups now lists and resolves default only while a replication instance in the region was created without a subnet group, and checks that before calling EC2, so an unused default never seeds the region's default VPC.
This commit is contained in:
1 parent
7ecd225dd1
commit
23e1b3a97c
4 files changed
+166
-10
No files matched your search
@@ -144,6 +144,67 @@ class DmsIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs in its own region so no instance another test left behind already uses {@code default}.
|
||||
* AWS creates that group only when an instance first needs it, so before then listing shows
|
||||
* just the created group and filtering on {@code default} faults.
|
||||
*/
|
||||
@Test
|
||||
void describeListsTheDefaultGroupOnlyOnceAnInstanceUsesIt() {
|
||||
String region = "ap-southeast-2";
|
||||
dms("CreateReplicationSubnetGroup", region)
|
||||
.body("{\"ReplicationSubnetGroupIdentifier\":\"tf-default-probe\","
|
||||
+ "\"ReplicationSubnetGroupDescription\":\"terraform managed\","
|
||||
+ "\"SubnetIds\":[\"subnet-default-ap-southeast-2-a\",\"subnet-default-ap-southeast-2-b\"]}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
dms("DescribeReplicationSubnetGroups", region)
|
||||
.body("{}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ReplicationSubnetGroups.ReplicationSubnetGroupIdentifier", contains("tf-default-probe"));
|
||||
|
||||
dms("DescribeReplicationSubnetGroups", region)
|
||||
.body("{\"Filters\":[{\"Name\":\"replication-subnet-group-id\",\"Values\":[\"default\"]}]}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ResourceNotFoundFault"));
|
||||
|
||||
dms("CreateReplicationInstance", region)
|
||||
.body("{\"ReplicationInstanceIdentifier\":\"tf-default-user\","
|
||||
+ "\"ReplicationInstanceClass\":\"dms.t3.micro\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
dms("DescribeReplicationSubnetGroups", region)
|
||||
.body("{}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ReplicationSubnetGroups.ReplicationSubnetGroupIdentifier",
|
||||
contains("default", "tf-default-probe"));
|
||||
|
||||
dms("DescribeReplicationSubnetGroups", region)
|
||||
.body("{\"Filters\":[{\"Name\":\"replication-subnet-group-id\",\"Values\":[\"default\"]}]}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ReplicationSubnetGroups", hasSize(1))
|
||||
.body("ReplicationSubnetGroups[0].ReplicationSubnetGroupIdentifier", equalTo("default"))
|
||||
.body("ReplicationSubnetGroups[0].VpcId", equalTo("vpc-default-ap-southeast-2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeRejectsMaxRecordsOutsideTheDocumentedRange() {
|
||||
for (int outOfRange : new int[] {19, 101}) {
|
||||
@@ -373,9 +434,13 @@ class DmsIntegrationTest {
|
||||
}
|
||||
|
||||
private static RequestSpecification dms(String action) {
|
||||
return dms(action, "us-east-1");
|
||||
}
|
||||
|
||||
private static RequestSpecification dms(String action, String region) {
|
||||
return given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", TARGET_PREFIX + action)
|
||||
.header("Authorization", AUTH_HEADER);
|
||||
.header("Authorization", AUTH_HEADER.replace("/us-east-1/", "/" + region + "/"));
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,8 @@ import static org.mockito.ArgumentMatchers.anyMap;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class DmsServiceTest {
|
||||
@@ -42,6 +44,9 @@ class DmsServiceTest {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private DmsService service;
|
||||
private Ec2Service ec2Service;
|
||||
/** What an unfiltered DescribeSubnets returns; empty means the region has no default VPC. */
|
||||
private List<Subnet> regionSubnets = List.of();
|
||||
|
||||
@BeforeEach
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
@@ -54,9 +59,12 @@ class DmsServiceTest {
|
||||
when(storageFactory.create(eq("dms"), eq("dms-replication-subnet-groups.json"), any(TypeReference.class)))
|
||||
.thenReturn((AccountAwareStorageBackend) store);
|
||||
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
ec2Service = mock(Ec2Service.class);
|
||||
when(ec2Service.describeSubnets(eq(REGION), anyList(), anyMap())).thenAnswer(invocation -> {
|
||||
List<String> requested = invocation.getArgument(1);
|
||||
if (requested.isEmpty()) {
|
||||
return regionSubnets;
|
||||
}
|
||||
return requested.stream().map(DmsServiceTest::subnet).filter(Objects::nonNull).toList();
|
||||
});
|
||||
RegionResolver regionResolver = mock(RegionResolver.class);
|
||||
@@ -151,6 +159,57 @@ class DmsServiceTest {
|
||||
.items().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeFilteredOnDefaultReturnsTheDefaultVpcGroup() {
|
||||
useDefaultVpc();
|
||||
createInstanceWithoutSubnetGroup();
|
||||
|
||||
List<ReplicationSubnetGroup> described =
|
||||
service.describeReplicationSubnetGroups(filterRequest("default"), REGION).items();
|
||||
|
||||
assertEquals(1, described.size());
|
||||
ReplicationSubnetGroup group = described.getFirst();
|
||||
assertEquals("default", group.getReplicationSubnetGroupIdentifier());
|
||||
assertEquals(VPC_ID, group.getVpcId());
|
||||
assertEquals(List.of("subnet-a", "subnet-b"), group.getSubnetIds());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeListsTheDefaultGroupAlongsideCreatedOnes() {
|
||||
useDefaultVpc();
|
||||
createInstanceWithoutSubnetGroup();
|
||||
service.createReplicationSubnetGroup(createRequest("user-group", "example", "subnet-a", "subnet-b"), REGION);
|
||||
|
||||
List<String> listed = service.describeReplicationSubnetGroups(mapper.createObjectNode(), REGION).items()
|
||||
.stream().map(ReplicationSubnetGroup::getReplicationSubnetGroupIdentifier).toList();
|
||||
|
||||
assertEquals(List.of("default", "user-group"), listed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeOmitsTheDefaultGroupUntilAnInstanceUsesIt() {
|
||||
useDefaultVpc();
|
||||
service.createReplicationSubnetGroup(createRequest("user-group", "example", "subnet-a", "subnet-b"), REGION);
|
||||
|
||||
List<String> listed = service.describeReplicationSubnetGroups(mapper.createObjectNode(), REGION).items()
|
||||
.stream().map(ReplicationSubnetGroup::getReplicationSubnetGroupIdentifier).toList();
|
||||
assertEquals(List.of("user-group"), listed);
|
||||
AwsException notFound = assertThrows(AwsException.class,
|
||||
() -> service.describeReplicationSubnetGroups(filterRequest("default"), REGION));
|
||||
assertEquals("ResourceNotFoundFault", notFound.getErrorCode());
|
||||
// An unfiltered DescribeSubnets is what seeds the region's default VPC in EC2.
|
||||
verify(ec2Service, never()).describeSubnets(eq(REGION), eq(List.of()), anyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeOmitsTheDefaultGroupWhenTheRegionHasNoDefaultSubnets() {
|
||||
createInstanceWithoutSubnetGroup();
|
||||
assertTrue(service.describeReplicationSubnetGroups(mapper.createObjectNode(), REGION).items().isEmpty());
|
||||
AwsException notFound = assertThrows(AwsException.class,
|
||||
() -> service.describeReplicationSubnetGroups(filterRequest("default"), REGION));
|
||||
assertEquals("ResourceNotFoundFault", notFound.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeOfMissingGroupFaults() {
|
||||
AwsException notFound = assertThrows(AwsException.class,
|
||||
@@ -550,6 +609,21 @@ class DmsServiceTest {
|
||||
return identifiers;
|
||||
}
|
||||
|
||||
private void createInstanceWithoutSubnetGroup() {
|
||||
ObjectNode request = mapper.createObjectNode();
|
||||
request.put("ReplicationInstanceIdentifier", "uses-default");
|
||||
request.put("ReplicationInstanceClass", "dms.t3.micro");
|
||||
service.createReplicationInstance(request, REGION);
|
||||
}
|
||||
|
||||
private void useDefaultVpc() {
|
||||
Subnet a = subnet("subnet-a");
|
||||
Subnet b = subnet("subnet-b");
|
||||
a.setDefaultForAz(true);
|
||||
b.setDefaultForAz(true);
|
||||
regionSubnets = List.of(a, b);
|
||||
}
|
||||
|
||||
private ObjectNode pageRequest(int maxRecords, String marker) {
|
||||
ObjectNode request = mapper.createObjectNode();
|
||||
request.put("MaxRecords", maxRecords);
|
||||
|
||||
Reference in new issue
Block a user