github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/openstack/resource_openstack_compute_volume_attach_v2_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 10 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach" 11 ) 12 13 func TestAccComputeV2VolumeAttach_basic(t *testing.T) { 14 var va volumeattach.VolumeAttachment 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckComputeV2VolumeAttachDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccComputeV2VolumeAttach_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckComputeV2VolumeAttachExists("openstack_compute_volume_attach_v2.va_1", &va), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func testAccCheckComputeV2VolumeAttachDestroy(s *terraform.State) error { 32 config := testAccProvider.Meta().(*Config) 33 computeClient, err := config.computeV2Client(OS_REGION_NAME) 34 if err != nil { 35 return fmt.Errorf("Error creating OpenStack compute client: %s", err) 36 } 37 38 for _, rs := range s.RootModule().Resources { 39 if rs.Type != "openstack_compute_volume_attach_v2" { 40 continue 41 } 42 43 instanceId, volumeId, err := parseComputeVolumeAttachmentId(rs.Primary.ID) 44 if err != nil { 45 return err 46 } 47 48 _, err = volumeattach.Get(computeClient, instanceId, volumeId).Extract() 49 if err == nil { 50 return fmt.Errorf("Volume attachment still exists") 51 } 52 } 53 54 return nil 55 } 56 57 func testAccCheckComputeV2VolumeAttachExists(n string, va *volumeattach.VolumeAttachment) resource.TestCheckFunc { 58 return func(s *terraform.State) error { 59 rs, ok := s.RootModule().Resources[n] 60 if !ok { 61 return fmt.Errorf("Not found: %s", n) 62 } 63 64 if rs.Primary.ID == "" { 65 return fmt.Errorf("No ID is set") 66 } 67 68 config := testAccProvider.Meta().(*Config) 69 computeClient, err := config.computeV2Client(OS_REGION_NAME) 70 if err != nil { 71 return fmt.Errorf("Error creating OpenStack compute client: %s", err) 72 } 73 74 instanceId, volumeId, err := parseComputeVolumeAttachmentId(rs.Primary.ID) 75 if err != nil { 76 return err 77 } 78 79 found, err := volumeattach.Get(computeClient, instanceId, volumeId).Extract() 80 if err != nil { 81 return err 82 } 83 84 if found.ServerID != instanceId || found.VolumeID != volumeId { 85 return fmt.Errorf("VolumeAttach not found") 86 } 87 88 *va = *found 89 90 return nil 91 } 92 } 93 94 const testAccComputeV2VolumeAttach_basic = ` 95 resource "openstack_blockstorage_volume_v2" "volume_1" { 96 name = "volume_1" 97 size = 1 98 } 99 100 resource "openstack_compute_instance_v2" "instance_1" { 101 name = "instance_1" 102 security_groups = ["default"] 103 } 104 105 resource "openstack_compute_volume_attach_v2" "va_1" { 106 instance_id = "${openstack_compute_instance_v2.instance_1.id}" 107 volume_id = "${openstack_blockstorage_volume_v2.volume_1.id}" 108 } 109 `