Skip to content

Commit

Permalink
Merge pull request #9745 from terraform-providers/b-aws_db_instance-p…
Browse files Browse the repository at this point in the history
…erformance_insights_kms_key_id-on-update

resource/aws_db_instance: Only send performance_insights_kms_key_id on update if configured
  • Loading branch information
bflad authored Aug 14, 2019
2 parents 96cbdf6 + b2b1839 commit 61aebf1
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 73 deletions.
13 changes: 9 additions & 4 deletions aws/resource_aws_db_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1630,11 +1630,16 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error
d.SetPartial("performance_insights_enabled")
req.EnablePerformanceInsights = aws.Bool(d.Get("performance_insights_enabled").(bool))

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

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

d.SetPartial("performance_insights_retention_period")
req.PerformanceInsightsRetentionPeriod = aws.Int64(int64(d.Get("performance_insights_retention_period").(int)))
requestUpdate = true
}

Expand Down
259 changes: 190 additions & 69 deletions aws/resource_aws_db_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2229,53 +2229,147 @@ func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestC
}
}

func TestAccAWSRDSDBInstance_withPerformanceInsights(t *testing.T) {
var v rds.DBInstance
rInt := acctest.RandInt()
resourceName := "aws_db_instance.bar"
// Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/8792
func TestAccAWSRDSDBInstance_PerformanceInsightsEnabled_DisabledToEnabled(t *testing.T) {
var dbInstance rds.DBInstance
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_db_instance.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDBInstancePerformanceInsightsEnabled(rInt, 7),
Config: testAccAWSDBInstancePerformanceInsightsDisabled(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &v),
testAccCheckAWSDBInstanceAttributes(&v),
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "performance_insights_enabled", "false"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"password",
"skip_final_snapshot",
"final_snapshot_identifier",
},
},
{
Config: testAccAWSDBInstancePerformanceInsightsEnabled(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "performance_insights_enabled", "true"),
resource.TestCheckResourceAttrPair(resourceName, "performance_insights_kms_key_id", "aws_kms_key.foo", "arn"),
resource.TestCheckResourceAttr(resourceName, "performance_insights_retention_period", "7"),
),
},
},
})
}

func TestAccAWSRDSDBInstance_PerformanceInsightsEnabled_EnabledToDisabled(t *testing.T) {
var dbInstance rds.DBInstance
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_db_instance.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDBInstancePerformanceInsightsDisabled(rInt),
Config: testAccAWSDBInstancePerformanceInsightsEnabled(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &v),
testAccCheckAWSDBInstanceAttributes(&v),
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "performance_insights_enabled", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"password",
"skip_final_snapshot",
"final_snapshot_identifier",
},
},
{
Config: testAccAWSDBInstancePerformanceInsightsDisabled(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "performance_insights_enabled", "false"),
resource.TestCheckResourceAttrPair(resourceName, "performance_insights_kms_key_id", "aws_kms_key.foo", "arn"),
resource.TestCheckResourceAttr(resourceName, "performance_insights_retention_period", "0"),
),
},
},
})
}

func TestAccAWSRDSDBInstance_PerformanceInsightsKmsKeyId(t *testing.T) {
var dbInstance rds.DBInstance
rName := acctest.RandomWithPrefix("tf-acc-test")
kmsKeyResourceName := "aws_kms_key.test"
resourceName := "aws_db_instance.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDBInstancePerformanceInsightsEnabled(rInt, 7),
Config: testAccAWSDBInstancePerformanceInsightsKmsKeyId(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &v),
testAccCheckAWSDBInstanceAttributes(&v),
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "performance_insights_enabled", "true"),
resource.TestCheckResourceAttrPair(resourceName, "performance_insights_kms_key_id", "aws_kms_key.foo", "arn"),
resource.TestCheckResourceAttr(resourceName, "performance_insights_retention_period", "7"),
resource.TestCheckResourceAttrPair(resourceName, "performance_insights_kms_key_id", kmsKeyResourceName, "arn"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"password",
"skip_final_snapshot",
"final_snapshot_identifier",
},
},
{
Config: testAccAWSDBInstancePerformanceInsightsKmsKeyIdDisabled(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "performance_insights_enabled", "false"),
resource.TestCheckResourceAttrPair(resourceName, "performance_insights_kms_key_id", kmsKeyResourceName, "arn"),
),
},
{
Config: testAccAWSDBInstancePerformanceInsightsEnabled(rInt, 731),
Config: testAccAWSDBInstancePerformanceInsightsKmsKeyId(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &v),
testAccCheckAWSDBInstanceAttributes(&v),
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "performance_insights_enabled", "true"),
resource.TestCheckResourceAttrPair(resourceName, "performance_insights_kms_key_id", kmsKeyResourceName, "arn"),
),
},
},
})
}

func TestAccAWSRDSDBInstance_PerformanceInsightsRetentionPeriod(t *testing.T) {
var dbInstance rds.DBInstance
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_db_instance.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDBInstancePerformanceInsightsRetentionPeriod(rName, 731),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "performance_insights_enabled", "true"),
resource.TestCheckResourceAttrPair(resourceName, "performance_insights_kms_key_id", "aws_kms_key.foo", "arn"),
resource.TestCheckResourceAttr(resourceName, "performance_insights_retention_period", "731"),
),
},
Expand All @@ -2289,6 +2383,14 @@ func TestAccAWSRDSDBInstance_withPerformanceInsights(t *testing.T) {
"final_snapshot_identifier",
},
},
{
Config: testAccAWSDBInstancePerformanceInsightsRetentionPeriod(rName, 7),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "performance_insights_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "performance_insights_retention_period", "7"),
),
},
},
})
}
Expand Down Expand Up @@ -5127,33 +5229,51 @@ resource "aws_db_instance" "test" {
`, rName, rName, rName, rName)
}

func testAccAWSDBInstancePerformanceInsightsDisabled(n int) string {
func testAccAWSDBInstancePerformanceInsightsDisabled(rName string) string {
return fmt.Sprintf(`
resource "aws_kms_key" "foo" {
description = "Terraform acc test %d"
resource "aws_db_instance" "test" {
allocated_storage = 5
backup_retention_period = 0
engine = "mysql"
engine_version = "5.6.41"
identifier = %[1]q
instance_class = "db.m3.medium"
name = "mydb"
password = "mustbeeightcharaters"
skip_final_snapshot = true
username = "foo"
}
`, rName)
}

policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "kms-tf-1",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "kms:*",
"Resource": "*"
}
]
func testAccAWSDBInstancePerformanceInsightsEnabled(rName string) string {
return fmt.Sprintf(`
resource "aws_db_instance" "test" {
allocated_storage = 5
backup_retention_period = 0
engine = "mysql"
engine_version = "5.6.41"
identifier = %[1]q
instance_class = "db.m3.medium"
name = "mydb"
password = "mustbeeightcharaters"
performance_insights_enabled = true
performance_insights_retention_period = 7
skip_final_snapshot = true
username = "foo"
}
POLICY
`, rName)
}

resource "aws_db_instance" "bar" {
func testAccAWSDBInstancePerformanceInsightsKmsKeyIdDisabled(rName string) string {
return fmt.Sprintf(`
resource "aws_kms_key" "test" {
deletion_window_in_days = 7
}
resource "aws_db_instance" "test" {
engine = "mysql"
identifier = "tf-db-test-%d"
identifier = %[1]q
instance_class = "db.m3.medium"
allocated_storage = 5
backup_retention_period = 0
Expand All @@ -5162,49 +5282,50 @@ resource "aws_db_instance" "bar" {
password = "mustbeeightcharaters"
skip_final_snapshot = true
}
`, n, n)
`, rName)
}

func testAccAWSDBInstancePerformanceInsightsEnabled(n int, piRetentionPeriod int) string {
func testAccAWSDBInstancePerformanceInsightsKmsKeyId(rName string) string {
return fmt.Sprintf(`
resource "aws_kms_key" "foo" {
description = "Terraform acc test %d"
resource "aws_kms_key" "test" {
deletion_window_in_days = 7
}
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "kms-tf-1",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "kms:*",
"Resource": "*"
}
]
resource "aws_db_instance" "test" {
allocated_storage = 5
backup_retention_period = 0
engine = "mysql"
engine_version = "5.6.41"
identifier = %[1]q
instance_class = "db.m3.medium"
name = "mydb"
password = "mustbeeightcharaters"
performance_insights_enabled = true
performance_insights_kms_key_id = "${aws_kms_key.test.arn}"
performance_insights_retention_period = 7
skip_final_snapshot = true
username = "foo"
}
POLICY
`, rName)
}

resource "aws_db_instance" "bar" {
func testAccAWSDBInstancePerformanceInsightsRetentionPeriod(rName string, performanceInsightsRetentionPeriod int) string {
return fmt.Sprintf(`
resource "aws_db_instance" "test" {
allocated_storage = 5
backup_retention_period = 0
engine = "mysql"
engine_version = "5.6.41"
identifier = "tf-db-test-%d"
identifier = %[1]q
instance_class = "db.m3.medium"
name = "mydb"
password = "mustbeeightcharaters"
performance_insights_enabled = true
performance_insights_kms_key_id = "${aws_kms_key.foo.arn}"
performance_insights_retention_period = %d
performance_insights_retention_period = %[2]d
skip_final_snapshot = true
username = "foo"
}
`, n, n, piRetentionPeriod)
`, rName, performanceInsightsRetentionPeriod)
}

func testAccAWSDBInstanceConfig_ReplicateSourceDb_PerformanceInsightsEnabled(rName string) string {
Expand Down

0 comments on commit 61aebf1

Please sign in to comment.