1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
package v2
import (
"fmt"
"strings"
"testing"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/messages"
"github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/shares"
)
// CreateShare will create a share with a name, and a size of 1Gb. An
// error will be returned if the share could not be created
func CreateShare(t *testing.T, client *gophercloud.ServiceClient, optShareType ...string) (*shares.Share, error) {
if testing.Short() {
t.Skip("Skipping test that requires share creation in short mode.")
}
iTrue := true
shareType := "dhss_false"
if len(optShareType) > 0 {
shareType = optShareType[0]
}
createOpts := shares.CreateOpts{
Size: 1,
Name: "My Test Share",
Description: "My Test Description",
ShareProto: "NFS",
ShareType: shareType,
IsPublic: &iTrue,
}
share, err := shares.Create(client, createOpts).Extract()
if err != nil {
t.Logf("Failed to create share")
return nil, err
}
_, err = waitForStatus(t, client, share.ID, "available")
if err != nil {
t.Logf("Failed to get %s share status", share.ID)
DeleteShare(t, client, share)
return share, err
}
return share, nil
}
// ListShares lists all shares that belong to this tenant's project.
// An error will be returned if the shares could not be listed..
func ListShares(t *testing.T, client *gophercloud.ServiceClient) ([]shares.Share, error) {
r, err := shares.ListDetail(client, &shares.ListOpts{}).AllPages()
if err != nil {
return nil, err
}
return shares.ExtractShares(r)
}
// GrantAccess will grant access to an existing share. A fatal error will occur if
// this operation fails.
func GrantAccess(t *testing.T, client *gophercloud.ServiceClient, share *shares.Share) (*shares.AccessRight, error) {
return shares.GrantAccess(client, share.ID, shares.GrantAccessOpts{
AccessType: "ip",
AccessTo: "0.0.0.0/32",
AccessLevel: "ro",
}).Extract()
}
// RevokeAccess will revoke an exisiting access of a share. A fatal error will occur
// if this operation fails.
func RevokeAccess(t *testing.T, client *gophercloud.ServiceClient, share *shares.Share, accessRight *shares.AccessRight) error {
return shares.RevokeAccess(client, share.ID, shares.RevokeAccessOpts{
AccessID: accessRight.ID,
}).ExtractErr()
}
// GetAccessRightsSlice will retrieve all access rules assigned to a share.
// A fatal error will occur if this operation fails.
func GetAccessRightsSlice(t *testing.T, client *gophercloud.ServiceClient, share *shares.Share) ([]shares.AccessRight, error) {
return shares.ListAccessRights(client, share.ID).Extract()
}
// DeleteShare will delete a share. A fatal error will occur if the share
// failed to be deleted. This works best when used as a deferred function.
func DeleteShare(t *testing.T, client *gophercloud.ServiceClient, share *shares.Share) {
err := shares.Delete(client, share.ID).ExtractErr()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
return
}
t.Errorf("Unable to delete share %s: %v", share.ID, err)
}
_, err = waitForStatus(t, client, share.ID, "deleted")
if err != nil {
t.Errorf("Failed to wait for 'deleted' status for %s share: %v", share.ID, err)
} else {
t.Logf("Deleted share: %s", share.ID)
}
}
// ExtendShare extends the capacity of an existing share
func ExtendShare(t *testing.T, client *gophercloud.ServiceClient, share *shares.Share, newSize int) error {
return shares.Extend(client, share.ID, &shares.ExtendOpts{NewSize: newSize}).ExtractErr()
}
// ShrinkShare shrinks the capacity of an existing share
func ShrinkShare(t *testing.T, client *gophercloud.ServiceClient, share *shares.Share, newSize int) error {
return shares.Shrink(client, share.ID, &shares.ShrinkOpts{NewSize: newSize}).ExtractErr()
}
func PrintMessages(t *testing.T, c *gophercloud.ServiceClient, id string) error {
c.Microversion = "2.37"
allPages, err := messages.List(c, messages.ListOpts{ResourceID: id}).AllPages()
if err != nil {
return fmt.Errorf("Unable to retrieve messages: %v", err)
}
allMessages, err := messages.ExtractMessages(allPages)
if err != nil {
return fmt.Errorf("Unable to extract messages: %v", err)
}
for _, message := range allMessages {
tools.PrintResource(t, message)
}
return nil
}
func waitForStatus(t *testing.T, c *gophercloud.ServiceClient, id, status string) (*shares.Share, error) {
var current *shares.Share
err := tools.WaitFor(func() (bool, error) {
var err error
current, err = shares.Get(c, id).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
switch status {
case "deleted":
return true, nil
default:
return false, err
}
}
return false, err
}
if current.Status == status {
return true, nil
}
if strings.Contains(current.Status, "error") {
return true, fmt.Errorf("An error occurred, wrong status: %s", current.Status)
}
return false, nil
})
if err != nil {
mErr := PrintMessages(t, c, id)
if mErr != nil {
return current, fmt.Errorf("Share status is '%s' and unable to get manila messages: %s", err, mErr)
}
}
return current, err
}
|