Skip to content

Commit

Permalink
[DBInstance] Send EnablePerformanceInsights when changing Performance…
Browse files Browse the repository at this point in the history
…InsightsRetentionPeriod (aws-cloudformation#531)

Co-authored-by: dbbh <hendiogo@amazon.com>
  • Loading branch information
dbbh and dbbh authored Apr 15, 2024
1 parent dad81f5 commit 1039659
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,6 @@ public static ModifyDbInstanceRequest modifyDbInstanceRequest(
.multiAZ(diff(previousModel.getMultiAZ(), desiredModel.getMultiAZ()))
.networkType(diff(previousModel.getNetworkType(), desiredModel.getNetworkType()))
.optionGroupName(diff(previousModel.getOptionGroupName(), desiredModel.getOptionGroupName()))
.performanceInsightsRetentionPeriod(diff(previousModel.getPerformanceInsightsRetentionPeriod(), desiredModel.getPerformanceInsightsRetentionPeriod()))
.preferredBackupWindow(diff(previousModel.getPreferredBackupWindow(), desiredModel.getPreferredBackupWindow()))
.preferredMaintenanceWindow(diff(previousModel.getPreferredMaintenanceWindow(), desiredModel.getPreferredMaintenanceWindow()))
.promotionTier(desiredModel.getPromotionTier()) // promotion tier is set unconditionally
Expand Down Expand Up @@ -514,13 +513,15 @@ public static ModifyDbInstanceRequest modifyDbInstanceRequest(
builder.useDefaultProcessorFeatures(desiredModel.getUseDefaultProcessorFeatures());
}

// EnablePerformanceInsights (EPI) and PerformanceInsightsKMSKeyId (PKI) are coupled parameters.
// The following logic stands:
// EnablePerformanceInsights (EPI), PerformanceInsightsKMSKeyId (PKI) and PerformanceInsightsRetentionPeriod (PIP)
// are coupled parameters. The following logic stands:
// 0. If EPI or PKI are changed, provide the diff unconditionally.
// 1. if EPI is true, provide the desired PKI unconditionally.
// 2. if PKI is changed, provide the desired EPI unconditionally.
// 3. if PIP is changed, provide the desired EPI unconditionally.
final Boolean enablePerformanceInsightsDiff = diff(previousModel.getEnablePerformanceInsights(), desiredModel.getEnablePerformanceInsights());
final String performanceInsightsKMSKeyIdDiff = diff(previousModel.getPerformanceInsightsKMSKeyId(), desiredModel.getPerformanceInsightsKMSKeyId());
final Integer performanceInsightsRetentionPeriodDiff = diff(previousModel.getPerformanceInsightsRetentionPeriod(), desiredModel.getPerformanceInsightsRetentionPeriod());

if (enablePerformanceInsightsDiff != null || performanceInsightsKMSKeyIdDiff != null) {
builder.enablePerformanceInsights(enablePerformanceInsightsDiff);
Expand All @@ -533,6 +534,11 @@ public static ModifyDbInstanceRequest modifyDbInstanceRequest(
}
}

if (performanceInsightsRetentionPeriodDiff != null) {
builder.performanceInsightsRetentionPeriod(performanceInsightsRetentionPeriodDiff);
builder.enablePerformanceInsights(desiredModel.getEnablePerformanceInsights());
}

if (BooleanUtils.isTrue(desiredModel.getManageMasterUserPassword())) {
builder.manageMasterUserPassword(true);
if (desiredModel.getMasterUserSecret() != null) {
Expand Down Expand Up @@ -617,6 +623,7 @@ public static ModifyDbInstanceRequest modifyDbInstanceAfterCreateRequest(final R
.maxAllocatedStorage(model.getMaxAllocatedStorage())
.monitoringInterval(model.getMonitoringInterval())
.monitoringRoleArn(model.getMonitoringRoleArn())
.performanceInsightsRetentionPeriod(model.getPerformanceInsightsRetentionPeriod())
.performanceInsightsKMSKeyId(model.getPerformanceInsightsKMSKeyId())
.preferredBackupWindow(model.getPreferredBackupWindow())
.preferredMaintenanceWindow(model.getPreferredMaintenanceWindow());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,38 @@ public void test_modifyDbInstanceRequest_PerformanceInsightsToggleEnabledToDisab
assertThat(request.performanceInsightsKMSKeyId()).isNull();
}

@Test
public void test_modifyDbInstanceRequest_PerformanceInsightsChangeRetentionPeriod() {
final int NEW_RETENTION_PERIOD = 31;
final ResourceModel previousModel = ResourceModel.builder()
.enablePerformanceInsights(true)
.performanceInsightsRetentionPeriod(7)
.build();
final ResourceModel desiredModel = ResourceModel.builder()
.enablePerformanceInsights(true)
.performanceInsightsRetentionPeriod(NEW_RETENTION_PERIOD)
.build();
final ModifyDbInstanceRequest request = Translator.modifyDbInstanceRequest(previousModel, desiredModel, false);
assertThat(request.enablePerformanceInsights()).isTrue();
assertThat(request.performanceInsightsRetentionPeriod()).isEqualTo(NEW_RETENTION_PERIOD);
}

@Test
public void test_modifyDbInstanceRequest_NoPerformanceInsightsChangeRetentionPeriod() {
final int NEW_RETENTION_PERIOD = 31;
final ResourceModel previousModel = ResourceModel.builder()
.enablePerformanceInsights(false)
.performanceInsightsRetentionPeriod(7)
.build();
final ResourceModel desiredModel = ResourceModel.builder()
.enablePerformanceInsights(false)
.performanceInsightsRetentionPeriod(NEW_RETENTION_PERIOD)
.build();
final ModifyDbInstanceRequest request = Translator.modifyDbInstanceRequest(previousModel, desiredModel, false);
assertThat(request.enablePerformanceInsights()).isFalse();
assertThat(request.performanceInsightsRetentionPeriod()).isEqualTo(NEW_RETENTION_PERIOD);
}

@Test
public void test_modifyDbInstanceRequest_PerformanceInsightsEnabledChangeKMSKeyId() {
final String kmsKeyId1 = "test-kms-key-id-1";
Expand Down Expand Up @@ -1097,6 +1129,18 @@ public void test_modifyAfterCreate_shouldSetEnablePerformanceInsights() {
assertThat(request.enablePerformanceInsights()).isTrue();
}

@Test
public void test_modifyAfterCreate_shouldSetEnablePerformanceInsightsRetentionPeriod() {
final int PI_RETENTION_PERIOD = 31;
final ResourceModel model = RESOURCE_MODEL_BLDR()
.enablePerformanceInsights(true)
.performanceInsightsRetentionPeriod(PI_RETENTION_PERIOD)
.build();
final ModifyDbInstanceRequest request = Translator.modifyDbInstanceAfterCreateRequest(model);
assertThat(request.enablePerformanceInsights()).isTrue();
assertThat(request.performanceInsightsRetentionPeriod()).isEqualTo(PI_RETENTION_PERIOD);
}

@Test
public void test_modifyAfterCreate_shouldSetEnhancedMonitoring() {
final String monitoringRoleArn = "monitoring-role-arn";
Expand Down

0 comments on commit 1039659

Please sign in to comment.