terraform/builtin/providers/aws/resource_aws_volume_attachm...

161 lines
3.7 KiB
Go
Raw Normal View History

package aws
import (
"fmt"
"log"
"testing"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSVolumeAttachment_basic(t *testing.T) {
var i ec2.Instance
var v ec2.Volume
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVolumeAttachmentDestroy,
Steps: []resource.TestStep{
provider/aws: Provide the option to skip_destroy on aws_volume_attachment (#9792) * provider/aws: Provide the option to skip_destroy on aws_volume_attachment When you want to attach and detach pre-existing EBS volumes to an instance, we would do that as follows: ``` resource "aws_instance" "web" { ami = "ami-21f78e11" availability_zone = "us-west-2a" instance_type = "t1.micro" tags { Name = "HelloWorld" } } data "aws_ebs_volume" "ebs_volume" { filter { name = "size" values = ["${aws_ebs_volume.example.size}"] } filter { name = "availability-zone" values = ["${aws_ebs_volume.example.availability_zone}"] } filter { name = "tag:Name" values = ["TestVolume"] } } resource "aws_volume_attachment" "ebs_att" { device_name = "/dev/sdh" volume_id = "${data.aws_ebs_volume.ebs_volume.id}" instance_id = "${aws_instance.web.id}" skip_destroy = true } ``` The issue here is that when we run a terraform destroy command, the volume tries to get detached from a running instance and goes into a non-responsive state. We would have to force_destroy the volume at that point and risk losing any data on it. This PR introduces the idea of `skip_destroy` on a volume attachment. tl;dr: We want the volume to be detached from the instane when the instance itself has been destroyed. This way the normal shut procedures will happen and protect the disk for attachment to another instance Volume Attachment Tests: ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSVolumeAttachment_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/11/02 00:47:27 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSVolumeAttachment_ -timeout 120m === RUN TestAccAWSVolumeAttachment_basic --- PASS: TestAccAWSVolumeAttachment_basic (133.49s) === RUN TestAccAWSVolumeAttachment_skipDestroy --- PASS: TestAccAWSVolumeAttachment_skipDestroy (119.64s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 253.158s ``` EBS Volume Tests: ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSEBSVolume_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/11/02 01:00:18 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSEBSVolume_ -timeout 120m === RUN TestAccAWSEBSVolume_importBasic --- PASS: TestAccAWSEBSVolume_importBasic (26.38s) === RUN TestAccAWSEBSVolume_basic --- PASS: TestAccAWSEBSVolume_basic (26.86s) === RUN TestAccAWSEBSVolume_NoIops --- PASS: TestAccAWSEBSVolume_NoIops (27.89s) === RUN TestAccAWSEBSVolume_withTags --- PASS: TestAccAWSEBSVolume_withTags (26.88s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 108.032s ``` * Update volume_attachment.html.markdown
2016-11-02 16:29:37 +01:00
{
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),
),
},
},
})
}
provider/aws: Provide the option to skip_destroy on aws_volume_attachment (#9792) * provider/aws: Provide the option to skip_destroy on aws_volume_attachment When you want to attach and detach pre-existing EBS volumes to an instance, we would do that as follows: ``` resource "aws_instance" "web" { ami = "ami-21f78e11" availability_zone = "us-west-2a" instance_type = "t1.micro" tags { Name = "HelloWorld" } } data "aws_ebs_volume" "ebs_volume" { filter { name = "size" values = ["${aws_ebs_volume.example.size}"] } filter { name = "availability-zone" values = ["${aws_ebs_volume.example.availability_zone}"] } filter { name = "tag:Name" values = ["TestVolume"] } } resource "aws_volume_attachment" "ebs_att" { device_name = "/dev/sdh" volume_id = "${data.aws_ebs_volume.ebs_volume.id}" instance_id = "${aws_instance.web.id}" skip_destroy = true } ``` The issue here is that when we run a terraform destroy command, the volume tries to get detached from a running instance and goes into a non-responsive state. We would have to force_destroy the volume at that point and risk losing any data on it. This PR introduces the idea of `skip_destroy` on a volume attachment. tl;dr: We want the volume to be detached from the instane when the instance itself has been destroyed. This way the normal shut procedures will happen and protect the disk for attachment to another instance Volume Attachment Tests: ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSVolumeAttachment_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/11/02 00:47:27 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSVolumeAttachment_ -timeout 120m === RUN TestAccAWSVolumeAttachment_basic --- PASS: TestAccAWSVolumeAttachment_basic (133.49s) === RUN TestAccAWSVolumeAttachment_skipDestroy --- PASS: TestAccAWSVolumeAttachment_skipDestroy (119.64s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 253.158s ``` EBS Volume Tests: ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSEBSVolume_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/11/02 01:00:18 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSEBSVolume_ -timeout 120m === RUN TestAccAWSEBSVolume_importBasic --- PASS: TestAccAWSEBSVolume_importBasic (26.38s) === RUN TestAccAWSEBSVolume_basic --- PASS: TestAccAWSEBSVolume_basic (26.86s) === RUN TestAccAWSEBSVolume_NoIops --- PASS: TestAccAWSEBSVolume_NoIops (27.89s) === RUN TestAccAWSEBSVolume_withTags --- PASS: TestAccAWSEBSVolume_withTags (26.88s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 108.032s ``` * Update volume_attachment.html.markdown
2016-11-02 16:29:37 +01:00
func TestAccAWSVolumeAttachment_skipDestroy(t *testing.T) {
var i ec2.Instance
var v ec2.Volume
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVolumeAttachmentDestroy,
Steps: []resource.TestStep{
{
Config: testAccVolumeAttachmentConfigSkipDestroy,
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]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}
for _, b := range i.BlockDeviceMappings {
if rs.Primary.Attributes["device_name"] == *b.DeviceName {
if b.Ebs.VolumeId != nil && rs.Primary.Attributes["volume_id"] == *b.Ebs.VolumeId {
// pass
return nil
}
}
}
return fmt.Errorf("Error finding instance/volume")
}
}
func testAccCheckVolumeAttachmentDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
log.Printf("\n\n----- This is never called")
if rs.Type != "aws_volume_attachment" {
continue
}
}
return nil
}
const testAccVolumeAttachmentConfig = `
resource "aws_instance" "web" {
ami = "ami-21f78e11"
availability_zone = "us-west-2a"
instance_type = "t1.micro"
tags {
Name = "HelloWorld"
}
}
resource "aws_ebs_volume" "example" {
availability_zone = "us-west-2a"
size = 1
}
resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = "${aws_ebs_volume.example.id}"
instance_id = "${aws_instance.web.id}"
}
`
provider/aws: Provide the option to skip_destroy on aws_volume_attachment (#9792) * provider/aws: Provide the option to skip_destroy on aws_volume_attachment When you want to attach and detach pre-existing EBS volumes to an instance, we would do that as follows: ``` resource "aws_instance" "web" { ami = "ami-21f78e11" availability_zone = "us-west-2a" instance_type = "t1.micro" tags { Name = "HelloWorld" } } data "aws_ebs_volume" "ebs_volume" { filter { name = "size" values = ["${aws_ebs_volume.example.size}"] } filter { name = "availability-zone" values = ["${aws_ebs_volume.example.availability_zone}"] } filter { name = "tag:Name" values = ["TestVolume"] } } resource "aws_volume_attachment" "ebs_att" { device_name = "/dev/sdh" volume_id = "${data.aws_ebs_volume.ebs_volume.id}" instance_id = "${aws_instance.web.id}" skip_destroy = true } ``` The issue here is that when we run a terraform destroy command, the volume tries to get detached from a running instance and goes into a non-responsive state. We would have to force_destroy the volume at that point and risk losing any data on it. This PR introduces the idea of `skip_destroy` on a volume attachment. tl;dr: We want the volume to be detached from the instane when the instance itself has been destroyed. This way the normal shut procedures will happen and protect the disk for attachment to another instance Volume Attachment Tests: ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSVolumeAttachment_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/11/02 00:47:27 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSVolumeAttachment_ -timeout 120m === RUN TestAccAWSVolumeAttachment_basic --- PASS: TestAccAWSVolumeAttachment_basic (133.49s) === RUN TestAccAWSVolumeAttachment_skipDestroy --- PASS: TestAccAWSVolumeAttachment_skipDestroy (119.64s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 253.158s ``` EBS Volume Tests: ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSEBSVolume_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/11/02 01:00:18 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSEBSVolume_ -timeout 120m === RUN TestAccAWSEBSVolume_importBasic --- PASS: TestAccAWSEBSVolume_importBasic (26.38s) === RUN TestAccAWSEBSVolume_basic --- PASS: TestAccAWSEBSVolume_basic (26.86s) === RUN TestAccAWSEBSVolume_NoIops --- PASS: TestAccAWSEBSVolume_NoIops (27.89s) === RUN TestAccAWSEBSVolume_withTags --- PASS: TestAccAWSEBSVolume_withTags (26.88s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 108.032s ``` * Update volume_attachment.html.markdown
2016-11-02 16:29:37 +01:00
const testAccVolumeAttachmentConfigSkipDestroy = `
resource "aws_instance" "web" {
ami = "ami-21f78e11"
availability_zone = "us-west-2a"
instance_type = "t1.micro"
tags {
Name = "HelloWorld"
}
}
resource "aws_ebs_volume" "example" {
availability_zone = "us-west-2a"
size = 1
tags {
Name = "TestVolume"
}
}
data "aws_ebs_volume" "ebs_volume" {
filter {
name = "size"
values = ["${aws_ebs_volume.example.size}"]
}
filter {
name = "availability-zone"
values = ["${aws_ebs_volume.example.availability_zone}"]
}
filter {
name = "tag:Name"
values = ["TestVolume"]
}
}
resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = "${data.aws_ebs_volume.ebs_volume.id}"
instance_id = "${aws_instance.web.id}"
skip_destroy = true
}
`