Skip to content

Commit

Permalink
Merge pull request #18013 from hashicorp/e-make_snapshot_identifier_c…
Browse files Browse the repository at this point in the history
…omputed

Make snapshot_identifier computed
  • Loading branch information
bill-rich authored Mar 11, 2021
2 parents a9ba178 + d9664f5 commit dce223f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/18013.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_db_instance: Allow `snapshot_identifier` to be removed from configuration without resource recreation
```
1 change: 1 addition & 0 deletions aws/resource_aws_db_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ func resourceAwsDbInstance() *schema.Resource {

"snapshot_identifier": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
Expand Down
66 changes: 66 additions & 0 deletions aws/resource_aws_db_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,34 @@ func TestAccAWSDBInstance_SnapshotIdentifier(t *testing.T) {
})
}

func TestAccAWSDBInstance_SnapshotIdentifierRemoved(t *testing.T) {
var dbInstance1, dbInstance2 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: testAccCheckAWSDBInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDBInstanceConfig_SnapshotIdentifier(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance1),
),
},
{
Config: testAccAWSDBInstanceConfig_SnapshotIdentifierRemoved(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance2),
testAccCheckAWSDBInstanceNotRecreated(&dbInstance1, &dbInstance2),
),
},
},
})
}

func TestAccAWSDBInstance_SnapshotIdentifier_AllocatedStorage(t *testing.T) {
var dbInstance, sourceDbInstance rds.DBInstance
var dbSnapshot rds.DBSnapshot
Expand Down Expand Up @@ -2703,6 +2731,15 @@ func testAccCheckAWSDBInstanceNoSnapshot(s *terraform.State) error {
return nil
}

func testAccCheckAWSDBInstanceNotRecreated(instance1, instance2 *rds.DBInstance) resource.TestCheckFunc {
return func(s *terraform.State) error {
if !aws.TimeValue(instance1.InstanceCreateTime).Equal(aws.TimeValue(instance2.InstanceCreateTime)) {
return fmt.Errorf("database instance was recreated. expected: %s, got: %s", instance1.InstanceCreateTime, instance2.InstanceCreateTime)
}
return nil
}
}

func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -6072,6 +6109,35 @@ resource "aws_db_instance" "test" {
`, rName))
}

func testAccAWSDBInstanceConfig_SnapshotIdentifierRemoved(rName string) string {
return composeConfig(testAccAWSDBInstanceConfig_orderableClassMariadb(), fmt.Sprintf(`
resource "aws_db_instance" "source" {
allocated_storage = 5
engine = data.aws_rds_orderable_db_instance.test.engine
identifier = "%[1]s-source"
instance_class = data.aws_rds_orderable_db_instance.test.instance_class
password = "avoid-plaintext-passwords"
username = "tfacctest"
skip_final_snapshot = true
}
resource "aws_db_snapshot" "test" {
db_instance_identifier = aws_db_instance.source.id
db_snapshot_identifier = %[1]q
}
resource "aws_db_instance" "test" {
allocated_storage = 5
engine = data.aws_rds_orderable_db_instance.test.engine
password = "avoid-plaintext-passwords"
username = "tfacctest"
identifier = %[1]q
instance_class = aws_db_instance.source.instance_class
skip_final_snapshot = true
}
`, rName))
}

func testAccAWSDBInstanceConfig_SnapshotIdentifier_AllocatedStorage(rName string, allocatedStorage int) string {
return composeConfig(testAccAWSDBInstanceConfig_orderableClassMariadb(), fmt.Sprintf(`
resource "aws_db_instance" "source" {
Expand Down

0 comments on commit dce223f

Please sign in to comment.