feat(rds): support instance snapshot lifecycle operations (#4066)

This commit is contained in:
XuJian authored and GitHub committed 2026-09-21 09:48:57 -04:00
1 parent 847ad256f2
commit 12cad79616
6 files changed
+454 -6

No files matched your search

@@ -21,6 +21,7 @@ import io.github.hectorvent.floci.services.rds.model.DbProxy;
import io.github.hectorvent.floci.services.rds.model.DbProxyAuth;
import io.github.hectorvent.floci.services.rds.model.DbProxyTarget;
import io.github.hectorvent.floci.services.rds.model.DbProxyTargetGroup;
import io.github.hectorvent.floci.services.rds.model.DbSnapshot;
import io.github.hectorvent.floci.services.rds.model.DbSubnetGroup;
import io.github.hectorvent.floci.services.rds.model.GlobalCluster;
import io.github.hectorvent.floci.services.rds.model.GlobalClusterMember;
@@ -109,6 +110,9 @@ public class RdsQueryHandler {
case "ModifyOptionGroup" -> handleModifyOptionGroup(params, region);
case "DeleteOptionGroup" -> handleDeleteOptionGroup(params, region);
case "CreateDBSnapshot" -> handleCreateDbSnapshot(params, region);
case "DeleteDBSnapshot" -> handleDeleteDbSnapshot(params, region);
case "CopyDBSnapshot" -> handleCopyDbSnapshot(params, region);
case "ModifyDBSnapshot" -> handleModifyDbSnapshot(params, region);
case "RestoreDBInstanceFromDBSnapshot" -> handleRestoreDbInstanceFromDbSnapshot(params, region);
case "DescribeDBSnapshots" -> handleDescribeDbSnapshots(params, region);
case "DescribeDBSnapshotAttributes" -> handleDescribeDbSnapshotAttributes(params, region);
@@ -1344,6 +1348,47 @@ public class RdsQueryHandler {
}
}
private Response handleDeleteDbSnapshot(MultivaluedMap<String, String> params, String region) {
String snapshotId = params.getFirst("DBSnapshotIdentifier");
Response missing = firstMissingParam("DBSnapshotIdentifier", snapshotId);
if (missing != null) {
return missing;
}
DbSnapshot snapshot = service.deleteDbSnapshot(snapshotId, region);
return Response.ok(AwsQueryResponse.envelope(
"DeleteDBSnapshot", AwsNamespaces.RDS, dbSnapshotXml(snapshot))).build();
}
private Response handleCopyDbSnapshot(MultivaluedMap<String, String> params, String region) {
String sourceId = params.getFirst("SourceDBSnapshotIdentifier");
String targetId = params.getFirst("TargetDBSnapshotIdentifier");
Response missing = firstMissingParam(
"SourceDBSnapshotIdentifier", sourceId,
"TargetDBSnapshotIdentifier", targetId);
if (missing != null) {
return missing;
}
Boolean copyTags = parseOptionalBoolean(params, "CopyTags");
DbSnapshot snapshot = service.copyDbSnapshot(
sourceId, targetId, Boolean.TRUE.equals(copyTags), parseTags(params),
params.getFirst("OptionGroupName"), params.getFirst("KmsKeyId"), region);
return Response.ok(AwsQueryResponse.envelope(
"CopyDBSnapshot", AwsNamespaces.RDS, dbSnapshotXml(snapshot))).build();
}
private Response handleModifyDbSnapshot(MultivaluedMap<String, String> params, String region) {
String snapshotId = params.getFirst("DBSnapshotIdentifier");
Response missing = firstMissingParam("DBSnapshotIdentifier", snapshotId);
if (missing != null) {
return missing;
}
DbSnapshot snapshot = service.modifyDbSnapshot(
snapshotId, params.getFirst("EngineVersion"),
params.getFirst("OptionGroupName"), region);
return Response.ok(AwsQueryResponse.envelope(
"ModifyDBSnapshot", AwsNamespaces.RDS, dbSnapshotXml(snapshot))).build();
}
private Response handleRestoreDbInstanceFromDbSnapshot(MultivaluedMap<String, String> params, String region) {
String instanceId = params.getFirst("DBInstanceIdentifier");
String snapshotId = params.getFirst("DBSnapshotIdentifier");
@@ -1920,7 +1965,7 @@ public class RdsQueryHandler {
return new XmlBuilder().start("DBInstance").raw(dbInstanceInnerXml(i)).end("DBInstance").build();
}
private String dbSnapshotXml(io.github.hectorvent.floci.services.rds.model.DbSnapshot s) {
private String dbSnapshotXml(DbSnapshot s) {
String engineStr = s.getEngine() != null ? s.getEngine().name().toLowerCase() : "";
XmlBuilder xml = new XmlBuilder().start("DBSnapshot")
.elem("DBSnapshotIdentifier", s.getDbSnapshotIdentifier())
@@ -1930,7 +1975,19 @@ public class RdsQueryHandler {
.elem("EngineVersion", s.getEngineVersion())
.elem("AllocatedStorage", s.getAllocatedStorage())
.elem("Status", s.getStatus())
.elem("MasterUsername", s.getMasterUsername());
.elem("MasterUsername", s.getMasterUsername())
.elem("SnapshotType", s.getSnapshotType() != null
&& !s.getSnapshotType().isBlank() ? s.getSnapshotType() : "manual");
if (s.getSourceDbSnapshotIdentifier() != null
&& !s.getSourceDbSnapshotIdentifier().isBlank()) {
xml.elem("SourceDBSnapshotIdentifier", s.getSourceDbSnapshotIdentifier());
}
if (s.getOptionGroupName() != null && !s.getOptionGroupName().isBlank()) {
xml.elem("OptionGroupName", s.getOptionGroupName());
}
if (s.getKmsKeyId() != null && !s.getKmsKeyId().isBlank()) {
xml.elem("KmsKeyId", s.getKmsKeyId());
}
if (s.getAvailabilityZone() != null) xml.elem("AvailabilityZone", s.getAvailabilityZone());
if (s.getVpcId() != null) xml.elem("VpcId", s.getVpcId());
xml.elem("InstanceCreateTime", s.getInstanceCreateTime() != null ? s.getInstanceCreateTime().toString() : "")
@@ -803,6 +803,9 @@ public class RdsService implements Resettable, ResourceProvider {
instance.getCreatedAt(), instance.getEndpoint() != null ? instance.getEndpoint().port() : instance.getProxyPort(),
instance.isIamDatabaseAuthenticationEnabled(), instance.getDbiResourceId(), instance.getDbInstanceClass());
snapshot.setDbName(instance.getDbName());
snapshot.setSnapshotType("manual");
snapshot.setOptionGroupName(instance.getOptionGroupName());
snapshot.setKmsKeyId(instance.getKmsKeyId());
snapshot.setTags(tags != null ? new java.util.LinkedHashMap<>(tags) : new java.util.LinkedHashMap<>());
snapshot.setDbSnapshotArn(regionResolver.buildArn("rds", effectiveRegion, "snapshot:" + snapshotId));
@@ -821,6 +824,199 @@ public class RdsService implements Resettable, ResourceProvider {
return snapshot;
}
public DbSnapshot deleteDbSnapshot(String snapshotId) {
return deleteDbSnapshot(snapshotId, regionResolver.getDefaultRegion());
}
public synchronized DbSnapshot deleteDbSnapshot(String snapshotId, String region) {
String effectiveRegion = effectiveRegion(region);
String accountId = currentAccountId();
DbSnapshot snapshot = Optional.ofNullable(findSnapshotForScope(accountId, effectiveRegion, snapshotId))
.orElseThrow(() -> new AwsException("DBSnapshotNotFound",
"DBSnapshot " + snapshotId + " not found.", 404));
if (!"available".equalsIgnoreCase(snapshot.getStatus())) {
throw new AwsException("InvalidDBSnapshotState",
"DBSnapshot " + snapshotId + " is not in an available state.", 400);
}
DbSnapshot deleted = copySnapshot(snapshot);
deleted.setStatus("deleted");
deleteSnapshotForScope(accountId, effectiveRegion, snapshotId);
deleteSnapshotDataForScope(accountId, effectiveRegion, snapshotId);
return deleted;
}
public DbSnapshot copyDbSnapshot(
String sourceIdentifier, String targetIdentifier, boolean copyTags,
Map<String, String> tags, String optionGroupName, String kmsKeyId) {
return copyDbSnapshot(sourceIdentifier, targetIdentifier, copyTags, tags,
optionGroupName, kmsKeyId, regionResolver.getDefaultRegion());
}
public synchronized DbSnapshot copyDbSnapshot(
String sourceIdentifier, String targetIdentifier, boolean copyTags,
Map<String, String> tags, String optionGroupName, String kmsKeyId, String region) {
String targetRegion = effectiveRegion(region);
String accountId = currentAccountId();
SnapshotReference sourceReference = resolveSnapshotReference(sourceIdentifier, targetRegion);
DbSnapshot source = sourceReference.snapshot();
if (!"available".equalsIgnoreCase(source.getStatus())) {
throw new AwsException("InvalidDBSnapshotState",
"DBSnapshot " + source.getDbSnapshotIdentifier() + " is not in an available state.", 400);
}
if (findSnapshotForScope(accountId, targetRegion, targetIdentifier) != null) {
throw new AwsException("DBSnapshotAlreadyExists",
"DBSnapshot " + targetIdentifier + " already exists.", 400);
}
String sourceData = getSnapshotDataForScope(
sourceReference.accountId(), sourceReference.region(), source.getDbSnapshotIdentifier())
.orElseThrow(() -> new AwsException("DBSnapshotNotFound",
"DBSnapshot data for " + source.getDbSnapshotIdentifier() + " not found.", 404));
DbSnapshot copy = copySnapshot(source);
copy.setDbSnapshotIdentifier(targetIdentifier);
copy.setDbSnapshotArn(regionResolver.buildArn("rds", targetRegion, "snapshot:" + targetIdentifier));
copy.setSnapshotCreateTime(Instant.now());
copy.setStatus("available");
copy.setSnapshotType("manual");
copy.setSourceDbSnapshotIdentifier(source.getDbSnapshotArn());
copy.setOptionGroupName(optionGroupName != null && !optionGroupName.isBlank()
? optionGroupName : source.getOptionGroupName());
copy.setKmsKeyId(kmsKeyId != null && !kmsKeyId.isBlank() ? kmsKeyId : source.getKmsKeyId());
copy.setRestoreAccountIds(new ArrayList<>());
Map<String, String> copiedTags = new LinkedHashMap<>();
if (copyTags) {
copiedTags.putAll(source.getTags());
}
if (tags != null) {
copiedTags.putAll(tags);
}
copy.setTags(copiedTags);
putSnapshotDataForScope(accountId, targetRegion, targetIdentifier, sourceData);
putSnapshotForScope(accountId, targetRegion, targetIdentifier, copy);
return copy;
}
public DbSnapshot modifyDbSnapshot(
String snapshotId, String engineVersion, String optionGroupName) {
return modifyDbSnapshot(snapshotId, engineVersion, optionGroupName,
regionResolver.getDefaultRegion());
}
public synchronized DbSnapshot modifyDbSnapshot(
String snapshotId, String engineVersion, String optionGroupName, String region) {
String effectiveRegion = effectiveRegion(region);
String accountId = currentAccountId();
DbSnapshot snapshot = Optional.ofNullable(findSnapshotForScope(accountId, effectiveRegion, snapshotId))
.orElseThrow(() -> new AwsException("DBSnapshotNotFound",
"DBSnapshot " + snapshotId + " not found.", 404));
if (!"available".equalsIgnoreCase(snapshot.getStatus())) {
throw new AwsException("InvalidDBSnapshotState",
"DBSnapshot " + snapshotId + " is not in an available state.", 400);
}
if ((engineVersion == null || engineVersion.isBlank())
&& (optionGroupName == null || optionGroupName.isBlank())) {
throw new AwsException("InvalidParameterCombination",
"At least one snapshot attribute must be specified.", 400);
}
if (engineVersion != null && !engineVersion.isBlank()) {
snapshot.setEngineVersion(engineVersion);
}
if (optionGroupName != null && !optionGroupName.isBlank()) {
snapshot.setOptionGroupName(optionGroupName);
}
putSnapshotForScope(accountId, effectiveRegion, snapshotId, snapshot);
return snapshot;
}
private DbSnapshot copySnapshot(DbSnapshot source) {
DbSnapshot copy = new DbSnapshot();
copy.setDbSnapshotIdentifier(source.getDbSnapshotIdentifier());
copy.setDbSnapshotArn(source.getDbSnapshotArn());
copy.setSnapshotType(source.getSnapshotType());
copy.setSourceDbSnapshotIdentifier(source.getSourceDbSnapshotIdentifier());
copy.setDbInstanceIdentifier(source.getDbInstanceIdentifier());
copy.setSnapshotCreateTime(source.getSnapshotCreateTime());
copy.setEngine(source.getEngine());
copy.setEngineVersion(source.getEngineVersion());
copy.setAllocatedStorage(source.getAllocatedStorage());
copy.setStatus(source.getStatus());
copy.setMasterUsername(source.getMasterUsername());
copy.setMasterPassword(source.getMasterPassword());
copy.setAvailabilityZone(source.getAvailabilityZone());
copy.setVpcId(source.getVpcId());
copy.setInstanceCreateTime(source.getInstanceCreateTime());
copy.setPort(source.getPort());
copy.setIamDatabaseAuthenticationEnabled(source.isIamDatabaseAuthenticationEnabled());
copy.setDbiResourceId(source.getDbiResourceId());
copy.setDbName(source.getDbName());
copy.setDbInstanceClass(source.getDbInstanceClass());
copy.setOptionGroupName(source.getOptionGroupName());
copy.setKmsKeyId(source.getKmsKeyId());
copy.setTags(new LinkedHashMap<>(source.getTags()));
copy.setRestoreAccountIds(new ArrayList<>(source.getRestoreAccountIds()));
return copy;
}
private SnapshotReference resolveSnapshotReference(String sourceIdentifier, String targetRegion) {
String sourceId = sourceIdentifier;
String sourceRegion = targetRegion;
String sourceAccount = currentAccountId();
if (sourceIdentifier != null && sourceIdentifier.startsWith("arn:")) {
try {
AwsArnUtils.Arn parsed = AwsArnUtils.parse(sourceIdentifier);
if (!"aws".equals(parsed.partition()) || !"rds".equals(parsed.service())
|| !parsed.resource().startsWith("snapshot:")) {
throw new IllegalArgumentException("not an RDS snapshot ARN");
}
sourceId = parsed.resource().substring("snapshot:".length());
sourceRegion = parsed.region();
sourceAccount = parsed.accountId();
} catch (IllegalArgumentException e) {
throw new AwsException("InvalidParameterValue",
"SourceDBSnapshotIdentifier must be a snapshot identifier or ARN.", 400);
}
}
if (!Objects.equals(sourceAccount, currentAccountId())) {
throw new AwsException("DBSnapshotNotFound",
"DBSnapshot " + sourceIdentifier + " not found.", 404);
}
DbSnapshot source = findSnapshotForScope(sourceAccount, sourceRegion, sourceId);
if (source == null) {
throw new AwsException("DBSnapshotNotFound",
"DBSnapshot " + sourceIdentifier + " not found.", 404);
}
return new SnapshotReference(sourceAccount, sourceRegion, source);
}
private Optional<String> getSnapshotDataForScope(String accountId, String region, String snapshotId) {
String key = dbResourceKey(region, snapshotId);
if (snapshotData instanceof AccountAwareStorageBackend<String> aware) {
return aware.getForAccount(accountId, key);
}
return snapshotData.get(key);
}
private void putSnapshotDataForScope(String accountId, String region, String snapshotId, String data) {
String key = dbResourceKey(region, snapshotId);
if (snapshotData instanceof AccountAwareStorageBackend<String> aware) {
aware.putForAccount(accountId, key, data);
} else {
snapshotData.put(key, data);
}
}
private void deleteSnapshotDataForScope(String accountId, String region, String snapshotId) {
String key = dbResourceKey(region, snapshotId);
if (snapshotData instanceof AccountAwareStorageBackend<String> aware) {
aware.deleteForAccount(accountId, key);
} else {
snapshotData.delete(key);
}
}
private record SnapshotReference(String accountId, String region, DbSnapshot snapshot) {}
public DbInstance restoreDbInstanceFromDbSnapshot(String instanceId, String snapshotId, String dbInstanceClass, String availabilityZone, boolean multiAz, String dbSubnetGroupName, java.util.List<String> vpcSecurityGroupIds, java.util.Map<String, String> tags) {
return restoreDbInstanceFromDbSnapshot(instanceId, snapshotId, dbInstanceClass, availabilityZone,
multiAz, dbSubnetGroupName, vpcSecurityGroupIds, tags, regionResolver.getDefaultRegion());
@@ -6494,6 +6690,15 @@ public class RdsService implements Resettable, ResourceProvider {
}
}
private void deleteSnapshotForScope(String accountId, String region, String snapshotId) {
String key = dbResourceKey(region, snapshotId);
if (snapshots instanceof AccountAwareStorageBackend<DbSnapshot> aware) {
aware.deleteForAccount(accountId, key);
} else {
snapshots.delete(key);
}
}
private synchronized DbSubnetGroup findSubnetGroupForScope(
String accountId, String region, String groupName) {
String effectiveRegion = effectiveRegion(region);
@@ -13,6 +13,8 @@ public class DbSnapshot {
private String dbSnapshotIdentifier;
private String dbSnapshotArn;
private String snapshotType;
private String sourceDbSnapshotIdentifier;
private String dbInstanceIdentifier;
private Instant snapshotCreateTime;
private DatabaseEngine engine;
@@ -29,6 +31,8 @@ public class DbSnapshot {
private String dbiResourceId;
private String dbName;
private String dbInstanceClass;
private String optionGroupName;
private String kmsKeyId;
private Map<String, String> tags = new LinkedHashMap<>();
/** Account IDs authorized to copy/restore this snapshot; the "restore" DBSnapshotAttribute. */
private List<String> restoreAccountIds = new ArrayList<>();
@@ -63,6 +67,14 @@ public class DbSnapshot {
public String getDbSnapshotArn() { return dbSnapshotArn; }
public void setDbSnapshotArn(String dbSnapshotArn) { this.dbSnapshotArn = dbSnapshotArn; }
public String getSnapshotType() { return snapshotType; }
public void setSnapshotType(String snapshotType) { this.snapshotType = snapshotType; }
public String getSourceDbSnapshotIdentifier() { return sourceDbSnapshotIdentifier; }
public void setSourceDbSnapshotIdentifier(String sourceDbSnapshotIdentifier) {
this.sourceDbSnapshotIdentifier = sourceDbSnapshotIdentifier;
}
public String getDbInstanceIdentifier() { return dbInstanceIdentifier; }
public void setDbInstanceIdentifier(String dbInstanceIdentifier) { this.dbInstanceIdentifier = dbInstanceIdentifier; }
@@ -118,4 +130,10 @@ public class DbSnapshot {
public String getDbInstanceClass() { return dbInstanceClass; }
public void setDbInstanceClass(String dbInstanceClass) { this.dbInstanceClass = dbInstanceClass; }
public String getOptionGroupName() { return optionGroupName; }
public void setOptionGroupName(String optionGroupName) { this.optionGroupName = optionGroupName; }
public String getKmsKeyId() { return kmsKeyId; }
public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; }
}