2015-05-23 00:02:05 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2015-05-23 00:02:05 +02:00
|
|
|
"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{
|
|
|
|
resource.TestStep{
|
|
|
|
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]
|
|
|
|
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 {
|
2015-08-17 20:27:16 +02:00
|
|
|
if b.Ebs.VolumeId != nil && rs.Primary.Attributes["volume_id"] == *b.Ebs.VolumeId {
|
2015-05-23 00:02:05 +02:00
|
|
|
// 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"
|
2015-05-28 17:13:46 +02:00
|
|
|
size = 1
|
2015-05-23 00:02:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_volume_attachment" "ebs_att" {
|
|
|
|
device_name = "/dev/sdh"
|
|
|
|
volume_id = "${aws_ebs_volume.example.id}"
|
|
|
|
instance_id = "${aws_instance.web.id}"
|
|
|
|
}
|
|
|
|
`
|