refactor the test to use caller_identity data source, and new ebs_snapshot resource
This commit is contained in:
parent
185ee439da
commit
42057045ff
|
@ -2,7 +2,6 @@ package aws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
|
@ -10,68 +9,70 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAccAWSSnapshotCreateVolumePermission_Basic(t *testing.T) {
|
func TestAccAWSSnapshotCreateVolumePermission_Basic(t *testing.T) {
|
||||||
snapshot_id := ""
|
var snapshotId, accountId string
|
||||||
account_id := os.Getenv("AWS_ACCOUNT_ID")
|
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
PreCheck: func() {
|
|
||||||
testAccPreCheck(t)
|
|
||||||
if os.Getenv("AWS_ACCOUNT_ID") == "" {
|
|
||||||
t.Fatal("AWS_ACCOUNT_ID must be set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Providers: testAccProviders,
|
Providers: testAccProviders,
|
||||||
Steps: []resource.TestStep{
|
Steps: []resource.TestStep{
|
||||||
// Scaffold everything
|
// Scaffold everything
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
Config: testAccAWSSnapshotCreateVolumePermissionConfig(account_id, true),
|
Config: testAccAWSSnapshotCreateVolumePermissionConfig(true),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testCheckResourceGetAttr("aws_ami_copy.test", "block_device_mappings.0.ebs.snapshot_id", &snapshot_id),
|
testCheckResourceGetAttr("aws_ebs_snapshot.example_snapshot", "id", &snapshotId),
|
||||||
testAccAWSSnapshotCreateVolumePermissionExists(account_id, &snapshot_id),
|
testCheckResourceGetAttr("data.aws_caller_identity.current", "account_id", &accountId),
|
||||||
|
testAccAWSSnapshotCreateVolumePermissionExists(&accountId, &snapshotId),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
// Drop just create volume permission to test destruction
|
// Drop just create volume permission to test destruction
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
Config: testAccAWSSnapshotCreateVolumePermissionConfig(account_id, false),
|
Config: testAccAWSSnapshotCreateVolumePermissionConfig(false),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccAWSSnapshotCreateVolumePermissionDestroyed(account_id, &snapshot_id),
|
testAccAWSSnapshotCreateVolumePermissionDestroyed(&accountId, &snapshotId),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAccAWSSnapshotCreateVolumePermissionExists(account_id string, snapshot_id *string) resource.TestCheckFunc {
|
func testAccAWSSnapshotCreateVolumePermissionExists(accountId, snapshotId *string) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
||||||
if has, err := hasCreateVolumePermission(conn, *snapshot_id, account_id); err != nil {
|
if has, err := hasCreateVolumePermission(conn, *snapshotId, *accountId); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if !has {
|
} else if !has {
|
||||||
return fmt.Errorf("create volume permission does not exist for '%s' on '%s'", account_id, *snapshot_id)
|
return fmt.Errorf("create volume permission does not exist for '%s' on '%s'", *accountId, *snapshotId)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAccAWSSnapshotCreateVolumePermissionDestroyed(account_id string, snapshot_id *string) resource.TestCheckFunc {
|
func testAccAWSSnapshotCreateVolumePermissionDestroyed(accountId, snapshotId *string) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
||||||
if has, err := hasCreateVolumePermission(conn, *snapshot_id, account_id); err != nil {
|
if has, err := hasCreateVolumePermission(conn, *snapshotId, *accountId); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if has {
|
} else if has {
|
||||||
return fmt.Errorf("create volume permission still exists for '%s' on '%s'", account_id, *snapshot_id)
|
return fmt.Errorf("create volume permission still exists for '%s' on '%s'", *accountId, *snapshotId)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAccAWSSnapshotCreateVolumePermissionConfig(account_id string, includeCreateVolumePermission bool) string {
|
func testAccAWSSnapshotCreateVolumePermissionConfig(includeCreateVolumePermission bool) string {
|
||||||
base := `
|
base := `
|
||||||
resource "aws_ami_copy" "test" {
|
data "aws_caller_identity" "current" {}
|
||||||
name = "create-volume-permission-test"
|
|
||||||
description = "Create Volume Permission Test Copy"
|
resource "aws_ebs_volume" "example" {
|
||||||
source_ami_id = "ami-7172b611"
|
availability_zone = "us-west-2a"
|
||||||
source_ami_region = "us-west-2"
|
size = 40
|
||||||
|
|
||||||
|
tags {
|
||||||
|
Name = "ebs_snap_perm"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_ebs_snapshot" "example_snapshot" {
|
||||||
|
volume_id = "${aws_ebs_volume.example.id}"
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
@ -81,8 +82,8 @@ resource "aws_ami_copy" "test" {
|
||||||
|
|
||||||
return base + fmt.Sprintf(`
|
return base + fmt.Sprintf(`
|
||||||
resource "aws_snapshot_create_volume_permission" "self-test" {
|
resource "aws_snapshot_create_volume_permission" "self-test" {
|
||||||
snapshot_id = "${lookup(lookup(element(aws_ami_copy.test.block_device_mappings, 0), "ebs"), "snapshot_id")}"
|
snapshot_id = "${aws_ebs_snapshot.example_snapshot.id}"
|
||||||
account_id = "%s"
|
account_id = "${data.aws_caller_identity.current.account_id}"
|
||||||
}
|
}
|
||||||
`, account_id)
|
`)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue