mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(rds): store and describe DB cluster monitoring and Insights settings (#9095)
CreateDBCluster and ModifyDBCluster dropped MonitoringInterval, MonitoringRoleArn, EnablePerformanceInsights, PerformanceInsightsRetentionPeriod and DatabaseInsightsMode, and DescribeDBClusters left them out, so the Terraform AWS provider planned the same change on every run for a cluster that sets them. DbCluster now keeps them, with the checks a DB instance gets plus advanced Database Insights needing Performance Insights with at least 465 days of retention. CreateDBCluster and ModifyDBCluster also check VpcSecurityGroupIds against EC2 as CreateDBInstance does. A modify runs these checks before any of its changes is stored, and a refused create leaves no cluster. An Aurora member reports its cluster's groups, and a cluster automated backup reports its cluster's backup window.
This commit is contained in:
1 parent
d096a6d64e
commit
ce02956615
9 files changed
+465
-30
No files matched your search
@@ -21,6 +21,7 @@ import io.floci.aws.services.rds.model.DatabaseEngine;
|
||||
import io.floci.aws.services.rds.model.DbCluster;
|
||||
import io.floci.aws.services.rds.model.DbClusterAutomatedBackup;
|
||||
import io.floci.aws.services.rds.model.DbClusterEndpoint;
|
||||
import io.floci.aws.services.rds.model.DbClusterMonitoringSettings;
|
||||
import io.floci.aws.services.rds.model.DbClusterParameterGroup;
|
||||
import io.floci.aws.services.rds.model.DbClusterSnapshot;
|
||||
import io.floci.aws.services.rds.model.DbEndpoint;
|
||||
@@ -1469,6 +1470,8 @@ public class RdsQueryHandler {
|
||||
// parsed before any mutating call so a malformed value leaves no cluster behind
|
||||
List<String> securityGroupIds = hasMemberKeys(params, "VpcSecurityGroupIds")
|
||||
? vpcSecurityGroupIds(params) : null;
|
||||
DbClusterMonitoringSettings monitoring = clusterMonitoringSettings(params);
|
||||
monitoring.validate();
|
||||
DbCluster cluster;
|
||||
if (globalClusterIdentifier != null && !globalClusterIdentifier.isBlank()) {
|
||||
cluster = service.createDbClusterInGlobalCluster(globalClusterIdentifier, id, engine,
|
||||
@@ -1495,16 +1498,20 @@ public class RdsQueryHandler {
|
||||
} else if (!tags.isEmpty()) {
|
||||
cluster = service.getDbCluster(id, region);
|
||||
}
|
||||
if (!associatedRoles.isEmpty()) {
|
||||
try {
|
||||
// Checks that need the stored cluster; a refusal deletes the cluster this request created.
|
||||
try {
|
||||
// the groups are checked against the VPC the new cluster was placed in
|
||||
service.requireClusterSecurityGroups(region, id, securityGroupIds);
|
||||
service.setClusterMonitoringSettings(region, id, monitoring, true);
|
||||
if (!associatedRoles.isEmpty()) {
|
||||
cluster = service.addRolesToNewDbCluster(region, id, associatedRoles);
|
||||
} catch (AwsException e) {
|
||||
if (protectedCluster) {
|
||||
service.setDeletionProtection(region, id, false);
|
||||
}
|
||||
service.deleteDbCluster(id, region);
|
||||
throw e;
|
||||
}
|
||||
} catch (AwsException e) {
|
||||
if (protectedCluster) {
|
||||
service.setDeletionProtection(region, id, false);
|
||||
}
|
||||
service.deleteDbCluster(id, region);
|
||||
throw e;
|
||||
}
|
||||
if (enableHttpEndpoint) {
|
||||
cluster = service.setHttpEndpoint(region, id, true);
|
||||
@@ -1523,6 +1530,16 @@ public class RdsQueryHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/** The Enhanced Monitoring, Performance Insights and Database Insights members of a Create/ModifyDBCluster. */
|
||||
private static DbClusterMonitoringSettings clusterMonitoringSettings(MultivaluedMap<String, String> params) {
|
||||
return new DbClusterMonitoringSettings(
|
||||
parseIntegerParam(params, "MonitoringInterval"),
|
||||
params.getFirst("MonitoringRoleArn"),
|
||||
parseOptionalBoolean(params, "EnablePerformanceInsights"),
|
||||
parseIntegerParam(params, "PerformanceInsightsRetentionPeriod"),
|
||||
params.getFirst("DatabaseInsightsMode"));
|
||||
}
|
||||
|
||||
private static Double parseDoubleParam(MultivaluedMap<String, String> params, String name) {
|
||||
String value = params.getFirst(name);
|
||||
if (value == null || value.isBlank()) {
|
||||
@@ -1652,6 +1669,11 @@ public class RdsQueryHandler {
|
||||
// parsed before any mutating call so a malformed value changes nothing
|
||||
List<String> securityGroupIds = hasMemberKeys(params, "VpcSecurityGroupIds")
|
||||
? vpcSecurityGroupIds(params) : null;
|
||||
service.requireClusterSecurityGroups(region, id, securityGroupIds);
|
||||
DbClusterMonitoringSettings monitoring = clusterMonitoringSettings(params);
|
||||
if (!monitoring.isEmpty()) {
|
||||
service.validateClusterMonitoringSettings(region, id, monitoring);
|
||||
}
|
||||
DbCluster cluster = service.modifyDbCluster(id, newPassword, iamEnabled,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId,
|
||||
@@ -1669,6 +1691,9 @@ public class RdsQueryHandler {
|
||||
}
|
||||
cluster = service.setDbClusterRequestSettings(id, region, securityGroupIds,
|
||||
params.getFirst("PreferredBackupWindow"), params.getFirst("PreferredMaintenanceWindow"));
|
||||
if (!monitoring.isEmpty()) {
|
||||
cluster = service.setClusterMonitoringSettings(region, id, monitoring, false);
|
||||
}
|
||||
if (deletionProtection != null) {
|
||||
cluster = service.setDeletionProtection(region, id, deletionProtection);
|
||||
}
|
||||
@@ -3314,7 +3339,8 @@ public class RdsQueryHandler {
|
||||
.elem("EngineVersion", c.getEngineVersion())
|
||||
.elem("DBClusterArn", c.getDbClusterArn())
|
||||
.elem("BackupRetentionPeriod", backup.getBackupRetentionPeriod())
|
||||
.elem("PreferredBackupWindow", "04:00-06:00")
|
||||
.elem("PreferredBackupWindow", c.getPreferredBackupWindow() != null
|
||||
? c.getPreferredBackupWindow() : BackupWindows.DEFAULT_BACKUP_WINDOW)
|
||||
.elem("EngineMode", c.getEngineMode() != null ? c.getEngineMode() : "provisioned");
|
||||
if (c.getEndpoint() != null) {
|
||||
xml.elem("Port", c.getEndpoint().port());
|
||||
@@ -3752,7 +3778,18 @@ public class RdsQueryHandler {
|
||||
.elem("PreferredBackupWindow", c.getPreferredBackupWindow() != null
|
||||
? c.getPreferredBackupWindow() : BackupWindows.DEFAULT_BACKUP_WINDOW)
|
||||
.raw(vpcSecurityGroupsXml(c.getVpcSecurityGroupIds()))
|
||||
.elem("DBSubnetGroup", c.getDbSubnetGroupName() != null ? c.getDbSubnetGroupName() : "default");
|
||||
.elem("MonitoringInterval", c.getMonitoringInterval());
|
||||
if (c.getMonitoringRoleArn() != null && !c.getMonitoringRoleArn().isBlank()) {
|
||||
xml.elem("MonitoringRoleArn", c.getMonitoringRoleArn());
|
||||
}
|
||||
xml.elem("PerformanceInsightsEnabled", c.isPerformanceInsightsEnabled());
|
||||
if (c.isPerformanceInsightsEnabled() && c.getPerformanceInsightsRetentionPeriod() != null) {
|
||||
xml.elem("PerformanceInsightsRetentionPeriod", c.getPerformanceInsightsRetentionPeriod());
|
||||
}
|
||||
if (c.getDatabaseInsightsMode() != null) {
|
||||
xml.elem("DatabaseInsightsMode", c.getDatabaseInsightsMode());
|
||||
}
|
||||
xml.elem("DBSubnetGroup", c.getDbSubnetGroupName() != null ? c.getDbSubnetGroupName() : "default");
|
||||
String clusterParameterGroup = dbClusterParameterGroupName(c, engineStr);
|
||||
if (clusterParameterGroup != null) {
|
||||
xml.elem("DBClusterParameterGroup", clusterParameterGroup);
|
||||
|
||||
@@ -40,6 +40,7 @@ import io.floci.aws.services.rds.model.DatabaseEngine;
|
||||
import io.floci.aws.services.rds.model.DbCluster;
|
||||
import io.floci.aws.services.rds.model.DbClusterAutomatedBackup;
|
||||
import io.floci.aws.services.rds.model.DbClusterEndpoint;
|
||||
import io.floci.aws.services.rds.model.DbClusterMonitoringSettings;
|
||||
import io.floci.aws.services.rds.model.DbClusterParameterGroup;
|
||||
import io.floci.aws.services.rds.model.DbClusterSnapshot;
|
||||
import io.floci.aws.services.rds.model.DbEndpoint;
|
||||
@@ -4342,11 +4343,18 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
/**
|
||||
* The default security group of {@code instance}'s VPC, which describe reports for an instance that
|
||||
* keeps no groups: an Aurora member, whose groups are its cluster's, or one stored before RDS gave
|
||||
* one. No group when its VPC is unknown or gone.
|
||||
* The groups describe reports for an instance that keeps none: an Aurora member reports its
|
||||
* cluster's groups, and otherwise, or for one stored before RDS gave one, the default security
|
||||
* group of its VPC. No group when its VPC is unknown or gone.
|
||||
*/
|
||||
public List<String> defaultVpcSecurityGroupIds(DbInstance instance) {
|
||||
if (!isBlank(instance.getDbClusterIdentifier())) {
|
||||
DbCluster cluster = findClusterForScope(accountIdFromArn(instance.getDbInstanceArn()),
|
||||
regionResolver.regionOf(instance.getDbInstanceArn()), instance.getDbClusterIdentifier());
|
||||
if (cluster != null && !cluster.getVpcSecurityGroupIds().isEmpty()) {
|
||||
return List.copyOf(cluster.getVpcSecurityGroupIds());
|
||||
}
|
||||
}
|
||||
try {
|
||||
return RdsSecurityGroups.vpcDefault(ec2Service, regionResolver.regionOf(instance.getDbInstanceArn()),
|
||||
instance.getVpcId());
|
||||
@@ -12055,6 +12063,85 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
return member == null ? 1 : member.getPromotionTier();
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuses {@code vpcSecurityGroupIds} unless each names a security group of the cluster's Region in
|
||||
* the cluster's VPC, as CreateDBCluster and ModifyDBCluster do. A null or empty list passes.
|
||||
*/
|
||||
public void requireClusterSecurityGroups(String region, String clusterId, List<String> vpcSecurityGroupIds) {
|
||||
if (vpcSecurityGroupIds == null || vpcSecurityGroupIds.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
RdsSecurityGroups.requireInVpc(ec2Service, effectiveRegion, vpcSecurityGroupIds,
|
||||
getDbCluster(clusterId, effectiveRegion).getVpcId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the Enhanced Monitoring, Performance Insights and Database Insights members of a
|
||||
* CreateDBCluster ({@code create} true) or ModifyDBCluster request. On create a member left out
|
||||
* takes the AWS default (no monitoring, Database Insights {@code standard}); on modify it keeps
|
||||
* the cluster's value. The monitoring pair and the Database Insights mode are judged on the
|
||||
* values in effect after the request.
|
||||
*/
|
||||
public synchronized DbCluster setClusterMonitoringSettings(String region, String clusterId,
|
||||
DbClusterMonitoringSettings settings, boolean create) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
DbCluster cluster = getDbCluster(clusterId, effectiveRegion);
|
||||
applyClusterMonitoringSettings(cluster, settings, create);
|
||||
putClusterForScope(currentAccountId(), effectiveRegion, cluster.getDbClusterIdentifier(), cluster);
|
||||
return RESPONSE_COPIER.convertValue(cluster, DbCluster.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the checks of {@link #setClusterMonitoringSettings} against the cluster as it stands, storing
|
||||
* nothing, so a ModifyDBCluster can refuse the request before any of its other changes is stored.
|
||||
*/
|
||||
public synchronized void validateClusterMonitoringSettings(String region, String clusterId,
|
||||
DbClusterMonitoringSettings settings) {
|
||||
DbCluster copy = RESPONSE_COPIER.convertValue(getDbCluster(clusterId, effectiveRegion(region)),
|
||||
DbCluster.class);
|
||||
applyClusterMonitoringSettings(copy, settings, false);
|
||||
}
|
||||
|
||||
private static void applyClusterMonitoringSettings(DbCluster cluster, DbClusterMonitoringSettings settings,
|
||||
boolean create) {
|
||||
settings.validate();
|
||||
int interval = settings.monitoringInterval() != null
|
||||
? settings.monitoringInterval() : cluster.getMonitoringInterval();
|
||||
String role = settings.monitoringRoleArn() != null
|
||||
? settings.monitoringRoleArn() : cluster.getMonitoringRoleArn();
|
||||
if (create) {
|
||||
DbInstanceSettings.validateMonitoringPairOnCreate(interval, role);
|
||||
} else if (interval != 0 && (role == null || role.isBlank())) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"A MonitoringRoleArn value is required if you specify a MonitoringInterval value "
|
||||
+ "other than 0.", 400);
|
||||
}
|
||||
boolean insightsEnabled = settings.performanceInsightsEnabled() != null
|
||||
? settings.performanceInsightsEnabled() : cluster.isPerformanceInsightsEnabled();
|
||||
Integer retention = settings.performanceInsightsRetentionPeriod() != null
|
||||
? settings.performanceInsightsRetentionPeriod() : cluster.getPerformanceInsightsRetentionPeriod();
|
||||
if (insightsEnabled && retention == null) {
|
||||
retention = 7;
|
||||
}
|
||||
String mode = settings.databaseInsightsMode() != null
|
||||
? settings.databaseInsightsMode() : cluster.getDatabaseInsightsMode();
|
||||
if (create && mode == null) {
|
||||
mode = DbClusterMonitoringSettings.DATABASE_INSIGHTS_STANDARD;
|
||||
}
|
||||
if (DbClusterMonitoringSettings.DATABASE_INSIGHTS_ADVANCED.equals(mode)
|
||||
&& (!insightsEnabled || retention < 465)) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"Database Insights advanced mode requires Performance Insights to be enabled "
|
||||
+ "with a retention period of at least 465 days.", 400);
|
||||
}
|
||||
cluster.setMonitoringInterval(interval);
|
||||
cluster.setMonitoringRoleArn(role);
|
||||
cluster.setPerformanceInsightsEnabled(insightsEnabled);
|
||||
cluster.setPerformanceInsightsRetentionPeriod(insightsEnabled ? retention : null);
|
||||
cluster.setDatabaseInsightsMode(mode);
|
||||
}
|
||||
|
||||
/** Stores what a Create/ModifyDBCluster request carries; a null argument leaves that field as it is. */
|
||||
public synchronized DbCluster setDbClusterRequestSettings(String id, String region,
|
||||
List<String> vpcSecurityGroupIds, String backupWindow, String maintenanceWindow) {
|
||||
|
||||
@@ -36,6 +36,13 @@ public class DbCluster {
|
||||
// Null until a request names them; the emitted XML then falls back to the defaults.
|
||||
private String preferredBackupWindow;
|
||||
private String preferredMaintenanceWindow;
|
||||
/** MonitoringInterval documents 0 as its default. */
|
||||
private int monitoringInterval;
|
||||
private String monitoringRoleArn;
|
||||
private boolean performanceInsightsEnabled;
|
||||
private Integer performanceInsightsRetentionPeriod;
|
||||
// Null on a cluster stored before it was kept; the response then leaves it out.
|
||||
private String databaseInsightsMode;
|
||||
private String availabilityZone;
|
||||
private boolean multiAz;
|
||||
private Map<String, String> subnetAvailabilityZones = new LinkedHashMap<>();
|
||||
@@ -283,4 +290,22 @@ public class DbCluster {
|
||||
enabledCloudwatchLogsExports = value == null ? new ArrayList<>() : new ArrayList<>(value);
|
||||
}
|
||||
|
||||
public int getMonitoringInterval() { return monitoringInterval; }
|
||||
public void setMonitoringInterval(int monitoringInterval) { this.monitoringInterval = monitoringInterval; }
|
||||
|
||||
public String getMonitoringRoleArn() { return monitoringRoleArn; }
|
||||
public void setMonitoringRoleArn(String monitoringRoleArn) { this.monitoringRoleArn = monitoringRoleArn; }
|
||||
|
||||
public boolean isPerformanceInsightsEnabled() { return performanceInsightsEnabled; }
|
||||
public void setPerformanceInsightsEnabled(boolean performanceInsightsEnabled) {
|
||||
this.performanceInsightsEnabled = performanceInsightsEnabled;
|
||||
}
|
||||
|
||||
public Integer getPerformanceInsightsRetentionPeriod() { return performanceInsightsRetentionPeriod; }
|
||||
public void setPerformanceInsightsRetentionPeriod(Integer performanceInsightsRetentionPeriod) {
|
||||
this.performanceInsightsRetentionPeriod = performanceInsightsRetentionPeriod;
|
||||
}
|
||||
|
||||
public String getDatabaseInsightsMode() { return databaseInsightsMode; }
|
||||
public void setDatabaseInsightsMode(String databaseInsightsMode) { this.databaseInsightsMode = databaseInsightsMode; }
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package io.floci.aws.services.rds.model;
|
||||
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
/**
|
||||
* The Enhanced Monitoring, Performance Insights and Database Insights members of a CreateDBCluster
|
||||
* or ModifyDBCluster request. A null member is one the request left out: on create that means the
|
||||
* AWS default, on modify it means unchanged.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
public record DbClusterMonitoringSettings(Integer monitoringInterval,
|
||||
String monitoringRoleArn,
|
||||
Boolean performanceInsightsEnabled,
|
||||
Integer performanceInsightsRetentionPeriod,
|
||||
String databaseInsightsMode) {
|
||||
|
||||
public static final String DATABASE_INSIGHTS_STANDARD = "standard";
|
||||
public static final String DATABASE_INSIGHTS_ADVANCED = "advanced";
|
||||
|
||||
public boolean isEmpty() {
|
||||
return monitoringInterval == null && monitoringRoleArn == null && performanceInsightsEnabled == null
|
||||
&& performanceInsightsRetentionPeriod == null && databaseInsightsMode == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The checks the members get on their own, before any is applied: the monitoring interval and
|
||||
* the Performance Insights retention period as on a DB instance, and the Database Insights mode.
|
||||
*/
|
||||
public void validate() {
|
||||
new DbInstanceSettings(null, null, null, null, null, null, monitoringInterval, monitoringRoleArn,
|
||||
performanceInsightsEnabled, performanceInsightsRetentionPeriod, null, null, null, null).validate();
|
||||
if (databaseInsightsMode != null
|
||||
&& !DATABASE_INSIGHTS_STANDARD.equals(databaseInsightsMode)
|
||||
&& !DATABASE_INSIGHTS_ADVANCED.equals(databaseInsightsMode)) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Invalid database insights mode: " + databaseInsightsMode
|
||||
+ ". Valid values are " + DATABASE_INSIGHTS_STANDARD + ", "
|
||||
+ DATABASE_INSIGHTS_ADVANCED + ".", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user