diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsStorageTypeTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsStorageTypeTest.java new file mode 100644 index 0000000000..0f644d2d71 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsStorageTypeTest.java @@ -0,0 +1,149 @@ +package com.floci.test; + +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.services.rds.RdsClient; +import software.amazon.awssdk.services.rds.model.DBInstance; +import software.amazon.awssdk.services.rds.model.DbInstanceNotFoundException; +import software.amazon.awssdk.services.rds.model.StorageTypeNotSupportedException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertAll; + +class RdsStorageTypeTest { + @Test + void requestedTypesRoundTripAndUnsupportedTypesLeaveTheInstanceUnchanged() { + Assumptions.assumeFalse(TestFixtures.isRealAws(), "Disposable emulator storage metadata fixtures"); + String id = TestFixtures.uniqueName("rds-storage"); + try (RdsClient rds = TestFixtures.rdsClient()) { + try { + DBInstance created = rds.createDBInstance(r -> r.dbInstanceIdentifier(id).engine("postgres") + .dbInstanceClass("db.t3.micro").masterUsername("admin").masterUserPassword("password123") + .allocatedStorage(20).storageType("gp3").backupRetentionPeriod(2)).dbInstance(); + assertThat(created.storageType()).isEqualTo("gp3"); + assertThat(rds.describeDBInstances(r -> r.dbInstanceIdentifier(id)).dbInstances().get(0).storageType()) + .isEqualTo("gp3"); + assertThatThrownBy(() -> rds.modifyDBInstance(r -> r.dbInstanceIdentifier(id) + .storageType("not-a-storage-type").backupRetentionPeriod(7))) + .isInstanceOfSatisfying(StorageTypeNotSupportedException.class, + error -> assertThat(error.statusCode()).isEqualTo(400)); + DBInstance retained = rds.describeDBInstances(r -> r.dbInstanceIdentifier(id)).dbInstances().get(0); + assertThat(retained.storageType()).isEqualTo("gp3"); + assertThat(retained.backupRetentionPeriod()).isEqualTo(2); + assertThat(rds.modifyDBInstance(r -> r.dbInstanceIdentifier(id).storageType("gp2")) + .dbInstance().storageType()).isEqualTo("gp2"); + assertThat(rds.modifyDBInstance(r -> r.dbInstanceIdentifier(id).backupRetentionPeriod(3)) + .dbInstance().storageType()).isEqualTo("gp2"); + assertThat(rds.describeValidDBInstanceModifications(r -> r.dbInstanceIdentifier(id)) + .validDBInstanceModificationsMessage().storage()) + .extracting(option -> option.storageType()).containsExactly("gp2", "gp3", "io1", "io2"); + } finally { + remove(rds, id); + } + } + } + + @ParameterizedTest + @ValueSource(strings = {"io1", "io2"}) + void provisionedTypesRoundTripWithoutIops(String storageType) { + Assumptions.assumeFalse(TestFixtures.isRealAws(), "Disposable emulator storage metadata fixtures"); + String id = TestFixtures.uniqueName("rds-provisioned"); + try (RdsClient rds = TestFixtures.rdsClient()) { + try { + DBInstance created = rds.createDBInstance(r -> r.dbInstanceIdentifier(id).engine("postgres") + .dbInstanceClass("db.t3.micro").masterUsername("admin").masterUserPassword("password123") + .allocatedStorage(100).storageType(storageType)).dbInstance(); + assertThat(created.storageType()).isEqualTo(storageType); + assertThat(created.iops()).isNull(); + assertThat(rds.modifyDBInstance(r -> r.dbInstanceIdentifier(id).storageType("gp3")) + .dbInstance().storageType()).isEqualTo("gp3"); + assertThat(rds.modifyDBInstance(r -> r.dbInstanceIdentifier(id).storageType(storageType)) + .dbInstance().storageType()).isEqualTo(storageType); + DBInstance retained = rds.modifyDBInstance(r -> r.dbInstanceIdentifier(id).backupRetentionPeriod(2)) + .dbInstance(); + assertThat(retained.storageType()).isEqualTo(storageType); + assertThat(retained.iops()).isNull(); + } finally { + remove(rds, id); + } + } + } + + @Test + void aRefusedCreateCanBeRetriedWithASupportedType() { + Assumptions.assumeFalse(TestFixtures.isRealAws(), "Disposable emulator storage metadata fixtures"); + String id = TestFixtures.uniqueName("rds-storage-retry"); + try (RdsClient rds = TestFixtures.rdsClient()) { + try { + assertThatThrownBy(() -> rds.createDBInstance(r -> r.dbInstanceIdentifier(id).engine("postgres") + .dbInstanceClass("db.t3.micro").masterUsername("admin").masterUserPassword("password123") + .allocatedStorage(20).storageType("not-a-storage-type"))) + .isInstanceOf(StorageTypeNotSupportedException.class); + assertThatThrownBy(() -> rds.describeDBInstances(r -> r.dbInstanceIdentifier(id))) + .isInstanceOf(DbInstanceNotFoundException.class); + assertThat(rds.createDBInstance(r -> r.dbInstanceIdentifier(id).engine("postgres") + .dbInstanceClass("db.t3.micro").masterUsername("admin").masterUserPassword("password123") + .allocatedStorage(20).storageType("gp3")).dbInstance().storageType()).isEqualTo("gp3"); + } finally { + remove(rds, id); + } + } + } + + @ParameterizedTest + @ValueSource(strings = {"snapshot", "pitr", "replica"}) + void explicitDerivedTypesOverrideTheSourcesType(String operation) { + Assumptions.assumeFalse(TestFixtures.isRealAws(), "Disposable emulator storage metadata fixtures"); + String source = TestFixtures.uniqueName("rds-storage-source"); + String target = TestFixtures.uniqueName("rds-storage-target"); + String snapshot = TestFixtures.uniqueName("rds-storage-snapshot"); + try (RdsClient rds = TestFixtures.rdsClient()) { + boolean snapshotCreated = false; + try { + rds.createDBInstance(r -> r.dbInstanceIdentifier(source).engine("postgres") + .dbInstanceClass("db.t3.micro").masterUsername("admin").masterUserPassword("password123") + .allocatedStorage(20).storageType("gp3").backupRetentionPeriod(2)); + DBInstance derived; + if ("snapshot".equals(operation)) { + assertThat(rds.createDBSnapshot(r -> r.dbInstanceIdentifier(source).dbSnapshotIdentifier(snapshot)) + .dbSnapshot().storageType()).isEqualTo("gp3"); + snapshotCreated = true; + derived = rds.restoreDBInstanceFromDBSnapshot(r -> r.dbInstanceIdentifier(target) + .dbSnapshotIdentifier(snapshot).storageType("gp2")).dbInstance(); + } else if ("pitr".equals(operation)) { + derived = rds.restoreDBInstanceToPointInTime(r -> r.targetDBInstanceIdentifier(target) + .sourceDBInstanceIdentifier(source).useLatestRestorableTime(true).storageType("gp2")) + .dbInstance(); + } else { + derived = rds.createDBInstanceReadReplica(r -> r.dbInstanceIdentifier(target) + .sourceDBInstanceIdentifier(source).storageType("gp2")).dbInstance(); + } + assertThat(derived.storageType()).isEqualTo("gp2"); + assertThat(rds.describeDBInstances(r -> r.dbInstanceIdentifier(target)) + .dbInstances().get(0).storageType()).isEqualTo("gp2"); + assertThat(rds.describeDBInstances(r -> r.dbInstanceIdentifier(source)) + .dbInstances().get(0).storageType()).isEqualTo("gp3"); + } finally { + boolean removeSnapshot = snapshotCreated; + assertAll(() -> remove(rds, target), + () -> { + if (removeSnapshot) { + rds.deleteDBSnapshot(r -> r.dbSnapshotIdentifier(snapshot)); + } + }, + () -> remove(rds, source)); + } + } + } + + private static void remove(RdsClient rds, String id) { + try { + rds.deleteDBInstance(r -> r.dbInstanceIdentifier(id).skipFinalSnapshot(true)); + } catch (DbInstanceNotFoundException ignored) { + // A refused create deliberately leaves nothing to remove. + } + } +} diff --git a/docs/services/rds.md b/docs/services/rds.md index 073cceef30..599548d7f9 100644 --- a/docs/services/rds.md +++ b/docs/services/rds.md @@ -438,8 +438,25 @@ Two deviations are worth knowing: and reported on read, but the container started at create time keeps running the image it was started with. This is control-plane compatibility, like Aurora Serverless v2 scaling above. -`StorageType`, `MultiAZ` and `Iops` are not read on a modify yet. Floci reports every instance as -`gp2`. +For standalone, non-Aurora instances with instance-owned storage, `CreateDBInstance` and +`ModifyDBInstance` retain the `StorageType` values `gp2`, `gp3`, `io1` and `io2`. A modify +that omits the member leaves it unchanged; a request naming another type is +refused with `StorageTypeNotSupported` before the instance is created or changed. Describe and +modification responses report the stored value, and valid-modification discovery lists these four +supported types. Aurora and cluster-member instance overrides remain ignored because their +storage belongs to the cluster. Explicit type overrides also work on snapshot restores, point-in-time copies +and read replicas. Snapshots and their copies retain the type; Floci carries it into snapshot +restores, point-in-time copies and read replicas. It also survives persistent, hybrid and WAL storage +reloads. Legacy records without the member retain the existing `gp2` fallback. + +Storage type changes apply immediately to control-plane metadata; the backing container volume's +performance is unchanged. `Iops`, `StorageThroughput` and `MultiAZ` changes remain unimplemented. +Omitted create requests retain Floci's existing `gp2` default, which differs from the current +[AWS create default](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBInstance.html). +Floci's snapshot and point-in-time copies inherit the source type when the request omits it, rather +than applying the [AWS restore default](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBInstanceFromDBSnapshot.html). +Floci does not enforce every engine-specific storage allocation, IOPS restriction or regional +storage-type availability. Magnetic `standard` storage is refused. ### Storage autoscaling threshold changes diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java index 4315eadc3d..84aa59e3ce 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java @@ -804,7 +804,8 @@ public class RdsQueryHandler { params.getFirst("EngineLifecycleSupport"), includeEncryption ? cloudwatchLogsExports(params) : null, includeEncryption ? null : cloudwatchLogsExportChanges(params), - optionalInt(params.getFirst("MaxAllocatedStorage"))); + optionalInt(params.getFirst("MaxAllocatedStorage")), + params.getFirst("StorageType")); } /** @@ -1167,7 +1168,7 @@ public class RdsQueryHandler { parseOptionalBoolean(params, "EnableIAMDatabaseAuthentication"), optionalInt(params.getFirst("AllocatedStorage")), params.getFirst("ReplicaMode"), - parseTags(params)); + parseTags(params), params.getFirst("StorageType")); DbInstance replica = service.createDbInstanceReadReplica(request, region); return Response.ok(AwsQueryResponse.envelope( "CreateDBInstanceReadReplica", AwsNamespaces.RDS, dbInstanceXml(replica))).build(); @@ -1982,7 +1983,7 @@ public class RdsQueryHandler { DbInstance instance = service.restoreDbInstanceFromDbSnapshot(instanceId, snapshotId, dbInstanceClass, availabilityZone, multiAz, dbSubnetGroupName, vpcSecurityGroupIds, tags, region, requestedPort, - parseOptionalBoolean(params, "EnableIAMDatabaseAuthentication")); + parseOptionalBoolean(params, "EnableIAMDatabaseAuthentication"), params.getFirst("StorageType")); String result = dbInstanceXml(instance); return Response.ok(AwsQueryResponse.envelope("RestoreDBInstanceFromDBSnapshot", AwsNamespaces.RDS, result)).build(); } catch (AwsException e) { @@ -2683,7 +2684,7 @@ public class RdsQueryHandler { parseIntegerParam(params, "BackupRetentionPeriod"), params.getFirst("PreferredBackupWindow"), parseOptionalBoolean(params, "DeletionProtection"), - parseTags(params)); + parseTags(params), params.getFirst("StorageType")); DbInstance instance = service.restoreDbInstanceToPointInTime(request, region); return Response.ok(AwsQueryResponse.envelope( "RestoreDBInstanceToPointInTime", AwsNamespaces.RDS, dbInstanceXml(instance))).build(); @@ -2767,6 +2768,7 @@ public class RdsQueryHandler { .elem("Engine", engineStr) .elem("EngineVersion", s.getEngineVersion()) .elem("AllocatedStorage", s.getAllocatedStorage()) + .elem("StorageType", s.getStorageType()) .elem("Status", s.getStatus()) .elem("MasterUsername", s.getMasterUsername()) .elem("SnapshotType", s.getSnapshotType() != null @@ -2878,7 +2880,7 @@ public class RdsQueryHandler { xml.elem("IAMDatabaseAuthenticationEnabled", i.isIamDatabaseAuthenticationEnabled()) .elem("MultiAZ", i.isMultiAz()) .elem("AutoMinorVersionUpgrade", i.isAutoMinorVersionUpgrade()) - .elem("StorageType", DbInstance.DEFAULT_STORAGE_TYPE) + .elem("StorageType", i.getStorageType()) .elem("PubliclyAccessible", i.isPubliclyAccessible()) .elem("AvailabilityZone", i.getAvailabilityZone() != null ? i.getAvailabilityZone() : config.defaultAvailabilityZone()) .elem("PreferredMaintenanceWindow", i.getPreferredMaintenanceWindow() != null diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java index 055adb7edd..f7d1ca39d6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java @@ -731,6 +731,11 @@ public class RdsService implements Resettable, ResourceProvider { // resolved with the other validations, before a port is taken or a container started DbInstanceSettings resolvedSettings = withEffectiveWindows(settings, null) .withKmsKeyId(resolveKmsKeyArn(settings.kmsKeyId(), effectiveRegion)); + if (isBlank(dbClusterIdentifier) && !isAuroraEngine(engineParam)) { + resolvedSettings.validateStorageType(); + } else { + resolvedSettings = resolvedSettings.withStorageType(null); + } DbInstanceSettings.validateMonitoringPairOnCreate( settings.monitoringInterval(), settings.monitoringRoleArn()); boolean mock = config.services().rds().mock(); @@ -956,6 +961,7 @@ public class RdsService implements Resettable, ResourceProvider { instance.getMasterUsername(), instance.getMasterPassword(), instance.getAvailabilityZone(), instance.getVpcId(), instance.getCreatedAt(), instance.getEndpoint() != null ? instance.getEndpoint().port() : instance.getProxyPort(), instance.isIamDatabaseAuthenticationEnabled(), instance.getDbiResourceId(), instance.getDbInstanceClass()); + snapshot.setStorageType(instance.getStorageType()); snapshot.setDbName(instance.getDbName()); snapshot.setSnapshotType("manual"); snapshot.setOptionGroupName(instance.getOptionGroupName()); @@ -1106,6 +1112,7 @@ public class RdsService implements Resettable, ResourceProvider { copy.setEngine(source.getEngine()); copy.setEngineVersion(source.getEngineVersion()); copy.setAllocatedStorage(source.getAllocatedStorage()); + copy.setStorageType(source.getStorageType()); copy.setStatus(source.getStatus()); copy.setMasterUsername(source.getMasterUsername()); copy.setMasterPassword(source.getMasterPassword()); @@ -1215,6 +1222,15 @@ public class RdsService implements Resettable, ResourceProvider { List vpcSecurityGroupIds, Map tags, String region, Integer requestedPort, Boolean iamEnabled) { + return restoreDbInstanceFromDbSnapshot(instanceId, snapshotId, dbInstanceClass, availabilityZone, + multiAz, dbSubnetGroupName, vpcSecurityGroupIds, tags, region, requestedPort, iamEnabled, null); + } + + public DbInstance restoreDbInstanceFromDbSnapshot(String instanceId, String snapshotId, String dbInstanceClass, + String availabilityZone, boolean multiAz, String dbSubnetGroupName, + List vpcSecurityGroupIds, + Map tags, String region, + Integer requestedPort, Boolean iamEnabled, String storageType) { String effectiveRegion = effectiveRegion(region); DbSnapshot snapshot = Optional.ofNullable(findSnapshotForScope(currentAccountId(), effectiveRegion, snapshotId)) .orElseThrow(() -> new AwsException("DBSnapshotNotFound", "DBSnapshot " + snapshotId + " not found.", 404)); @@ -1228,7 +1244,8 @@ public class RdsService implements Resettable, ResourceProvider { } // Use the parameters from the snapshot DbInstanceSettings restoreSettings = new DbInstanceSettings( - snapshot.isStorageEncrypted(), snapshot.getKmsKeyId(), null, null, null, null); + snapshot.isStorageEncrypted(), snapshot.getKmsKeyId(), null, null, null, null) + .withStorageType(storageType != null ? storageType : snapshot.getStorageType()); DbInstance instance = createDbInstance(instanceId, snapshot.getEngine().name().toLowerCase(), snapshot.getEngineVersion(), snapshot.getMasterUsername(), snapshot.getMasterPassword(), snapshot.getDbName(), targetClass, snapshot.getAllocatedStorage(), Boolean.TRUE.equals(iamEnabled), @@ -1329,7 +1346,8 @@ public class RdsService implements Resettable, ResourceProvider { source.isStorageEncrypted() ? Boolean.TRUE : null, source.isStorageEncrypted() ? source.getKmsKeyId() : null, request.backupRetentionPeriod(), request.preferredBackupWindow(), null, - request.copyTagsToSnapshot()); + request.copyTagsToSnapshot()).withStorageType( + request.storageType() != null ? request.storageType() : source.getStorageType()); DbInstance instance = createDbInstance(targetId, engineParam, source.getEngineVersion(), source.getMasterUsername(), source.getMasterPassword(), dbName, firstNonBlank(request.dbInstanceClass(), source.getDbInstanceClass()), @@ -1536,7 +1554,8 @@ public class RdsService implements Resettable, ResourceProvider { source.isStorageEncrypted() ? Boolean.TRUE : null, source.isStorageEncrypted() && sameRegion ? source.getKmsKeyId() : null, 0, source.getPreferredBackupWindow(), source.getPreferredMaintenanceWindow(), - copyTagsToSnapshot); + copyTagsToSnapshot).withStorageType( + request.storageType() != null ? request.storageType() : source.getStorageType()); Map tags = request.tags() != null ? request.tags() : Map.of(); DbInstance replica = createDbInstance(id, engineParam, source.getEngineVersion(), @@ -3081,6 +3100,11 @@ public class RdsService implements Resettable, ResourceProvider { // current size and engine version: a refusal here must leave the whole request unapplied. DbInstanceScalingChanges resolvedScaling = scaling.resolveFor(instance); DbInstanceSettings effective = withEffectiveWindows(settings, instance); + if (isBlank(instance.getDbClusterIdentifier()) && !isAuroraEngine(engineName(instance))) { + effective.validateStorageType(); + } else { + effective = effective.withStorageType(null); + } validateAutoscalingMinimum(instance, effective.maxAllocatedStorage()); instance.setStatus(DbInstanceStatus.AVAILABLE); if (optionGroupName != null && !optionGroupName.isBlank()) { @@ -3389,7 +3413,8 @@ public class RdsService implements Resettable, ResourceProvider { List sizes = smallestIncrease <= maximum ? List.of(new SizeRange(current, current), new SizeRange(smallestIncrease, maximum)) : List.of(new SizeRange(current, current)); - return List.of(new StorageModification(DbInstance.DEFAULT_STORAGE_TYPE, sizes)); + return List.of(new StorageModification("gp2", sizes), new StorageModification("gp3", sizes), + new StorageModification("io1", sizes), new StorageModification("io2", sizes)); } // ── Stop, start and reboot ──────────────────────────────────────────────── diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstance.java b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstance.java index c626993c75..deb38c639c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstance.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstance.java @@ -11,7 +11,7 @@ import java.util.Map; @RegisterForReflection public class DbInstance { - /** The only storage type Floci models, and the one DescribeDBInstances reports. */ + /** The fallback for existing records and requests that do not name a storage type. */ public static final String DEFAULT_STORAGE_TYPE = "gp2"; private String dbInstanceIdentifier; @@ -25,6 +25,7 @@ public class DbInstance { private String dbName; private String dbInstanceClass; private int allocatedStorage; + private String storageType; private DbInstanceStatus status; private DbEndpoint endpoint; private boolean iamDatabaseAuthenticationEnabled; @@ -133,6 +134,9 @@ public class DbInstance { public String getDbInstanceClass() { return dbInstanceClass; } public void setDbInstanceClass(String dbInstanceClass) { this.dbInstanceClass = dbInstanceClass; } + public String getStorageType() { return storageType != null ? storageType : DEFAULT_STORAGE_TYPE; } + public void setStorageType(String storageType) { this.storageType = storageType; } + public int getAllocatedStorage() { return allocatedStorage; } public void setAllocatedStorage(int allocatedStorage) { this.allocatedStorage = allocatedStorage; } diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstanceScalingChanges.java b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstanceScalingChanges.java index 1b614a5bcb..ce5ed95460 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstanceScalingChanges.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstanceScalingChanges.java @@ -75,7 +75,7 @@ public record DbInstanceScalingChanges(String dbInstanceClass, if (allocatedStorage < current) { throw new AwsException("InvalidParameterCombination", "Invalid storage size for engine name " + instance.getEngineIdentifier() - + " and storage type " + DbInstance.DEFAULT_STORAGE_TYPE + ": " + + " and storage type " + instance.getStorageType() + ": " + allocatedStorage, 400); } if (!TEN_PERCENT_MINIMUM_INCREASE.contains(instance.getEngine())) { diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstanceSettings.java b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstanceSettings.java index b897e3b1b8..721b8635a7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstanceSettings.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstanceSettings.java @@ -26,7 +26,20 @@ public record DbInstanceSettings(Boolean storageEncrypted, String engineLifecycleSupport, List enabledCloudwatchLogsExports, LogExportChanges logExportChanges, - Integer maxAllocatedStorage) { + Integer maxAllocatedStorage, + String storageType) { + + public DbInstanceSettings(Boolean storageEncrypted, String kmsKeyId, Integer backupRetentionPeriod, + String preferredBackupWindow, String preferredMaintenanceWindow, + Boolean copyTagsToSnapshot, Integer monitoringInterval, String monitoringRoleArn, + Boolean performanceInsightsEnabled, Integer performanceInsightsRetentionPeriod, + String engineLifecycleSupport, List enabledCloudwatchLogsExports, + LogExportChanges logExportChanges, Integer maxAllocatedStorage) { + this(storageEncrypted, kmsKeyId, backupRetentionPeriod, preferredBackupWindow, + preferredMaintenanceWindow, copyTagsToSnapshot, monitoringInterval, monitoringRoleArn, + performanceInsightsEnabled, performanceInsightsRetentionPeriod, engineLifecycleSupport, + enabledCloudwatchLogsExports, logExportChanges, maxAllocatedStorage, null); + } /** The settings a caller that touches none of the monitoring members gives. */ public DbInstanceSettings(Boolean storageEncrypted, @@ -174,7 +187,7 @@ public record DbInstanceSettings(Boolean storageEncrypted, backupWindow, maintenanceWindow, copyTagsToSnapshot, monitoringInterval, monitoringRoleArn, performanceInsightsEnabled, performanceInsightsRetentionPeriod, engineLifecycleSupport, - enabledCloudwatchLogsExports, logExportChanges, maxAllocatedStorage); + enabledCloudwatchLogsExports, logExportChanges, maxAllocatedStorage, storageType); } public DbInstanceSettings withKmsKeyId(String resolvedKmsKeyId) { @@ -182,7 +195,22 @@ public record DbInstanceSettings(Boolean storageEncrypted, preferredBackupWindow, preferredMaintenanceWindow, copyTagsToSnapshot, monitoringInterval, monitoringRoleArn, performanceInsightsEnabled, performanceInsightsRetentionPeriod, engineLifecycleSupport, - enabledCloudwatchLogsExports, logExportChanges, maxAllocatedStorage); + enabledCloudwatchLogsExports, logExportChanges, maxAllocatedStorage, storageType); + } + + public DbInstanceSettings withStorageType(String resolvedStorageType) { + return new DbInstanceSettings(storageEncrypted, kmsKeyId, backupRetentionPeriod, + preferredBackupWindow, preferredMaintenanceWindow, copyTagsToSnapshot, + monitoringInterval, monitoringRoleArn, performanceInsightsEnabled, + performanceInsightsRetentionPeriod, engineLifecycleSupport, + enabledCloudwatchLogsExports, logExportChanges, maxAllocatedStorage, resolvedStorageType); + } + + public void validateStorageType() { + if (storageType != null && !Set.of("gp2", "gp3", "io1", "io2").contains(storageType)) { + throw new AwsException("StorageTypeNotSupported", + "Floci supports the gp2, gp3, io1 and io2 storage types for DB instances.", 400); + } } public void applyTo(DbInstance instance) { @@ -227,6 +255,9 @@ public record DbInstanceSettings(Boolean storageEncrypted, instance.setEnabledCloudwatchLogsExports( logExportChanges.applyTo(instance.getEnabledCloudwatchLogsExports())); } + if (storageType != null) { + instance.setStorageType(storageType); + } if (maxAllocatedStorage != null) { instance.setMaxAllocatedStorage(maxAllocatedStorage); } diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbSnapshot.java b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbSnapshot.java index eef7a44375..68beb57cc8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbSnapshot.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbSnapshot.java @@ -20,6 +20,7 @@ public class DbSnapshot { private DatabaseEngine engine; private String engineVersion; private int allocatedStorage; + private String storageType; private String status; private String masterUsername; private String masterPassword; @@ -88,6 +89,9 @@ public class DbSnapshot { public String getEngineVersion() { return engineVersion; } public void setEngineVersion(String engineVersion) { this.engineVersion = engineVersion; } + public String getStorageType() { return storageType != null ? storageType : DbInstance.DEFAULT_STORAGE_TYPE; } + public void setStorageType(String storageType) { this.storageType = storageType; } + public int getAllocatedStorage() { return allocatedStorage; } public void setAllocatedStorage(int allocatedStorage) { this.allocatedStorage = allocatedStorage; } diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/model/PointInTimeRestoreRequest.java b/src/main/java/io/github/hectorvent/floci/services/rds/model/PointInTimeRestoreRequest.java index 7ba2adba11..dce6c9d9f4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/model/PointInTimeRestoreRequest.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/model/PointInTimeRestoreRequest.java @@ -35,5 +35,37 @@ public record PointInTimeRestoreRequest(String targetDbInstanceIdentifier, Integer backupRetentionPeriod, String preferredBackupWindow, Boolean deletionProtection, - Map tags) { + Map tags, + String storageType) { + + public PointInTimeRestoreRequest(String targetDbInstanceIdentifier, + String sourceDbInstanceIdentifier, + String sourceDbiResourceId, + String sourceDbInstanceAutomatedBackupsArn, + Instant restoreTime, + Boolean useLatestRestorableTime, + String dbInstanceClass, + Integer port, + String availabilityZone, + Boolean multiAz, + Boolean autoMinorVersionUpgrade, + String dbName, + String optionGroupName, + String dbParameterGroupName, + Boolean publiclyAccessible, + String dbSubnetGroupName, + List vpcSecurityGroupIds, + Boolean copyTagsToSnapshot, + Boolean iamDatabaseAuthenticationEnabled, + Integer backupRetentionPeriod, + String preferredBackupWindow, + Boolean deletionProtection, + Map tags) { + this(targetDbInstanceIdentifier, sourceDbInstanceIdentifier, sourceDbiResourceId, + sourceDbInstanceAutomatedBackupsArn, restoreTime, useLatestRestorableTime, dbInstanceClass, + port, availabilityZone, multiAz, autoMinorVersionUpgrade, dbName, optionGroupName, + dbParameterGroupName, publiclyAccessible, dbSubnetGroupName, vpcSecurityGroupIds, + copyTagsToSnapshot, iamDatabaseAuthenticationEnabled, backupRetentionPeriod, + preferredBackupWindow, deletionProtection, tags, null); + } } diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/model/ReadReplicaRequest.java b/src/main/java/io/github/hectorvent/floci/services/rds/model/ReadReplicaRequest.java index f04549e206..fd5fd707b4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/model/ReadReplicaRequest.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/model/ReadReplicaRequest.java @@ -26,5 +26,28 @@ public record ReadReplicaRequest(String dbInstanceIdentifier, Boolean iamDatabaseAuthenticationEnabled, Integer allocatedStorage, String replicaMode, - Map tags) { + Map tags, + String storageType) { + + public ReadReplicaRequest(String dbInstanceIdentifier, + String sourceDbInstanceIdentifier, + String dbInstanceClass, + String availabilityZone, + Boolean multiAz, + Boolean autoMinorVersionUpgrade, + String optionGroupName, + String dbParameterGroupName, + Boolean publiclyAccessible, + String dbSubnetGroupName, + List vpcSecurityGroupIds, + Boolean copyTagsToSnapshot, + Boolean iamDatabaseAuthenticationEnabled, + Integer allocatedStorage, + String replicaMode, + Map tags) { + this(dbInstanceIdentifier, sourceDbInstanceIdentifier, dbInstanceClass, availabilityZone, multiAz, + autoMinorVersionUpgrade, optionGroupName, dbParameterGroupName, publiclyAccessible, + dbSubnetGroupName, vpcSecurityGroupIds, copyTagsToSnapshot, iamDatabaseAuthenticationEnabled, + allocatedStorage, replicaMode, tags, null); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java index d4d2517855..2ab1d9d591 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java @@ -2522,7 +2522,7 @@ class RdsQueryHandlerTest { }) void restoreBooleanFlagsRejectMalformedValuesBeforeServiceCalls(String action, String parameter) { when(service.restoreDbInstanceFromDbSnapshot(any(), any(), any(), any(), anyBoolean(), - any(), any(), any(), any(), any(), any())).thenReturn(makeInstance("restored")); + any(), any(), any(), any(), any(), any(), isNull())).thenReturn(makeInstance("restored")); when(service.restoreDbClusterFromSnapshot(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(makeCluster("restored")); when(service.restoreDbClusterToPointInTime(any(), any(), any(), any(), any(), any(), @@ -2553,10 +2553,10 @@ class RdsQueryHandlerTest { void restoreInstanceSnapshotPreservesMultiAzValuesAndDefault(String value) { boolean enabled = Boolean.parseBoolean(value); when(service.restoreDbInstanceFromDbSnapshot(eq("restored"), eq("source"), isNull(), isNull(), - eq(enabled), isNull(), eq(List.of()), eq(Map.of()), isNull(), isNull(), isNull())) + eq(enabled), isNull(), eq(List.of()), eq(Map.of()), isNull(), isNull(), isNull(), isNull())) .thenReturn(makeInstance("restored")); when(service.restoreDbInstanceFromDbSnapshot(eq("restored"), eq("source"), isNull(), isNull(), - eq(enabled), isNull(), eq(List.of()), eq(Map.of()), isNull(), eq(5433), isNull())) + eq(enabled), isNull(), eq(List.of()), eq(Map.of()), isNull(), eq(5433), isNull(), isNull())) .thenReturn(makeInstance("restored")); MultivaluedMap p = params(); p.putSingle("DBInstanceIdentifier", "restored"); @@ -2567,11 +2567,11 @@ class RdsQueryHandlerTest { assertEquals(200, handler.handle("RestoreDBInstanceFromDBSnapshot", p).getStatus()); verify(service).restoreDbInstanceFromDbSnapshot("restored", "source", null, null, - enabled, null, List.of(), Map.of(), null, null, null); + enabled, null, List.of(), Map.of(), null, null, null, null); p.putSingle("Port", "5433"); assertEquals(200, handler.handle("RestoreDBInstanceFromDBSnapshot", p).getStatus()); verify(service).restoreDbInstanceFromDbSnapshot("restored", "source", null, null, - enabled, null, List.of(), Map.of(), null, 5433, null); + enabled, null, List.of(), Map.of(), null, 5433, null, null); } @ParameterizedTest @@ -2580,7 +2580,7 @@ class RdsQueryHandlerTest { void restoreInstanceSnapshotForwardsIamFlagWithAndWithoutPort(String value) { Boolean enabled = value == null ? null : Boolean.parseBoolean(value); when(service.restoreDbInstanceFromDbSnapshot(any(), any(), any(), any(), anyBoolean(), - any(), any(), any(), any(), any(), eq(enabled))).thenReturn(makeInstance("restored")); + any(), any(), any(), any(), any(), eq(enabled), isNull())).thenReturn(makeInstance("restored")); MultivaluedMap p = params(); p.putSingle("DBInstanceIdentifier", "restored"); p.putSingle("DBSnapshotIdentifier", "source"); @@ -2589,11 +2589,11 @@ class RdsQueryHandlerTest { } assertEquals(200, handler.handle("RestoreDBInstanceFromDBSnapshot", p).getStatus()); verify(service).restoreDbInstanceFromDbSnapshot("restored", "source", null, null, - false, null, List.of(), Map.of(), null, null, enabled); + false, null, List.of(), Map.of(), null, null, enabled, null); p.putSingle("Port", "5433"); assertEquals(200, handler.handle("RestoreDBInstanceFromDBSnapshot", p).getStatus()); verify(service).restoreDbInstanceFromDbSnapshot("restored", "source", null, null, - false, null, List.of(), Map.of(), null, 5433, enabled); + false, null, List.of(), Map.of(), null, 5433, enabled, null); } @ParameterizedTest @@ -2628,7 +2628,7 @@ class RdsQueryHandlerTest { @Test void restoreDbInstanceFromDbSnapshot_success() { DbInstance instance = makeInstance("mydb"); - when(service.restoreDbInstanceFromDbSnapshot(eq("mydb"), eq("mysnap"), eq("db.t3.large"), eq("us-east-1a"), eq(true), eq("my-subnets"), eq(List.of("sg-123")), eq(Map.of("Env", "Prod")), isNull(), isNull(), isNull())) + when(service.restoreDbInstanceFromDbSnapshot(eq("mydb"), eq("mysnap"), eq("db.t3.large"), eq("us-east-1a"), eq(true), eq("my-subnets"), eq(List.of("sg-123")), eq(Map.of("Env", "Prod")), isNull(), isNull(), isNull(), isNull())) .thenReturn(instance); MultivaluedMap p = params(); diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsStorageTypeIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsStorageTypeIntegrationTest.java new file mode 100644 index 0000000000..843dba073a --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsStorageTypeIntegrationTest.java @@ -0,0 +1,380 @@ +package io.github.hectorvent.floci.services.rds; + +import io.github.hectorvent.floci.testing.RdsMockProfile; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.response.Response; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.List; + +import static io.restassured.RestAssured.given; +import static io.restassured.http.ContentType.URLENC; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +@TestProfile(RdsMockProfile.class) +class RdsStorageTypeIntegrationTest { + + private static RequestSpecification query(String action) { + return query(action, "us-east-1", "000000000000"); + } + + private static RequestSpecification query(String action, String region, String account) { + return given().header("Authorization", "AWS4-HMAC-SHA256 Credential=" + account + "/20261020/" + + region + "/rds/aws4_request, SignedHeaders=content-type;host, Signature=test") + .contentType(URLENC).formParam("Action", action).formParam("Version", "2014-10-31"); + } + + private static RequestSpecification create(String id) { + return create(id, "postgres"); + } + + private static RequestSpecification create(String id, String engine) { + return query("CreateDBInstance").formParam("DBInstanceIdentifier", id) + .formParam("Engine", engine).formParam("DBInstanceClass", "db.t3.micro") + .formParam("MasterUsername", "admin").formParam("MasterUserPassword", "password123") + .formParam("AllocatedStorage", "20"); + } + + private static String type(Response response, String action) { + return response.xmlPath().getString(action + "Response." + action + "Result.DBInstance.StorageType"); + } + + private static void delete(String id) { + query("DeleteDBInstance").formParam("DBInstanceIdentifier", id) + .formParam("SkipFinalSnapshot", "true").post("/"); + } + + @ParameterizedTest + @ValueSource(strings = {"io1", "io2"}) + void provisionedTypesRoundTripOnCreateAndModifyWithoutIops(String storageType) { + String createdId = "storage-provisioned-new-" + storageType; + String changedId = "storage-provisioned-change-" + storageType; + try { + create(changedId).formParam("StorageType", "gp2").post("/").then().statusCode(200); + Response created = create(createdId).formParam("StorageType", storageType).post("/"); + Response changed = query("ModifyDBInstance").formParam("DBInstanceIdentifier", changedId) + .formParam("StorageType", storageType).post("/"); + assertAll(() -> assertEquals(200, created.statusCode(), created.asString()), + () -> assertEquals(storageType, type(created, "CreateDBInstance")), + () -> assertEquals(200, changed.statusCode(), changed.asString()), + () -> assertEquals(storageType, type(changed, "ModifyDBInstance"))); + Response omitted = query("ModifyDBInstance").formParam("DBInstanceIdentifier", changedId) + .formParam("BackupRetentionPeriod", "3").post("/"); + assertEquals(storageType, type(omitted, "ModifyDBInstance")); + for (String id : List.of(createdId, changedId)) { + Response read = query("DescribeDBInstances").formParam("DBInstanceIdentifier", id).post("/"); + String path = "DescribeDBInstancesResponse.DescribeDBInstancesResult.DBInstances.DBInstance."; + assertEquals(200, read.statusCode()); + assertEquals(storageType, read.xmlPath().getString(path + "StorageType")); + assertTrue(read.xmlPath().getList(path + "Iops").isEmpty()); + } + } finally { + delete(createdId); + delete(changedId); + } + } + + @Test + void createRetainsTheRequestedTypeInItsResponseAndDescribe() { + String id = "storage-type-create"; + try { + Response created = create(id).formParam("StorageType", "gp3").post("/"); + Response described = query("DescribeDBInstances").formParam("DBInstanceIdentifier", id).post("/"); + assertAll(() -> assertEquals(200, created.statusCode()), + () -> assertEquals("gp3", type(created, "CreateDBInstance")), + () -> assertEquals("gp3", described.xmlPath().getString( + "DescribeDBInstancesResponse.DescribeDBInstancesResult.DBInstances.DBInstance.StorageType"))); + } finally { + delete(id); + } + } + + @Test + void modifyChangesTheTypeAndOmissionKeepsIt() { + String id = "storage-type-modify"; + try { + create(id).formParam("StorageType", "gp2").post("/").then().statusCode(200); + Response changed = query("ModifyDBInstance").formParam("DBInstanceIdentifier", id) + .formParam("StorageType", "gp3").formParam("BackupRetentionPeriod", "5").post("/"); + Response omitted = query("ModifyDBInstance").formParam("DBInstanceIdentifier", id) + .formParam("BackupRetentionPeriod", "6").post("/"); + assertAll(() -> assertEquals(200, changed.statusCode()), + () -> assertEquals("gp3", type(changed, "ModifyDBInstance")), + () -> assertEquals("gp3", type(omitted, "ModifyDBInstance"))); + } finally { + delete(id); + } + } + + @Test + void anUnsupportedTypeCannotPublishOrPartiallyModifyAnInstance() { + String absent = "storage-type-refused"; + String existing = "storage-type-atomic"; + try { + Response refusedCreate = create(absent).formParam("StorageType", "not-a-storage-type").post("/"); + create(existing).formParam("StorageType", "gp2").formParam("BackupRetentionPeriod", "2") + .post("/").then().statusCode(200); + Response refusedModify = query("ModifyDBInstance").formParam("DBInstanceIdentifier", existing) + .formParam("StorageType", "not-a-storage-type").formParam("BackupRetentionPeriod", "7").post("/"); + Response read = query("DescribeDBInstances").formParam("DBInstanceIdentifier", existing).post("/"); + Response absentRead = query("DescribeDBInstances").formParam("DBInstanceIdentifier", absent).post("/"); + assertAll(() -> assertEquals(400, refusedCreate.statusCode()), + () -> assertEquals(400, refusedModify.statusCode()), + () -> assertEquals(404, absentRead.statusCode()), + () -> assertEquals("2", read.xmlPath().getString( + "DescribeDBInstancesResponse.DescribeDBInstancesResult.DBInstances.DBInstance.BackupRetentionPeriod"))); + create(absent).formParam("StorageType", "gp3").post("/").then().statusCode(200); + query("ModifyDBInstance").formParam("DBInstanceIdentifier", existing).formParam("StorageType", "gp3") + .post("/").then().statusCode(200); + } finally { + delete(absent); + delete(existing); + } + } + @ParameterizedTest + @ValueSource(strings = {"gp3", "io1", "io2"}) + void snapshotsCopiesAndDerivedInstancesKeepTheirStorageMetadata(String storageType) { + String source = "storage-type-source"; + String snapshot = "storage-type-snapshot"; + String copied = "storage-type-snapshot-copy"; + String restored = "storage-type-restored"; + String replica = "storage-type-replica"; + String pitr = "storage-type-pitr"; + try { + create(source).formParam("StorageType", storageType).formParam("BackupRetentionPeriod", "2") + .post("/").then().statusCode(200); + Response taken = query("CreateDBSnapshot").formParam("DBSnapshotIdentifier", snapshot) + .formParam("DBInstanceIdentifier", source).post("/"); + assertEquals(storageType, taken.xmlPath().getString( + "CreateDBSnapshotResponse.CreateDBSnapshotResult.DBSnapshot.StorageType")); + Response copy = query("CopyDBSnapshot").formParam("SourceDBSnapshotIdentifier", snapshot) + .formParam("TargetDBSnapshotIdentifier", copied).post("/"); + assertEquals(storageType, copy.xmlPath().getString( + "CopyDBSnapshotResponse.CopyDBSnapshotResult.DBSnapshot.StorageType")); + Response restore = query("RestoreDBInstanceFromDBSnapshot").formParam("DBSnapshotIdentifier", copied) + .formParam("DBInstanceIdentifier", restored).post("/"); + assertEquals(200, restore.statusCode()); + assertEquals(storageType, type(restore, "RestoreDBInstanceFromDBSnapshot")); + Response readReplica = query("CreateDBInstanceReadReplica").formParam("SourceDBInstanceIdentifier", source) + .formParam("DBInstanceIdentifier", replica).post("/"); + assertEquals(200, readReplica.statusCode()); + assertEquals(storageType, type(readReplica, "CreateDBInstanceReadReplica")); + Response point = query("RestoreDBInstanceToPointInTime").formParam("SourceDBInstanceIdentifier", source) + .formParam("TargetDBInstanceIdentifier", pitr).formParam("UseLatestRestorableTime", "true").post("/"); + assertEquals(200, point.statusCode()); + assertEquals(storageType, type(point, "RestoreDBInstanceToPointInTime")); + Response options = query("DescribeValidDBInstanceModifications") + .formParam("DBInstanceIdentifier", source).post("/"); + assertEquals(List.of("gp2", "gp3", "io1", "io2"), options.xmlPath().getList( + "DescribeValidDBInstanceModificationsResponse.DescribeValidDBInstanceModificationsResult." + + "ValidDBInstanceModificationsMessage.Storage.ValidStorageOptions.StorageType")); + } finally { + delete(pitr); + delete(replica); + delete(restored); + query("DeleteDBSnapshot").formParam("DBSnapshotIdentifier", copied).post("/"); + query("DeleteDBSnapshot").formParam("DBSnapshotIdentifier", snapshot).post("/"); + delete(source); + } + } + + @Test + void omittedCreateUsesTheExistingFallbackAndInvalidTypesLeaveTheLegacyValueAlone() { + String id = "storage-type-default"; + try { + Response created = create(id).post("/"); + assertEquals("gp2", type(created, "CreateDBInstance")); + for (String invalid : List.of("", "GP3", "standard")) { + Response refused = query("ModifyDBInstance").formParam("DBInstanceIdentifier", id) + .formParam("StorageType", invalid).formParam("BackupRetentionPeriod", "9").post("/"); + assertEquals(400, refused.statusCode(), invalid); + assertEquals("StorageTypeNotSupported", refused.xmlPath().getString("ErrorResponse.Error.Code")); + } + Response modified = query("ModifyDBInstance").formParam("DBInstanceIdentifier", id) + .formParam("StorageType", "gp3").post("/"); + assertEquals("gp3", type(modified, "ModifyDBInstance")); + } finally { + delete(id); + } + } + + @Test + void aStorageRefusalNamesTheStoredTypeAndKeepsTheEntireRequestUnapplied() { + String id = "storage-type-shrink"; + try { + create(id).formParam("StorageType", "gp3").formParam("BackupRetentionPeriod", "2") + .post("/").then().statusCode(200); + Response rejected = query("ModifyDBInstance").formParam("DBInstanceIdentifier", id) + .formParam("AllocatedStorage", "19").formParam("StorageType", "gp2") + .formParam("BackupRetentionPeriod", "9").post("/"); + Response described = query("DescribeDBInstances").formParam("DBInstanceIdentifier", id).post("/"); + String path = "DescribeDBInstancesResponse.DescribeDBInstancesResult.DBInstances.DBInstance."; + assertAll(() -> assertEquals(400, rejected.statusCode()), + () -> assertEquals("Invalid storage size for engine name postgres and storage type gp3: 19", + rejected.xmlPath().getString("ErrorResponse.Error.Message")), + () -> assertEquals("gp3", described.xmlPath().getString(path + "StorageType")), + () -> assertEquals("20", described.xmlPath().getString(path + "AllocatedStorage")), + () -> assertEquals("2", described.xmlPath().getString(path + "BackupRetentionPeriod"))); + } finally { + delete(id); + } + } + + @ParameterizedTest + @ValueSource(strings = {"gp2", "io1", "io2"}) + void explicitTypesOverrideDerivedInstanceDefaults(String storageType) { + String source = "storage-override-source"; + String snapshot = "storage-override-snapshot"; + String restore = "storage-override-restored"; + String replica = "storage-override-replica"; + String pitr = "storage-override-pitr"; + try { + create(source).formParam("StorageType", "gp3").formParam("BackupRetentionPeriod", "2") + .post("/").then().statusCode(200); + query("CreateDBSnapshot").formParam("DBSnapshotIdentifier", snapshot) + .formParam("DBInstanceIdentifier", source).post("/").then().statusCode(200); + Response restored = query("RestoreDBInstanceFromDBSnapshot").formParam("DBSnapshotIdentifier", snapshot) + .formParam("DBInstanceIdentifier", restore).formParam("StorageType", storageType).post("/"); + Response replicated = query("CreateDBInstanceReadReplica").formParam("SourceDBInstanceIdentifier", source) + .formParam("DBInstanceIdentifier", replica).formParam("StorageType", storageType).post("/"); + Response point = query("RestoreDBInstanceToPointInTime").formParam("SourceDBInstanceIdentifier", source) + .formParam("TargetDBInstanceIdentifier", pitr).formParam("UseLatestRestorableTime", "true") + .formParam("StorageType", storageType).post("/"); + assertAll(() -> assertEquals(200, restored.statusCode()), + () -> assertEquals(storageType, type(restored, "RestoreDBInstanceFromDBSnapshot")), + () -> assertEquals(200, replicated.statusCode()), + () -> assertEquals(storageType, type(replicated, "CreateDBInstanceReadReplica")), + () -> assertEquals(200, point.statusCode()), + () -> assertEquals(storageType, type(point, "RestoreDBInstanceToPointInTime"))); + } finally { + delete(pitr); + delete(replica); + delete(restore); + query("DeleteDBSnapshot").formParam("DBSnapshotIdentifier", snapshot).post("/"); + delete(source); + } + } + + @ParameterizedTest + @ValueSource(strings = {"mysql", "mariadb", "sqlserver-se"}) + void theOtherSupportedEnginesRetainGeneralPurposeTypes(String engine) { + String id = "storage-engine-" + engine; + try { + Response created = create(id, engine).formParam("StorageType", "gp3").post("/"); + assertEquals(200, created.statusCode()); + assertEquals("gp3", type(created, "CreateDBInstance")); + assertEquals(engine, created.xmlPath().getString("CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.Engine")); + Response modified = query("ModifyDBInstance").formParam("DBInstanceIdentifier", id) + .formParam("StorageType", "gp2").post("/"); + assertEquals(200, modified.statusCode()); + assertEquals("gp2", type(modified, "ModifyDBInstance")); + } finally { + delete(id); + } + } + + @Test + void theSameIdentifierKeepsIndependentOwnerAndRegionSettings() { + String id = "storage-scoped"; + List scopes = List.of(new String[]{"us-east-1", "111111111111", "gp3"}, + new String[]{"us-east-1", "222222222222", "gp2"}, + new String[]{"eu-west-1", "111111111111", "gp2"}); + try { + for (String[] scope : scopes) { + query("CreateDBInstance", scope[0], scope[1]).formParam("DBInstanceIdentifier", id) + .formParam("Engine", "postgres").formParam("DBInstanceClass", "db.t3.micro") + .formParam("MasterUsername", "admin").formParam("MasterUserPassword", "password123") + .formParam("AllocatedStorage", "20").formParam("StorageType", scope[2]) + .post("/").then().statusCode(200); + } + query("ModifyDBInstance", "us-east-1", "222222222222").formParam("DBInstanceIdentifier", id) + .formParam("StorageType", "gp3").post("/").then().statusCode(200); + query("ModifyDBInstance", "us-east-1", "111111111111").formParam("DBInstanceIdentifier", id) + .formParam("StorageType", "gp2").post("/").then().statusCode(200); + List expected = List.of("gp2", "gp3", "gp2"); + for (int index = 0; index < scopes.size(); index++) { + String[] scope = scopes.get(index); + Response described = query("DescribeDBInstances", scope[0], scope[1]) + .formParam("DBInstanceIdentifier", id).post("/"); + assertEquals(expected.get(index), described.xmlPath().getString( + "DescribeDBInstancesResponse.DescribeDBInstancesResult.DBInstances.DBInstance.StorageType")); + } + } finally { + for (String[] scope : scopes) { + query("DeleteDBInstance", scope[0], scope[1]).formParam("DBInstanceIdentifier", id) + .formParam("SkipFinalSnapshot", "true").post("/"); + } + } + } + + @Test + void unsupportedDerivedOverridesCannotPublishATarget() { + String source = "storage-invalid-source"; + String snapshot = "storage-invalid-snapshot"; + List targets = List.of("storage-invalid-restore", "storage-invalid-pitr", "storage-invalid-replica"); + try { + create(source).formParam("StorageType", "gp3").formParam("BackupRetentionPeriod", "2") + .post("/").then().statusCode(200); + query("CreateDBSnapshot").formParam("DBSnapshotIdentifier", snapshot) + .formParam("DBInstanceIdentifier", source).post("/").then().statusCode(200); + Response restore = query("RestoreDBInstanceFromDBSnapshot").formParam("DBSnapshotIdentifier", snapshot) + .formParam("DBInstanceIdentifier", targets.get(0)).formParam("StorageType", "unsupported").post("/"); + Response pitr = query("RestoreDBInstanceToPointInTime").formParam("SourceDBInstanceIdentifier", source) + .formParam("TargetDBInstanceIdentifier", targets.get(1)).formParam("UseLatestRestorableTime", "true") + .formParam("StorageType", "unsupported").post("/"); + Response replica = query("CreateDBInstanceReadReplica").formParam("SourceDBInstanceIdentifier", source) + .formParam("DBInstanceIdentifier", targets.get(2)).formParam("StorageType", "unsupported").post("/"); + for (Response response : List.of(restore, pitr, replica)) { + assertEquals(400, response.statusCode()); + assertEquals("StorageTypeNotSupported", response.xmlPath().getString("ErrorResponse.Error.Code")); + } + for (String target : targets) { + query("DescribeDBInstances").formParam("DBInstanceIdentifier", target).post("/").then().statusCode(404); + } + } finally { + for (String target : targets) { + delete(target); + } + query("DeleteDBSnapshot").formParam("DBSnapshotIdentifier", snapshot).post("/"); + delete(source); + } + } + + @Test + void clusterOwnedStorageDoesNotAcquireAnInstanceTypeOverride() { + String cluster = "storage-cluster-owned"; + String member = "storage-cluster-member"; + try { + query("CreateDBCluster").formParam("DBClusterIdentifier", cluster) + .formParam("Engine", "aurora-postgresql").formParam("MasterUsername", "admin") + .formParam("MasterUserPassword", "password123").post("/").then().statusCode(200); + Response created = query("CreateDBInstance").formParam("DBInstanceIdentifier", member) + .formParam("Engine", "aurora-postgresql").formParam("DBInstanceClass", "db.t3.micro") + .formParam("DBClusterIdentifier", cluster).formParam("StorageType", "unsupported") + .post("/"); + assertEquals(200, created.statusCode()); + assertEquals("gp2", type(created, "CreateDBInstance")); + Response changed = query("ModifyDBInstance").formParam("DBInstanceIdentifier", member) + .formParam("StorageType", "unsupported").post("/"); + assertEquals(200, changed.statusCode()); + assertEquals("gp2", type(changed, "ModifyDBInstance")); + Response options = query("DescribeValidDBInstanceModifications") + .formParam("DBInstanceIdentifier", member).post("/"); + assertEquals(0, options.xmlPath().getList("DescribeValidDBInstanceModificationsResponse." + + "DescribeValidDBInstanceModificationsResult.ValidDBInstanceModificationsMessage." + + "Storage.ValidStorageOptions").size()); + } finally { + delete(member); + query("DeleteDBCluster").formParam("DBClusterIdentifier", cluster) + .formParam("SkipFinalSnapshot", "true").post("/"); + } + } + +} diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/model/DbStorageTypePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/model/DbStorageTypePersistenceTest.java new file mode 100644 index 0000000000..1a4ceb7b60 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/rds/model/DbStorageTypePersistenceTest.java @@ -0,0 +1,81 @@ +package io.github.hectorvent.floci.services.rds.model; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.ServiceConfigAccess; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.nio.file.Path; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class DbStorageTypePersistenceTest { + private static StorageFactory factory(String mode, Path directory) { + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + when(config.defaultAccountId()).thenReturn("000000000000"); + when(config.storage().persistentPath()).thenReturn(directory.toString()); + when(config.storage().wal().compactionIntervalMs()).thenReturn(600000L); + ServiceConfigAccess access = mock(ServiceConfigAccess.class); + when(access.storageMode("rds")).thenReturn(mode); + when(access.storageFlushInterval("rds")).thenReturn(600000L); + return new StorageFactory(config, access); + } + + @ParameterizedTest + @ValueSource(strings = {"persistent", "hybrid", "wal"}) + void instanceAndSnapshotTypesSurviveQuiescentFactoryReload(String mode, @TempDir Path directory) { + StorageFactory first = factory(mode, directory); + StorageBackend instances = first.create("rds", "rds-instances.json", + new TypeReference>() {}); + StorageBackend snapshots = first.create("rds", "rds-snapshots.json", + new TypeReference>() {}); + try { + for (String storageType : new String[]{"gp3", "io1", "io2"}) { + DbInstance instance = new DbInstance(); + instance.setDbInstanceIdentifier("storage-db-" + storageType); + instance.setStorageType(storageType); + DbSnapshot snapshot = new DbSnapshot(); + snapshot.setDbSnapshotIdentifier("storage-snapshot-" + storageType); + snapshot.setStorageType(storageType); + instances.put("us-east-1/storage-db-" + storageType, instance); + snapshots.put("us-east-1/storage-snapshot-" + storageType, snapshot); + } + } finally { + first.shutdownAll(); + } + StorageFactory second = factory(mode, directory); + try { + StorageBackend restoredInstances = second.create("rds", "rds-instances.json", + new TypeReference>() {}); + StorageBackend restoredSnapshots = second.create("rds", "rds-snapshots.json", + new TypeReference>() {}); + for (String storageType : new String[]{"gp3", "io1", "io2"}) { + assertEquals(storageType, restoredInstances.get("us-east-1/storage-db-" + storageType) + .orElseThrow().getStorageType()); + assertEquals(storageType, restoredSnapshots.get("us-east-1/storage-snapshot-" + storageType) + .orElseThrow().getStorageType()); + } + } finally { + second.shutdownAll(); + } + } + + @Test + void absentAndNullLegacyMembersUseThePriorGp2Fallback() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + for (String json : new String[]{"{}", "{\"storageType\":null}"}) { + assertEquals("gp2", mapper.readValue(json, DbInstance.class).getStorageType()); + assertEquals("gp2", mapper.readValue(json, DbSnapshot.class).getStorageType()); + } + } +}