From 8c769c5d9762b242f1fe23c77d971cb64df8fd33 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 27 Apr 2020 15:35:50 -0400 Subject: [PATCH] r/aws_snapshot_create_volume_permission: Remove extraneous Exists function. --- ...e_aws_snapshot_create_volume_permission.go | 29 +++++++++------ ..._snapshot_create_volume_permission_test.go | 35 +++++++++++++++---- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/aws/resource_aws_snapshot_create_volume_permission.go b/aws/resource_aws_snapshot_create_volume_permission.go index 6b937e1dc68..32c333b8945 100644 --- a/aws/resource_aws_snapshot_create_volume_permission.go +++ b/aws/resource_aws_snapshot_create_volume_permission.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "log" "strings" "time" @@ -13,7 +14,6 @@ import ( func resourceAwsSnapshotCreateVolumePermission() *schema.Resource { return &schema.Resource{ - Exists: resourceAwsSnapshotCreateVolumePermissionExists, Create: resourceAwsSnapshotCreateVolumePermissionCreate, Read: resourceAwsSnapshotCreateVolumePermissionRead, Delete: resourceAwsSnapshotCreateVolumePermissionDelete, @@ -33,16 +33,6 @@ func resourceAwsSnapshotCreateVolumePermission() *schema.Resource { } } -func resourceAwsSnapshotCreateVolumePermissionExists(d *schema.ResourceData, meta interface{}) (bool, error) { - conn := meta.(*AWSClient).ec2conn - - snapshotID, accountID, err := resourceAwsSnapshotCreateVolumePermissionParseID(d.Id()) - if err != nil { - return false, err - } - return hasCreateVolumePermission(conn, snapshotID, accountID) -} - func resourceAwsSnapshotCreateVolumePermissionCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn @@ -83,6 +73,23 @@ func resourceAwsSnapshotCreateVolumePermissionCreate(d *schema.ResourceData, met } func resourceAwsSnapshotCreateVolumePermissionRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + + snapshotID, accountID, err := resourceAwsSnapshotCreateVolumePermissionParseID(d.Id()) + if err != nil { + return err + } + + exists, err := hasCreateVolumePermission(conn, snapshotID, accountID) + if err != nil { + return err + } + if !exists { + log.Printf("[WARN] snapshot createVolumePermission (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + return nil } diff --git a/aws/resource_aws_snapshot_create_volume_permission_test.go b/aws/resource_aws_snapshot_create_volume_permission_test.go index dda97f48d53..a8ed74b4e98 100644 --- a/aws/resource_aws_snapshot_create_volume_permission_test.go +++ b/aws/resource_aws_snapshot_create_volume_permission_test.go @@ -14,6 +14,7 @@ func TestAccAWSSnapshotCreateVolumePermission_Basic(t *testing.T) { accountId := "111122223333" resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccAWSSnapshotCreateVolumePermissionDestroy, Steps: []resource.TestStep{ @@ -21,7 +22,7 @@ func TestAccAWSSnapshotCreateVolumePermission_Basic(t *testing.T) { { Config: testAccAWSSnapshotCreateVolumePermissionConfig(true, accountId), Check: resource.ComposeTestCheckFunc( - testCheckResourceGetAttr("aws_ebs_snapshot.example_snapshot", "id", &snapshotId), + testCheckResourceGetAttr("aws_ebs_snapshot.test", "id", &snapshotId), testAccAWSSnapshotCreateVolumePermissionExists(&accountId, &snapshotId), ), }, @@ -36,6 +37,28 @@ func TestAccAWSSnapshotCreateVolumePermission_Basic(t *testing.T) { }) } +func TestAccAWSSnapshotCreateVolumePermission_disappears(t *testing.T) { + var snapshotId string + accountId := "111122223333" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccAWSSnapshotCreateVolumePermissionDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSnapshotCreateVolumePermissionConfig(true, accountId), + Check: resource.ComposeTestCheckFunc( + testCheckResourceGetAttr("aws_ebs_snapshot.test", "id", &snapshotId), + testAccAWSSnapshotCreateVolumePermissionExists(&accountId, &snapshotId), + testAccCheckResourceDisappears(testAccProvider, resourceAwsSnapshotCreateVolumePermission(), "aws_snapshot_create_volume_permission.test"), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func testAccAWSSnapshotCreateVolumePermissionDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).ec2conn @@ -93,7 +116,7 @@ data "aws_availability_zones" "available" { } } -resource "aws_ebs_volume" "example" { +resource "aws_ebs_volume" "test" { availability_zone = "${data.aws_availability_zones.available.names[0]}" size = 1 @@ -102,8 +125,8 @@ resource "aws_ebs_volume" "example" { } } -resource "aws_ebs_snapshot" "example_snapshot" { - volume_id = "${aws_ebs_volume.example.id}" +resource "aws_ebs_snapshot" "test" { + volume_id = "${aws_ebs_volume.test.id}" } ` @@ -112,8 +135,8 @@ resource "aws_ebs_snapshot" "example_snapshot" { } return base + fmt.Sprintf(` -resource "aws_snapshot_create_volume_permission" "self-test" { - snapshot_id = "${aws_ebs_snapshot.example_snapshot.id}" +resource "aws_snapshot_create_volume_permission" "test" { + snapshot_id = "${aws_ebs_snapshot.test.id}" account_id = %q } `, accountID)