Joe Topjian | 68bed5f | 2016-08-10 15:30:57 -0600 | [diff] [blame] | 1 | // Package extensions contains common functions for creating block storage |
| 2 | // resources that are extensions of the block storage API. See the `*_test.go` |
| 3 | // files for example usages. |
| 4 | package extensions |
| 5 | |
| 6 | import ( |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/gophercloud/gophercloud" |
jrperritt | 18a8bcc | 2016-10-11 15:23:03 -0500 | [diff] [blame^] | 10 | "github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumeactions" |
Joe Topjian | 68bed5f | 2016-08-10 15:30:57 -0600 | [diff] [blame] | 11 | "github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes" |
| 12 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
| 13 | ) |
| 14 | |
| 15 | // CreateVolumeAttach will attach a volume to an instance. An error will be |
| 16 | // returned if the attachment failed. |
| 17 | func CreateVolumeAttach(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.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 := volumeactions.AttachOpts{ |
| 23 | MountPoint: "/mnt", |
| 24 | Mode: "rw", |
| 25 | InstanceUUID: server.ID, |
| 26 | } |
| 27 | |
| 28 | t.Logf("Attempting to attach volume %s to server %s", volume.ID, server.ID) |
| 29 | |
| 30 | if err := volumeactions.Attach(client, volume.ID, attachOpts).ExtractErr(); err != nil { |
| 31 | return err |
| 32 | } |
| 33 | |
| 34 | if err := volumes.WaitForStatus(client, volume.ID, "in-use", 60); err != nil { |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | t.Logf("Attached volume %s to server %s", volume.ID, server.ID) |
| 39 | |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | // CreateVolumeReserve creates a volume reservation. An error will be returned |
| 44 | // if the reservation failed. |
| 45 | func CreateVolumeReserve(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) error { |
| 46 | if testing.Short() { |
| 47 | t.Skip("Skipping test that requires volume reservation in short mode.") |
| 48 | } |
| 49 | |
| 50 | t.Logf("Attempting to reserve volume %s", volume.ID) |
| 51 | |
| 52 | if err := volumeactions.Reserve(client, volume.ID).ExtractErr(); err != nil { |
| 53 | return err |
| 54 | } |
| 55 | |
| 56 | t.Logf("Reserved volume %s", volume.ID) |
| 57 | |
| 58 | return nil |
| 59 | } |
| 60 | |
| 61 | // DeleteVolumeAttach will detach a volume from an instance. A fatal error will |
| 62 | // occur if the snapshot failed to be deleted. This works best when used as a |
| 63 | // deferred function. |
| 64 | func DeleteVolumeAttach(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) { |
| 65 | t.Logf("Attepting to detach volume volume: %s", volume.ID) |
| 66 | |
| 67 | detachOpts := volumeactions.DetachOpts{ |
| 68 | AttachmentID: volume.Attachments[0].AttachmentID, |
| 69 | } |
| 70 | |
| 71 | if err := volumeactions.Detach(client, volume.ID, detachOpts).ExtractErr(); err != nil { |
| 72 | t.Fatalf("Unable to detach volume %s: %v", volume.ID, err) |
| 73 | } |
| 74 | |
| 75 | if err := volumes.WaitForStatus(client, volume.ID, "available", 60); err != nil { |
| 76 | t.Fatalf("Volume %s failed to become unavailable in 60 seconds: %v", volume.ID, err) |
| 77 | } |
| 78 | |
| 79 | t.Logf("Detached volume: %s", volume.ID) |
| 80 | } |
| 81 | |
| 82 | // DeleteVolumeReserve deletes a volume reservation. A fatal error will occur |
| 83 | // if the deletion request failed. This works best when used as a deferred |
| 84 | // function. |
| 85 | func DeleteVolumeReserve(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) { |
| 86 | if testing.Short() { |
| 87 | t.Skip("Skipping test that requires volume reservation in short mode.") |
| 88 | } |
| 89 | |
| 90 | t.Logf("Attempting to unreserve volume %s", volume.ID) |
| 91 | |
| 92 | if err := volumeactions.Unreserve(client, volume.ID).ExtractErr(); err != nil { |
| 93 | t.Fatalf("Unable to unreserve volume %s: %v", volume.ID, err) |
| 94 | } |
| 95 | |
| 96 | t.Logf("Unreserved volume %s", volume.ID) |
| 97 | } |