github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_snapshot_create_volume_permission_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 func TestAccAWSSnapshotCreateVolumePermission_Basic(t *testing.T) { 12 var snapshotId, accountId string 13 14 resource.Test(t, resource.TestCase{ 15 Providers: testAccProviders, 16 Steps: []resource.TestStep{ 17 // Scaffold everything 18 resource.TestStep{ 19 Config: testAccAWSSnapshotCreateVolumePermissionConfig(true), 20 Check: resource.ComposeTestCheckFunc( 21 testCheckResourceGetAttr("aws_ebs_snapshot.example_snapshot", "id", &snapshotId), 22 testCheckResourceGetAttr("data.aws_caller_identity.current", "account_id", &accountId), 23 testAccAWSSnapshotCreateVolumePermissionExists(&accountId, &snapshotId), 24 ), 25 }, 26 // Drop just create volume permission to test destruction 27 resource.TestStep{ 28 Config: testAccAWSSnapshotCreateVolumePermissionConfig(false), 29 Check: resource.ComposeTestCheckFunc( 30 testAccAWSSnapshotCreateVolumePermissionDestroyed(&accountId, &snapshotId), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func testAccAWSSnapshotCreateVolumePermissionExists(accountId, snapshotId *string) resource.TestCheckFunc { 38 return func(s *terraform.State) error { 39 conn := testAccProvider.Meta().(*AWSClient).ec2conn 40 if has, err := hasCreateVolumePermission(conn, *snapshotId, *accountId); err != nil { 41 return err 42 } else if !has { 43 return fmt.Errorf("create volume permission does not exist for '%s' on '%s'", *accountId, *snapshotId) 44 } 45 return nil 46 } 47 } 48 49 func testAccAWSSnapshotCreateVolumePermissionDestroyed(accountId, snapshotId *string) resource.TestCheckFunc { 50 return func(s *terraform.State) error { 51 conn := testAccProvider.Meta().(*AWSClient).ec2conn 52 if has, err := hasCreateVolumePermission(conn, *snapshotId, *accountId); err != nil { 53 return err 54 } else if has { 55 return fmt.Errorf("create volume permission still exists for '%s' on '%s'", *accountId, *snapshotId) 56 } 57 return nil 58 } 59 } 60 61 func testAccAWSSnapshotCreateVolumePermissionConfig(includeCreateVolumePermission bool) string { 62 base := ` 63 data "aws_caller_identity" "current" {} 64 65 resource "aws_ebs_volume" "example" { 66 availability_zone = "us-west-2a" 67 size = 40 68 69 tags { 70 Name = "ebs_snap_perm" 71 } 72 } 73 74 resource "aws_ebs_snapshot" "example_snapshot" { 75 volume_id = "${aws_ebs_volume.example.id}" 76 } 77 ` 78 79 if !includeCreateVolumePermission { 80 return base 81 } 82 83 return base + fmt.Sprintf(` 84 resource "aws_snapshot_create_volume_permission" "self-test" { 85 snapshot_id = "${aws_ebs_snapshot.example_snapshot.id}" 86 account_id = "${data.aws_caller_identity.current.account_id}" 87 } 88 `) 89 }