github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/aws/resource_aws_db_snapshot_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/rds" 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSDBSnapshot_basic(t *testing.T) { 15 var v rds.DBSnapshot 16 rInt := acctest.RandInt() 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 Steps: []resource.TestStep{ 21 { 22 Config: testAccAwsDbSnapshotConfig(rInt), 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckDbSnapshotExists("aws_db_snapshot.test", &v), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func testAccCheckDbSnapshotExists(n string, v *rds.DBSnapshot) resource.TestCheckFunc { 32 return func(s *terraform.State) error { 33 rs, ok := s.RootModule().Resources[n] 34 if !ok { 35 return fmt.Errorf("Not found: %s", n) 36 } 37 38 if rs.Primary.ID == "" { 39 return fmt.Errorf("No ID is set") 40 } 41 42 conn := testAccProvider.Meta().(*AWSClient).rdsconn 43 44 request := &rds.DescribeDBSnapshotsInput{ 45 DBSnapshotIdentifier: aws.String(rs.Primary.ID), 46 } 47 48 response, err := conn.DescribeDBSnapshots(request) 49 if err == nil { 50 if response.DBSnapshots != nil && len(response.DBSnapshots) > 0 { 51 *v = *response.DBSnapshots[0] 52 return nil 53 } 54 } 55 return fmt.Errorf("Error finding RDS DB Snapshot %s", rs.Primary.ID) 56 } 57 } 58 59 func testAccAwsDbSnapshotConfig(rInt int) string { 60 return fmt.Sprintf(` 61 resource "aws_db_instance" "bar" { 62 allocated_storage = 10 63 engine = "MySQL" 64 engine_version = "5.6.21" 65 instance_class = "db.t1.micro" 66 name = "baz" 67 password = "barbarbarbar" 68 username = "foo" 69 70 maintenance_window = "Fri:09:00-Fri:09:30" 71 72 backup_retention_period = 0 73 74 parameter_group_name = "default.mysql5.6" 75 76 skip_final_snapshot = true 77 } 78 79 resource "aws_db_snapshot" "test" { 80 db_instance_identifier = "${aws_db_instance.bar.id}" 81 db_snapshot_identifier = "testsnapshot%d" 82 }`, rInt) 83 }