mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 23:59:27 -05:00
fix(rds): align subnet group placement handling with AWS behavior (#1295)
* fix(rds): align subnet group placement handling with AWS behavior * fix(rds): align DB subnet group wire responses with AWS model * fix(rds): align subnet group placement validation with AWS * fix(rds): tighten subnet group placement validation * chore: rerun ci
This commit is contained in:
1 parent
82170dd081
commit
ad1cc22c19
9 files changed
+708
-142
No files matched your search
+17
-3
@@ -4,11 +4,15 @@ import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.DescribeSubnetsResponse;
|
||||
import software.amazon.awssdk.services.rds.RdsClient;
|
||||
import software.amazon.awssdk.services.rds.model.CreateDbSubnetGroupResponse;
|
||||
import software.amazon.awssdk.services.rds.model.DescribeDbSubnetGroupsResponse;
|
||||
import software.amazon.awssdk.services.rds.model.DescribeOrderableDbInstanceOptionsResponse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("RDS Control Plane")
|
||||
@@ -16,11 +20,21 @@ class RdsControlPlaneTest {
|
||||
|
||||
private static RdsClient rds;
|
||||
private static String subnetGroupName;
|
||||
private static List<String> subnetIds;
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
rds = TestFixtures.rdsClient();
|
||||
subnetGroupName = TestFixtures.uniqueName("rds-subnets");
|
||||
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
|
||||
DescribeSubnetsResponse response = ec2.describeSubnets();
|
||||
subnetIds = response.subnets().stream()
|
||||
.map(subnet -> subnet.subnetId())
|
||||
.sorted()
|
||||
.limit(2)
|
||||
.toList();
|
||||
}
|
||||
assertThat(subnetIds).hasSizeGreaterThanOrEqualTo(2);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
@@ -39,11 +53,11 @@ class RdsControlPlaneTest {
|
||||
CreateDbSubnetGroupResponse createResponse = rds.createDBSubnetGroup(b -> b
|
||||
.dbSubnetGroupName(subnetGroupName)
|
||||
.dbSubnetGroupDescription("SDK subnet group shape")
|
||||
.subnetIds("subnet-a", "subnet-b"));
|
||||
.subnetIds(subnetIds));
|
||||
|
||||
assertThat(createResponse.dbSubnetGroup().subnets())
|
||||
.extracting("subnetIdentifier")
|
||||
.containsExactly("subnet-a", "subnet-b");
|
||||
.containsExactlyElementsOf(subnetIds);
|
||||
|
||||
DescribeDbSubnetGroupsResponse describeResponse = rds.describeDBSubnetGroups(b -> b
|
||||
.dbSubnetGroupName(subnetGroupName));
|
||||
@@ -51,7 +65,7 @@ class RdsControlPlaneTest {
|
||||
assertThat(describeResponse.dbSubnetGroups()).hasSize(1);
|
||||
assertThat(describeResponse.dbSubnetGroups().get(0).subnets())
|
||||
.extracting("subnetIdentifier")
|
||||
.containsExactly("subnet-a", "subnet-b");
|
||||
.containsExactlyElementsOf(subnetIds);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -107,6 +107,8 @@ public class RdsQueryHandler {
|
||||
boolean manageMasterUserPassword = "true".equalsIgnoreCase(params.getFirst("ManageMasterUserPassword"));
|
||||
String masterUserSecretKmsKeyId = params.getFirst("MasterUserSecretKmsKeyId");
|
||||
Map<String, String> tags = parseTags(params);
|
||||
String availabilityZone = params.getFirst("AvailabilityZone");
|
||||
boolean multiAz = "true".equalsIgnoreCase(params.getFirst("MultiAZ"));
|
||||
|
||||
if (dbInstanceClass == null) {
|
||||
dbInstanceClass = "db.t3.micro";
|
||||
@@ -118,7 +120,7 @@ public class RdsQueryHandler {
|
||||
try {
|
||||
DbInstance instance = service.createDbInstance(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, dbName, dbInstanceClass, allocatedStorage, iamEnabled,
|
||||
paramGroupName, dbSubnetGroupName, dbClusterIdentifier,
|
||||
paramGroupName, dbSubnetGroupName, dbClusterIdentifier, availabilityZone, multiAz,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, tags);
|
||||
String result = dbInstanceXml(instance);
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateDBInstance", AwsNamespaces.RDS, result)).build();
|
||||
@@ -237,8 +239,8 @@ public class RdsQueryHandler {
|
||||
private Response handleCreateDbSubnetGroup(MultivaluedMap<String, String> params) {
|
||||
String name = params.getFirst("DBSubnetGroupName");
|
||||
if (name == null || name.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue",
|
||||
"DBSubnetGroupName is required.", AwsNamespaces.RDS, 400);
|
||||
return AwsQueryResponse.error("MissingParameter",
|
||||
"The request must contain the parameter DBSubnetGroupName.", AwsNamespaces.RDS, 400);
|
||||
}
|
||||
String description = params.getFirst("DBSubnetGroupDescription");
|
||||
List<String> subnetIds = memberList(params, "SubnetIds");
|
||||
@@ -325,6 +327,9 @@ public class RdsQueryHandler {
|
||||
String databaseName = params.getFirst("DatabaseName");
|
||||
boolean iamEnabled = "true".equalsIgnoreCase(params.getFirst("EnableIAMDatabaseAuthentication"));
|
||||
String paramGroupName = params.getFirst("DBClusterParameterGroupName");
|
||||
String dbSubnetGroupName = params.getFirst("DBSubnetGroupName");
|
||||
String availabilityZone = params.getFirst("AvailabilityZone");
|
||||
boolean multiAz = "true".equalsIgnoreCase(params.getFirst("MultiAZ"));
|
||||
|
||||
if (engineVersion == null) {
|
||||
engineVersion = defaultEngineVersion(engine);
|
||||
@@ -332,7 +337,8 @@ public class RdsQueryHandler {
|
||||
|
||||
try {
|
||||
DbCluster cluster = service.createDbCluster(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, databaseName, iamEnabled, paramGroupName);
|
||||
masterPassword, databaseName, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, availabilityZone, multiAz);
|
||||
String result = dbClusterXml(cluster);
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateDBCluster", AwsNamespaces.RDS, result)).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -609,10 +615,10 @@ public class RdsQueryHandler {
|
||||
.end("Endpoint");
|
||||
}
|
||||
xml.elem("IAMDatabaseAuthenticationEnabled", i.isIamDatabaseAuthenticationEnabled())
|
||||
.elem("MultiAZ", false)
|
||||
.elem("MultiAZ", i.isMultiAz())
|
||||
.elem("StorageType", "gp2")
|
||||
.elem("PubliclyAccessible", false)
|
||||
.elem("AvailabilityZone", config.defaultAvailabilityZone())
|
||||
.elem("AvailabilityZone", i.getAvailabilityZone() != null ? i.getAvailabilityZone() : config.defaultAvailabilityZone())
|
||||
.elem("PreferredMaintenanceWindow", "mon:00:00-mon:03:00")
|
||||
.elem("PreferredBackupWindow", "04:00-06:00")
|
||||
.start("VpcSecurityGroups")
|
||||
@@ -693,42 +699,6 @@ public class RdsQueryHandler {
|
||||
.end("Tag"));
|
||||
}
|
||||
|
||||
private String dbSubnetGroupXml(DbSubnetGroup group) {
|
||||
return new XmlBuilder().start("DBSubnetGroup").raw(dbSubnetGroupInnerXml(group)).end("DBSubnetGroup").build();
|
||||
}
|
||||
|
||||
private String dbSubnetGroupInnerXml(DbSubnetGroup group) {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.elem("DBSubnetGroupName", group.getDbSubnetGroupName())
|
||||
.elem("DBSubnetGroupDescription", group.getDbSubnetGroupDescription())
|
||||
.elem("VpcId", group.getVpcId() != null ? group.getVpcId() : "vpc-00000000")
|
||||
.elem("SubnetGroupStatus", "Complete")
|
||||
.start("Subnets");
|
||||
for (String subnetId : group.getSubnetIds()) {
|
||||
xml.start("Subnet")
|
||||
.elem("SubnetIdentifier", subnetId)
|
||||
.start("SubnetAvailabilityZone")
|
||||
.elem("Name", config.defaultAvailabilityZone())
|
||||
.end("SubnetAvailabilityZone")
|
||||
.elem("SubnetStatus", "Active")
|
||||
.end("Subnet");
|
||||
}
|
||||
return xml.end("Subnets").build();
|
||||
}
|
||||
|
||||
private DbSubnetGroup dbSubnetGroupForInstance(DbInstance instance) {
|
||||
String groupName = instance.getDbSubnetGroupName();
|
||||
if (groupName != null && !groupName.isBlank()) {
|
||||
try {
|
||||
return service.getDbSubnetGroup(groupName);
|
||||
} catch (AwsException ignored) {
|
||||
return new DbSubnetGroup(groupName, "DB subnet group " + groupName,
|
||||
"vpc-00000000", List.of("subnet-00000000"));
|
||||
}
|
||||
}
|
||||
return new DbSubnetGroup("default", "default", "vpc-00000000", List.of("subnet-00000000"));
|
||||
}
|
||||
|
||||
private String dbClusterXml(DbCluster c) {
|
||||
return new XmlBuilder().start("DBCluster").raw(dbClusterInnerXml(c)).end("DBCluster").build();
|
||||
}
|
||||
@@ -756,8 +726,8 @@ public class RdsQueryHandler {
|
||||
xml.elem("ReaderEndpoint", readerEp.address());
|
||||
}
|
||||
xml.elem("IAMDatabaseAuthenticationEnabled", c.isIamDatabaseAuthenticationEnabled())
|
||||
.elem("MultiAZ", false)
|
||||
.elem("AvailabilityZone", config.defaultAvailabilityZone())
|
||||
.elem("MultiAZ", c.isMultiAz())
|
||||
.elem("AvailabilityZone", c.getAvailabilityZone() != null ? c.getAvailabilityZone() : config.defaultAvailabilityZone())
|
||||
.elem("PreferredMaintenanceWindow", "mon:00:00-mon:03:00")
|
||||
.elem("PreferredBackupWindow", "04:00-06:00")
|
||||
.start("VpcSecurityGroups")
|
||||
@@ -766,7 +736,7 @@ public class RdsQueryHandler {
|
||||
.elem("Status", "active")
|
||||
.end("VpcSecurityGroupMembership")
|
||||
.end("VpcSecurityGroups")
|
||||
.elem("DBSubnetGroup", "default")
|
||||
.elem("DBSubnetGroup", c.getDbSubnetGroupName() != null ? c.getDbSubnetGroupName() : "default")
|
||||
.elem("DbClusterResourceId", c.getDbClusterResourceId())
|
||||
.elem("DBClusterArn", c.getDbClusterArn())
|
||||
.start("DBClusterMembers");
|
||||
@@ -786,6 +756,72 @@ public class RdsQueryHandler {
|
||||
return new XmlBuilder().start("DBParameterGroup").raw(paramGroupInnerXml(g)).end("DBParameterGroup").build();
|
||||
}
|
||||
|
||||
private String dbSubnetGroupXml(DbSubnetGroup g) {
|
||||
return new XmlBuilder().start("DBSubnetGroup").raw(dbSubnetGroupInnerXml(g)).end("DBSubnetGroup").build();
|
||||
}
|
||||
|
||||
private String dbSubnetGroupInnerXml(DbSubnetGroup g) {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.elem("DBSubnetGroupName", g.getDbSubnetGroupName())
|
||||
.elem("DBSubnetGroupDescription", g.getDescription())
|
||||
.elem("VpcId", g.getVpcId() != null ? g.getVpcId() : "vpc-00000000")
|
||||
.elem("SubnetGroupStatus", g.getSubnetGroupStatus() != null ? g.getSubnetGroupStatus() : "Complete")
|
||||
.elem("DBSubnetGroupArn", g.getDbSubnetGroupArn())
|
||||
.start("Subnets");
|
||||
for (String subnetId : g.getSubnetIds()) {
|
||||
String az = g.getSubnetAvailabilityZones().get(subnetId);
|
||||
xml.start("Subnet")
|
||||
.elem("SubnetIdentifier", subnetId)
|
||||
.start("SubnetAvailabilityZone")
|
||||
.elem("Name", az != null ? az : config.defaultAvailabilityZone())
|
||||
.end("SubnetAvailabilityZone")
|
||||
.elem("SubnetStatus", "Active")
|
||||
.end("Subnet");
|
||||
}
|
||||
return xml.end("Subnets").build();
|
||||
}
|
||||
|
||||
private DbSubnetGroup dbSubnetGroupForInstance(DbInstance instance) {
|
||||
String groupName = instance.getDbSubnetGroupName();
|
||||
if (groupName != null && !groupName.isBlank()) {
|
||||
try {
|
||||
DbSubnetGroup group = service.getDbSubnetGroup(groupName);
|
||||
if (group != null) {
|
||||
return group;
|
||||
}
|
||||
} catch (AwsException ignored) {
|
||||
}
|
||||
}
|
||||
try {
|
||||
DbSubnetGroup group = service.resolveDbSubnetGroupView(groupName);
|
||||
if (group != null) {
|
||||
return group;
|
||||
}
|
||||
} catch (AwsException ignored) {
|
||||
}
|
||||
if (groupName != null && !groupName.isBlank()) {
|
||||
return fallbackSubnetGroup(instance, groupName, "DB subnet group " + groupName);
|
||||
}
|
||||
return fallbackSubnetGroup(instance, "default", "default");
|
||||
}
|
||||
|
||||
private DbSubnetGroup fallbackSubnetGroup(DbInstance instance, String name, String description) {
|
||||
DbSubnetGroup fallback = new DbSubnetGroup();
|
||||
fallback.setDbSubnetGroupName(name);
|
||||
fallback.setDescription(description);
|
||||
fallback.setVpcId(instance.getVpcId() != null ? instance.getVpcId() : "vpc-00000000");
|
||||
fallback.setSubnetGroupStatus("Complete");
|
||||
Map<String, String> zones = instance.getSubnetAvailabilityZones();
|
||||
if (!zones.isEmpty()) {
|
||||
fallback.setSubnetIds(List.copyOf(zones.keySet()));
|
||||
fallback.setSubnetAvailabilityZones(zones);
|
||||
} else {
|
||||
fallback.setSubnetIds(List.of("subnet-00000000"));
|
||||
fallback.setSubnetAvailabilityZones(Map.of("subnet-00000000", config.defaultAvailabilityZone()));
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
private String paramGroupInnerXml(DbParameterGroup g) {
|
||||
return new XmlBuilder()
|
||||
.elem("DBParameterGroupName", g.getDbParameterGroupName())
|
||||
|
||||
@@ -11,6 +11,8 @@ import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Subnet;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsContainerHandle;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
@@ -32,8 +34,10 @@ import java.security.SecureRandom;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -56,6 +60,7 @@ public class RdsService {
|
||||
private final StorageBackend<String, DbSubnetGroup> subnetGroups;
|
||||
private final RdsContainerManager containerManager;
|
||||
private final RdsProxyManager proxyManager;
|
||||
private final Ec2Service ec2Service;
|
||||
private final RegionResolver regionResolver;
|
||||
private final EmulatorConfig config;
|
||||
private final SecretsManagerService secretsManagerService;
|
||||
@@ -67,6 +72,7 @@ public class RdsService {
|
||||
@Inject
|
||||
public RdsService(RdsContainerManager containerManager,
|
||||
RdsProxyManager proxyManager,
|
||||
Ec2Service ec2Service,
|
||||
RegionResolver regionResolver,
|
||||
EmulatorConfig config,
|
||||
StorageFactory storageFactory,
|
||||
@@ -74,6 +80,7 @@ public class RdsService {
|
||||
DockerHostResolver dockerHostResolver) {
|
||||
this.containerManager = containerManager;
|
||||
this.proxyManager = proxyManager;
|
||||
this.ec2Service = ec2Service;
|
||||
this.regionResolver = regionResolver;
|
||||
this.config = config;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
@@ -92,6 +99,7 @@ public class RdsService {
|
||||
|
||||
RdsService(RdsContainerManager containerManager,
|
||||
RdsProxyManager proxyManager,
|
||||
Ec2Service ec2Service,
|
||||
RegionResolver regionResolver,
|
||||
EmulatorConfig config,
|
||||
StorageBackend<String, DbInstance> instances,
|
||||
@@ -99,27 +107,14 @@ public class RdsService {
|
||||
StorageBackend<String, DbParameterGroup> parameterGroups,
|
||||
StorageBackend<String, DbClusterParameterGroup> clusterParameterGroups,
|
||||
StorageBackend<String, DbSubnetGroup> subnetGroups) {
|
||||
this(containerManager, proxyManager, regionResolver, config,
|
||||
instances, clusters, parameterGroups, clusterParameterGroups, subnetGroups, null, null);
|
||||
}
|
||||
|
||||
RdsService(RdsContainerManager containerManager,
|
||||
RdsProxyManager proxyManager,
|
||||
RegionResolver regionResolver,
|
||||
EmulatorConfig config,
|
||||
StorageBackend<String, DbInstance> instances,
|
||||
StorageBackend<String, DbCluster> clusters,
|
||||
StorageBackend<String, DbParameterGroup> parameterGroups,
|
||||
StorageBackend<String, DbClusterParameterGroup> clusterParameterGroups,
|
||||
StorageBackend<String, DbSubnetGroup> subnetGroups,
|
||||
SecretsManagerService secretsManagerService) {
|
||||
this(containerManager, proxyManager, regionResolver, config,
|
||||
this(containerManager, proxyManager, ec2Service, regionResolver, config,
|
||||
instances, clusters, parameterGroups, clusterParameterGroups, subnetGroups,
|
||||
secretsManagerService, null);
|
||||
null, null);
|
||||
}
|
||||
|
||||
RdsService(RdsContainerManager containerManager,
|
||||
RdsProxyManager proxyManager,
|
||||
Ec2Service ec2Service,
|
||||
RegionResolver regionResolver,
|
||||
EmulatorConfig config,
|
||||
StorageBackend<String, DbInstance> instances,
|
||||
@@ -131,6 +126,7 @@ public class RdsService {
|
||||
DockerHostResolver dockerHostResolver) {
|
||||
this.containerManager = containerManager;
|
||||
this.proxyManager = proxyManager;
|
||||
this.ec2Service = ec2Service;
|
||||
this.regionResolver = regionResolver;
|
||||
this.config = config;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
@@ -157,7 +153,7 @@ public class RdsService {
|
||||
String dbClusterIdentifier) {
|
||||
return createDbInstance(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
dbName, dbInstanceClass, allocatedStorage, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, dbClusterIdentifier, false, null);
|
||||
dbSubnetGroupName, dbClusterIdentifier, null, false, false, null, Map.of());
|
||||
}
|
||||
|
||||
public DbInstance createDbInstance(String id, String engineParam, String engineVersion,
|
||||
@@ -170,7 +166,7 @@ public class RdsService {
|
||||
String masterUserSecretKmsKeyId) {
|
||||
return createDbInstance(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
dbName, dbInstanceClass, allocatedStorage, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, dbClusterIdentifier, manageMasterUserPassword,
|
||||
dbSubnetGroupName, dbClusterIdentifier, null, false, manageMasterUserPassword,
|
||||
masterUserSecretKmsKeyId, Map.of());
|
||||
}
|
||||
|
||||
@@ -183,6 +179,34 @@ public class RdsService {
|
||||
boolean manageMasterUserPassword,
|
||||
String masterUserSecretKmsKeyId,
|
||||
Map<String, String> tags) {
|
||||
return createDbInstance(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
dbName, dbInstanceClass, allocatedStorage, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, dbClusterIdentifier, null, false, manageMasterUserPassword,
|
||||
masterUserSecretKmsKeyId, tags);
|
||||
}
|
||||
|
||||
public DbInstance createDbInstance(String id, String engineParam, String engineVersion,
|
||||
String masterUsername, String masterPassword,
|
||||
String dbName, String dbInstanceClass,
|
||||
int allocatedStorage, boolean iamEnabled,
|
||||
String paramGroupName, String dbSubnetGroupName,
|
||||
String dbClusterIdentifier, String availabilityZone,
|
||||
boolean multiAz) {
|
||||
return createDbInstance(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
dbName, dbInstanceClass, allocatedStorage, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, dbClusterIdentifier, availabilityZone, multiAz,
|
||||
false, null, Map.of());
|
||||
}
|
||||
|
||||
public DbInstance createDbInstance(String id, String engineParam, String engineVersion,
|
||||
String masterUsername, String masterPassword,
|
||||
String dbName, String dbInstanceClass,
|
||||
int allocatedStorage, boolean iamEnabled,
|
||||
String paramGroupName, String dbSubnetGroupName,
|
||||
String dbClusterIdentifier, String availabilityZone,
|
||||
boolean multiAz, boolean manageMasterUserPassword,
|
||||
String masterUserSecretKmsKeyId,
|
||||
Map<String, String> tags) {
|
||||
if (instances.get(id).isPresent()) {
|
||||
throw new AwsException("DBInstanceAlreadyExists",
|
||||
"DB instance " + id + " already exists.", 400);
|
||||
@@ -208,6 +232,7 @@ public class RdsService {
|
||||
int containerPort = 0;
|
||||
String instanceVolumeId = null;
|
||||
String instanceDockerVolumeName = null;
|
||||
PlacementResolution placement;
|
||||
|
||||
if (dbClusterIdentifier != null && !dbClusterIdentifier.isBlank()) {
|
||||
// Cluster member — share the cluster's container
|
||||
@@ -222,7 +247,9 @@ public class RdsService {
|
||||
instanceDockerVolumeName = cluster.getDockerVolumeName() != null
|
||||
? cluster.getDockerVolumeName()
|
||||
: volumeName(cluster.getVolumeId(), cluster.getDbClusterIdentifier());
|
||||
placement = PlacementResolution.fromCluster(cluster);
|
||||
} else {
|
||||
placement = resolvePlacement(dbSubnetGroupName, availabilityZone, multiAz);
|
||||
// Standalone instance — start its own container
|
||||
String image = imageForEngine(engine, engineVersion);
|
||||
instanceVolumeId = String.format("%06x", new SecureRandom().nextInt(0xFFFFFF));
|
||||
@@ -246,6 +273,11 @@ public class RdsService {
|
||||
instance.setVolumeId(instanceVolumeId);
|
||||
instance.setDockerVolumeName(instanceDockerVolumeName);
|
||||
instance.setTags(tags);
|
||||
instance.setDbSubnetGroupName(placement.dbSubnetGroupName());
|
||||
instance.setVpcId(placement.vpcId());
|
||||
instance.setAvailabilityZone(placement.availabilityZone());
|
||||
instance.setMultiAz(placement.multiAz());
|
||||
instance.setSubnetAvailabilityZones(placement.subnetAvailabilityZones());
|
||||
|
||||
String region = regionResolver.getDefaultRegion();
|
||||
instance.setDbiResourceId("db-" + java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 24).toUpperCase());
|
||||
@@ -480,6 +512,15 @@ public class RdsService {
|
||||
String masterUsername, String masterPassword,
|
||||
String databaseName, boolean iamEnabled,
|
||||
String paramGroupName) {
|
||||
return createDbCluster(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
databaseName, iamEnabled, paramGroupName, null, null, false);
|
||||
}
|
||||
|
||||
public DbCluster createDbCluster(String id, String engineParam, String engineVersion,
|
||||
String masterUsername, String masterPassword,
|
||||
String databaseName, boolean iamEnabled,
|
||||
String paramGroupName, String dbSubnetGroupName,
|
||||
String availabilityZone, boolean multiAz) {
|
||||
if (clusters.get(id).isPresent()) {
|
||||
throw new AwsException("DBClusterAlreadyExistsFault",
|
||||
"DB cluster " + id + " already exists.", 400);
|
||||
@@ -487,6 +528,7 @@ public class RdsService {
|
||||
|
||||
DatabaseEngine engine = resolveEngine(engineParam);
|
||||
validateClusterParameterGroup(paramGroupName, engineParam, engineVersion);
|
||||
PlacementResolution placement = resolvePlacement(dbSubnetGroupName, availabilityZone, multiAz);
|
||||
int proxyPort = allocateProxyPort();
|
||||
String image = imageForEngine(engine, engineVersion);
|
||||
String clusterVolumeId = String.format("%06x", new SecureRandom().nextInt(0xFFFFFF));
|
||||
@@ -502,6 +544,11 @@ public class RdsService {
|
||||
cluster.setContainerPort(handle.getPort());
|
||||
cluster.setVolumeId(clusterVolumeId);
|
||||
cluster.setDockerVolumeName(volumeName(clusterVolumeId, id));
|
||||
cluster.setDbSubnetGroupName(placement.dbSubnetGroupName());
|
||||
cluster.setVpcId(placement.vpcId());
|
||||
cluster.setAvailabilityZone(placement.availabilityZone());
|
||||
cluster.setMultiAz(placement.multiAz());
|
||||
cluster.setSubnetAvailabilityZones(placement.subnetAvailabilityZones());
|
||||
|
||||
String region = regionResolver.getDefaultRegion();
|
||||
cluster.setDbClusterResourceId("cluster-" + java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 24).toUpperCase());
|
||||
@@ -568,6 +615,48 @@ public class RdsService {
|
||||
LOG.infov("DB cluster {0} deleted", id);
|
||||
}
|
||||
|
||||
// ── DB Subnet Groups ──────────────────────────────────────────────────────
|
||||
|
||||
public DbSubnetGroup createDbSubnetGroup(String name, String description, List<String> subnetIds) {
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter DBSubnetGroupName.", 400);
|
||||
}
|
||||
if (subnetGroups.get(name).isPresent() || "default".equalsIgnoreCase(name)) {
|
||||
throw new AwsException("DBSubnetGroupAlreadyExists",
|
||||
"DB subnet group " + name + " already exists.", 400);
|
||||
}
|
||||
if (subnetIds == null || subnetIds.isEmpty()) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter SubnetIds.", 400);
|
||||
}
|
||||
|
||||
DbSubnetGroup group = buildSubnetGroup(name, description, subnetIds);
|
||||
subnetGroups.put(name, group);
|
||||
return group;
|
||||
}
|
||||
|
||||
public Collection<DbSubnetGroup> listDbSubnetGroups(String filterName) {
|
||||
List<DbSubnetGroup> groups = new ArrayList<>();
|
||||
if (filterName == null || filterName.isBlank() || "default".equalsIgnoreCase(filterName)) {
|
||||
groups.add(buildDefaultSubnetGroup());
|
||||
}
|
||||
if (filterName != null && !filterName.isBlank()) {
|
||||
subnetGroups.get(filterName).ifPresent(groups::add);
|
||||
return groups;
|
||||
}
|
||||
groups.addAll(subnetGroups.scan(k -> true));
|
||||
return groups;
|
||||
}
|
||||
|
||||
public DbSubnetGroup resolveDbSubnetGroupView(String name) {
|
||||
String effectiveName = (name == null || name.isBlank()) ? "default" : name;
|
||||
if ("default".equalsIgnoreCase(effectiveName)) {
|
||||
return buildDefaultSubnetGroup();
|
||||
}
|
||||
return subnetGroups.get(effectiveName).orElseThrow(() ->
|
||||
new AwsException("DBSubnetGroupNotFoundFault",
|
||||
"DB subnet group " + effectiveName + " not found.", 404));
|
||||
}
|
||||
|
||||
// ── Parameter Groups ──────────────────────────────────────────────────────
|
||||
|
||||
public DbParameterGroup createDbParameterGroup(String name, String family, String description) {
|
||||
@@ -611,40 +700,22 @@ public class RdsService {
|
||||
return group;
|
||||
}
|
||||
|
||||
public DbSubnetGroup createDbSubnetGroup(String name, String description, List<String> subnetIds) {
|
||||
if (subnetGroups.get(name).isPresent()) {
|
||||
throw new AwsException("DBSubnetGroupAlreadyExists",
|
||||
"DB subnet group " + name + " already exists.", 400);
|
||||
}
|
||||
if (subnetIds == null || subnetIds.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"SubnetIds must contain at least one subnet.", 400);
|
||||
}
|
||||
DbSubnetGroup group = new DbSubnetGroup(name, description, "vpc-00000000", subnetIds);
|
||||
subnetGroups.put(name, group);
|
||||
return group;
|
||||
}
|
||||
|
||||
public DbSubnetGroup getDbSubnetGroup(String name) {
|
||||
if ("default".equalsIgnoreCase(name)) {
|
||||
return buildDefaultSubnetGroup();
|
||||
}
|
||||
return subnetGroups.get(name).orElseThrow(() ->
|
||||
new AwsException("DBSubnetGroupNotFoundFault",
|
||||
"DB subnet group " + name + " not found.", 404));
|
||||
}
|
||||
|
||||
public Collection<DbSubnetGroup> listDbSubnetGroups(String filterName) {
|
||||
if (filterName != null && !filterName.isBlank()) {
|
||||
return subnetGroups.get(filterName).map(List::of).orElse(List.of());
|
||||
}
|
||||
return subnetGroups.scan(k -> true);
|
||||
}
|
||||
|
||||
public DbSubnetGroup modifyDbSubnetGroup(String name, List<String> subnetIds) {
|
||||
DbSubnetGroup group = getDbSubnetGroup(name);
|
||||
DbSubnetGroup existing = getDbSubnetGroup(name);
|
||||
if (subnetIds == null || subnetIds.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"SubnetIds must contain at least one subnet.", 400);
|
||||
}
|
||||
group.setSubnetIds(subnetIds);
|
||||
DbSubnetGroup group = buildSubnetGroup(name, existing.getDescription(), subnetIds);
|
||||
subnetGroups.put(name, group);
|
||||
return group;
|
||||
}
|
||||
@@ -963,6 +1034,99 @@ public class RdsService {
|
||||
return allocateProxyPort();
|
||||
}
|
||||
|
||||
private PlacementResolution resolvePlacement(String dbSubnetGroupName, String availabilityZone, boolean multiAz) {
|
||||
String effectiveSubnetGroupName = (dbSubnetGroupName == null || dbSubnetGroupName.isBlank())
|
||||
? "default"
|
||||
: dbSubnetGroupName;
|
||||
DbSubnetGroup group = "default".equals(effectiveSubnetGroupName)
|
||||
? buildDefaultSubnetGroup()
|
||||
: subnetGroups.get(effectiveSubnetGroupName).orElseThrow(() ->
|
||||
new AwsException("DBSubnetGroupNotFoundFault",
|
||||
"DB subnet group " + effectiveSubnetGroupName + " not found.", 404));
|
||||
|
||||
Map<String, String> subnetAvailabilityZones = group.getSubnetAvailabilityZones();
|
||||
String vpcId = group.getVpcId();
|
||||
|
||||
if (multiAz && availabilityZone != null && !availabilityZone.isBlank()) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"AvailabilityZone cannot be specified when MultiAZ is enabled.", 400);
|
||||
}
|
||||
|
||||
String effectiveAvailabilityZone = availabilityZone;
|
||||
if (effectiveAvailabilityZone == null || effectiveAvailabilityZone.isBlank()) {
|
||||
effectiveAvailabilityZone = subnetAvailabilityZones.values().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.findFirst()
|
||||
.orElse(config.defaultAvailabilityZone());
|
||||
} else if (!subnetAvailabilityZones.containsValue(effectiveAvailabilityZone)) {
|
||||
throw new AwsException("InvalidVPCNetworkStateFault",
|
||||
"Availability Zone " + effectiveAvailabilityZone
|
||||
+ " is not valid for DB subnet group " + effectiveSubnetGroupName + ".", 400);
|
||||
}
|
||||
|
||||
if (multiAz) {
|
||||
long distinctZoneCount = subnetAvailabilityZones.values().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.distinct()
|
||||
.count();
|
||||
if (distinctZoneCount < 2) {
|
||||
throw new AwsException("DBSubnetGroupDoesNotCoverEnoughAZs",
|
||||
"DB subnet group " + effectiveSubnetGroupName
|
||||
+ " does not cover multiple Availability Zones.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
return new PlacementResolution(
|
||||
effectiveSubnetGroupName,
|
||||
vpcId,
|
||||
effectiveAvailabilityZone,
|
||||
multiAz,
|
||||
new LinkedHashMap<>(subnetAvailabilityZones));
|
||||
}
|
||||
|
||||
private DbSubnetGroup buildDefaultSubnetGroup() {
|
||||
String region = regionResolver.getDefaultRegion();
|
||||
List<Subnet> subnets = ec2Service.describeSubnets(region, List.of(), Map.of("vpc-id", List.of("vpc-default")));
|
||||
if (subnets.isEmpty()) {
|
||||
throw new AwsException("InvalidVPCNetworkStateFault",
|
||||
"No subnets available for DB subnet group default.", 400);
|
||||
}
|
||||
return buildSubnetGroup("default", "default subnet group", extractSubnetIds(subnets));
|
||||
}
|
||||
|
||||
private DbSubnetGroup buildSubnetGroup(String name, String description, List<String> subnetIds) {
|
||||
String region = regionResolver.getDefaultRegion();
|
||||
List<Subnet> resolvedSubnets = ec2Service.describeSubnets(region, subnetIds, Map.of());
|
||||
if (resolvedSubnets.size() != subnetIds.size()) {
|
||||
throw new AwsException("InvalidSubnet",
|
||||
"One or more subnets for DB subnet group " + name + " do not exist.", 400);
|
||||
}
|
||||
|
||||
String vpcId = resolvedSubnets.getFirst().getVpcId();
|
||||
boolean sameVpc = resolvedSubnets.stream()
|
||||
.map(Subnet::getVpcId)
|
||||
.filter(Objects::nonNull)
|
||||
.allMatch(vpcId::equals);
|
||||
if (!sameVpc) {
|
||||
throw new AwsException("InvalidVPCNetworkStateFault",
|
||||
"DB subnet group " + name + " contains subnets in multiple VPCs.", 400);
|
||||
}
|
||||
|
||||
Map<String, String> subnetAvailabilityZones = new LinkedHashMap<>();
|
||||
for (Subnet subnet : resolvedSubnets) {
|
||||
subnetAvailabilityZones.put(subnet.getSubnetId(), subnet.getAvailabilityZone());
|
||||
}
|
||||
|
||||
DbSubnetGroup group = new DbSubnetGroup(name, description, vpcId, subnetIds, subnetAvailabilityZones);
|
||||
group.setDbSubnetGroupArn(regionResolver.buildArn("rds", region, "subgrp:" + name));
|
||||
group.setSubnetGroupStatus("Complete");
|
||||
return group;
|
||||
}
|
||||
|
||||
private static List<String> extractSubnetIds(List<Subnet> subnets) {
|
||||
return subnets.stream().map(Subnet::getSubnetId).toList();
|
||||
}
|
||||
|
||||
private String volumeName(String volumeId, String fallbackId) {
|
||||
return ContainerStorageHelper.resourceName("rds", volumeId, fallbackId);
|
||||
}
|
||||
@@ -976,4 +1140,16 @@ public class RdsService {
|
||||
return new RdsContainerHandle(cluster.getContainerId(), cluster.getDbClusterIdentifier(),
|
||||
cluster.getContainerHost(), cluster.getContainerPort());
|
||||
}
|
||||
|
||||
private record PlacementResolution(String dbSubnetGroupName, String vpcId, String availabilityZone,
|
||||
boolean multiAz, Map<String, String> subnetAvailabilityZones) {
|
||||
private static PlacementResolution fromCluster(DbCluster cluster) {
|
||||
return new PlacementResolution(
|
||||
cluster.getDbSubnetGroupName(),
|
||||
cluster.getVpcId(),
|
||||
cluster.getAvailabilityZone(),
|
||||
cluster.isMultiAz(),
|
||||
cluster.getSubnetAvailabilityZones());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,9 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
public class DbCluster {
|
||||
@@ -21,6 +23,11 @@ public class DbCluster {
|
||||
private boolean iamDatabaseAuthenticationEnabled;
|
||||
private List<String> dbClusterMembers = new ArrayList<>();
|
||||
private String parameterGroupName;
|
||||
private String dbSubnetGroupName;
|
||||
private String vpcId;
|
||||
private String availabilityZone;
|
||||
private boolean multiAz;
|
||||
private Map<String, String> subnetAvailabilityZones = new LinkedHashMap<>();
|
||||
private String dbClusterResourceId;
|
||||
private String dbClusterArn;
|
||||
private Instant createdAt;
|
||||
@@ -95,6 +102,25 @@ public class DbCluster {
|
||||
public String getParameterGroupName() { return parameterGroupName; }
|
||||
public void setParameterGroupName(String parameterGroupName) { this.parameterGroupName = parameterGroupName; }
|
||||
|
||||
public String getDbSubnetGroupName() { return dbSubnetGroupName; }
|
||||
public void setDbSubnetGroupName(String dbSubnetGroupName) { this.dbSubnetGroupName = dbSubnetGroupName; }
|
||||
|
||||
public String getVpcId() { return vpcId; }
|
||||
public void setVpcId(String vpcId) { this.vpcId = vpcId; }
|
||||
|
||||
public String getAvailabilityZone() { return availabilityZone; }
|
||||
public void setAvailabilityZone(String availabilityZone) { this.availabilityZone = availabilityZone; }
|
||||
|
||||
public boolean isMultiAz() { return multiAz; }
|
||||
public void setMultiAz(boolean multiAz) { this.multiAz = multiAz; }
|
||||
|
||||
public Map<String, String> getSubnetAvailabilityZones() { return subnetAvailabilityZones; }
|
||||
public void setSubnetAvailabilityZones(Map<String, String> subnetAvailabilityZones) {
|
||||
this.subnetAvailabilityZones = subnetAvailabilityZones != null
|
||||
? new LinkedHashMap<>(subnetAvailabilityZones)
|
||||
: new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
public String getDbClusterResourceId() { return dbClusterResourceId; }
|
||||
public void setDbClusterResourceId(String dbClusterResourceId) { this.dbClusterResourceId = dbClusterResourceId; }
|
||||
|
||||
|
||||
@@ -23,6 +23,10 @@ public class DbInstance {
|
||||
private String parameterGroupName;
|
||||
private String dbSubnetGroupName;
|
||||
private String dbClusterIdentifier;
|
||||
private String vpcId;
|
||||
private String availabilityZone;
|
||||
private boolean multiAz;
|
||||
private Map<String, String> subnetAvailabilityZones = new LinkedHashMap<>();
|
||||
private String dbiResourceId;
|
||||
private String dbInstanceArn;
|
||||
private String masterUserSecretArn;
|
||||
@@ -32,10 +36,9 @@ public class DbInstance {
|
||||
private Instant createdAt;
|
||||
private int proxyPort;
|
||||
|
||||
private String dockerVolumeName; // null in in-memory mode; "floci-rds-<id>" otherwise
|
||||
private String volumeId; // 6-char hex, generated once at creation; null on older persisted resources
|
||||
private String dockerVolumeName;
|
||||
private String volumeId;
|
||||
|
||||
// Transient — not persisted; restored on startup by re-launching containers
|
||||
private transient String containerId;
|
||||
private transient String containerHost;
|
||||
private transient int containerPort;
|
||||
@@ -109,6 +112,22 @@ public class DbInstance {
|
||||
public String getDbClusterIdentifier() { return dbClusterIdentifier; }
|
||||
public void setDbClusterIdentifier(String dbClusterIdentifier) { this.dbClusterIdentifier = dbClusterIdentifier; }
|
||||
|
||||
public String getVpcId() { return vpcId; }
|
||||
public void setVpcId(String vpcId) { this.vpcId = vpcId; }
|
||||
|
||||
public String getAvailabilityZone() { return availabilityZone; }
|
||||
public void setAvailabilityZone(String availabilityZone) { this.availabilityZone = availabilityZone; }
|
||||
|
||||
public boolean isMultiAz() { return multiAz; }
|
||||
public void setMultiAz(boolean multiAz) { this.multiAz = multiAz; }
|
||||
|
||||
public Map<String, String> getSubnetAvailabilityZones() { return subnetAvailabilityZones; }
|
||||
public void setSubnetAvailabilityZones(Map<String, String> subnetAvailabilityZones) {
|
||||
this.subnetAvailabilityZones = subnetAvailabilityZones != null
|
||||
? new LinkedHashMap<>(subnetAvailabilityZones)
|
||||
: new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
public String getDbiResourceId() { return dbiResourceId; }
|
||||
public void setDbiResourceId(String dbiResourceId) { this.dbiResourceId = dbiResourceId; }
|
||||
|
||||
|
||||
@@ -3,37 +3,62 @@ package io.github.hectorvent.floci.services.rds.model;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
public class DbSubnetGroup {
|
||||
|
||||
private String dbSubnetGroupName;
|
||||
private String dbSubnetGroupDescription;
|
||||
private String description;
|
||||
private String dbSubnetGroupArn;
|
||||
private String vpcId;
|
||||
private String subnetGroupStatus = "Complete";
|
||||
private List<String> subnetIds = new ArrayList<>();
|
||||
private Map<String, String> subnetAvailabilityZones = new LinkedHashMap<>();
|
||||
|
||||
public DbSubnetGroup() {}
|
||||
|
||||
public DbSubnetGroup(String dbSubnetGroupName, String dbSubnetGroupDescription,
|
||||
String vpcId, List<String> subnetIds) {
|
||||
public DbSubnetGroup(String dbSubnetGroupName, String description, String vpcId,
|
||||
List<String> subnetIds, Map<String, String> subnetAvailabilityZones) {
|
||||
this.dbSubnetGroupName = dbSubnetGroupName;
|
||||
this.dbSubnetGroupDescription = dbSubnetGroupDescription;
|
||||
this.description = description;
|
||||
this.vpcId = vpcId;
|
||||
this.subnetIds = subnetIds != null ? new ArrayList<>(subnetIds) : new ArrayList<>();
|
||||
if (subnetIds != null) {
|
||||
this.subnetIds = new ArrayList<>(subnetIds);
|
||||
}
|
||||
if (subnetAvailabilityZones != null) {
|
||||
this.subnetAvailabilityZones = new LinkedHashMap<>(subnetAvailabilityZones);
|
||||
}
|
||||
}
|
||||
|
||||
public String getDbSubnetGroupName() { return dbSubnetGroupName; }
|
||||
public void setDbSubnetGroupName(String dbSubnetGroupName) { this.dbSubnetGroupName = dbSubnetGroupName; }
|
||||
|
||||
public String getDbSubnetGroupDescription() { return dbSubnetGroupDescription; }
|
||||
public void setDbSubnetGroupDescription(String dbSubnetGroupDescription) { this.dbSubnetGroupDescription = dbSubnetGroupDescription; }
|
||||
public String getDescription() { return description; }
|
||||
public void setDescription(String description) { this.description = description; }
|
||||
|
||||
public String getDbSubnetGroupDescription() { return description; }
|
||||
public void setDbSubnetGroupDescription(String description) { this.description = description; }
|
||||
public String getDbSubnetGroupArn() { return dbSubnetGroupArn; }
|
||||
public void setDbSubnetGroupArn(String dbSubnetGroupArn) { this.dbSubnetGroupArn = dbSubnetGroupArn; }
|
||||
|
||||
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<>();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -33,7 +33,7 @@ class CloudFormationRdsIntegrationTest {
|
||||
"Properties": {
|
||||
"DBSubnetGroupName": "%s",
|
||||
"DBSubnetGroupDescription": "managed by cfn",
|
||||
"SubnetIds": ["subnet-aaaa1111", "subnet-bbbb2222"]
|
||||
"SubnetIds": ["subnet-default-a", "subnet-default-b"]
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
@@ -37,6 +38,7 @@ class RdsQueryHandlerTest {
|
||||
when(config.services()).thenReturn(servicesConfig);
|
||||
when(servicesConfig.rds()).thenReturn(rdsConfig);
|
||||
when(config.defaultAvailabilityZone()).thenReturn("us-east-1a");
|
||||
when(service.resolveDbSubnetGroupView(nullable(String.class))).thenReturn(defaultSubnetGroup());
|
||||
handler = new RdsQueryHandler(service, config);
|
||||
}
|
||||
|
||||
@@ -120,6 +122,22 @@ class RdsQueryHandlerTest {
|
||||
verify(service).listDbInstances("direct-id");
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeDbInstances_dbSubnetGroupUsesSubnetTag() {
|
||||
DbInstance instance = makeInstance("mydb");
|
||||
instance.setDbSubnetGroupName("custom-group");
|
||||
when(service.resolveDbSubnetGroupView("custom-group")).thenReturn(customSubnetGroup());
|
||||
when(service.listDbInstances(null)).thenReturn(List.of(instance));
|
||||
|
||||
Response response = handler.handle("DescribeDBInstances", params());
|
||||
|
||||
String body = (String) response.getEntity();
|
||||
assertTrue(body.contains("<Subnets><Subnet>") || body.contains("<Subnets>\n<Subnet>"));
|
||||
assertFalse(body.contains("<Subnets><member>"), "Did not expect <member> elements inside DBSubnetGroup.Subnets");
|
||||
assertTrue(body.contains("<SubnetIdentifier>subnet-a</SubnetIdentifier>"));
|
||||
assertTrue(body.contains("<SubnetIdentifier>subnet-b</SubnetIdentifier>"));
|
||||
}
|
||||
|
||||
// ──────────────────────────── DBClusters XML tag ────────────────────────────
|
||||
|
||||
@Test
|
||||
@@ -164,7 +182,8 @@ class RdsQueryHandlerTest {
|
||||
instance.setDbSubnetGroupName("sample-db-subnets");
|
||||
when(service.listDbInstances(null)).thenReturn(List.of(instance));
|
||||
when(service.getDbSubnetGroup("sample-db-subnets")).thenReturn(new DbSubnetGroup(
|
||||
"sample-db-subnets", "test subnets", "vpc-123", List.of("subnet-aaa", "subnet-bbb")));
|
||||
"sample-db-subnets", "test subnets", "vpc-123", List.of("subnet-aaa", "subnet-bbb"),
|
||||
Map.of("subnet-aaa", "us-east-1a", "subnet-bbb", "us-east-1b")));
|
||||
|
||||
Response response = handler.handle("DescribeDBInstances", params());
|
||||
|
||||
@@ -196,7 +215,7 @@ class RdsQueryHandlerTest {
|
||||
DbInstance instance = makeInstance("mydb");
|
||||
when(service.createDbInstance(eq("mydb"), eq("postgres"), eq("16.3"),
|
||||
eq(null), eq(null), eq(null), eq("db.t3.micro"),
|
||||
eq(20), eq(false), eq(null), eq(null), eq(null), eq(false), eq(null),
|
||||
eq(20), eq(false), eq(null), eq(null), eq(null), eq(null), eq(false), eq(false), eq(null),
|
||||
eq(java.util.Map.of("example:ClusterId", "cluster-a", "Name", "mydb"))))
|
||||
.thenReturn(instance);
|
||||
|
||||
@@ -210,7 +229,7 @@ class RdsQueryHandlerTest {
|
||||
handler.handle("CreateDBInstance", p);
|
||||
|
||||
verify(service).createDbInstance("mydb", "postgres", "16.3",
|
||||
null, null, null, "db.t3.micro", 20, false, null, null, null, false, null,
|
||||
null, null, null, "db.t3.micro", 20, false, null, null, null, null, false, false, null,
|
||||
java.util.Map.of("example:ClusterId", "cluster-a", "Name", "mydb"));
|
||||
}
|
||||
|
||||
@@ -286,7 +305,8 @@ class RdsQueryHandlerTest {
|
||||
DbInstance instance = makeInstance("mydb");
|
||||
when(service.createDbInstance(eq("mydb"), eq("postgres"), eq("16.3"),
|
||||
eq("admin"), eq("secret"), eq("dbname"), eq("db.t3.micro"),
|
||||
eq(20), eq(false), eq(null), eq(null), eq(null), eq(false), eq(null), eq(java.util.Map.of())))
|
||||
eq(20), eq(false), eq(null), eq(null), eq(null), eq(null), eq(false), eq(false),
|
||||
eq(null), eq(java.util.Map.of())))
|
||||
.thenReturn(instance);
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
@@ -299,7 +319,8 @@ class RdsQueryHandlerTest {
|
||||
handler.handle("CreateDBInstance", p);
|
||||
|
||||
verify(service).createDbInstance("mydb", "postgres", "16.3",
|
||||
"admin", "secret", "dbname", "db.t3.micro", 20, false, null, null, null, false, null, java.util.Map.of());
|
||||
"admin", "secret", "dbname", "db.t3.micro", 20, false, null, null, null, null, false, false,
|
||||
null, java.util.Map.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -310,7 +331,8 @@ class RdsQueryHandlerTest {
|
||||
instance.setMasterUserSecretKmsKeyId("kms-key-1");
|
||||
when(service.createDbInstance(eq("mydb"), eq("postgres"), eq("16.3"),
|
||||
eq("admin"), eq(null), eq("dbname"), eq("db.t3.micro"),
|
||||
eq(20), eq(false), eq(null), eq(null), eq(null), eq(true), eq("kms-key-1"), eq(java.util.Map.of())))
|
||||
eq(20), eq(false), eq(null), eq(null), eq(null), eq(null), eq(false), eq(true),
|
||||
eq("kms-key-1"), eq(java.util.Map.of())))
|
||||
.thenReturn(instance);
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
@@ -328,35 +350,71 @@ class RdsQueryHandlerTest {
|
||||
assertTrue(body.contains("<SecretStatus>active</SecretStatus>"));
|
||||
assertTrue(body.contains("<KmsKeyId>kms-key-1</KmsKeyId>"));
|
||||
verify(service).createDbInstance("mydb", "postgres", "16.3",
|
||||
"admin", null, "dbname", "db.t3.micro", 20, false, null, null, null, true, "kms-key-1", java.util.Map.of());
|
||||
"admin", null, "dbname", "db.t3.micro", 20, false, null, null, null, null, false, true,
|
||||
"kms-key-1", java.util.Map.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbInstance_unknownEngineReturnsInvalidParameterValue() {
|
||||
// Handler defaults version to "1.0" for unknown engines, then the service
|
||||
// rejects the engine. Verify the full error path: version defaulting +
|
||||
// AwsException wrapping into a 400 query error.
|
||||
when(service.createDbInstance(eq("mydb"), eq("oracle"), eq("1.0"),
|
||||
eq(null), eq(null), eq(null), eq("db.t3.micro"),
|
||||
eq(20), eq(false), eq(null), eq(null), eq(null), eq(false), eq(null), eq(java.util.Map.of())))
|
||||
.thenThrow(new AwsException("InvalidParameterValue",
|
||||
"A value that you provided for a parameter isn't valid. Check the parameter constraints and try again.", 400));
|
||||
void createDbInstance_withPlacementInputsShouldReflectRequestedPlacement() {
|
||||
DbInstance instance = makeInstance("mydb");
|
||||
instance.setDbSubnetGroupName("default");
|
||||
instance.setAvailabilityZone("ap-northeast-1a");
|
||||
instance.setMultiAz(true);
|
||||
when(service.createDbInstance(eq("mydb"), eq("postgres"), eq("16.3"),
|
||||
eq("admin"), eq("secret"), eq("dbname"), eq("db.t3.micro"),
|
||||
eq(20), eq(false), eq(null), eq("default"), eq(null), eq("ap-northeast-1a"), eq(true),
|
||||
eq(false), eq(null), eq(java.util.Map.of())))
|
||||
.thenReturn(instance);
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("DBInstanceIdentifier", "mydb");
|
||||
p.add("Engine", "oracle");
|
||||
p.add("Engine", "postgres");
|
||||
p.add("MasterUsername", "admin");
|
||||
p.add("MasterUserPassword", "secret");
|
||||
p.add("DBName", "dbname");
|
||||
p.add("DBSubnetGroupName", "default");
|
||||
p.add("AvailabilityZone", "ap-northeast-1a");
|
||||
p.add("MultiAZ", "true");
|
||||
|
||||
Response response = handler.handle("CreateDBInstance", p);
|
||||
|
||||
assertEquals(400, response.getStatus());
|
||||
assertTrue(((String) response.getEntity()).contains("InvalidParameterValue"));
|
||||
assertTrue(((String) response.getEntity()).contains("Check the parameter constraints and try again."));
|
||||
assertEquals(200, response.getStatus());
|
||||
String body = (String) response.getEntity();
|
||||
assertTrue(body.contains("<AvailabilityZone>ap-northeast-1a</AvailabilityZone>"));
|
||||
assertTrue(body.contains("<DBSubnetGroupName>default</DBSubnetGroupName>"));
|
||||
assertTrue(body.contains("<DBSubnetGroupArn>arn:aws:rds:us-east-1:123456789012:subgrp:default</DBSubnetGroupArn>"));
|
||||
assertTrue(body.contains("<MultiAZ>true</MultiAZ>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbInstance_unknownSubnetGroupShouldFailValidation() {
|
||||
when(service.createDbInstance(eq("mydb"), eq("postgres"), eq("16.3"),
|
||||
eq("admin"), eq("secret"), eq("dbname"), eq("db.t3.micro"),
|
||||
eq(20), eq(false), eq(null), eq("missing-subnet-group"), eq(null), eq(null), eq(false),
|
||||
eq(false), eq(null), eq(java.util.Map.of())))
|
||||
.thenThrow(new AwsException("DBSubnetGroupNotFoundFault",
|
||||
"DB subnet group missing-subnet-group not found.", 404));
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("DBInstanceIdentifier", "mydb");
|
||||
p.add("Engine", "postgres");
|
||||
p.add("MasterUsername", "admin");
|
||||
p.add("MasterUserPassword", "secret");
|
||||
p.add("DBName", "dbname");
|
||||
p.add("DBSubnetGroupName", "missing-subnet-group");
|
||||
|
||||
Response response = handler.handle("CreateDBInstance", p);
|
||||
|
||||
assertEquals(404, response.getStatus());
|
||||
assertTrue(((String) response.getEntity()).contains("DBSubnetGroupNotFoundFault"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbSubnetGroup_passesSubnetMembersToService() {
|
||||
when(service.createDbSubnetGroup("sample-db-subnets", "test", List.of("subnet-aaa", "subnet-bbb")))
|
||||
.thenReturn(new DbSubnetGroup(
|
||||
"sample-db-subnets", "test", "vpc-123", List.of("subnet-aaa", "subnet-bbb")));
|
||||
"sample-db-subnets", "test", "vpc-123", List.of("subnet-aaa", "subnet-bbb"),
|
||||
Map.of("subnet-aaa", "us-east-1a", "subnet-bbb", "us-east-1b")));
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("DBSubnetGroupName", "sample-db-subnets");
|
||||
@@ -379,7 +437,8 @@ class RdsQueryHandlerTest {
|
||||
void modifyDbSubnetGroup_passesSubnetMembersToService() {
|
||||
when(service.modifyDbSubnetGroup("sample-db-subnets", List.of("subnet-new-a", "subnet-new-b")))
|
||||
.thenReturn(new DbSubnetGroup(
|
||||
"sample-db-subnets", "test", "vpc-123", List.of("subnet-new-a", "subnet-new-b")));
|
||||
"sample-db-subnets", "test", "vpc-123", List.of("subnet-new-a", "subnet-new-b"),
|
||||
Map.of("subnet-new-a", "us-east-1a", "subnet-new-b", "us-east-1b")));
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("DBSubnetGroupName", "sample-db-subnets");
|
||||
@@ -397,6 +456,27 @@ class RdsQueryHandlerTest {
|
||||
assertTrue(body.contains("<SubnetIdentifier>subnet-new-b</SubnetIdentifier>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbInstance_unknownEngineReturnsInvalidParameterValue() {
|
||||
// Handler defaults version to "1.0" for unknown engines, then the service
|
||||
// rejects the engine. Verify the full error path: version defaulting +
|
||||
// AwsException wrapping into a 400 query error.
|
||||
when(service.createDbInstance(eq("mydb"), eq("oracle"), eq("1.0"),
|
||||
eq(null), eq(null), eq(null), eq("db.t3.micro"),
|
||||
eq(20), eq(false), eq(null), eq(null), eq(null), eq(null), eq(false), eq(false),
|
||||
eq(null), eq(java.util.Map.of())))
|
||||
.thenThrow(new AwsException("InvalidParameterValue",
|
||||
"Unsupported engine: oracle. Supported: postgres, mysql, mariadb.", 400));
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("DBInstanceIdentifier", "mydb");
|
||||
p.add("Engine", "oracle");
|
||||
Response response = handler.handle("CreateDBInstance", p);
|
||||
|
||||
assertEquals(400, response.getStatus());
|
||||
assertTrue(((String) response.getEntity()).contains("InvalidParameterValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyDbParameterGroup_ignoresParametersWithoutValue() {
|
||||
DbParameterGroup group = new DbParameterGroup("pg1", "postgres15", "test group");
|
||||
@@ -451,6 +531,15 @@ class RdsQueryHandlerTest {
|
||||
assertTrue(((String) response.getEntity()).contains("DBClusterParameterGroupName is required."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbSubnetGroup_requiresNameWithMissingParameter() {
|
||||
Response response = handler.handle("CreateDBSubnetGroup", params());
|
||||
|
||||
assertEquals(400, response.getStatus());
|
||||
assertTrue(((String) response.getEntity()).contains("MissingParameter"));
|
||||
assertTrue(((String) response.getEntity()).contains("DBSubnetGroupName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbClusterParameterGroup_passesArgumentsToService() {
|
||||
DbClusterParameterGroup group = new DbClusterParameterGroup("cpg1", "aurora-postgresql16", "desc");
|
||||
@@ -517,6 +606,49 @@ class RdsQueryHandlerTest {
|
||||
"Did not expect nested DBSubnetGroupName inside DBCluster");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbSubnetGroup_shouldBeSupportedForCustomSubnetGroups() {
|
||||
DbSubnetGroup group = new DbSubnetGroup();
|
||||
group.setDbSubnetGroupName("my-subnet-group");
|
||||
group.setDescription("test subnet group");
|
||||
group.setDbSubnetGroupArn("arn:aws:rds:us-east-1:123456789012:subgrp:my-subnet-group");
|
||||
group.setVpcId("vpc-12345678");
|
||||
group.setSubnetIds(List.of("subnet-a", "subnet-b"));
|
||||
group.setSubnetAvailabilityZones(Map.of("subnet-a", "us-east-1a", "subnet-b", "us-east-1b"));
|
||||
when(service.createDbSubnetGroup("my-subnet-group", "test subnet group", List.of("subnet-a", "subnet-b")))
|
||||
.thenReturn(group);
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("DBSubnetGroupName", "my-subnet-group");
|
||||
p.add("DBSubnetGroupDescription", "test subnet group");
|
||||
p.add("SubnetIds.SubnetIdentifier.1", "subnet-a");
|
||||
p.add("SubnetIds.SubnetIdentifier.2", "subnet-b");
|
||||
|
||||
Response response = handler.handle("CreateDBSubnetGroup", p);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
String body = (String) response.getEntity();
|
||||
assertTrue(body.contains("<DBSubnetGroupName>my-subnet-group</DBSubnetGroupName>"));
|
||||
assertTrue(body.contains("<DBSubnetGroupArn>arn:aws:rds:us-east-1:123456789012:subgrp:my-subnet-group</DBSubnetGroupArn>"));
|
||||
assertTrue(body.contains("<SubnetIdentifier>subnet-a</SubnetIdentifier>"));
|
||||
assertTrue(body.contains("<SubnetIdentifier>subnet-b</SubnetIdentifier>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeDbSubnetGroups_shouldBeSupported() {
|
||||
DbSubnetGroup group = new DbSubnetGroup();
|
||||
group.setDbSubnetGroupName("default");
|
||||
group.setDbSubnetGroupArn("arn:aws:rds:us-east-1:123456789012:subgrp:default");
|
||||
when(service.listDbSubnetGroups(null)).thenReturn(List.of(group));
|
||||
|
||||
Response response = handler.handle("DescribeDBSubnetGroups", params());
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
String body = (String) response.getEntity();
|
||||
assertTrue(body.contains("<DBSubnetGroups>"));
|
||||
assertTrue(body.contains("<DBSubnetGroupArn>arn:aws:rds:us-east-1:123456789012:subgrp:default</DBSubnetGroupArn>"));
|
||||
}
|
||||
|
||||
// ──────────────────────────── Helpers ────────────────────────────
|
||||
|
||||
private static MultivaluedMap<String, String> params() {
|
||||
@@ -535,6 +667,28 @@ class RdsQueryHandlerTest {
|
||||
return i;
|
||||
}
|
||||
|
||||
private static DbSubnetGroup defaultSubnetGroup() {
|
||||
DbSubnetGroup group = new DbSubnetGroup();
|
||||
group.setDbSubnetGroupName("default");
|
||||
group.setDbSubnetGroupArn("arn:aws:rds:us-east-1:123456789012:subgrp:default");
|
||||
group.setVpcId("vpc-default");
|
||||
group.setSubnetGroupStatus("Complete");
|
||||
group.setSubnetIds(List.of("subnet-default-a", "subnet-default-b"));
|
||||
group.setSubnetAvailabilityZones(Map.of("subnet-default-a", "us-east-1a", "subnet-default-b", "us-east-1b"));
|
||||
return group;
|
||||
}
|
||||
|
||||
private static DbSubnetGroup customSubnetGroup() {
|
||||
DbSubnetGroup group = new DbSubnetGroup();
|
||||
group.setDbSubnetGroupName("custom-group");
|
||||
group.setDbSubnetGroupArn("arn:aws:rds:us-east-1:123456789012:subgrp:custom-group");
|
||||
group.setVpcId("vpc-12345678");
|
||||
group.setSubnetGroupStatus("Complete");
|
||||
group.setSubnetIds(List.of("subnet-a", "subnet-b"));
|
||||
group.setSubnetAvailabilityZones(Map.of("subnet-a", "us-east-1a", "subnet-b", "us-east-1b"));
|
||||
return group;
|
||||
}
|
||||
|
||||
private static DbCluster makeCluster(String id) {
|
||||
DbCluster c = new DbCluster();
|
||||
c.setDbClusterIdentifier(id);
|
||||
|
||||
@@ -6,6 +6,8 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Subnet;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbCluster;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbClusterParameterGroup;
|
||||
@@ -22,11 +24,15 @@ import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
@@ -38,6 +44,7 @@ class RdsServiceTest {
|
||||
private RdsService rdsService;
|
||||
private RdsContainerManager containerManager;
|
||||
private RdsProxyManager proxyManager;
|
||||
private Ec2Service ec2Service;
|
||||
private RegionResolver regionResolver;
|
||||
private EmulatorConfig config;
|
||||
|
||||
@@ -45,6 +52,7 @@ class RdsServiceTest {
|
||||
void setUp() {
|
||||
containerManager = mock(RdsContainerManager.class);
|
||||
proxyManager = mock(RdsProxyManager.class);
|
||||
ec2Service = mock(Ec2Service.class);
|
||||
regionResolver = new RegionResolver("us-east-1", "123456789012");
|
||||
config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig servicesConfig = mock(EmulatorConfig.ServicesConfig.class);
|
||||
@@ -60,17 +68,31 @@ class RdsServiceTest {
|
||||
|
||||
rdsService = newService(containerManager, proxyManager,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
|
||||
when(containerManager.start(any(), any(), any(), any(), any(), any(), any()))
|
||||
.thenReturn(new RdsContainerHandle("cont-id", "id", "localhost", 5432));
|
||||
when(ec2Service.describeSubnets(eq("us-east-1"), anyList(), any()))
|
||||
.thenAnswer(invocation -> {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> subnetIds = invocation.getArgument(1, List.class);
|
||||
if (subnetIds == null || subnetIds.isEmpty()) {
|
||||
return defaultSubnets();
|
||||
}
|
||||
Map<String, Subnet> byId = defaultSubnets().stream()
|
||||
.collect(Collectors.toMap(Subnet::getSubnetId, subnet -> subnet));
|
||||
return subnetIds.stream()
|
||||
.map(byId::get)
|
||||
.filter(java.util.Objects::nonNull)
|
||||
.toList();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbInstanceGeneratesMissingFields() {
|
||||
DbInstance instance = rdsService.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
20, false, null, null, null, null, false);
|
||||
|
||||
assertEquals("mydb", instance.getDbInstanceIdentifier());
|
||||
assertNotNull(instance.getDbiResourceId());
|
||||
@@ -123,7 +145,7 @@ class RdsServiceTest {
|
||||
void dbInstanceEndpointUsesResolvedProxyHost() {
|
||||
DockerHostResolver dockerHostResolver = mock(DockerHostResolver.class);
|
||||
when(dockerHostResolver.resolve()).thenReturn("floci.local");
|
||||
RdsService service = new RdsService(containerManager, proxyManager, regionResolver, config,
|
||||
RdsService service = new RdsService(containerManager, proxyManager, ec2Service, regionResolver, config,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), null, dockerHostResolver);
|
||||
|
||||
@@ -192,7 +214,7 @@ class RdsServiceTest {
|
||||
void listDbInstancesIsCaseInsensitive() {
|
||||
rdsService.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
20, false, null, null, null, null, false);
|
||||
|
||||
Collection<DbInstance> result = rdsService.listDbInstances("MYDB");
|
||||
assertEquals(1, result.size());
|
||||
@@ -212,7 +234,7 @@ class RdsServiceTest {
|
||||
void modifyDbInstanceBlankPasswordDoesNotOverwriteExistingPassword() {
|
||||
rdsService.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "original-password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
20, false, null, null, null, null, false);
|
||||
|
||||
DbInstance modified = rdsService.modifyDbInstance("mydb", " ", null, null);
|
||||
|
||||
@@ -224,7 +246,7 @@ class RdsServiceTest {
|
||||
void modifyDbInstanceCanToggleIamWithoutChangingPassword() {
|
||||
rdsService.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "original-password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
20, false, null, null, null, null, false);
|
||||
|
||||
DbInstance modified = rdsService.modifyDbInstance("mydb", null, true, null);
|
||||
|
||||
@@ -247,10 +269,10 @@ class RdsServiceTest {
|
||||
@Test
|
||||
void dbSubnetGroupRoundTrip() {
|
||||
DbSubnetGroup group = rdsService.createDbSubnetGroup(
|
||||
"sample-db-subnets", "test", java.util.List.of("subnet-aaa", "subnet-bbb"));
|
||||
"sample-db-subnets", "test", java.util.List.of("subnet-default-a", "subnet-default-b"));
|
||||
|
||||
assertEquals("sample-db-subnets", group.getDbSubnetGroupName());
|
||||
assertEquals(java.util.List.of("subnet-aaa", "subnet-bbb"), group.getSubnetIds());
|
||||
assertEquals(java.util.List.of("subnet-default-a", "subnet-default-b"), group.getSubnetIds());
|
||||
assertEquals(1, rdsService.listDbSubnetGroups("sample-db-subnets").size());
|
||||
|
||||
rdsService.deleteDbSubnetGroup("sample-db-subnets");
|
||||
@@ -309,7 +331,7 @@ class RdsServiceTest {
|
||||
@Test
|
||||
void deleteDbClusterFailsWhenMembersRemain() {
|
||||
DbCluster cluster = rdsService.createDbCluster("cluster1", "postgres", "13",
|
||||
"admin", "password", "dbname", false, null);
|
||||
"admin", "password", "dbname", false, null, null, null, false);
|
||||
cluster.getDbClusterMembers().add("instance-1");
|
||||
|
||||
AwsException exception = assertThrows(AwsException.class,
|
||||
@@ -365,6 +387,83 @@ class RdsServiceTest {
|
||||
assertEquals("DBParameterGroupAlreadyExists", exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbSubnetGroupRejectsDuplicateWithModelCode() {
|
||||
rdsService.createDbSubnetGroup("my-subnet-group", "desc", List.of("subnet-default-a", "subnet-default-b"));
|
||||
|
||||
AwsException exception = assertThrows(AwsException.class, () ->
|
||||
rdsService.createDbSubnetGroup("my-subnet-group", "desc", List.of("subnet-default-a", "subnet-default-b")));
|
||||
|
||||
assertEquals("DBSubnetGroupAlreadyExists", exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbSubnetGroupPopulatesArn() {
|
||||
DbSubnetGroup group = rdsService.createDbSubnetGroup("my-subnet-group", "desc",
|
||||
List.of("subnet-default-a", "subnet-default-b"));
|
||||
|
||||
assertEquals("arn:aws:rds:us-east-1:123456789012:subgrp:my-subnet-group", group.getDbSubnetGroupArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbSubnetGroupRequiresSubnetIdsWithMissingParameter() {
|
||||
AwsException exception = assertThrows(AwsException.class, () ->
|
||||
rdsService.createDbSubnetGroup("my-subnet-group", "desc", List.of()));
|
||||
|
||||
assertEquals("MissingParameter", exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbInstanceMultiAzRequiresSubnetGroupCoverageAcrossAvailabilityZones() {
|
||||
StorageBackend<String, DbSubnetGroup> subnetGroups = new InMemoryStorage<>();
|
||||
subnetGroups.put("single-az-group", new DbSubnetGroup(
|
||||
"single-az-group",
|
||||
"desc",
|
||||
"vpc-default",
|
||||
List.of("subnet-a", "subnet-b"),
|
||||
Map.of("subnet-a", "us-east-1a", "subnet-b", "us-east-1a")));
|
||||
RdsService service = newService(containerManager, proxyManager,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), subnetGroups);
|
||||
|
||||
AwsException exception = assertThrows(AwsException.class, () ->
|
||||
service.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, "single-az-group", null, null, true));
|
||||
|
||||
assertEquals("DBSubnetGroupDoesNotCoverEnoughAZs", exception.getErrorCode());
|
||||
verify(containerManager, never()).start(any(), any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbClusterRejectsAvailabilityZoneWhenMultiAzEnabled() {
|
||||
AwsException exception = assertThrows(AwsException.class, () ->
|
||||
rdsService.createDbCluster("cluster1", "postgres", "13",
|
||||
"admin", "password", "dbname", false,
|
||||
null, null, "us-east-1a", true));
|
||||
|
||||
assertEquals("InvalidParameterCombination", exception.getErrorCode());
|
||||
verify(containerManager, never()).start(any(), any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveDbSubnetGroupViewReturnsStoredCustomGroup() {
|
||||
rdsService.createDbSubnetGroup("my-subnet-group", "desc", List.of("subnet-default-a", "subnet-default-b"));
|
||||
|
||||
DbSubnetGroup group = rdsService.resolveDbSubnetGroupView("my-subnet-group");
|
||||
|
||||
assertEquals("my-subnet-group", group.getDbSubnetGroupName());
|
||||
assertEquals("arn:aws:rds:us-east-1:123456789012:subgrp:my-subnet-group", group.getDbSubnetGroupArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveDbSubnetGroupViewReturnsDefaultGroupForBlankName() {
|
||||
DbSubnetGroup group = rdsService.resolveDbSubnetGroupView(null);
|
||||
|
||||
assertEquals("default", group.getDbSubnetGroupName());
|
||||
assertEquals("arn:aws:rds:us-east-1:123456789012:subgrp:default", group.getDbSubnetGroupArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyDbClusterParameterGroupAppliesParameters() {
|
||||
rdsService.createDbClusterParameterGroup("cpg1", "aurora-postgresql16", "desc");
|
||||
@@ -405,10 +504,10 @@ class RdsServiceTest {
|
||||
.thenReturn(new RdsContainerHandle("initial-container", "mydb", "localhost", 5432));
|
||||
|
||||
RdsService initialService = newService(containerManager, proxyManager,
|
||||
instances, clusters, parameterGroups, clusterParameterGroups);
|
||||
instances, clusters, parameterGroups, clusterParameterGroups, new InMemoryStorage<>());
|
||||
DbInstance created = initialService.createDbInstance("mydb", "postgres", "16.3",
|
||||
"admin", "secret", "app", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
20, false, null, null, null, null, false);
|
||||
|
||||
String persistedVolumeId = created.getVolumeId();
|
||||
int persistedProxyPort = created.getProxyPort();
|
||||
@@ -419,7 +518,7 @@ class RdsServiceTest {
|
||||
.thenReturn(new RdsContainerHandle("restored-container", "mydb", "127.0.0.1", 15432));
|
||||
|
||||
RdsService restoredService = newService(restoredContainerManager, restoredProxyManager,
|
||||
instances, clusters, parameterGroups, clusterParameterGroups);
|
||||
instances, clusters, parameterGroups, clusterParameterGroups, new InMemoryStorage<>());
|
||||
restoredService.restorePersistedRuntime();
|
||||
|
||||
DbInstance restored = restoredService.getDbInstance("mydb");
|
||||
@@ -449,12 +548,12 @@ class RdsServiceTest {
|
||||
.thenReturn(new RdsContainerHandle("initial-cluster-container", "cluster1", "localhost", 5432));
|
||||
|
||||
RdsService initialService = newService(containerManager, proxyManager,
|
||||
instances, clusters, parameterGroups, clusterParameterGroups);
|
||||
instances, clusters, parameterGroups, clusterParameterGroups, new InMemoryStorage<>());
|
||||
DbCluster cluster = initialService.createDbCluster("cluster1", "aurora-postgresql", "16.3",
|
||||
"admin", "secret", "app", false, null);
|
||||
"admin", "secret", "app", false, null, null, null, false);
|
||||
DbInstance member = initialService.createDbInstance("member1", "aurora-postgresql", "16.3",
|
||||
"admin", "secret", "app", "db.t3.medium",
|
||||
20, false, null, null, "cluster1");
|
||||
20, false, null, null, "cluster1", null, false);
|
||||
|
||||
RdsContainerManager restoredContainerManager = mock(RdsContainerManager.class);
|
||||
RdsProxyManager restoredProxyManager = mock(RdsProxyManager.class);
|
||||
@@ -462,7 +561,7 @@ class RdsServiceTest {
|
||||
.thenReturn(new RdsContainerHandle("restored-cluster-container", "cluster1", "127.0.0.1", 15432));
|
||||
|
||||
RdsService restoredService = newService(restoredContainerManager, restoredProxyManager,
|
||||
instances, clusters, parameterGroups, clusterParameterGroups);
|
||||
instances, clusters, parameterGroups, clusterParameterGroups, new InMemoryStorage<>());
|
||||
restoredService.restorePersistedRuntime();
|
||||
|
||||
DbCluster restoredCluster = restoredService.getDbCluster("cluster1");
|
||||
@@ -491,8 +590,10 @@ class RdsServiceTest {
|
||||
StorageBackend<String, DbInstance> instances,
|
||||
StorageBackend<String, DbCluster> clusters,
|
||||
StorageBackend<String, DbParameterGroup> parameterGroups,
|
||||
StorageBackend<String, DbClusterParameterGroup> clusterParameterGroups) {
|
||||
return newService(containerManager, proxyManager, instances, clusters, parameterGroups, clusterParameterGroups, null);
|
||||
StorageBackend<String, DbClusterParameterGroup> clusterParameterGroups,
|
||||
StorageBackend<String, DbSubnetGroup> subnetGroups) {
|
||||
return new RdsService(containerManager, proxyManager, ec2Service, regionResolver, config,
|
||||
instances, clusters, parameterGroups, clusterParameterGroups, subnetGroups);
|
||||
}
|
||||
|
||||
private RdsService newService(RdsContainerManager containerManager,
|
||||
@@ -502,7 +603,22 @@ class RdsServiceTest {
|
||||
StorageBackend<String, DbParameterGroup> parameterGroups,
|
||||
StorageBackend<String, DbClusterParameterGroup> clusterParameterGroups,
|
||||
SecretsManagerService secretsManager) {
|
||||
return new RdsService(containerManager, proxyManager, regionResolver, config,
|
||||
instances, clusters, parameterGroups, clusterParameterGroups, new InMemoryStorage<>(), secretsManager);
|
||||
return new RdsService(containerManager, proxyManager, ec2Service, regionResolver, config,
|
||||
instances, clusters, parameterGroups, clusterParameterGroups, new InMemoryStorage<>(),
|
||||
secretsManager, null);
|
||||
}
|
||||
|
||||
private static List<Subnet> defaultSubnets() {
|
||||
Subnet subnetA = new Subnet();
|
||||
subnetA.setSubnetId("subnet-default-a");
|
||||
subnetA.setVpcId("vpc-default");
|
||||
subnetA.setAvailabilityZone("us-east-1a");
|
||||
|
||||
Subnet subnetB = new Subnet();
|
||||
subnetB.setSubnetId("subnet-default-b");
|
||||
subnetB.setVpcId("vpc-default");
|
||||
subnetB.setAvailabilityZone("us-east-1b");
|
||||
|
||||
return List.of(subnetA, subnetB);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user