github.com/leeclow-ops/gophercloud@v1.2.1/acceptance/openstack/blockstorage/v3/volumeattachments.go (about) 1 package v3 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/leeclow-ops/gophercloud" 8 "github.com/leeclow-ops/gophercloud/openstack/blockstorage/v3/attachments" 9 v3 "github.com/leeclow-ops/gophercloud/openstack/blockstorage/v3/volumes" 10 "github.com/leeclow-ops/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 Connector: map[string]interface{}{ 24 "mode": "rw", 25 "initiator": "fake", 26 }, 27 } 28 29 t.Logf("Attempting to attach volume %s to server %s", volume.ID, server.ID) 30 31 var err error 32 var attachment *attachments.Attachment 33 if attachment, err = attachments.Create(client, attachOpts).Extract(); err != nil { 34 return err 35 } 36 37 mv := client.Microversion 38 client.Microversion = "3.44" 39 defer func() { 40 client.Microversion = mv 41 }() 42 if err = attachments.Complete(client, attachment.ID).ExtractErr(); err != nil { 43 return err 44 } 45 46 if err = attachments.WaitForStatus(client, attachment.ID, "attached", 60); err != nil { 47 e := attachments.Delete(client, attachment.ID).ExtractErr() 48 if e != nil { 49 t.Logf("Failed to delete %q attachment: %s", attachment.ID, err) 50 } 51 return err 52 } 53 54 attachment, err = attachments.Get(client, attachment.ID).Extract() 55 if err != nil { 56 return err 57 } 58 59 /* 60 // Not clear how perform a proper update, OpenStack returns "Unable to update the attachment." 61 updateOpts := &attachments.UpdateOpts{ 62 Connector: map[string]interface{}{ 63 "mode": "ro", 64 "initiator": "fake", 65 }, 66 } 67 attachment, err = attachments.Update(client, attachment.ID, updateOpts).Extract() 68 if err != nil { 69 return err 70 } 71 */ 72 73 listOpts := &attachments.ListOpts{ 74 VolumeID: volume.ID, 75 InstanceID: server.ID, 76 } 77 allPages, err := attachments.List(client, listOpts).AllPages() 78 if err != nil { 79 return err 80 } 81 82 allAttachments, err := attachments.ExtractAttachments(allPages) 83 if err != nil { 84 return err 85 } 86 87 if allAttachments[0].ID != attachment.ID { 88 return fmt.Errorf("Attachment IDs from get and list are not equal: %q != %q", allAttachments[0].ID, attachment.ID) 89 } 90 91 t.Logf("Attached volume %s to server %s within %q attachment", volume.ID, server.ID, attachment.ID) 92 93 return nil 94 } 95 96 // DeleteVolumeAttachment will detach a volume from an instance. A fatal error 97 // will occur if the attachment failed to be deleted. 98 func DeleteVolumeAttachment(t *testing.T, client *gophercloud.ServiceClient, volume *v3.Volume) { 99 t.Logf("Attepting to detach volume volume: %s", volume.ID) 100 101 if err := attachments.Delete(client, volume.Attachments[0].AttachmentID).ExtractErr(); err != nil { 102 t.Fatalf("Unable to detach volume %s: %v", volume.ID, err) 103 } 104 105 if err := v3.WaitForStatus(client, volume.ID, "available", 60); err != nil { 106 t.Fatalf("Volume %s failed to become unavailable in 60 seconds: %v", volume.ID, err) 107 } 108 109 t.Logf("Detached volume: %s", volume.ID) 110 }