github.com/gophercloud/gophercloud@v1.11.0/internal/acceptance/openstack/blockstorage/v3/volumeattachments.go (about)

     1  package v3
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/attachments"
     9  	v3 "github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
    10  	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
    11  )
    12  
    13  // CreateVolumeAttachment will attach a volume to an instance. An error will be
    14  // returned if the attachment failed.
    15  func CreateVolumeAttachment(t *testing.T, client *gophercloud.ServiceClient, volume *v3.Volume, server *servers.Server) error {
    16  	if testing.Short() {
    17  		t.Skip("Skipping test that requires volume attachment in short mode.")
    18  	}
    19  
    20  	attachOpts := &attachments.CreateOpts{
    21  		VolumeUUID:   volume.ID,
    22  		InstanceUUID: server.ID,
    23  	}
    24  
    25  	t.Logf("Attempting to attach volume %s to server %s", volume.ID, server.ID)
    26  
    27  	var err error
    28  	var attachment *attachments.Attachment
    29  	if attachment, err = attachments.Create(client, attachOpts).Extract(); err != nil {
    30  		return err
    31  	}
    32  
    33  	mv := client.Microversion
    34  	client.Microversion = "3.44"
    35  	defer func() {
    36  		client.Microversion = mv
    37  	}()
    38  	if err = attachments.Complete(client, attachment.ID).ExtractErr(); err != nil {
    39  		return err
    40  	}
    41  
    42  	if err = attachments.WaitForStatus(client, attachment.ID, "attached", 60); err != nil {
    43  		e := attachments.Delete(client, attachment.ID).ExtractErr()
    44  		if e != nil {
    45  			t.Logf("Failed to delete %q attachment: %s", attachment.ID, err)
    46  		}
    47  		return err
    48  	}
    49  
    50  	attachment, err = attachments.Get(client, attachment.ID).Extract()
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	listOpts := &attachments.ListOpts{
    56  		VolumeID:   volume.ID,
    57  		InstanceID: server.ID,
    58  	}
    59  	allPages, err := attachments.List(client, listOpts).AllPages()
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	allAttachments, err := attachments.ExtractAttachments(allPages)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	if allAttachments[0].ID != attachment.ID {
    70  		return fmt.Errorf("Attachment IDs from get and list are not equal: %q != %q", allAttachments[0].ID, attachment.ID)
    71  	}
    72  
    73  	t.Logf("Attached volume %s to server %s within %q attachment", volume.ID, server.ID, attachment.ID)
    74  
    75  	return nil
    76  }
    77  
    78  // DeleteVolumeAttachment will detach a volume from an instance. A fatal error
    79  // will occur if the attachment failed to be deleted.
    80  func DeleteVolumeAttachment(t *testing.T, client *gophercloud.ServiceClient, volume *v3.Volume) {
    81  	t.Logf("Attepting to detach volume volume: %s", volume.ID)
    82  
    83  	if err := attachments.Delete(client, volume.Attachments[0].AttachmentID).ExtractErr(); err != nil {
    84  		t.Fatalf("Unable to detach volume %s: %v", volume.ID, err)
    85  	}
    86  
    87  	if err := v3.WaitForStatus(client, volume.ID, "available", 60); err != nil {
    88  		t.Fatalf("Volume %s failed to become unavailable in 60 seconds: %v", volume.ID, err)
    89  	}
    90  
    91  	t.Logf("Detached volume: %s", volume.ID)
    92  }