Mikko Valkonen | 9368c00 | 2017-01-16 18:31:39 +0200 | [diff] [blame] | 1 | package v2 |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "github.com/gophercloud/gophercloud" |
| 6 | "github.com/gophercloud/gophercloud/acceptance/clients" |
| 7 | "github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/shares" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // CreateShare will create a share with a name, and a size of 1Gb. An |
| 12 | // error will be returned if the share could not be created |
| 13 | func CreateShare(t *testing.T, client *gophercloud.ServiceClient) (*shares.Share, error) { |
| 14 | if testing.Short() { |
| 15 | t.Skip("Skipping test that requres share creation in short mode.") |
| 16 | } |
| 17 | |
| 18 | choices, err := clients.AcceptanceTestChoicesFromEnv() |
| 19 | if err != nil { |
| 20 | t.Fatalf("Unable to fetch environment information") |
| 21 | } |
| 22 | |
| 23 | t.Logf("Share network id %s", choices.ShareNetworkID) |
| 24 | createOpts := shares.CreateOpts{ |
| 25 | Size: 1, |
| 26 | Name: "My Test Share", |
| 27 | ShareProto: "NFS", |
| 28 | ShareNetworkID: choices.ShareNetworkID, |
| 29 | } |
| 30 | |
| 31 | share, err := shares.Create(client, createOpts).Extract() |
| 32 | if err != nil { |
| 33 | return share, err |
| 34 | } |
| 35 | |
| 36 | err = waitForStatus(client, share.ID, "available", 60) |
| 37 | if err != nil { |
| 38 | return share, err |
| 39 | } |
| 40 | |
| 41 | return share, nil |
| 42 | } |
| 43 | |
| 44 | // DeleteShare will delete a share. A fatal error will occur if the share |
| 45 | // failed to be deleted. This works best when used as a deferred function. |
| 46 | func DeleteShare(t *testing.T, client *gophercloud.ServiceClient, share *shares.Share) { |
| 47 | err := shares.Delete(client, share.ID).ExtractErr() |
| 48 | if err != nil { |
| 49 | t.Fatalf("Unable to delete share %s: %v", share.ID, err) |
| 50 | } |
| 51 | |
| 52 | t.Logf("Deleted share: %s", share.ID) |
| 53 | } |
| 54 | |
| 55 | // PrintShare prints some information of the share |
| 56 | func PrintShare(t *testing.T, share *shares.Share) { |
| 57 | asJSON, err := json.MarshalIndent(share, "", " ") |
| 58 | if err != nil { |
| 59 | t.Logf("Cannot print the contents of %s", share.ID) |
| 60 | } |
| 61 | |
| 62 | t.Logf("Share %s", string(asJSON)) |
| 63 | } |
| 64 | |
| 65 | func waitForStatus(c *gophercloud.ServiceClient, id, status string, secs int) error { |
| 66 | return gophercloud.WaitFor(secs, func() (bool, error) { |
| 67 | current, err := shares.Get(c, id).Extract() |
| 68 | if err != nil { |
| 69 | return false, err |
| 70 | } |
| 71 | |
| 72 | if current.Status == status { |
| 73 | return true, nil |
| 74 | } |
| 75 | |
| 76 | return false, nil |
| 77 | }) |
| 78 | } |