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:
shrimptails-f authored and GitHub committed 2026-06-25 17:54:50 -05:00
1 parent 82170dd081
commit ad1cc22c19
9 files changed
+708 -142

No files matched your search

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