mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(rds): preserve network placement metadata (#1735)
* fix(rds): honor request region for subnet placement * fix(rds): persist DB instance VPC security groups * docs: sync service action tables * fix(rds): tidy region-aware query dispatch
This commit is contained in:
1 parent
10f799a2b7
commit
fe2008848f
9 files changed
+450
-106
No files matched your search
@@ -281,7 +281,7 @@ public class AwsQueryController {
|
||||
|| docDbService.hasInstance(instanceId)) {
|
||||
yield docDbQueryHandler.handle(action, formParams);
|
||||
}
|
||||
yield rdsQueryHandler.handle(action, formParams);
|
||||
yield rdsQueryHandler.handle(action, formParams, region);
|
||||
}
|
||||
case "neptune" -> neptuneQueryHandler.handle(action, formParams);
|
||||
case "docdb" -> docDbQueryHandler.handle(action, formParams);
|
||||
|
||||
+11
-9
@@ -326,12 +326,12 @@ public class CloudFormationResourceProvisioner {
|
||||
provisionFirehoseDeliveryStream(resource, properties, engine, stackName);
|
||||
case "AWS::EC2::Instance" -> provisionEc2Instance(resource, properties, engine, region);
|
||||
// RDS. DBInstance/DBCluster start real RDS containers (same as the direct API).
|
||||
case "AWS::RDS::DBSubnetGroup" -> provisionDbSubnetGroup(resource, properties, engine, stackName);
|
||||
case "AWS::RDS::DBSubnetGroup" -> provisionDbSubnetGroup(resource, properties, engine, stackName, region);
|
||||
case "AWS::RDS::DBParameterGroup" -> provisionDbParameterGroup(resource, properties, engine, stackName);
|
||||
case "AWS::RDS::DBClusterParameterGroup" ->
|
||||
provisionDbClusterParameterGroup(resource, properties, engine, stackName);
|
||||
case "AWS::RDS::DBInstance" -> provisionDbInstance(resource, properties, engine, stackName);
|
||||
case "AWS::RDS::DBCluster" -> provisionDbCluster(resource, properties, engine, stackName);
|
||||
case "AWS::RDS::DBInstance" -> provisionDbInstance(resource, properties, engine, stackName, region);
|
||||
case "AWS::RDS::DBCluster" -> provisionDbCluster(resource, properties, engine, stackName, region);
|
||||
case "AWS::EKS::Cluster" -> provisionEksCluster(resource, properties, engine, stackName);
|
||||
case "AWS::EKS::Nodegroup" -> provisionEksNodegroup(resource, properties, engine, stackName);
|
||||
case "AWS::Logs::LogGroup" -> provisionLogGroup(resource, properties, engine, region, accountId, stackName);
|
||||
@@ -949,7 +949,7 @@ public class CloudFormationResourceProvisioner {
|
||||
// ── RDS ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
private void provisionDbSubnetGroup(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
|
||||
String stackName) {
|
||||
String stackName, String region) {
|
||||
String name = resolveOptional(props, "DBSubnetGroupName", engine);
|
||||
if (name == null || name.isBlank()) {
|
||||
name = generatePhysicalName(stackName, r.getLogicalId(), 60, true);
|
||||
@@ -962,7 +962,7 @@ public class CloudFormationResourceProvisioner {
|
||||
subnetIds.add(engine.resolve(subnet));
|
||||
}
|
||||
}
|
||||
var group = rdsService.createDbSubnetGroup(name, description, subnetIds);
|
||||
var group = rdsService.createDbSubnetGroup(name, description, subnetIds, region);
|
||||
r.setPhysicalId(group.getDbSubnetGroupName());
|
||||
r.getAttributes().put("DBSubnetGroupName", group.getDbSubnetGroupName());
|
||||
}
|
||||
@@ -996,7 +996,7 @@ public class CloudFormationResourceProvisioner {
|
||||
}
|
||||
|
||||
private void provisionDbInstance(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
|
||||
String stackName) {
|
||||
String stackName, String region) {
|
||||
String id = resolveOptional(props, "DBInstanceIdentifier", engine);
|
||||
if (id == null || id.isBlank()) {
|
||||
id = generatePhysicalName(stackName, r.getLogicalId(), 60, true);
|
||||
@@ -1013,7 +1013,8 @@ public class CloudFormationResourceProvisioner {
|
||||
parseBoolProp(props, "EnableIAMDatabaseAuthentication", engine),
|
||||
resolveOptional(props, "DBParameterGroupName", engine),
|
||||
resolveOptional(props, "DBSubnetGroupName", engine),
|
||||
resolveOptional(props, "DBClusterIdentifier", engine));
|
||||
resolveOptional(props, "DBClusterIdentifier", engine),
|
||||
null, false, false, null, Map.of(), region);
|
||||
r.setPhysicalId(instance.getDbInstanceIdentifier());
|
||||
r.getAttributes().put("DBInstanceIdentifier", instance.getDbInstanceIdentifier());
|
||||
if (instance.getEndpoint() != null) {
|
||||
@@ -1026,7 +1027,7 @@ public class CloudFormationResourceProvisioner {
|
||||
}
|
||||
|
||||
private void provisionDbCluster(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
|
||||
String stackName) {
|
||||
String stackName, String region) {
|
||||
String id = resolveOptional(props, "DBClusterIdentifier", engine);
|
||||
if (id == null || id.isBlank()) {
|
||||
id = generatePhysicalName(stackName, r.getLogicalId(), 60, true);
|
||||
@@ -1039,7 +1040,8 @@ public class CloudFormationResourceProvisioner {
|
||||
resolveOptional(props, "MasterUserPassword", engine),
|
||||
resolveOptional(props, "DatabaseName", engine),
|
||||
parseBoolProp(props, "EnableIAMDatabaseAuthentication", engine),
|
||||
resolveOptional(props, "DBClusterParameterGroupName", engine));
|
||||
resolveOptional(props, "DBClusterParameterGroupName", engine),
|
||||
null, null, false, region);
|
||||
r.setPhysicalId(cluster.getDbClusterIdentifier());
|
||||
r.getAttributes().put("DBClusterIdentifier", cluster.getDbClusterIdentifier());
|
||||
if (cluster.getEndpoint() != null) {
|
||||
|
||||
@@ -42,20 +42,24 @@ public class RdsQueryHandler {
|
||||
}
|
||||
|
||||
public Response handle(String action, MultivaluedMap<String, String> params) {
|
||||
return handle(action, params, null);
|
||||
}
|
||||
|
||||
public Response handle(String action, MultivaluedMap<String, String> params, String region) {
|
||||
LOG.infov("RDS action: {0}", action);
|
||||
try {
|
||||
return switch (action) {
|
||||
case "CreateDBInstance" -> handleCreateDbInstance(params);
|
||||
case "CreateDBInstance" -> handleCreateDbInstance(params, region);
|
||||
case "DescribeDBInstances" -> handleDescribeDbInstances(params);
|
||||
case "DeleteDBInstance" -> handleDeleteDbInstance(params);
|
||||
case "ModifyDBInstance" -> handleModifyDbInstance(params);
|
||||
case "RebootDBInstance" -> handleRebootDbInstance(params);
|
||||
case "DescribeOrderableDBInstanceOptions" -> handleDescribeOrderableDbInstanceOptions(params);
|
||||
case "CreateDBSubnetGroup" -> handleCreateDbSubnetGroup(params);
|
||||
case "DescribeDBSubnetGroups" -> handleDescribeDbSubnetGroups(params);
|
||||
case "ModifyDBSubnetGroup" -> handleModifyDbSubnetGroup(params);
|
||||
case "CreateDBSubnetGroup" -> handleCreateDbSubnetGroup(params, region);
|
||||
case "DescribeDBSubnetGroups" -> handleDescribeDbSubnetGroups(params, region);
|
||||
case "ModifyDBSubnetGroup" -> handleModifyDbSubnetGroup(params, region);
|
||||
case "DeleteDBSubnetGroup" -> handleDeleteDbSubnetGroup(params);
|
||||
case "CreateDBCluster" -> handleCreateDbCluster(params);
|
||||
case "CreateDBCluster" -> handleCreateDbCluster(params, region);
|
||||
case "DescribeDBClusters" -> handleDescribeDbClusters(params);
|
||||
case "DeleteDBCluster" -> handleDeleteDbCluster(params);
|
||||
case "ModifyDBCluster" -> handleModifyDbCluster(params);
|
||||
@@ -88,7 +92,7 @@ public class RdsQueryHandler {
|
||||
|
||||
// ── DB Instances ──────────────────────────────────────────────────────────
|
||||
|
||||
private Response handleCreateDbInstance(MultivaluedMap<String, String> params) {
|
||||
private Response handleCreateDbInstance(MultivaluedMap<String, String> params, String region) {
|
||||
String id = params.getFirst("DBInstanceIdentifier");
|
||||
if (id == null || id.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue",
|
||||
@@ -121,10 +125,11 @@ public class RdsQueryHandler {
|
||||
}
|
||||
|
||||
try {
|
||||
List<String> vpcSecurityGroupIds = vpcSecurityGroupIds(params);
|
||||
DbInstance instance = service.createDbInstance(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, dbName, dbInstanceClass, allocatedStorage, iamEnabled,
|
||||
paramGroupName, dbSubnetGroupName, dbClusterIdentifier, availabilityZone, multiAz,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, tags);
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, tags, vpcSecurityGroupIds, region);
|
||||
String result = dbInstanceXml(instance);
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateDBInstance", AwsNamespaces.RDS, result)).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -175,7 +180,9 @@ public class RdsQueryHandler {
|
||||
Boolean iamEnabled = iamStr != null ? Boolean.parseBoolean(iamStr) : null;
|
||||
String dbSubnetGroupName = params.getFirst("DBSubnetGroupName");
|
||||
try {
|
||||
DbInstance instance = service.modifyDbInstance(id, newPassword, iamEnabled, dbSubnetGroupName);
|
||||
List<String> vpcSecurityGroupIds = vpcSecurityGroupIds(params);
|
||||
DbInstance instance = service.modifyDbInstance(
|
||||
id, newPassword, iamEnabled, dbSubnetGroupName, vpcSecurityGroupIds);
|
||||
String result = dbInstanceXml(instance);
|
||||
return Response.ok(AwsQueryResponse.envelope("ModifyDBInstance", AwsNamespaces.RDS, result)).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -239,7 +246,7 @@ public class RdsQueryHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleCreateDbSubnetGroup(MultivaluedMap<String, String> params) {
|
||||
private Response handleCreateDbSubnetGroup(MultivaluedMap<String, String> params, String region) {
|
||||
String name = params.getFirst("DBSubnetGroupName");
|
||||
if (name == null || name.isBlank()) {
|
||||
return AwsQueryResponse.error("MissingParameter",
|
||||
@@ -248,7 +255,7 @@ public class RdsQueryHandler {
|
||||
String description = params.getFirst("DBSubnetGroupDescription");
|
||||
List<String> subnetIds = memberList(params, "SubnetIds");
|
||||
try {
|
||||
DbSubnetGroup group = service.createDbSubnetGroup(name, description, subnetIds);
|
||||
DbSubnetGroup group = service.createDbSubnetGroup(name, description, subnetIds, region);
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateDBSubnetGroup",
|
||||
AwsNamespaces.RDS, dbSubnetGroupXml(group))).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -256,10 +263,10 @@ public class RdsQueryHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleDescribeDbSubnetGroups(MultivaluedMap<String, String> params) {
|
||||
private Response handleDescribeDbSubnetGroups(MultivaluedMap<String, String> params, String region) {
|
||||
String filterName = params.getFirst("DBSubnetGroupName");
|
||||
try {
|
||||
Collection<DbSubnetGroup> result = service.listDbSubnetGroups(filterName);
|
||||
Collection<DbSubnetGroup> result = service.listDbSubnetGroups(filterName, region);
|
||||
XmlBuilder xml = new XmlBuilder().start("DBSubnetGroups");
|
||||
for (DbSubnetGroup group : result) {
|
||||
xml.start("DBSubnetGroup").raw(dbSubnetGroupInnerXml(group)).end("DBSubnetGroup");
|
||||
@@ -271,7 +278,7 @@ public class RdsQueryHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleModifyDbSubnetGroup(MultivaluedMap<String, String> params) {
|
||||
private Response handleModifyDbSubnetGroup(MultivaluedMap<String, String> params, String region) {
|
||||
String name = params.getFirst("DBSubnetGroupName");
|
||||
if (name == null || name.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue",
|
||||
@@ -279,7 +286,7 @@ public class RdsQueryHandler {
|
||||
}
|
||||
List<String> subnetIds = memberList(params, "SubnetIds");
|
||||
try {
|
||||
DbSubnetGroup group = service.modifyDbSubnetGroup(name, subnetIds);
|
||||
DbSubnetGroup group = service.modifyDbSubnetGroup(name, subnetIds, region);
|
||||
return Response.ok(AwsQueryResponse.envelope("ModifyDBSubnetGroup",
|
||||
AwsNamespaces.RDS, dbSubnetGroupXml(group))).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -317,7 +324,7 @@ public class RdsQueryHandler {
|
||||
|
||||
// ── DB Clusters ───────────────────────────────────────────────────────────
|
||||
|
||||
private Response handleCreateDbCluster(MultivaluedMap<String, String> params) {
|
||||
private Response handleCreateDbCluster(MultivaluedMap<String, String> params, String region) {
|
||||
String id = params.getFirst("DBClusterIdentifier");
|
||||
if (id == null || id.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue", "DBClusterIdentifier is required.", AwsNamespaces.RDS, 400);
|
||||
@@ -341,7 +348,7 @@ public class RdsQueryHandler {
|
||||
try {
|
||||
DbCluster cluster = service.createDbCluster(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, databaseName, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, availabilityZone, multiAz);
|
||||
dbSubnetGroupName, availabilityZone, multiAz, region);
|
||||
String result = dbClusterXml(cluster);
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateDBCluster", AwsNamespaces.RDS, result)).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -650,12 +657,7 @@ public class RdsQueryHandler {
|
||||
.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")
|
||||
.start("VpcSecurityGroupMembership")
|
||||
.elem("VpcSecurityGroupId", "sg-00000000")
|
||||
.elem("Status", "active")
|
||||
.end("VpcSecurityGroupMembership")
|
||||
.end("VpcSecurityGroups")
|
||||
.raw(vpcSecurityGroupsXml(i))
|
||||
.raw(dbParameterGroupsXml(i))
|
||||
.raw(dbSubnetGroupXml(dbSubnetGroupForInstance(i)))
|
||||
.elem("DbiResourceId", i.getDbiResourceId())
|
||||
@@ -678,6 +680,29 @@ public class RdsQueryHandler {
|
||||
return xml.build();
|
||||
}
|
||||
|
||||
private String vpcSecurityGroupsXml(DbInstance i) {
|
||||
List<String> groupIds = i.getVpcSecurityGroupIds().isEmpty()
|
||||
? List.of("sg-00000000")
|
||||
: i.getVpcSecurityGroupIds();
|
||||
XmlBuilder xml = new XmlBuilder().start("VpcSecurityGroups");
|
||||
for (String groupId : groupIds) {
|
||||
xml.start("VpcSecurityGroupMembership")
|
||||
.elem("VpcSecurityGroupId", groupId)
|
||||
.elem("Status", "active")
|
||||
.end("VpcSecurityGroupMembership");
|
||||
}
|
||||
return xml.end("VpcSecurityGroups").build();
|
||||
}
|
||||
|
||||
private static List<String> vpcSecurityGroupIds(MultivaluedMap<String, String> params) {
|
||||
List<String> values = memberList(params, "VpcSecurityGroupIds");
|
||||
if (values.isEmpty() && hasMemberKeys(params, "VpcSecurityGroupIds")) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"VpcSecurityGroupIds must contain at least one non-empty VpcSecurityGroupId.", 400);
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
private static String dbParameterGroupsXml(DbInstance instance) {
|
||||
String name = dbParameterGroupName(instance);
|
||||
|
||||
@@ -812,26 +837,10 @@ public class RdsQueryHandler {
|
||||
|
||||
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) {
|
||||
}
|
||||
if (groupName == null || groupName.isBlank() || "default".equalsIgnoreCase(groupName)) {
|
||||
return fallbackSubnetGroup(instance, "default", "default subnet group");
|
||||
}
|
||||
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");
|
||||
return service.getDbSubnetGroup(groupName);
|
||||
}
|
||||
|
||||
private DbSubnetGroup fallbackSubnetGroup(DbInstance instance, String name, String description) {
|
||||
@@ -840,6 +849,7 @@ public class RdsQueryHandler {
|
||||
fallback.setDescription(description);
|
||||
fallback.setVpcId(instance.getVpcId() != null ? instance.getVpcId() : "vpc-00000000");
|
||||
fallback.setSubnetGroupStatus("Complete");
|
||||
fallback.setDbSubnetGroupArn(subnetGroupArnForInstance(instance, name));
|
||||
Map<String, String> zones = instance.getSubnetAvailabilityZones();
|
||||
if (!zones.isEmpty()) {
|
||||
fallback.setSubnetIds(List.copyOf(zones.keySet()));
|
||||
@@ -851,6 +861,18 @@ public class RdsQueryHandler {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
private static String subnetGroupArnForInstance(DbInstance instance, String name) {
|
||||
String arn = instance.getDbInstanceArn();
|
||||
if (arn == null || arn.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String[] parts = arn.split(":", 6);
|
||||
if (parts.length < 6) {
|
||||
return null;
|
||||
}
|
||||
return String.join(":", parts[0], parts[1], parts[2], parts[3], parts[4], "subgrp:" + name);
|
||||
}
|
||||
|
||||
private String paramGroupInnerXml(DbParameterGroup g) {
|
||||
return new XmlBuilder()
|
||||
.elem("DBParameterGroupName", g.getDbParameterGroupName())
|
||||
@@ -901,14 +923,26 @@ public class RdsQueryHandler {
|
||||
|
||||
private static List<String> memberList(MultivaluedMap<String, String> params, String baseName) {
|
||||
return params.keySet().stream()
|
||||
.filter(key -> key.matches(java.util.regex.Pattern.quote(baseName)
|
||||
+ "(\\.member|\\.SubnetIdentifier)?\\.\\d+"))
|
||||
.filter(key -> key.matches(memberKeyRegex(baseName)))
|
||||
.sorted(java.util.Comparator.comparingInt(RdsQueryHandler::numericSuffix))
|
||||
.map(params::getFirst)
|
||||
.filter(value -> value != null && !value.isBlank())
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static boolean hasMemberKeys(MultivaluedMap<String, String> params, String baseName) {
|
||||
return params.keySet().stream().anyMatch(key -> key.matches(memberKeyRegex(baseName)));
|
||||
}
|
||||
|
||||
private static String memberKeyRegex(String baseName) {
|
||||
String quoted = java.util.regex.Pattern.quote(baseName);
|
||||
return switch (baseName) {
|
||||
case "SubnetIds" -> quoted + "(\\.member|\\.SubnetIdentifier)?\\.\\d+";
|
||||
case "VpcSecurityGroupIds" -> quoted + "(\\.member|\\.VpcSecurityGroupId)?\\.\\d+";
|
||||
default -> quoted + "(\\.member)?\\.\\d+";
|
||||
};
|
||||
}
|
||||
|
||||
private static int numericSuffix(String key) {
|
||||
int dot = key.lastIndexOf('.');
|
||||
if (dot < 0 || dot == key.length() - 1) {
|
||||
|
||||
@@ -213,13 +213,63 @@ public class RdsService implements Resettable {
|
||||
boolean multiAz, boolean manageMasterUserPassword,
|
||||
String masterUserSecretKmsKeyId,
|
||||
Map<String, String> tags) {
|
||||
return createDbInstance(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
dbName, dbInstanceClass, allocatedStorage, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, dbClusterIdentifier, availabilityZone, multiAz,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, tags, List.of(), regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
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,
|
||||
List<String> vpcSecurityGroupIds) {
|
||||
return createDbInstance(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
dbName, dbInstanceClass, allocatedStorage, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, dbClusterIdentifier, availabilityZone, multiAz,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, tags, vpcSecurityGroupIds,
|
||||
regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
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, String region) {
|
||||
return createDbInstance(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
dbName, dbInstanceClass, allocatedStorage, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, dbClusterIdentifier, availabilityZone, multiAz,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, tags, List.of(), region);
|
||||
}
|
||||
|
||||
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,
|
||||
List<String> vpcSecurityGroupIds,
|
||||
String region) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
if (instances.get(id).isPresent()) {
|
||||
throw new AwsException("DBInstanceAlreadyExists",
|
||||
"DB instance " + id + " already exists.", 400);
|
||||
}
|
||||
|
||||
DatabaseEngine engine = resolveEngine(engineParam);
|
||||
if (dbSubnetGroupName != null && !dbSubnetGroupName.isBlank()) {
|
||||
if (dbSubnetGroupName != null && !dbSubnetGroupName.isBlank() && !"default".equalsIgnoreCase(dbSubnetGroupName)) {
|
||||
getDbSubnetGroup(dbSubnetGroupName);
|
||||
}
|
||||
validateInstanceParameterGroup(paramGroupName, engineParam, engineVersion);
|
||||
@@ -262,7 +312,7 @@ public class RdsService implements Resettable {
|
||||
}
|
||||
placement = PlacementResolution.fromCluster(cluster);
|
||||
} else {
|
||||
placement = resolvePlacement(dbSubnetGroupName, availabilityZone, multiAz);
|
||||
placement = resolvePlacement(dbSubnetGroupName, availabilityZone, multiAz, effectiveRegion);
|
||||
if (!mock) {
|
||||
// Standalone instance — start its own container
|
||||
String image = imageForEngine(engine, engineVersion);
|
||||
@@ -288,17 +338,17 @@ public class RdsService implements Resettable {
|
||||
instance.setVolumeId(instanceVolumeId);
|
||||
instance.setDockerVolumeName(instanceDockerVolumeName);
|
||||
instance.setTags(tags);
|
||||
instance.setVpcSecurityGroupIds(vpcSecurityGroupIds);
|
||||
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());
|
||||
instance.setDbInstanceArn(regionResolver.buildArn("rds", region, "db:" + id));
|
||||
instance.setDbInstanceArn(regionResolver.buildArn("rds", effectiveRegion, "db:" + id));
|
||||
if (manageMasterUserPassword) {
|
||||
attachManagedMasterUserSecret(instance, region, masterUserSecretKmsKeyId);
|
||||
attachManagedMasterUserSecret(instance, effectiveRegion, masterUserSecretKmsKeyId);
|
||||
}
|
||||
|
||||
if (!mock) {
|
||||
@@ -459,6 +509,11 @@ public class RdsService implements Resettable {
|
||||
|
||||
public DbInstance modifyDbInstance(String id, String newPassword, Boolean iamEnabled,
|
||||
String dbSubnetGroupName) {
|
||||
return modifyDbInstance(id, newPassword, iamEnabled, dbSubnetGroupName, null);
|
||||
}
|
||||
|
||||
public DbInstance modifyDbInstance(String id, String newPassword, Boolean iamEnabled,
|
||||
String dbSubnetGroupName, List<String> vpcSecurityGroupIds) {
|
||||
DbInstance instance = getDbInstance(id);
|
||||
instance.setStatus(DbInstanceStatus.AVAILABLE);
|
||||
if (newPassword != null && !newPassword.isBlank()) {
|
||||
@@ -471,6 +526,9 @@ public class RdsService implements Resettable {
|
||||
getDbSubnetGroup(dbSubnetGroupName);
|
||||
instance.setDbSubnetGroupName(dbSubnetGroupName);
|
||||
}
|
||||
if (vpcSecurityGroupIds != null && !vpcSecurityGroupIds.isEmpty()) {
|
||||
instance.setVpcSecurityGroupIds(vpcSecurityGroupIds);
|
||||
}
|
||||
instances.put(id, instance);
|
||||
LOG.infov("DB instance {0} modified", id);
|
||||
return instance;
|
||||
@@ -600,6 +658,17 @@ public class RdsService implements Resettable {
|
||||
String databaseName, boolean iamEnabled,
|
||||
String paramGroupName, String dbSubnetGroupName,
|
||||
String availabilityZone, boolean multiAz) {
|
||||
return createDbCluster(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
databaseName, iamEnabled, paramGroupName, dbSubnetGroupName,
|
||||
availabilityZone, multiAz, regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
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, String region) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
if (clusters.get(id).isPresent()) {
|
||||
throw new AwsException("DBClusterAlreadyExistsFault",
|
||||
"DB cluster " + id + " already exists.", 400);
|
||||
@@ -607,7 +676,7 @@ public class RdsService implements Resettable {
|
||||
|
||||
DatabaseEngine engine = resolveEngine(engineParam);
|
||||
validateClusterParameterGroup(paramGroupName, engineParam, engineVersion);
|
||||
PlacementResolution placement = resolvePlacement(dbSubnetGroupName, availabilityZone, multiAz);
|
||||
PlacementResolution placement = resolvePlacement(dbSubnetGroupName, availabilityZone, multiAz, effectiveRegion);
|
||||
|
||||
boolean mock = config.services().rds().mock();
|
||||
// Always reserve a unique port (even in mock) so endpoints stay distinct and usedPorts
|
||||
@@ -633,9 +702,8 @@ public class RdsService implements Resettable {
|
||||
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());
|
||||
cluster.setDbClusterArn(regionResolver.buildArn("rds", region, "cluster:" + id));
|
||||
cluster.setDbClusterArn(regionResolver.buildArn("rds", effectiveRegion, "cluster:" + id));
|
||||
|
||||
if (!mock) {
|
||||
String effectiveMasterUser = masterUsername != null ? masterUsername : "root";
|
||||
@@ -705,6 +773,10 @@ public class RdsService implements Resettable {
|
||||
// ── DB Subnet Groups ──────────────────────────────────────────────────────
|
||||
|
||||
public DbSubnetGroup createDbSubnetGroup(String name, String description, List<String> subnetIds) {
|
||||
return createDbSubnetGroup(name, description, subnetIds, regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
public DbSubnetGroup createDbSubnetGroup(String name, String description, List<String> subnetIds, String region) {
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter DBSubnetGroupName.", 400);
|
||||
}
|
||||
@@ -716,15 +788,19 @@ public class RdsService implements Resettable {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter SubnetIds.", 400);
|
||||
}
|
||||
|
||||
DbSubnetGroup group = buildSubnetGroup(name, description, subnetIds);
|
||||
DbSubnetGroup group = buildSubnetGroup(name, description, subnetIds, effectiveRegion(region));
|
||||
subnetGroups.put(name, group);
|
||||
return group;
|
||||
}
|
||||
|
||||
public Collection<DbSubnetGroup> listDbSubnetGroups(String filterName) {
|
||||
return listDbSubnetGroups(filterName, regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
public Collection<DbSubnetGroup> listDbSubnetGroups(String filterName, String region) {
|
||||
List<DbSubnetGroup> groups = new ArrayList<>();
|
||||
if (filterName == null || filterName.isBlank() || "default".equalsIgnoreCase(filterName)) {
|
||||
groups.add(buildDefaultSubnetGroup());
|
||||
groups.add(buildDefaultSubnetGroup(effectiveRegion(region)));
|
||||
}
|
||||
if (filterName != null && !filterName.isBlank()) {
|
||||
subnetGroups.get(filterName).ifPresent(groups::add);
|
||||
@@ -735,9 +811,13 @@ public class RdsService implements Resettable {
|
||||
}
|
||||
|
||||
public DbSubnetGroup resolveDbSubnetGroupView(String name) {
|
||||
return resolveDbSubnetGroupView(name, regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
public DbSubnetGroup resolveDbSubnetGroupView(String name, String region) {
|
||||
String effectiveName = (name == null || name.isBlank()) ? "default" : name;
|
||||
if ("default".equalsIgnoreCase(effectiveName)) {
|
||||
return buildDefaultSubnetGroup();
|
||||
return buildDefaultSubnetGroup(effectiveRegion(region));
|
||||
}
|
||||
return subnetGroups.get(effectiveName).orElseThrow(() ->
|
||||
new AwsException("DBSubnetGroupNotFoundFault",
|
||||
@@ -788,8 +868,12 @@ public class RdsService implements Resettable {
|
||||
}
|
||||
|
||||
public DbSubnetGroup getDbSubnetGroup(String name) {
|
||||
return getDbSubnetGroup(name, regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
public DbSubnetGroup getDbSubnetGroup(String name, String region) {
|
||||
if ("default".equalsIgnoreCase(name)) {
|
||||
return buildDefaultSubnetGroup();
|
||||
return buildDefaultSubnetGroup(effectiveRegion(region));
|
||||
}
|
||||
return subnetGroups.get(name).orElseThrow(() ->
|
||||
new AwsException("DBSubnetGroupNotFoundFault",
|
||||
@@ -797,12 +881,16 @@ public class RdsService implements Resettable {
|
||||
}
|
||||
|
||||
public DbSubnetGroup modifyDbSubnetGroup(String name, List<String> subnetIds) {
|
||||
return modifyDbSubnetGroup(name, subnetIds, regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
public DbSubnetGroup modifyDbSubnetGroup(String name, List<String> subnetIds, String region) {
|
||||
DbSubnetGroup existing = getDbSubnetGroup(name);
|
||||
if (subnetIds == null || subnetIds.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"SubnetIds must contain at least one subnet.", 400);
|
||||
}
|
||||
DbSubnetGroup group = buildSubnetGroup(name, existing.getDescription(), subnetIds);
|
||||
DbSubnetGroup group = buildSubnetGroup(name, existing.getDescription(), subnetIds, effectiveRegion(region));
|
||||
group.setTags(existing.getTags());
|
||||
subnetGroups.put(name, group);
|
||||
return group;
|
||||
@@ -1138,11 +1226,16 @@ public class RdsService implements Resettable {
|
||||
}
|
||||
|
||||
private PlacementResolution resolvePlacement(String dbSubnetGroupName, String availabilityZone, boolean multiAz) {
|
||||
return resolvePlacement(dbSubnetGroupName, availabilityZone, multiAz, regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
private PlacementResolution resolvePlacement(String dbSubnetGroupName, String availabilityZone, boolean multiAz,
|
||||
String region) {
|
||||
String effectiveSubnetGroupName = (dbSubnetGroupName == null || dbSubnetGroupName.isBlank())
|
||||
? "default"
|
||||
: dbSubnetGroupName;
|
||||
DbSubnetGroup group = "default".equals(effectiveSubnetGroupName)
|
||||
? buildDefaultSubnetGroup()
|
||||
? buildDefaultSubnetGroup(region)
|
||||
: subnetGroups.get(effectiveSubnetGroupName).orElseThrow(() ->
|
||||
new AwsException("DBSubnetGroupNotFoundFault",
|
||||
"DB subnet group " + effectiveSubnetGroupName + " not found.", 404));
|
||||
@@ -1187,18 +1280,16 @@ public class RdsService implements Resettable {
|
||||
new LinkedHashMap<>(subnetAvailabilityZones));
|
||||
}
|
||||
|
||||
private DbSubnetGroup buildDefaultSubnetGroup() {
|
||||
String region = regionResolver.getDefaultRegion();
|
||||
private DbSubnetGroup buildDefaultSubnetGroup(String region) {
|
||||
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));
|
||||
return buildSubnetGroup("default", "default subnet group", extractSubnetIds(subnets), region);
|
||||
}
|
||||
|
||||
private DbSubnetGroup buildSubnetGroup(String name, String description, List<String> subnetIds) {
|
||||
String region = regionResolver.getDefaultRegion();
|
||||
private DbSubnetGroup buildSubnetGroup(String name, String description, List<String> subnetIds, String region) {
|
||||
List<Subnet> resolvedSubnets = ec2Service.describeSubnets(region, subnetIds, Map.of());
|
||||
if (resolvedSubnets.size() != subnetIds.size()) {
|
||||
throw new AwsException("InvalidSubnet",
|
||||
@@ -1226,6 +1317,10 @@ public class RdsService implements Resettable {
|
||||
return group;
|
||||
}
|
||||
|
||||
private String effectiveRegion(String region) {
|
||||
return region == null || region.isBlank() ? regionResolver.getDefaultRegion() : region;
|
||||
}
|
||||
|
||||
private static List<String> extractSubnetIds(List<Subnet> subnets) {
|
||||
return subnets.stream().map(Subnet::getSubnetId).toList();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@ package io.github.hectorvent.floci.services.rds.model;
|
||||
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
|
||||
@@ -24,6 +26,7 @@ public class DbInstance {
|
||||
private String dbSubnetGroupName;
|
||||
private String dbClusterIdentifier;
|
||||
private String vpcId;
|
||||
private List<String> vpcSecurityGroupIds = new ArrayList<>();
|
||||
private String availabilityZone;
|
||||
private boolean multiAz;
|
||||
private Map<String, String> subnetAvailabilityZones = new LinkedHashMap<>();
|
||||
@@ -115,6 +118,11 @@ public class DbInstance {
|
||||
public String getVpcId() { return vpcId; }
|
||||
public void setVpcId(String vpcId) { this.vpcId = vpcId; }
|
||||
|
||||
public List<String> getVpcSecurityGroupIds() { return vpcSecurityGroupIds; }
|
||||
public void setVpcSecurityGroupIds(List<String> vpcSecurityGroupIds) {
|
||||
this.vpcSecurityGroupIds = vpcSecurityGroupIds != null ? new ArrayList<>(vpcSecurityGroupIds) : new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getAvailabilityZone() { return availabilityZone; }
|
||||
public void setAvailabilityZone(String availabilityZone) { this.availabilityZone = availabilityZone; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user