github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/internal/acceptance/openstack/blockstorage/v3/volumeattachments.go (about)

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