Skip to content

Commit

Permalink
Merge pull request #12978 from DrFaust92/r/db_snap_import
Browse files Browse the repository at this point in the history
r/db_snapshot: import support
  • Loading branch information
anGie44 committed Apr 24, 2020
2 parents 9f9d72f + 342dff2 commit 84664a1
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 93 deletions.
16 changes: 11 additions & 5 deletions aws/resource_aws_db_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func resourceAwsDbSnapshot() *schema.Resource {
Read: resourceAwsDbSnapshotRead,
Update: resourceAwsDbSnapshotUpdate,
Delete: resourceAwsDbSnapshotDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(20 * time.Minute),
Expand Down Expand Up @@ -119,11 +122,11 @@ func resourceAwsDbSnapshotCreate(d *schema.ResourceData, meta interface{}) error
Tags: tags,
}

_, err := conn.CreateDBSnapshot(params)
resp, err := conn.CreateDBSnapshot(params)
if err != nil {
return fmt.Errorf("Error creating AWS DB Snapshot %s: %s", dBInstanceIdentifier, err)
}
d.SetId(d.Get("db_snapshot_identifier").(string))
d.SetId(aws.StringValue(resp.DBSnapshot.DBSnapshotIdentifier))

stateConf := &resource.StateChangeConf{
Pending: []string{"creating"},
Expand Down Expand Up @@ -163,9 +166,12 @@ func resourceAwsDbSnapshotRead(d *schema.ResourceData, meta interface{}) error {

snapshot := resp.DBSnapshots[0]

arn := aws.StringValue(snapshot.DBSnapshotArn)
d.Set("db_snapshot_identifier", snapshot.DBSnapshotIdentifier)
d.Set("db_instance_identifier", snapshot.DBInstanceIdentifier)
d.Set("allocated_storage", snapshot.AllocatedStorage)
d.Set("availability_zone", snapshot.AvailabilityZone)
d.Set("db_snapshot_arn", snapshot.DBSnapshotArn)
d.Set("db_snapshot_arn", arn)
d.Set("encrypted", snapshot.Encrypted)
d.Set("engine", snapshot.Engine)
d.Set("engine_version", snapshot.EngineVersion)
Expand All @@ -180,10 +186,10 @@ func resourceAwsDbSnapshotRead(d *schema.ResourceData, meta interface{}) error {
d.Set("status", snapshot.Status)
d.Set("vpc_id", snapshot.VpcId)

tags, err := keyvaluetags.RdsListTags(conn, d.Get("db_snapshot_arn").(string))
tags, err := keyvaluetags.RdsListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for RDS DB Snapshot (%s): %s", d.Get("db_snapshot_arn").(string), err)
return fmt.Errorf("error listing tags for RDS DB Snapshot (%s): %s", arn, err)
}

if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
Expand Down
165 changes: 77 additions & 88 deletions aws/resource_aws_db_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -76,45 +77,69 @@ func testSweepDbSnapshots(region string) error {

func TestAccAWSDBSnapshot_basic(t *testing.T) {
var v rds.DBSnapshot
rInt := acctest.RandInt()
resourceName := "aws_db_snapshot.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDbSnapshotDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsDbSnapshotConfig(rInt),
Config: testAccAwsDbSnapshotConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDbSnapshotExists("aws_db_snapshot.test", &v),
testAccCheckDbSnapshotExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
testAccMatchResourceAttrRegionalARN(resourceName, "db_snapshot_arn", "rds", regexp.MustCompile(`snapshot:.+`)),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSDBSnapshot_tags(t *testing.T) {
var v rds.DBSnapshot
rInt := acctest.RandInt()
resourceName := "aws_db_snapshot.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDbSnapshotDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsDbSnapshotConfigTags1(rInt, "key1", "value1"),
Config: testAccAwsDbSnapshotConfigTags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDbSnapshotExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAwsDbSnapshotConfigTags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDbSnapshotExists("aws_db_snapshot.test", &v),
resource.TestCheckResourceAttr("aws_db_snapshot.test", "tags.%", "1"),
resource.TestCheckResourceAttr("aws_db_snapshot.test", "tags.key1", "value1"),
testAccCheckDbSnapshotExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAwsDbSnapshotConfigTags2(rInt, "key1", "value1updated", "key2", "value2"),
Config: testAccAwsDbSnapshotConfigTags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDbSnapshotExists("aws_db_snapshot.test", &v),
resource.TestCheckResourceAttr("aws_db_snapshot.test", "tags.%", "2"),
resource.TestCheckResourceAttr("aws_db_snapshot.test", "tags.key1", "value1updated"),
resource.TestCheckResourceAttr("aws_db_snapshot.test", "tags.key2", "value2"),
testAccCheckDbSnapshotExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
Expand All @@ -123,7 +148,7 @@ func TestAccAWSDBSnapshot_tags(t *testing.T) {

func TestAccAWSDBSnapshot_disappears(t *testing.T) {
var v rds.DBSnapshot
rInt := acctest.RandInt()
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_db_snapshot.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -132,7 +157,7 @@ func TestAccAWSDBSnapshot_disappears(t *testing.T) {
CheckDestroy: testAccCheckDbSnapshotDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsDbSnapshotConfig(rInt),
Config: testAccAwsDbSnapshotConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDbSnapshotExists(resourceName, &v),
testAccCheckDbSnapshotDisappears(&v),
Expand Down Expand Up @@ -217,92 +242,56 @@ func testAccCheckDbSnapshotDisappears(snapshot *rds.DBSnapshot) resource.TestChe
}
}

func testAccAwsDbSnapshotConfig(rInt int) string {
func testAccAwsDbSnapshotConfigBase(rName string) string {
return fmt.Sprintf(`
resource "aws_db_instance" "bar" {
allocated_storage = 10
engine = "MySQL"
engine_version = "5.6.35"
instance_class = "db.t2.micro"
name = "baz"
password = "barbarbarbar"
username = "foo"
maintenance_window = "Fri:09:00-Fri:09:30"
resource "aws_db_instance" "test" {
allocated_storage = 10
engine = "MySQL"
engine_version = "5.6.35"
instance_class = "db.t2.micro"
name = "baz"
identifier = %[1]q
password = "barbarbarbar"
username = "foo"
maintenance_window = "Fri:09:00-Fri:09:30"
backup_retention_period = 0
parameter_group_name = "default.mysql5.6"
skip_final_snapshot = true
parameter_group_name = "default.mysql5.6"
skip_final_snapshot = true
}`, rName)
}

func testAccAwsDbSnapshotConfig(rName string) string {
return testAccAwsDbSnapshotConfigBase(rName) + fmt.Sprintf(`
resource "aws_db_snapshot" "test" {
db_instance_identifier = "${aws_db_instance.bar.id}"
db_snapshot_identifier = "testsnapshot%d"
}
`, rInt)
db_instance_identifier = "${aws_db_instance.test.id}"
db_snapshot_identifier = %[1]q
}

func testAccAwsDbSnapshotConfigTags1(rInt int, tag1Key, tag1Value string) string {
return fmt.Sprintf(`
resource "aws_db_instance" "bar" {
allocated_storage = 10
engine = "MySQL"
engine_version = "5.6.35"
instance_class = "db.t2.micro"
name = "baz"
password = "barbarbarbar"
username = "foo"
maintenance_window = "Fri:09:00-Fri:09:30"
backup_retention_period = 0
parameter_group_name = "default.mysql5.6"
skip_final_snapshot = true
`, rName)
}

func testAccAwsDbSnapshotConfigTags1(rName, tag1Key, tag1Value string) string {
return testAccAwsDbSnapshotConfigBase(rName) + fmt.Sprintf(`
resource "aws_db_snapshot" "test" {
db_instance_identifier = "${aws_db_instance.bar.id}"
db_snapshot_identifier = "testsnapshot%d"
db_instance_identifier = "${aws_db_instance.test.id}"
db_snapshot_identifier = %[1]q
tags = {
%q = %q
}
}
`, rInt, tag1Key, tag1Value)
tags = {
%[2]q = %[3]q
}
}

func testAccAwsDbSnapshotConfigTags2(rInt int, tag1Key, tag1Value, tag2Key, tag2Value string) string {
return fmt.Sprintf(`
resource "aws_db_instance" "bar" {
allocated_storage = 10
engine = "MySQL"
engine_version = "5.6.35"
instance_class = "db.t2.micro"
name = "baz"
password = "barbarbarbar"
username = "foo"
maintenance_window = "Fri:09:00-Fri:09:30"
backup_retention_period = 0
parameter_group_name = "default.mysql5.6"
skip_final_snapshot = true
`, rName, tag1Key, tag1Value)
}

func testAccAwsDbSnapshotConfigTags2(rName, tag1Key, tag1Value, tag2Key, tag2Value string) string {
return testAccAwsDbSnapshotConfigBase(rName) + fmt.Sprintf(`
resource "aws_db_snapshot" "test" {
db_instance_identifier = "${aws_db_instance.bar.id}"
db_snapshot_identifier = "testsnapshot%d"
db_instance_identifier = "${aws_db_instance.test.id}"
db_snapshot_identifier = %[1]q
tags = {
%q = %q
%q = %q
}
}
`, rInt, tag1Key, tag1Value, tag2Key, tag2Value)
tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName, tag1Key, tag1Value, tag2Key, tag2Value)
}
8 changes: 8 additions & 0 deletions website/docs/r/db_snapshot.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ In addition to all arguments above, the following attributes are exported:
* `status` - Specifies the status of this DB snapshot.
* `storage_type` - Specifies the storage type associated with DB snapshot.
* `vpc_id` - Specifies the storage type associated with DB snapshot.

## Import

`aws_db_snapshot` can be imported by using the snapshot identifier, e.g.

```
$ terraform import aws_db_snapshot.example my-snapshot
```

0 comments on commit 84664a1

Please sign in to comment.