Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make snapshot_identifier computed #18013

Merged
merged 3 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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