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

Add check destroy for aws ebs snapshot copy resource & Fix error handling #9106

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
10 changes: 4 additions & 6 deletions aws/resource_aws_ebs_snapshot_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -116,7 +115,7 @@ func resourceAwsEbsSnapshotCopyRead(d *schema.ResourceData, meta interface{}) er
SnapshotIds: []*string{aws.String(d.Id())},
}
res, err := conn.DescribeSnapshots(req)
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidSnapshotID.NotFound" {
if isAWSErr(err, "InvalidSnapshot.NotFound", "") {
log.Printf("Snapshot %q Not found - removing from state", d.Id())
d.SetId("")
return nil
Expand Down Expand Up @@ -152,13 +151,12 @@ func resourceAwsEbsSnapshotCopyDelete(d *schema.ResourceData, meta interface{})
return nil
}

ebsErr, ok := err.(awserr.Error)
if ebsErr.Code() == "SnapshotInUse" {
if isAWSErr(err, "SnapshotInUse", "") {
return resource.RetryableError(fmt.Errorf("EBS SnapshotInUse - trying again while it detaches"))
}

if !ok {
return resource.NonRetryableError(err)
if isAWSErr(err, "InvalidSnapshot.NotFound", "") {
return nil
}

return resource.NonRetryableError(err)
Expand Down
77 changes: 71 additions & 6 deletions aws/resource_aws_ebs_snapshot_copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
func TestAccAWSEbsSnapshotCopy_basic(t *testing.T) {
var v ec2.Snapshot
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckEbsSnapshotCopyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsEbsSnapshotCopyConfig,
Expand All @@ -32,8 +33,9 @@ func TestAccAWSEbsSnapshotCopy_basic(t *testing.T) {
func TestAccAWSEbsSnapshotCopy_withDescription(t *testing.T) {
var v ec2.Snapshot
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckEbsSnapshotCopyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsEbsSnapshotCopyConfigWithDescription,
Expand Down Expand Up @@ -63,6 +65,7 @@ func TestAccAWSEbsSnapshotCopy_withRegions(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckEbsSnapshotCopyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsEbsSnapshotCopyConfigWithRegions,
Expand All @@ -78,8 +81,9 @@ func TestAccAWSEbsSnapshotCopy_withRegions(t *testing.T) {
func TestAccAWSEbsSnapshotCopy_withKms(t *testing.T) {
var v ec2.Snapshot
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckEbsSnapshotCopyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsEbsSnapshotCopyConfigWithKms,
Expand All @@ -93,6 +97,67 @@ func TestAccAWSEbsSnapshotCopy_withKms(t *testing.T) {
})
}

func TestAccAWSEbsSnapshotCopy_disappears(t *testing.T) {
var v ec2.Snapshot
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckEbsSnapshotCopyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsEbsSnapshotCopyConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckEbsSnapshotCopyExists("aws_ebs_snapshot_copy.test", &v),
testAccCheckEbsSnapshotCopyDisappears(&v),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckEbsSnapshotCopyDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_ebs_snapshot_copy" {
continue
}

resp, err := conn.DescribeSnapshots(&ec2.DescribeSnapshotsInput{
SnapshotIds: []*string{aws.String(rs.Primary.ID)},
})

if isAWSErr(err, "InvalidSnapshot.NotFound", "") {
continue
}

if err == nil {
for _, snapshot := range resp.Snapshots {
if aws.StringValue(snapshot.SnapshotId) == rs.Primary.ID {
return fmt.Errorf("EBS Snapshot still exists")
}
}
}

return err
}

return nil
}

func testAccCheckEbsSnapshotCopyDisappears(snapshot *ec2.Snapshot) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

_, err := conn.DeleteSnapshot(&ec2.DeleteSnapshotInput{
SnapshotId: snapshot.SnapshotId,
})

return err
}
}

func testAccCheckEbsSnapshotCopyExists(n string, v *ec2.Snapshot) resource.TestCheckFunc {
providers := []*schema.Provider{testAccProvider}
return testAccCheckEbsSnapshotCopyExistsWithProviders(n, v, &providers)
Expand Down