Skip to content

Commit

Permalink
r/aws_snapshot_create_volume_permission: Remove extraneous Exists fun…
Browse files Browse the repository at this point in the history
…ction.
  • Loading branch information
ewbankkit authored and appilon committed Apr 29, 2020
1 parent 3d32bba commit 8c769c5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
29 changes: 18 additions & 11 deletions aws/resource_aws_snapshot_create_volume_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"log"
"strings"
"time"

Expand All @@ -13,7 +14,6 @@ import (

func resourceAwsSnapshotCreateVolumePermission() *schema.Resource {
return &schema.Resource{
Exists: resourceAwsSnapshotCreateVolumePermissionExists,
Create: resourceAwsSnapshotCreateVolumePermissionCreate,
Read: resourceAwsSnapshotCreateVolumePermissionRead,
Delete: resourceAwsSnapshotCreateVolumePermissionDelete,
Expand All @@ -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

Expand Down Expand Up @@ -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
}

Expand Down
35 changes: 29 additions & 6 deletions aws/resource_aws_snapshot_create_volume_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ func TestAccAWSSnapshotCreateVolumePermission_Basic(t *testing.T) {
accountId := "111122223333"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccAWSSnapshotCreateVolumePermissionDestroy,
Steps: []resource.TestStep{
// Scaffold everything
{
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),
),
},
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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}"
}
`

Expand All @@ -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)
Expand Down

0 comments on commit 8c769c5

Please sign in to comment.