From 2e01673bb72c9f58140b2f874b63a13c3df15a07 Mon Sep 17 00:00:00 2001 From: Shooks Date: Mon, 20 Jan 2025 13:30:34 +0800 Subject: [PATCH 1/3] feat: Add enhanced monitoring inputs for aws_rds_cluster --- internal/service/rds/cluster.go | 28 ++++ internal/service/rds/cluster_test.go | 196 +++++++++++++++++++++++++++ 2 files changed, 224 insertions(+) diff --git a/internal/service/rds/cluster.go b/internal/service/rds/cluster.go index 11ace2a5d15..fd8171046a7 100644 --- a/internal/service/rds/cluster.go +++ b/internal/service/rds/cluster.go @@ -343,6 +343,16 @@ func resourceCluster() *schema.Resource { Optional: true, ForceNew: true, }, + "monitoring_interval": { + Type: schema.TypeInt, + Optional: true, + Default: 0, + }, + "monitoring_role_arn": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, "network_type": { Type: schema.TypeString, Optional: true, @@ -1180,6 +1190,14 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, meta int input.MasterUsername = aws.String(v.(string)) } + if v, ok := d.GetOk("monitoring_interval"); ok { + input.MonitoringInterval = aws.Int32(int32(v.(int))) + } + + if v, ok := d.GetOk("monitoring_role_arn"); ok { + input.MonitoringRoleArn = aws.String(v.(string)) + } + if v, ok := d.GetOk("network_type"); ok { input.NetworkType = aws.String(v.(string)) } @@ -1363,6 +1381,8 @@ func resourceClusterRead(ctx context.Context, d *schema.ResourceData, meta inter d.Set("master_user_secret", nil) } d.Set("master_username", dbc.MasterUsername) + d.Set("monitoring_interval", dbc.MonitoringInterval) + d.Set("monitoring_role_arn", dbc.MonitoringRoleArn) d.Set("network_type", dbc.NetworkType) d.Set("performance_insights_enabled", dbc.PerformanceInsightsEnabled) d.Set("performance_insights_kms_key_id", dbc.PerformanceInsightsKMSKeyId) @@ -1568,6 +1588,14 @@ func resourceClusterUpdate(ctx context.Context, d *schema.ResourceData, meta int } } + if d.HasChange("monitoring_interval") { + input.MonitoringInterval = aws.Int32(int32(d.Get("monitoring_interval").(int))) + } + + if d.HasChange("monitoring_role_arn") { + input.MonitoringRoleArn = aws.String(d.Get("monitoring_role_arn").(string)) + } + if d.HasChange("network_type") { input.NetworkType = aws.String(d.Get("network_type").(string)) } diff --git a/internal/service/rds/cluster_test.go b/internal/service/rds/cluster_test.go index caafc4b670e..df90be332f5 100644 --- a/internal/service/rds/cluster_test.go +++ b/internal/service/rds/cluster_test.go @@ -3099,6 +3099,114 @@ func TestAccRDSCluster_performanceInsightsRetentionPeriod(t *testing.T) { }) } +func TestAccRDSCluster_enhancedMonitoring_enabledToUpdatedMonitoringInterval(t *testing.T) { + ctx := acctest.Context(t) + if testing.Short() { + t.Skip("skipping long-running test in short mode") + } + + var dbCluster types.DBCluster + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + iamRoleResourceName := "aws_iam_role.test" + resourceName := "aws_rds_cluster.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.RDSServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckClusterDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccClusterConfig_enhancedMonitoring(rName, 30), + Check: resource.ComposeTestCheckFunc( + testAccCheckClusterExists(ctx, resourceName, &dbCluster), + resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "30"), + resource.TestCheckResourceAttrPair(resourceName, "monitoring_role_arn", iamRoleResourceName, names.AttrARN), + ), + }, + { + Config: testAccClusterConfig_enhancedMonitoring(rName, 10), + Check: resource.ComposeTestCheckFunc( + testAccCheckClusterExists(ctx, resourceName, &dbCluster), + resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "10"), + ), + }, + }, + }) +} + +func TestAccRDSCluster_enhancedMonitoring_enabledToDisabled(t *testing.T) { + ctx := acctest.Context(t) + if testing.Short() { + t.Skip("skipping long-running test in short mode") + } + + var dbCluster types.DBCluster + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + iamRoleResourceName := "aws_iam_role.test" + resourceName := "aws_rds_cluster.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.RDSServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckClusterDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccClusterConfig_enhancedMonitoring(rName, 30), + Check: resource.ComposeTestCheckFunc( + testAccCheckClusterExists(ctx, resourceName, &dbCluster), + resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "30"), + resource.TestCheckResourceAttrPair(resourceName, "monitoring_role_arn", iamRoleResourceName, names.AttrARN), + ), + }, + { + Config: testAccClusterConfig_enhancedMonitoring_disabled(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckClusterExists(ctx, resourceName, &dbCluster), + resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "0"), + ), + }, + }, + }) +} + +func TestAccRDSCluster_enhancedMonitoring_disabledToEnabled(t *testing.T) { + ctx := acctest.Context(t) + if testing.Short() { + t.Skip("skipping long-running test in short mode") + } + + var dbCluster types.DBCluster + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + iamRoleResourceName := "aws_iam_role.test" + resourceName := "aws_rds_cluster.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.RDSServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckClusterDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccClusterConfig_enhancedMonitoring_disabled(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckClusterExists(ctx, resourceName, &dbCluster), + resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "0"), + ), + }, + { + Config: testAccClusterConfig_enhancedMonitoring(rName, 30), + Check: resource.ComposeTestCheckFunc( + testAccCheckClusterExists(ctx, resourceName, &dbCluster), + resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "30"), + resource.TestCheckResourceAttrPair(resourceName, "monitoring_role_arn", iamRoleResourceName, names.AttrARN), + ), + }, + }, + }) +} + func testAccCheckClusterDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { return testAccCheckClusterDestroyWithProvider(ctx)(s, acctest.Provider) @@ -6163,3 +6271,91 @@ resource "aws_rds_cluster" "test" { } `, rName, tfrds.ClusterEngineMySQL) } + +func testAccClusterConfig_enhancedMonitoring(rName string, monitoringInterval int) string { + return fmt.Sprintf(` +data "aws_partition" "current" {} + +resource "aws_iam_role" "test" { + name = %[1]q + + assume_role_policy = < Date: Mon, 20 Jan 2025 13:31:52 +0800 Subject: [PATCH 2/3] doc: Add enhanced monitoring doc for aws_rds_cluster --- website/docs/r/rds_cluster.html.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/docs/r/rds_cluster.html.markdown b/website/docs/r/rds_cluster.html.markdown index 23128910e20..569d4f427be 100644 --- a/website/docs/r/rds_cluster.html.markdown +++ b/website/docs/r/rds_cluster.html.markdown @@ -246,6 +246,8 @@ This resource supports the following arguments: * `master_password` - (Required unless `manage_master_user_password` is set to true or unless a `snapshot_identifier` or `replication_source_identifier` is provided or unless a `global_cluster_identifier` is provided when the cluster is the "secondary" cluster of a global database) Password for the master DB user. Note that this may show up in logs, and it will be stored in the state file. Please refer to the [RDS Naming Constraints][5]. Cannot be set if `manage_master_user_password` is set to `true`. * `master_user_secret_kms_key_id` - (Optional) Amazon Web Services KMS key identifier is the key ARN, key ID, alias ARN, or alias name for the KMS key. To use a KMS key in a different Amazon Web Services account, specify the key ARN or alias ARN. If not specified, the default KMS key for your Amazon Web Services account is used. * `master_username` - (Required unless a `snapshot_identifier` or `replication_source_identifier` is provided or unless a `global_cluster_identifier` is provided when the cluster is the "secondary" cluster of a global database) Username for the master DB user. Please refer to the [RDS Naming Constraints][5]. This argument does not support in-place updates and cannot be changed during a restore from snapshot. +* `monitoring_interval` - (Optional) Interval, in seconds, in seconds, between points when Enhanced Monitoring metrics are collected for the DB cluster. To turn off collecting Enhanced Monitoring metrics, specify 0. The default is 0. Valid Values: 0, 1, 5, 10, 15, 30, 60. +* `monitoring_role_arn` - (Optional) ARN for the IAM role that permits RDS to send enhanced monitoring metrics to CloudWatch Logs. You can find more information on the [AWS Documentation](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_Monitoring.html#USER_Monitoring.OS.IAMRole.html) what IAM permissions are needed to allow Enhanced Monitoring for RDS Clusters. * `network_type` - (Optional) Network type of the cluster. Valid values: `IPV4`, `DUAL`. * `performance_insights_enabled` - (Optional) Enables Performance Insights. * `performance_insights_kms_key_id` - (Optional) Specifies the KMS Key ID to encrypt Performance Insights data. If not specified, the default RDS KMS key will be used (`aws/rds`). From 3e6406442e1696dbb03a84e4cf2ea4bc43943a55 Mon Sep 17 00:00:00 2001 From: Shooks Date: Mon, 20 Jan 2025 13:32:07 +0800 Subject: [PATCH 3/3] Add changelog entry --- .changelog/41002.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/41002.txt diff --git a/.changelog/41002.txt b/.changelog/41002.txt new file mode 100644 index 00000000000..3f01980ead2 --- /dev/null +++ b/.changelog/41002.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_rds_cluster: Add `monitoring_interval` and `monitoring_role_arn` arguments for Enhanced Monitoring for RDS Cluster +``` \ No newline at end of file