diff --git a/aws/data_source_aws_ebs_snapshot.go b/aws/data_source_aws_ebs_snapshot.go index 5bd91216a679..a3245d0d4a4f 100644 --- a/aws/data_source_aws_ebs_snapshot.go +++ b/aws/data_source_aws_ebs_snapshot.go @@ -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" @@ -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": { @@ -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") @@ -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) @@ -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 } diff --git a/aws/data_source_aws_ebs_snapshot_test.go b/aws/data_source_aws_ebs_snapshot_test.go index b1bd08121b32..6c1ae372f483 100644 --- a/aws/data_source_aws_ebs_snapshot_test.go +++ b/aws/data_source_aws_ebs_snapshot_test.go @@ -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"), diff --git a/aws/resource_aws_ebs_snapshot.go b/aws/resource_aws_ebs_snapshot.go index b84b41f06b6b..3680a05810ff 100644 --- a/aws/resource_aws_ebs_snapshot.go +++ b/aws/resource_aws_ebs_snapshot.go @@ -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" @@ -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, @@ -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 { @@ -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 } @@ -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 } diff --git a/aws/resource_aws_ebs_snapshot_copy.go b/aws/resource_aws_ebs_snapshot_copy.go index 163cf18259f4..1c13d3cfb161 100644 --- a/aws/resource_aws_ebs_snapshot_copy.go +++ b/aws/resource_aws_ebs_snapshot_copy.go @@ -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" @@ -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, @@ -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 { @@ -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 } diff --git a/aws/resource_aws_ebs_snapshot_copy_test.go b/aws/resource_aws_ebs_snapshot_copy_test.go index 031271fc3c5b..dc2e137c9f40 100644 --- a/aws/resource_aws_ebs_snapshot_copy_test.go +++ b/aws/resource_aws_ebs_snapshot_copy_test.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "regexp" "testing" "github.com/aws/aws-sdk-go/aws" @@ -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-.+`)), ), }, }, @@ -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, }, @@ -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] @@ -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" - } } ` diff --git a/aws/resource_aws_ebs_snapshot_test.go b/aws/resource_aws_ebs_snapshot_test.go index d21422f2ae24..19cb8418e1be 100644 --- a/aws/resource_aws_ebs_snapshot_test.go +++ b/aws/resource_aws_ebs_snapshot_test.go @@ -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, @@ -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"), ), }, }, @@ -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] @@ -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" diff --git a/website/docs/d/ebs_snapshot.html.markdown b/website/docs/d/ebs_snapshot.html.markdown index 428297c1381d..f0a7ea1c1107 100644 --- a/website/docs/d/ebs_snapshot.html.markdown +++ b/website/docs/d/ebs_snapshot.html.markdown @@ -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 diff --git a/website/docs/r/ebs_snapshot.html.markdown b/website/docs/r/ebs_snapshot.html.markdown index 1e3f0310b1c8..938c9f4f5310 100644 --- a/website/docs/r/ebs_snapshot.html.markdown +++ b/website/docs/r/ebs_snapshot.html.markdown @@ -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. diff --git a/website/docs/r/ebs_snapshot_copy.html.markdown b/website/docs/r/ebs_snapshot_copy.html.markdown index daac4423bca7..96a53fce1d82 100644 --- a/website/docs/r/ebs_snapshot_copy.html.markdown +++ b/website/docs/r/ebs_snapshot_copy.html.markdown @@ -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.