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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

rd/ebs_snapshot resources - add arn attribute #13840

Merged
merged 4 commits into from Jun 22, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions aws/data_source_aws_ebs_snapshot.go
Expand Up @@ -6,6 +6,7 @@ import (
"sort"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
Expand All @@ -16,6 +17,10 @@ func dataSourceAwsEbsSnapshot() *schema.Resource {
Read: dataSourceAwsEbsSnapshotRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
//selection criteria
"filter": dataSourceFiltersSchema(),
"most_recent": {
Expand Down Expand Up @@ -86,7 +91,6 @@ func dataSourceAwsEbsSnapshot() *schema.Resource {

func dataSourceAwsEbsSnapshotRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

restorableUsers, restorableUsersOk := d.GetOk("restorable_by_user_ids")
filters, filtersOk := d.GetOk("filter")
Expand Down Expand Up @@ -132,11 +136,13 @@ func dataSourceAwsEbsSnapshotRead(d *schema.ResourceData, meta interface{}) erro
}

//Single Snapshot found so set to state
return snapshotDescriptionAttributes(d, resp.Snapshots[0], ignoreTagsConfig)
return snapshotDescriptionAttributes(d, resp.Snapshots[0], meta)
}

func snapshotDescriptionAttributes(d *schema.ResourceData, snapshot *ec2.Snapshot, ignoreTagsConfig *keyvaluetags.IgnoreConfig) error {
d.SetId(*snapshot.SnapshotId)
func snapshotDescriptionAttributes(d *schema.ResourceData, snapshot *ec2.Snapshot, meta interface{}) error {
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

d.SetId(aws.StringValue(snapshot.SnapshotId))
d.Set("snapshot_id", snapshot.SnapshotId)
d.Set("volume_id", snapshot.VolumeId)
d.Set("data_encryption_key_id", snapshot.DataEncryptionKeyId)
Expand All @@ -152,5 +158,14 @@ func snapshotDescriptionAttributes(d *schema.ResourceData, snapshot *ec2.Snapsho
return fmt.Errorf("error setting tags: %s", err)
}

snapshotArn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Resource: fmt.Sprintf("snapshot/%s", d.Id()),
Service: "ec2",
}.String()

d.Set("arn", snapshotArn)

return nil
}
1 change: 1 addition & 0 deletions aws/data_source_aws_ebs_snapshot_test.go
Expand Up @@ -21,6 +21,7 @@ func TestAccAWSEbsSnapshotDataSource_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsEbsSnapshotDataSourceID(dataSourceName),
resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrPair(dataSourceName, "encrypted", resourceName, "encrypted"),
resource.TestCheckResourceAttrPair(dataSourceName, "kms_key_id", resourceName, "kms_key_id"),
Expand Down
17 changes: 16 additions & 1 deletion aws/resource_aws_ebs_snapshot.go
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand All @@ -25,6 +26,10 @@ func resourceAwsEbsSnapshot() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"volume_id": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -97,7 +102,7 @@ func resourceAwsEbsSnapshotCreate(d *schema.ResourceData, meta interface{}) erro
return fmt.Errorf("error creating EC2 EBS Snapshot: %s", err)
}

d.SetId(*res.SnapshotId)
d.SetId(aws.StringValue(res.SnapshotId))

err = resourceAwsEbsSnapshotWaitForAvailable(d, conn)
if err != nil {
Expand Down Expand Up @@ -125,6 +130,7 @@ func resourceAwsEbsSnapshotRead(d *schema.ResourceData, meta interface{}) error
}

if len(res.Snapshots) == 0 {
log.Printf("[WARN] Snapshot %q Not found - removing from state", d.Id())
d.SetId("")
return nil
}
Expand All @@ -144,6 +150,15 @@ func resourceAwsEbsSnapshotRead(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("error setting tags: %s", err)
}

snapshotArn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Resource: fmt.Sprintf("snapshot/%s", d.Id()),
Service: "ec2",
}.String()

d.Set("arn", snapshotArn)

return nil
}

Expand Down
16 changes: 15 additions & 1 deletion aws/resource_aws_ebs_snapshot_copy.go
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand All @@ -20,6 +21,10 @@ func resourceAwsEbsSnapshotCopy() *schema.Resource {
Delete: resourceAwsEbsSnapshotCopyDelete,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"volume_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -93,7 +98,7 @@ func resourceAwsEbsSnapshotCopyCreate(d *schema.ResourceData, meta interface{})
return err
}

d.SetId(*res.SnapshotId)
d.SetId(aws.StringValue(res.SnapshotId))

err = resourceAwsEbsSnapshotCopyWaitForAvailable(d.Id(), conn)
if err != nil {
Expand Down Expand Up @@ -136,6 +141,15 @@ func resourceAwsEbsSnapshotCopyRead(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf("error setting tags: %s", err)
}

snapshotArn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Resource: fmt.Sprintf("snapshot/%s", d.Id()),
Service: "ec2",
}.String()

d.Set("arn", snapshotArn)

return nil
}

Expand Down
23 changes: 4 additions & 19 deletions aws/resource_aws_ebs_snapshot_copy_test.go
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -24,8 +25,8 @@ func TestAccAWSEbsSnapshotCopy_basic(t *testing.T) {
Config: testAccAwsEbsSnapshotCopyConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckEbsSnapshotCopyExists(resourceName, &snapshot),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", "testAccAwsEbsSnapshotCopyConfig"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
testAccMatchResourceAttrRegionalARNNoAccount(resourceName, "arn", "ec2", regexp.MustCompile(`snapshot/snap-.+`)),
),
},
},
Expand Down Expand Up @@ -149,7 +150,7 @@ func TestAccAWSEbsSnapshotCopy_disappears(t *testing.T) {
Config: testAccAwsEbsSnapshotCopyConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckEbsSnapshotCopyExists(resourceName, &snapshot),
testAccCheckEbsSnapshotCopyDisappears(&snapshot),
testAccCheckResourceDisappears(testAccProvider, resourceAwsEbsSnapshotCopy(), resourceName),
),
ExpectNonEmptyPlan: true,
},
Expand Down Expand Up @@ -187,18 +188,6 @@ func testAccCheckEbsSnapshotCopyDestroy(s *terraform.State) error {
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 {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -260,10 +249,6 @@ resource "aws_ebs_snapshot" "test" {
resource "aws_ebs_snapshot_copy" "test" {
source_snapshot_id = "${aws_ebs_snapshot.test.id}"
source_region = "${data.aws_region.current.name}"

tags = {
Name = "testAccAwsEbsSnapshotCopyConfig"
}
}
`

Expand Down
66 changes: 28 additions & 38 deletions aws/resource_aws_ebs_snapshot_test.go
Expand Up @@ -17,29 +17,6 @@ func TestAccAWSEBSSnapshot_basic(t *testing.T) {
rName := fmt.Sprintf("tf-acc-ebs-snapshot-basic-%s", acctest.RandString(7))
resourceName := "aws_ebs_snapshot.test"

deleteSnapshot := func() {
conn := testAccProvider.Meta().(*AWSClient).ec2conn
resp, err := conn.DescribeSnapshots(&ec2.DescribeSnapshotsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:Name"),
Values: []*string{aws.String(rName)},
},
},
})
if err != nil {
t.Fatalf("Error fetching snapshot: %s", err)
}
if len(resp.Snapshots) == 0 {
t.Fatalf("No snapshot exists with tag:Name = %s", rName)
}
snapshotId := resp.Snapshots[0].SnapshotId
_, err = conn.DeleteSnapshot(&ec2.DeleteSnapshotInput{SnapshotId: snapshotId})
if err != nil {
t.Fatalf("Error deleting snapshot %s with tag:Name = %s: %s", *snapshotId, rName, err)
}
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -49,17 +26,8 @@ func TestAccAWSEBSSnapshot_basic(t *testing.T) {
Config: testAccAwsEbsSnapshotConfigBasic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckSnapshotExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", rName),
),
},
{
PreConfig: deleteSnapshot,
Config: testAccAwsEbsSnapshotConfigBasic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckSnapshotExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", rName),
testAccMatchResourceAttrRegionalARNNoAccount(resourceName, "arn", "ec2", regexp.MustCompile(`snapshot/snap-.+`)),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
},
Expand Down Expand Up @@ -148,6 +116,28 @@ func TestAccAWSEBSSnapshot_withKms(t *testing.T) {
})
}

func TestAccAWSEBSSnapshot_disappears(t *testing.T) {
var v ec2.Snapshot
rName := fmt.Sprintf("tf-acc-ebs-snapshot-basic-%s", acctest.RandString(7))
resourceName := "aws_ebs_snapshot.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEbsSnapshotDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsEbsSnapshotConfigBasic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckSnapshotExists(resourceName, &v),
testAccCheckResourceDisappears(testAccProvider, resourceAwsEbsSnapshot(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckSnapshotExists(n string, v *ec2.Snapshot) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -209,14 +199,14 @@ data "aws_region" "current" {}
resource "aws_ebs_volume" "test" {
availability_zone = "${data.aws_region.current.name}a"
size = 1
}

resource "aws_ebs_snapshot" "test" {
volume_id = "${aws_ebs_volume.test.id}"

tags = {
Name = "%s"
}
}

resource "aws_ebs_snapshot" "test" {
volume_id = "${aws_ebs_volume.test.id}"

timeouts {
create = "10m"
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/ebs_snapshot.html.markdown
Expand Up @@ -50,6 +50,7 @@ several valid keys, for a full reference, check out

In addition to all arguments above, the following attributes are exported:

* `arn` - Amazon Resource Name (ARN) of the EBS Snapshot.
* `id` - The snapshot ID (e.g. snap-59fcb34e).
* `snapshot_id` - The snapshot ID (e.g. snap-59fcb34e).
* `description` - A description for the snapshot
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/ebs_snapshot.html.markdown
Expand Up @@ -51,6 +51,7 @@ The following arguments are supported:

In addition to all arguments above, the following attributes are exported:

* `arn` - Amazon Resource Name (ARN) of the EBS Snapshot.
* `id` - The snapshot ID (e.g. snap-59fcb34e).
* `owner_id` - The AWS account ID of the EBS snapshot owner.
* `owner_alias` - Value from an Amazon-maintained list (`amazon`, `aws-marketplace`, `microsoft`) of snapshot owners.
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/ebs_snapshot_copy.html.markdown
Expand Up @@ -55,6 +55,7 @@ The following arguments are supported:

The following attributes are exported:

* `arn` - Amazon Resource Name (ARN) of the EBS Snapshot.
* `id` - The snapshot ID (e.g. snap-59fcb34e).
* `owner_id` - The AWS account ID of the snapshot owner.
* `owner_alias` - Value from an Amazon-maintained list (`amazon`, `aws-marketplace`, `microsoft`) of snapshot owners.
Expand Down