From e2e8042688b2f21a3179db3c1450b8cfd6ffab6c Mon Sep 17 00:00:00 2001 From: Jason Wieringa Date: Fri, 19 Jan 2018 18:04:46 -0800 Subject: [PATCH 1/5] Added resource_aws_copy_snapshot --- aws/provider.go | 1 + aws/resource_aws_copy_snapshot.go | 176 +++++++++++++++++++++++++ aws/resource_aws_copy_snapshot_test.go | 127 ++++++++++++++++++ 3 files changed, 304 insertions(+) create mode 100644 aws/resource_aws_copy_snapshot.go create mode 100644 aws/resource_aws_copy_snapshot_test.go diff --git a/aws/provider.go b/aws/provider.go index fc8848da8f9..69bdc0e51ed 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -389,6 +389,7 @@ func Provider() terraform.ResourceProvider { "aws_dynamodb_table_item": resourceAwsDynamoDbTableItem(), "aws_dynamodb_global_table": resourceAwsDynamoDbGlobalTable(), "aws_ebs_snapshot": resourceAwsEbsSnapshot(), + "aws_copy_snapshot": resourceAwsCopySnapshot(), "aws_ebs_volume": resourceAwsEbsVolume(), "aws_ecr_lifecycle_policy": resourceAwsEcrLifecyclePolicy(), "aws_ecr_repository": resourceAwsEcrRepository(), diff --git a/aws/resource_aws_copy_snapshot.go b/aws/resource_aws_copy_snapshot.go new file mode 100644 index 00000000000..9a6e10228c1 --- /dev/null +++ b/aws/resource_aws_copy_snapshot.go @@ -0,0 +1,176 @@ +package aws + +import ( + "fmt" + "log" + "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" +) + +func resourceAwsCopySnapshot() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsCopySnapshotCreate, + Read: resourceAwsCopySnapshotRead, + Delete: resourceAwsCopySnapshotDelete, + + Schema: map[string]*schema.Schema{ + "volume_id": { + Type: schema.TypeString, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "owner_id": { + Type: schema.TypeString, + Computed: true, + }, + "owner_alias": { + Type: schema.TypeString, + Computed: true, + }, + "encrypted": { + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + }, + "volume_size": { + Type: schema.TypeInt, + Computed: true, + }, + "kms_key_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "data_encryption_key_id": { + Type: schema.TypeString, + Computed: true, + }, + "source_region": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "source_snapshot_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "tags": { + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + }, + }, + } +} + +func resourceAwsCopySnapshotCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + + request := &ec2.CopySnapshotInput{ + SourceRegion: aws.String(d.Get("source_region").(string)), + SourceSnapshotId: aws.String(d.Get("source_snapshot_id").(string)), + } + if v, ok := d.GetOk("description"); ok { + request.Description = aws.String(v.(string)) + } + if v, ok := d.GetOk("encrypted"); ok { + request.Encrypted = aws.Bool(v.(bool)) + } + if v, ok := d.GetOk("kms_key_id"); ok { + request.KmsKeyId = aws.String(v.(string)) + } + + res, err := conn.CopySnapshot(request) + if err != nil { + return err + } + + d.SetId(*res.SnapshotId) + + err = resourceAwsCopySnapshotWaitForAvailable(d.Id(), conn) + if err != nil { + return err + } + + if err := setTags(conn, d); err != nil { + log.Printf("[WARN] error setting tags: %s", err) + } + + return resourceAwsCopySnapshotRead(d, meta) +} + +func resourceAwsCopySnapshotRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + + req := &ec2.DescribeSnapshotsInput{ + SnapshotIds: []*string{aws.String(d.Id())}, + } + res, err := conn.DescribeSnapshots(req) + if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidSnapshotID.NotFound" { + log.Printf("Snapshot %q Not found - removing from state", d.Id()) + d.SetId("") + return nil + } + + snapshot := res.Snapshots[0] + + d.Set("description", snapshot.Description) + d.Set("owner_id", snapshot.OwnerId) + d.Set("encrypted", snapshot.Encrypted) + d.Set("owner_alias", snapshot.OwnerAlias) + d.Set("volume_id", snapshot.VolumeId) + d.Set("data_encryption_key_id", snapshot.DataEncryptionKeyId) + d.Set("kms_key_id", snapshot.KmsKeyId) + d.Set("volume_size", snapshot.VolumeSize) + + if err := d.Set("tags", tagsToMap(snapshot.Tags)); err != nil { + log.Printf("[WARN] error saving tags to state: %s", err) + } + + return nil +} + +func resourceAwsCopySnapshotDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + + return resource.Retry(5*time.Minute, func() *resource.RetryError { + request := &ec2.DeleteSnapshotInput{ + SnapshotId: aws.String(d.Id()), + } + _, err := conn.DeleteSnapshot(request) + if err == nil { + return nil + } + + ebsErr, ok := err.(awserr.Error) + if ebsErr.Code() == "SnapshotInUse" { + return resource.RetryableError(fmt.Errorf("EBS SnapshotInUse - trying again while it detaches")) + } + + if !ok { + return resource.NonRetryableError(err) + } + + return resource.NonRetryableError(err) + }) +} + +func resourceAwsCopySnapshotWaitForAvailable(id string, conn *ec2.EC2) error { + log.Printf("Waiting for Snapshot %s to become available...", id) + + req := &ec2.DescribeSnapshotsInput{ + SnapshotIds: []*string{aws.String(id)}, + } + err := conn.WaitUntilSnapshotCompleted(req) + return err +} diff --git a/aws/resource_aws_copy_snapshot_test.go b/aws/resource_aws_copy_snapshot_test.go new file mode 100644 index 00000000000..b8e100e270f --- /dev/null +++ b/aws/resource_aws_copy_snapshot_test.go @@ -0,0 +1,127 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSCopySnapshot_basic(t *testing.T) { + var v ec2.Snapshot + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccAwsCopySnapshotConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckCopySnapshotExists("aws_copy_snapshot.test", &v), + testAccCheckTags(&v.Tags, "Name", "testAccAwsCopySnapshotConfig"), + ), + }, + }, + }) +} + +func TestAccAWSCopySnapshot_withDescription(t *testing.T) { + var v ec2.Snapshot + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccAwsCopySnapshotConfigWithDescription, + Check: resource.ComposeTestCheckFunc( + testAccCheckCopySnapshotExists("aws_copy_snapshot.description_test", &v), + resource.TestCheckResourceAttr("aws_copy_snapshot.description_test", "description", "Copy Snapshot Acceptance Test"), + ), + }, + }, + }) +} + +func testAccCheckCopySnapshotExists(n string, v *ec2.Snapshot) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).ec2conn + + request := &ec2.DescribeSnapshotsInput{ + SnapshotIds: []*string{aws.String(rs.Primary.ID)}, + } + + response, err := conn.DescribeSnapshots(request) + if err == nil { + if response.Snapshots != nil && len(response.Snapshots) > 0 { + *v = *response.Snapshots[0] + return nil + } + } + return fmt.Errorf("Error finding EC2 Snapshot %s", rs.Primary.ID) + } +} + +const testAccAwsCopySnapshotConfig = ` +resource "aws_ebs_volume" "test" { + availability_zone = "us-west-2a" + size = 1 +} + +resource "aws_ebs_snapshot" "test" { + volume_id = "${aws_ebs_volume.test.id}" + + tags { + Name = "testAccAwsEbsSnapshotConfig" + } +} + +resource "aws_copy_snapshot" "test" { + source_snapshot_id = "${aws_ebs_snapshot.test.id}" + source_region = "us-west-2" + + tags { + Name = "testAccAwsCopySnapshotConfig" + } +} +` + +const testAccAwsCopySnapshotConfigWithDescription = ` +resource "aws_ebs_volume" "description_test" { + availability_zone = "us-west-2a" + size = 1 + + tags { + Name = "testAccAwsCopySnapshotConfigWithDescription" + } +} + +resource "aws_ebs_snapshot" "description_test" { + volume_id = "${aws_ebs_volume.description_test.id}" + description = "EBS Snapshot Acceptance Test" + + tags { + Name = "testAccAwsCopySnapshotConfigWithDescription" + } +} + +resource "aws_copy_snapshot" "description_test" { + description = "Copy Snapshot Acceptance Test" + source_snapshot_id = "${aws_ebs_snapshot.description_test.id}" + source_region = "us-west-2" + + tags { + Name = "testAccAwsCopySnapshotConfigWithDescription" + } +} +` From 226f5ce647d5d75b49e475a06c08facb28bf0821 Mon Sep 17 00:00:00 2001 From: Jason Wieringa Date: Mon, 22 Jan 2018 13:32:10 -0800 Subject: [PATCH 2/5] Added aws_copy_snapshot multi region acceptance test Used TestAccAWSInstance_multipleRegions as an example: https://github.com/terraform-providers/terraform-provider-aws/blob/2a36215957d15aab4c7260a20742980ba70533cb/aws/resource_aws_instance_test.go#L655 --- aws/resource_aws_copy_snapshot_test.go | 101 ++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 9 deletions(-) diff --git a/aws/resource_aws_copy_snapshot_test.go b/aws/resource_aws_copy_snapshot_test.go index b8e100e270f..3d5a89aaf15 100644 --- a/aws/resource_aws_copy_snapshot_test.go +++ b/aws/resource_aws_copy_snapshot_test.go @@ -7,6 +7,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/terraform" ) @@ -44,7 +45,41 @@ func TestAccAWSCopySnapshot_withDescription(t *testing.T) { }) } +func TestAccAWSCopySnapshot_withRegions(t *testing.T) { + var v ec2.Snapshot + + // record the initialized providers so that we can use them to + // check for the instances in each region + var providers []*schema.Provider + providerFactories := map[string]terraform.ResourceProviderFactory{ + "aws": func() (terraform.ResourceProvider, error) { + p := Provider() + providers = append(providers, p.(*schema.Provider)) + return p, nil + }, + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: testAccAwsCopySnapshotConfigWithRegions, + Check: resource.ComposeTestCheckFunc( + testAccCheckCopySnapshotExistsWithProviders("aws_copy_snapshot.region_test", &v, &providers), + ), + }, + }, + }) + +} + func testAccCheckCopySnapshotExists(n string, v *ec2.Snapshot) resource.TestCheckFunc { + providers := []*schema.Provider{testAccProvider} + return testAccCheckCopySnapshotExistsWithProviders(n, v, &providers) +} + +func testAccCheckCopySnapshotExistsWithProviders(n string, v *ec2.Snapshot, providers *[]*schema.Provider) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -55,17 +90,24 @@ func testAccCheckCopySnapshotExists(n string, v *ec2.Snapshot) resource.TestChec return fmt.Errorf("No ID is set") } - conn := testAccProvider.Meta().(*AWSClient).ec2conn + for _, provider := range *providers { + // Ignore if Meta is empty, this can happen for validation providers + if provider.Meta() == nil { + continue + } - request := &ec2.DescribeSnapshotsInput{ - SnapshotIds: []*string{aws.String(rs.Primary.ID)}, - } + conn := provider.Meta().(*AWSClient).ec2conn + + request := &ec2.DescribeSnapshotsInput{ + SnapshotIds: []*string{aws.String(rs.Primary.ID)}, + } - response, err := conn.DescribeSnapshots(request) - if err == nil { - if response.Snapshots != nil && len(response.Snapshots) > 0 { - *v = *response.Snapshots[0] - return nil + response, err := conn.DescribeSnapshots(request) + if err == nil { + if response.Snapshots != nil && len(response.Snapshots) > 0 { + *v = *response.Snapshots[0] + return nil + } } } return fmt.Errorf("Error finding EC2 Snapshot %s", rs.Primary.ID) @@ -125,3 +167,44 @@ resource "aws_copy_snapshot" "description_test" { } } ` + +const testAccAwsCopySnapshotConfigWithRegions = ` +provider "aws" { + region = "us-west-2" + alias = "uswest2" +} + +provider "aws" { + region = "us-east-1" + alias = "useast1" +} + +resource "aws_ebs_volume" "region_test" { + provider = "aws.uswest2" + availability_zone = "us-west-2a" + size = 1 + + tags { + Name = "testAccAwsCopySnapshotConfigWithRegions" + } +} + +resource "aws_ebs_snapshot" "region_test" { + provider = "aws.uswest2" + volume_id = "${aws_ebs_volume.region_test.id}" + + tags { + Name = "testAccAwsCopySnapshotConfigWithRegions" + } +} + +resource "aws_copy_snapshot" "region_test" { + provider = "aws.useast1" + source_snapshot_id = "${aws_ebs_snapshot.region_test.id}" + source_region = "us-west-2" + + tags { + Name = "testAccAwsCopySnapshotConfigWithRegions" + } +} +` From f414b7d571aa867a24f7716f6dd6ac91ba9bde8f Mon Sep 17 00:00:00 2001 From: Jason Wieringa Date: Mon, 22 Jan 2018 15:15:43 -0800 Subject: [PATCH 3/5] Added aws_copy_snapshot with KMS acceptance test --- aws/resource_aws_copy_snapshot_test.go | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/aws/resource_aws_copy_snapshot_test.go b/aws/resource_aws_copy_snapshot_test.go index 3d5a89aaf15..95936b4ab53 100644 --- a/aws/resource_aws_copy_snapshot_test.go +++ b/aws/resource_aws_copy_snapshot_test.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "regexp" "testing" "github.com/aws/aws-sdk-go/aws" @@ -74,6 +75,24 @@ func TestAccAWSCopySnapshot_withRegions(t *testing.T) { } +func TestAccAWSCopySnapshot_withKms(t *testing.T) { + var v ec2.Snapshot + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccAwsCopySnapshotConfigWithKms, + Check: resource.ComposeTestCheckFunc( + testAccCheckCopySnapshotExists("aws_copy_snapshot.kms_test", &v), + resource.TestMatchResourceAttr("aws_copy_snapshot.kms_test", "kms_key_id", + regexp.MustCompile("^arn:aws:kms:[a-z]{2}-[a-z]+-\\d{1}:[0-9]{12}:key/[a-z0-9-]{36}$")), + ), + }, + }, + }) +} + func testAccCheckCopySnapshotExists(n string, v *ec2.Snapshot) resource.TestCheckFunc { providers := []*schema.Provider{testAccProvider} return testAccCheckCopySnapshotExistsWithProviders(n, v, &providers) @@ -208,3 +227,42 @@ resource "aws_copy_snapshot" "region_test" { } } ` + +const testAccAwsCopySnapshotConfigWithKms = ` +provider "aws" { + region = "us-west-2" +} + +resource "aws_kms_key" "kms_test" { + description = "testAccAwsCopySnapshotConfigWithKms" + deletion_window_in_days = 7 +} + +resource "aws_ebs_volume" "kms_test" { + availability_zone = "us-west-2a" + size = 1 + + tags { + Name = "testAccAwsCopySnapshotConfigWithKms" + } +} + +resource "aws_ebs_snapshot" "kms_test" { + volume_id = "${aws_ebs_volume.kms_test.id}" + + tags { + Name = "testAccAwsCopySnapshotConfigWithKms" + } +} + +resource "aws_copy_snapshot" "kms_test" { + source_snapshot_id = "${aws_ebs_snapshot.kms_test.id}" + source_region = "us-west-2" + encrypted = true + kms_key_id = "${aws_kms_key.kms_test.arn}" + + tags { + Name = "testAccAwsCopySnapshotConfigWithKms" + } +} +` From 0fe1b9cdb04704aa8a5ffa05a98313abe9780d1a Mon Sep 17 00:00:00 2001 From: Jason Wieringa Date: Thu, 25 Jan 2018 18:49:28 -0800 Subject: [PATCH 4/5] website/docs/r/copy_snapshot added documentation for new resource --- website/docs/r/copy_snapshot.html.md | 66 ++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 website/docs/r/copy_snapshot.html.md diff --git a/website/docs/r/copy_snapshot.html.md b/website/docs/r/copy_snapshot.html.md new file mode 100644 index 00000000000..f51ed369ecf --- /dev/null +++ b/website/docs/r/copy_snapshot.html.md @@ -0,0 +1,66 @@ +--- +layout: "aws" +page_title: "AWS: aws_copy_snapshot" +sidebar_current: "docs-aws-resource-copy-snapshot" +description: |- + Duplicates an existing Amazon snapshot +--- + +# aws_copy_snapshot + +Creates a Snapshot of a snapshot. + +## Example Usage + +```hcl +resource "aws_ebs_volume" "example" { + availability_zone = "us-west-2a" + size = 40 + tags { + Name = "HelloWorld" + } +} + +resource "aws_ebs_snapshot" "example_snapshot" { + volume_id = "${aws_ebs_volume.example.id}" + + tags { + Name = "HelloWorld_snap" + } +} + +resource "aws_copy_snapshot" "example_copy" { + source_snapshot_id = "${aws_ebs_snapshot.example_snapshot.id}" + source_region = "us-west-2" + + tags { + Name = "HelloWorld_copy_snap" + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `description` - (Optional) A description of what the snapshot is. +* `encrypted` - Whether the snapshot is encrypted. +* `kms_key_id` - The ARN for the KMS encryption key. +* `source_snapshot_id` The ARN for the snapshot to be copied. +* `source_region` The region of the source snapshot. +* `tags` - A mapping of tags for the snapshot. + +## Attributes Reference + +The following attributes are exported: + +* `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. +* `encrypted` - Whether the snapshot is encrypted. +* `volume_size` - The size of the drive in GiBs. +* `kms_key_id` - The ARN for the KMS encryption key. +* `data_encryption_key_id` - The data encryption key identifier for the snapshot. +* `source_snapshot_id` The ARN of the copied snapshot. +* `source_region` The region of the source snapshot. +* `tags` - A mapping of tags for the snapshot. From 7e7dc07eca1ec8fe3e7ae4f741a9ef7d7004f3d8 Mon Sep 17 00:00:00 2001 From: Jason Wieringa Date: Tue, 10 Jul 2018 11:15:18 -0700 Subject: [PATCH 5/5] Renamed aws_copy_snapshot to aws_ebs_snapshot_copy I took another look at the original naming, aws_copy_snapshot, and I've reasoned that it was not consistent with resources like aws_ami_copy. Thus, I've modified the resource to be aws_ebs_snapshot_copy to conform to aws__ which I thought matched existing resources better. --- aws/provider.go | 2 +- ...t.go => resource_aws_ebs_snapshot_copy.go} | 20 ++--- ...=> resource_aws_ebs_snapshot_copy_test.go} | 76 +++++++++---------- website/docs/r/copy_snapshot.html.md | 8 +- 4 files changed, 53 insertions(+), 53 deletions(-) rename aws/{resource_aws_copy_snapshot.go => resource_aws_ebs_snapshot_copy.go} (85%) rename aws/{resource_aws_copy_snapshot_test.go => resource_aws_ebs_snapshot_copy_test.go} (64%) diff --git a/aws/provider.go b/aws/provider.go index 69bdc0e51ed..1b04b6a2daa 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -389,7 +389,7 @@ func Provider() terraform.ResourceProvider { "aws_dynamodb_table_item": resourceAwsDynamoDbTableItem(), "aws_dynamodb_global_table": resourceAwsDynamoDbGlobalTable(), "aws_ebs_snapshot": resourceAwsEbsSnapshot(), - "aws_copy_snapshot": resourceAwsCopySnapshot(), + "aws_ebs_snapshot_copy": resourceAwsEbsSnapshotCopy(), "aws_ebs_volume": resourceAwsEbsVolume(), "aws_ecr_lifecycle_policy": resourceAwsEcrLifecyclePolicy(), "aws_ecr_repository": resourceAwsEcrRepository(), diff --git a/aws/resource_aws_copy_snapshot.go b/aws/resource_aws_ebs_snapshot_copy.go similarity index 85% rename from aws/resource_aws_copy_snapshot.go rename to aws/resource_aws_ebs_snapshot_copy.go index 9a6e10228c1..f1595412888 100644 --- a/aws/resource_aws_copy_snapshot.go +++ b/aws/resource_aws_ebs_snapshot_copy.go @@ -12,11 +12,11 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -func resourceAwsCopySnapshot() *schema.Resource { +func resourceAwsEbsSnapshotCopy() *schema.Resource { return &schema.Resource{ - Create: resourceAwsCopySnapshotCreate, - Read: resourceAwsCopySnapshotRead, - Delete: resourceAwsCopySnapshotDelete, + Create: resourceAwsEbsSnapshotCopyCreate, + Read: resourceAwsEbsSnapshotCopyRead, + Delete: resourceAwsEbsSnapshotCopyDelete, Schema: map[string]*schema.Schema{ "volume_id": { @@ -73,7 +73,7 @@ func resourceAwsCopySnapshot() *schema.Resource { } } -func resourceAwsCopySnapshotCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsEbsSnapshotCopyCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn request := &ec2.CopySnapshotInput{ @@ -97,7 +97,7 @@ func resourceAwsCopySnapshotCreate(d *schema.ResourceData, meta interface{}) err d.SetId(*res.SnapshotId) - err = resourceAwsCopySnapshotWaitForAvailable(d.Id(), conn) + err = resourceAwsEbsSnapshotCopyWaitForAvailable(d.Id(), conn) if err != nil { return err } @@ -106,10 +106,10 @@ func resourceAwsCopySnapshotCreate(d *schema.ResourceData, meta interface{}) err log.Printf("[WARN] error setting tags: %s", err) } - return resourceAwsCopySnapshotRead(d, meta) + return resourceAwsEbsSnapshotCopyRead(d, meta) } -func resourceAwsCopySnapshotRead(d *schema.ResourceData, meta interface{}) error { +func resourceAwsEbsSnapshotCopyRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn req := &ec2.DescribeSnapshotsInput{ @@ -140,7 +140,7 @@ func resourceAwsCopySnapshotRead(d *schema.ResourceData, meta interface{}) error return nil } -func resourceAwsCopySnapshotDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAwsEbsSnapshotCopyDelete(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn return resource.Retry(5*time.Minute, func() *resource.RetryError { @@ -165,7 +165,7 @@ func resourceAwsCopySnapshotDelete(d *schema.ResourceData, meta interface{}) err }) } -func resourceAwsCopySnapshotWaitForAvailable(id string, conn *ec2.EC2) error { +func resourceAwsEbsSnapshotCopyWaitForAvailable(id string, conn *ec2.EC2) error { log.Printf("Waiting for Snapshot %s to become available...", id) req := &ec2.DescribeSnapshotsInput{ diff --git a/aws/resource_aws_copy_snapshot_test.go b/aws/resource_aws_ebs_snapshot_copy_test.go similarity index 64% rename from aws/resource_aws_copy_snapshot_test.go rename to aws/resource_aws_ebs_snapshot_copy_test.go index 95936b4ab53..965a00a283a 100644 --- a/aws/resource_aws_copy_snapshot_test.go +++ b/aws/resource_aws_ebs_snapshot_copy_test.go @@ -12,41 +12,41 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestAccAWSCopySnapshot_basic(t *testing.T) { +func TestAccAWSEbsSnapshotCopy_basic(t *testing.T) { var v ec2.Snapshot resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccAwsCopySnapshotConfig, + Config: testAccAwsEbsSnapshotCopyConfig, Check: resource.ComposeTestCheckFunc( - testAccCheckCopySnapshotExists("aws_copy_snapshot.test", &v), - testAccCheckTags(&v.Tags, "Name", "testAccAwsCopySnapshotConfig"), + testAccCheckEbsSnapshotCopyExists("aws_ebs_snapshot_copy.test", &v), + testAccCheckTags(&v.Tags, "Name", "testAccAwsEbsSnapshotCopyConfig"), ), }, }, }) } -func TestAccAWSCopySnapshot_withDescription(t *testing.T) { +func TestAccAWSEbsSnapshotCopy_withDescription(t *testing.T) { var v ec2.Snapshot resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccAwsCopySnapshotConfigWithDescription, + Config: testAccAwsEbsSnapshotCopyConfigWithDescription, Check: resource.ComposeTestCheckFunc( - testAccCheckCopySnapshotExists("aws_copy_snapshot.description_test", &v), - resource.TestCheckResourceAttr("aws_copy_snapshot.description_test", "description", "Copy Snapshot Acceptance Test"), + testAccCheckEbsSnapshotCopyExists("aws_ebs_snapshot_copy.description_test", &v), + resource.TestCheckResourceAttr("aws_ebs_snapshot_copy.description_test", "description", "Copy Snapshot Acceptance Test"), ), }, }, }) } -func TestAccAWSCopySnapshot_withRegions(t *testing.T) { +func TestAccAWSEbsSnapshotCopy_withRegions(t *testing.T) { var v ec2.Snapshot // record the initialized providers so that we can use them to @@ -65,9 +65,9 @@ func TestAccAWSCopySnapshot_withRegions(t *testing.T) { ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: testAccAwsCopySnapshotConfigWithRegions, + Config: testAccAwsEbsSnapshotCopyConfigWithRegions, Check: resource.ComposeTestCheckFunc( - testAccCheckCopySnapshotExistsWithProviders("aws_copy_snapshot.region_test", &v, &providers), + testAccCheckEbsSnapshotCopyExistsWithProviders("aws_ebs_snapshot_copy.region_test", &v, &providers), ), }, }, @@ -75,17 +75,17 @@ func TestAccAWSCopySnapshot_withRegions(t *testing.T) { } -func TestAccAWSCopySnapshot_withKms(t *testing.T) { +func TestAccAWSEbsSnapshotCopy_withKms(t *testing.T) { var v ec2.Snapshot resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccAwsCopySnapshotConfigWithKms, + Config: testAccAwsEbsSnapshotCopyConfigWithKms, Check: resource.ComposeTestCheckFunc( - testAccCheckCopySnapshotExists("aws_copy_snapshot.kms_test", &v), - resource.TestMatchResourceAttr("aws_copy_snapshot.kms_test", "kms_key_id", + testAccCheckEbsSnapshotCopyExists("aws_ebs_snapshot_copy.kms_test", &v), + resource.TestMatchResourceAttr("aws_ebs_snapshot_copy.kms_test", "kms_key_id", regexp.MustCompile("^arn:aws:kms:[a-z]{2}-[a-z]+-\\d{1}:[0-9]{12}:key/[a-z0-9-]{36}$")), ), }, @@ -93,12 +93,12 @@ func TestAccAWSCopySnapshot_withKms(t *testing.T) { }) } -func testAccCheckCopySnapshotExists(n string, v *ec2.Snapshot) resource.TestCheckFunc { +func testAccCheckEbsSnapshotCopyExists(n string, v *ec2.Snapshot) resource.TestCheckFunc { providers := []*schema.Provider{testAccProvider} - return testAccCheckCopySnapshotExistsWithProviders(n, v, &providers) + return testAccCheckEbsSnapshotCopyExistsWithProviders(n, v, &providers) } -func testAccCheckCopySnapshotExistsWithProviders(n string, v *ec2.Snapshot, providers *[]*schema.Provider) resource.TestCheckFunc { +func testAccCheckEbsSnapshotCopyExistsWithProviders(n string, v *ec2.Snapshot, providers *[]*schema.Provider) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -133,7 +133,7 @@ func testAccCheckCopySnapshotExistsWithProviders(n string, v *ec2.Snapshot, prov } } -const testAccAwsCopySnapshotConfig = ` +const testAccAwsEbsSnapshotCopyConfig = ` resource "aws_ebs_volume" "test" { availability_zone = "us-west-2a" size = 1 @@ -143,27 +143,27 @@ resource "aws_ebs_snapshot" "test" { volume_id = "${aws_ebs_volume.test.id}" tags { - Name = "testAccAwsEbsSnapshotConfig" + Name = "testAccAwsEbsSnapshotCopyConfig" } } -resource "aws_copy_snapshot" "test" { +resource "aws_ebs_snapshot_copy" "test" { source_snapshot_id = "${aws_ebs_snapshot.test.id}" source_region = "us-west-2" tags { - Name = "testAccAwsCopySnapshotConfig" + Name = "testAccAwsEbsSnapshotCopyConfig" } } ` -const testAccAwsCopySnapshotConfigWithDescription = ` +const testAccAwsEbsSnapshotCopyConfigWithDescription = ` resource "aws_ebs_volume" "description_test" { availability_zone = "us-west-2a" size = 1 tags { - Name = "testAccAwsCopySnapshotConfigWithDescription" + Name = "testAccAwsEbsSnapshotCopyConfigWithDescription" } } @@ -172,22 +172,22 @@ resource "aws_ebs_snapshot" "description_test" { description = "EBS Snapshot Acceptance Test" tags { - Name = "testAccAwsCopySnapshotConfigWithDescription" + Name = "testAccAwsEbsSnapshotCopyConfigWithDescription" } } -resource "aws_copy_snapshot" "description_test" { +resource "aws_ebs_snapshot_copy" "description_test" { description = "Copy Snapshot Acceptance Test" source_snapshot_id = "${aws_ebs_snapshot.description_test.id}" source_region = "us-west-2" tags { - Name = "testAccAwsCopySnapshotConfigWithDescription" + Name = "testAccAwsEbsSnapshotCopyConfigWithDescription" } } ` -const testAccAwsCopySnapshotConfigWithRegions = ` +const testAccAwsEbsSnapshotCopyConfigWithRegions = ` provider "aws" { region = "us-west-2" alias = "uswest2" @@ -204,7 +204,7 @@ resource "aws_ebs_volume" "region_test" { size = 1 tags { - Name = "testAccAwsCopySnapshotConfigWithRegions" + Name = "testAccAwsEbsSnapshotCopyConfigWithRegions" } } @@ -213,28 +213,28 @@ resource "aws_ebs_snapshot" "region_test" { volume_id = "${aws_ebs_volume.region_test.id}" tags { - Name = "testAccAwsCopySnapshotConfigWithRegions" + Name = "testAccAwsEbsSnapshotCopyConfigWithRegions" } } -resource "aws_copy_snapshot" "region_test" { +resource "aws_ebs_snapshot_copy" "region_test" { provider = "aws.useast1" source_snapshot_id = "${aws_ebs_snapshot.region_test.id}" source_region = "us-west-2" tags { - Name = "testAccAwsCopySnapshotConfigWithRegions" + Name = "testAccAwsEbsSnapshotCopyConfigWithRegions" } } ` -const testAccAwsCopySnapshotConfigWithKms = ` +const testAccAwsEbsSnapshotCopyConfigWithKms = ` provider "aws" { region = "us-west-2" } resource "aws_kms_key" "kms_test" { - description = "testAccAwsCopySnapshotConfigWithKms" + description = "testAccAwsEbsSnapshotCopyConfigWithKms" deletion_window_in_days = 7 } @@ -243,7 +243,7 @@ resource "aws_ebs_volume" "kms_test" { size = 1 tags { - Name = "testAccAwsCopySnapshotConfigWithKms" + Name = "testAccAwsEbsSnapshotCopyConfigWithKms" } } @@ -251,18 +251,18 @@ resource "aws_ebs_snapshot" "kms_test" { volume_id = "${aws_ebs_volume.kms_test.id}" tags { - Name = "testAccAwsCopySnapshotConfigWithKms" + Name = "testAccAwsEbsSnapshotCopyConfigWithKms" } } -resource "aws_copy_snapshot" "kms_test" { +resource "aws_ebs_snapshot_copy" "kms_test" { source_snapshot_id = "${aws_ebs_snapshot.kms_test.id}" source_region = "us-west-2" encrypted = true kms_key_id = "${aws_kms_key.kms_test.arn}" tags { - Name = "testAccAwsCopySnapshotConfigWithKms" + Name = "testAccAwsEbsSnapshotCopyConfigWithKms" } } ` diff --git a/website/docs/r/copy_snapshot.html.md b/website/docs/r/copy_snapshot.html.md index f51ed369ecf..d5d7a6fe759 100644 --- a/website/docs/r/copy_snapshot.html.md +++ b/website/docs/r/copy_snapshot.html.md @@ -1,12 +1,12 @@ --- layout: "aws" -page_title: "AWS: aws_copy_snapshot" -sidebar_current: "docs-aws-resource-copy-snapshot" +page_title: "AWS: aws_ebs_snapshot_copy" +sidebar_current: "docs-aws-resource-ebs-snapshot-copy" description: |- Duplicates an existing Amazon snapshot --- -# aws_copy_snapshot +# aws_ebs_snapshot_copy Creates a Snapshot of a snapshot. @@ -29,7 +29,7 @@ resource "aws_ebs_snapshot" "example_snapshot" { } } -resource "aws_copy_snapshot" "example_copy" { +resource "aws_ebs_snapshot_copy" "example_copy" { source_snapshot_id = "${aws_ebs_snapshot.example_snapshot.id}" source_region = "us-west-2"