Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Smith committed Feb 15, 2023
1 parent 4a3352f commit 6e57b85
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 12 deletions.
43 changes: 43 additions & 0 deletions internal/service/rds/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,21 @@ func ResourceCluster() *schema.Resource {
Computed: true,
ValidateFunc: validation.StringInSlice(NetworkType_Values(), false),
},
"performance_insights_enabled": {
Type: schema.TypeBool,
Optional: true,
},
"performance_insights_kms_key_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: verify.ValidARN,
},
"performance_insights_retention_period": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"port": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -893,6 +908,18 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, meta int
input.NetworkType = aws.String(v.(string))
}

if v, ok := d.GetOk("performance_insights_enabled"); ok {
input.EnablePerformanceInsights = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("performance_insights_kms_key_id"); ok {
input.PerformanceInsightsKMSKeyId = aws.String(v.(string))
}

if v, ok := d.GetOk("performance_insights_retention_period"); ok {
input.PerformanceInsightsRetentionPeriod = aws.Int64(int64(v.(int)))
}

if v, ok := d.GetOk("port"); ok {
input.Port = aws.Int64(int64(v.(int)))
}
Expand Down Expand Up @@ -1033,6 +1060,9 @@ func resourceClusterRead(ctx context.Context, d *schema.ResourceData, meta inter
d.Set("kms_key_id", dbc.KmsKeyId)
d.Set("master_username", dbc.MasterUsername)
d.Set("network_type", dbc.NetworkType)
d.Set("performance_insights_enabled", dbc.PerformanceInsightsEnabled)
d.Set("performance_insights_kms_key_id", dbc.PerformanceInsightsKMSKeyId)
d.Set("performance_insights_retention_period", dbc.PerformanceInsightsRetentionPeriod)
d.Set("port", dbc.Port)
d.Set("preferred_backup_window", dbc.PreferredBackupWindow)
d.Set("preferred_maintenance_window", dbc.PreferredMaintenanceWindow)
Expand Down Expand Up @@ -1187,6 +1217,18 @@ func resourceClusterUpdate(ctx context.Context, d *schema.ResourceData, meta int
input.NetworkType = aws.String(d.Get("network_type").(string))
}

if d.HasChange("performance_insights_enabled") {
input.EnablePerformanceInsights = aws.Bool(d.Get("performance_insights_enabled").(bool))
}

if d.HasChange("performance_insights_kms_key_id") {
input.PerformanceInsightsKMSKeyId = aws.String(d.Get("performance_insights_kms_key_id").(string))
}

if d.HasChange("performance_insights_retention_period") {
input.PerformanceInsightsRetentionPeriod = aws.Int64(int64(d.Get("performance_insights_retention_period").(int)))
}

if d.HasChange("port") {
input.Port = aws.Int64(int64(d.Get("port").(int)))
}
Expand Down Expand Up @@ -1524,6 +1566,7 @@ func waitDBClusterUpdated(ctx context.Context, conn *rds.RDS, id string, timeout
ClusterStatusRenaming,
ClusterStatusResettingMasterCredentials,
ClusterStatusUpgrading,
ClusterStatusConfiguringEnhancedMonitoring,
},
Target: []string{ClusterStatusAvailable},
Refresh: statusDBCluster(ctx, conn, id),
Expand Down
150 changes: 150 additions & 0 deletions internal/service/rds/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2193,6 +2193,98 @@ func TestAccRDSCluster_enableHTTPEndpoint(t *testing.T) {
})
}

func TestAccRDSCluster_performanceInsightsEnabled(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var dbCluster rds.DBCluster

rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_rds_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, rds.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckClusterDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccClusterConfig_performanceInsightsEnabled(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
resource.TestCheckResourceAttr(resourceName, "performance_insights_enabled", "true"),
),
},
{
Config: testAccClusterConfig_performanceInsightsEnabled(rName, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
resource.TestCheckResourceAttr(resourceName, "performance_insights_enabled", "false"),
),
},
},
})
}

func TestAccRDSCluster_performanceInsightsKMSKeyID(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var dbCluster rds.DBCluster

rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_rds_cluster.test"
kmsKeyResourceName := "aws_kms_key.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, rds.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckClusterDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccClusterConfig_performanceInsightsKMSKeyID(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
resource.TestCheckResourceAttrPair(resourceName, "performance_insights_kms_key_id", kmsKeyResourceName, "arn"),
),
},
},
})
}

func TestAccRDSCluster_performanceInsightsRetentionPeriod(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var dbCluster rds.DBCluster

rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_rds_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, rds.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckClusterDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccClusterConfig_performanceInsightsRetentionPeriod(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
resource.TestCheckResourceAttr(resourceName, "performance_insights_retention_period", "62"),
),
},
},
})
}

func testAccCheckClusterDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
return testAccCheckClusterDestroyWithProvider(ctx)(s, acctest.Provider)
Expand Down Expand Up @@ -4161,3 +4253,61 @@ resource "aws_rds_cluster" "test" {
}
`, rName, enableHttpEndpoint)
}

func testAccClusterConfig_performanceInsightsEnabled(rName string, performanceInsightsEnabled bool) string {
return fmt.Sprintf(`
resource "aws_rds_cluster" "test" {
cluster_identifier = "%s"
engine = "mysql"
db_cluster_instance_class = "db.m6gd.large"
storage_type = "io1"
allocated_storage = 100
iops = 1000
master_username = "foo"
master_password = "Test1234!"
skip_final_snapshot = true
performance_insights_enabled = %t
}
`, rName, performanceInsightsEnabled)
}

func testAccClusterConfig_performanceInsightsKMSKeyID(rName string) string {
return fmt.Sprintf(`
resource "aws_kms_key" "test" {}
resource "aws_rds_cluster" "test" {
cluster_identifier = "%s"
engine = "mysql"
db_cluster_instance_class = "db.m6gd.large"
storage_type = "io1"
allocated_storage = 100
iops = 1000
master_username = "foo"
master_password = "Test1234!"
skip_final_snapshot = true
performance_insights_enabled = true
performance_insights_kms_key_id = aws_kms_key.test.arn
}
`, rName)
}

func testAccClusterConfig_performanceInsightsRetentionPeriod(rName string) string {
return fmt.Sprintf(`
resource "aws_rds_cluster" "test" {
cluster_identifier = "%s"
engine = "mysql"
db_cluster_instance_class = "db.m6gd.large"
storage_type = "io1"
allocated_storage = 100
iops = 1000
master_username = "foo"
master_password = "Test1234!"
skip_final_snapshot = true
performance_insights_enabled = true
performance_insights_retention_period = 62
}
`, rName)
}
25 changes: 13 additions & 12 deletions internal/service/rds/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ const (
)

const (
ClusterStatusAvailable = "available"
ClusterStatusBackingUp = "backing-up"
ClusterStatusConfiguringIAMDatabaseAuth = "configuring-iam-database-auth"
ClusterStatusCreating = "creating"
ClusterStatusDeleting = "deleting"
ClusterStatusMigrating = "migrating"
ClusterStatusModifying = "modifying"
ClusterStatusPreparingDataMigration = "preparing-data-migration"
ClusterStatusRebooting = "rebooting"
ClusterStatusRenaming = "renaming"
ClusterStatusResettingMasterCredentials = "resetting-master-credentials"
ClusterStatusUpgrading = "upgrading"
ClusterStatusAvailable = "available"
ClusterStatusBackingUp = "backing-up"
ClusterStatusConfiguringIAMDatabaseAuth = "configuring-iam-database-auth"
ClusterStatusConfiguringEnhancedMonitoring = "configuring-enhanced-monitoring"
ClusterStatusCreating = "creating"
ClusterStatusDeleting = "deleting"
ClusterStatusMigrating = "migrating"
ClusterStatusModifying = "modifying"
ClusterStatusPreparingDataMigration = "preparing-data-migration"
ClusterStatusRebooting = "rebooting"
ClusterStatusRenaming = "renaming"
ClusterStatusResettingMasterCredentials = "resetting-master-credentials"
ClusterStatusUpgrading = "upgrading"
)

const (
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/rds_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ The following arguments are supported:
* `kms_key_id` - (Optional) The ARN for the KMS encryption key. When specifying `kms_key_id`, `storage_encrypted` needs to be set to true.
* `master_password` - (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) 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]
* `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.
* `performance_insights_enabled` - (Optional) Valid only for Non-Aurora Multi-AZ DB Clusters. Enables Performance Insights for the RDS Cluster
* `performance_insights_kms_key_id` - (Optional) Valid only for Non-Aurora Multi-AZ DB Clusters. Specifies the KMS Key ID to encrypt Performance Insights data. If not specified, the default RDS KMS key will be used (`aws/rds`)
* `performance_insights_retention_period` - (Optional) Valid only for Non-Aurora Multi-AZ DB Clusters. Specifies the amount of time to retain performance insights data for. Defaults to 7 days if Performance Insights are enabled. Valid values are `7`, `month * 31` (where month is a number of months from 1-23), and `731`. See [here](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PerfInsights.Overview.cost.html) for more information on retention periods.
* `port` - (Optional) The port on which the DB accepts connections
* `preferred_backup_window` - (Optional) The daily time range during which automated backups are created if automated backups are enabled using the BackupRetentionPeriod parameter.Time in UTC. Default: A 30-minute window selected at random from an 8-hour block of time per regionE.g., 04:00-09:00
* `preferred_maintenance_window` - (Optional) The weekly time range during which system maintenance can occur, in (UTC) e.g., wed:04:00-wed:04:30
Expand Down

0 comments on commit 6e57b85

Please sign in to comment.