github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/openstack/resource_openstack_blockstorage_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"
    11  	"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
    12  )
    13  
    14  func TestAccBlockStorageVolumeAttachV2_basic(t *testing.T) {
    15  	var va volumes.Attachment
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckBlockStorageVolumeAttachV2Destroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccBlockStorageVolumeAttachV2_basic,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckBlockStorageVolumeAttachV2Exists("openstack_blockstorage_volume_attach_v2.va_1", &va),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func testAccCheckBlockStorageVolumeAttachV2Destroy(s *terraform.State) error {
    33  	config := testAccProvider.Meta().(*Config)
    34  	client, err := config.blockStorageV2Client(OS_REGION_NAME)
    35  	if err != nil {
    36  		return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
    37  	}
    38  
    39  	for _, rs := range s.RootModule().Resources {
    40  		if rs.Type != "openstack_blockstorage_volume_attach_v2" {
    41  			continue
    42  		}
    43  
    44  		volumeId, attachmentId, err := blockStorageVolumeAttachV2ParseId(rs.Primary.ID)
    45  		if err != nil {
    46  			return err
    47  		}
    48  
    49  		volume, err := volumes.Get(client, volumeId).Extract()
    50  		if err != nil {
    51  			if _, ok := err.(gophercloud.ErrDefault404); ok {
    52  				return nil
    53  			}
    54  			return err
    55  		}
    56  
    57  		for _, v := range volume.Attachments {
    58  			if attachmentId == v.AttachmentID {
    59  				return fmt.Errorf("Volume attachment still exists")
    60  			}
    61  		}
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  func testAccCheckBlockStorageVolumeAttachV2Exists(n string, va *volumes.Attachment) resource.TestCheckFunc {
    68  	return func(s *terraform.State) error {
    69  		rs, ok := s.RootModule().Resources[n]
    70  		if !ok {
    71  			return fmt.Errorf("Not found: %s", n)
    72  		}
    73  
    74  		if rs.Primary.ID == "" {
    75  			return fmt.Errorf("No ID is set")
    76  		}
    77  
    78  		config := testAccProvider.Meta().(*Config)
    79  		client, err := config.blockStorageV2Client(OS_REGION_NAME)
    80  		if err != nil {
    81  			return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
    82  		}
    83  
    84  		volumeId, attachmentId, err := blockStorageVolumeAttachV2ParseId(rs.Primary.ID)
    85  		if err != nil {
    86  			return err
    87  		}
    88  
    89  		volume, err := volumes.Get(client, volumeId).Extract()
    90  		if err != nil {
    91  			return err
    92  		}
    93  
    94  		var found bool
    95  		for _, v := range volume.Attachments {
    96  			if attachmentId == v.AttachmentID {
    97  				found = true
    98  				*va = v
    99  			}
   100  		}
   101  
   102  		if !found {
   103  			return fmt.Errorf("Volume Attachment not found")
   104  		}
   105  
   106  		return nil
   107  	}
   108  }
   109  
   110  const testAccBlockStorageVolumeAttachV2_basic = `
   111  resource "openstack_blockstorage_volume_v2" "volume_1" {
   112    name = "volume_1"
   113    size = 1
   114  }
   115  
   116  resource "openstack_blockstorage_volume_attach_v2" "va_1" {
   117    volume_id = "${openstack_blockstorage_volume_v2.volume_1.id}"
   118    device = "auto"
   119  
   120    host_name = "devstack"
   121    ip_address = "192.168.255.10"
   122    initiator = "iqn.1993-08.org.debian:01:e9861fb1859"
   123    os_type = "linux2"
   124    platform = "x86_64"
   125  }
   126  `