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