Skip to content

Commit

Permalink
Merge pull request #9106 from kterada0509/feature/add-check-destroy-f…
Browse files Browse the repository at this point in the history
…or-aws_ebs_snapshot_copy-resource

Add check destroy for aws ebs snapshot copy resource & Fix error handling
  • Loading branch information
bflad authored Jun 25, 2019
2 parents 918ada5 + 874e12f commit 941a12c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
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

0 comments on commit 941a12c

Please sign in to comment.