From 858e32ca82c42ea0a4962206c4d0c2abad093385 Mon Sep 17 00:00:00 2001 From: Matt Dainty Date: Thu, 17 Aug 2017 12:58:05 +0000 Subject: [PATCH 1/2] Add `stopped` as a valid state for attachment --- aws/resource_aws_volume_attachment.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_volume_attachment.go b/aws/resource_aws_volume_attachment.go index 2afcd6c676a..4e7b78489a6 100644 --- a/aws/resource_aws_volume_attachment.go +++ b/aws/resource_aws_volume_attachment.go @@ -81,8 +81,8 @@ func resourceAwsVolumeAttachmentCreate(d *schema.ResourceData, meta interface{}) // a spot request and whilst the request has been fulfilled the // instance is not running yet stateConf := &resource.StateChangeConf{ - Pending: []string{"pending"}, - Target: []string{"running"}, + Pending: []string{"pending", "stopping"}, + Target: []string{"running", "stopped"}, Refresh: InstanceStateRefreshFunc(conn, iID, "terminated"), Timeout: 10 * time.Minute, Delay: 10 * time.Second, From 8a516c92c7d76bfe65858aa5e88396c62659229b Mon Sep 17 00:00:00 2001 From: Matt Dainty Date: Mon, 11 Dec 2017 09:56:05 +0000 Subject: [PATCH 2/2] Add test for attaching volume to stopped instance --- aws/resource_aws_volume_attachment_test.go | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/aws/resource_aws_volume_attachment_test.go b/aws/resource_aws_volume_attachment_test.go index 0b99d1ffad5..0ce66b7c948 100644 --- a/aws/resource_aws_volume_attachment_test.go +++ b/aws/resource_aws_volume_attachment_test.go @@ -4,7 +4,9 @@ import ( "fmt" "log" "testing" + "time" + "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" @@ -62,6 +64,62 @@ func TestAccAWSVolumeAttachment_skipDestroy(t *testing.T) { }) } +func TestAccAWSVolumeAttachment_attachStopped(t *testing.T) { + var i ec2.Instance + var v ec2.Volume + + stopInstance := func() { + conn := testAccProvider.Meta().(*AWSClient).ec2conn + + _, err := conn.StopInstances(&ec2.StopInstancesInput{ + InstanceIds: []*string{aws.String(*i.InstanceId)}, + }) + + stateConf := &resource.StateChangeConf{ + Pending: []string{"pending", "running", "stopping"}, + Target: []string{"stopped"}, + Refresh: InstanceStateRefreshFunc(conn, *i.InstanceId, ""), + Timeout: 10 * time.Minute, + Delay: 10 * time.Second, + MinTimeout: 3 * time.Second, + } + + _, err = stateConf.WaitForState() + if err != nil { + t.Fatalf("Error waiting for instance(%s) to stop: %s", *i.InstanceId, err) + } + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckVolumeAttachmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccVolumeAttachmentConfigInstanceOnly, + Check: resource.ComposeTestCheckFunc( + testAccCheckInstanceExists( + "aws_instance.web", &i), + ), + }, + { + PreConfig: stopInstance, + Config: testAccVolumeAttachmentConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "aws_volume_attachment.ebs_att", "device_name", "/dev/sdh"), + testAccCheckInstanceExists( + "aws_instance.web", &i), + testAccCheckVolumeExists( + "aws_ebs_volume.example", &v), + testAccCheckVolumeAttachmentExists( + "aws_volume_attachment.ebs_att", &i, &v), + ), + }, + }, + }) +} + func testAccCheckVolumeAttachmentExists(n string, i *ec2.Instance, v *ec2.Volume) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -96,6 +154,17 @@ func testAccCheckVolumeAttachmentDestroy(s *terraform.State) error { return nil } +const testAccVolumeAttachmentConfigInstanceOnly = ` +resource "aws_instance" "web" { + ami = "ami-21f78e11" + availability_zone = "us-west-2a" + instance_type = "t1.micro" + tags { + Name = "HelloWorld" + } +} +` + const testAccVolumeAttachmentConfig = ` resource "aws_instance" "web" { ami = "ami-21f78e11"