Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3002039

Browse filesBrowse files
authored
Merge pull request #2496 from gman0/shareaccessrules-get
Manila: add Get for share-access-rules API
2 parents 92af431 + 41a6bc5 commit 3002039
Copy full SHA for 3002039

File tree

7 files changed

+238
-0
lines changed
Filter options

7 files changed

+238
-0
lines changed
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package v2
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gophercloud/gophercloud"
7+
"github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/shareaccessrules"
8+
)
9+
10+
func ShareAccessRuleGet(t *testing.T, client *gophercloud.ServiceClient, accessID string) (*shareaccessrules.ShareAccess, error) {
11+
accessRule, err := shareaccessrules.Get(client, accessID).Extract()
12+
if err != nil {
13+
t.Logf("Failed to get share access rule %s: %v", accessID, err)
14+
return nil, err
15+
}
16+
17+
return accessRule, nil
18+
}
+48Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//go:build acceptance
2+
// +build acceptance
3+
4+
package v2
5+
6+
import (
7+
"testing"
8+
9+
"github.com/gophercloud/gophercloud/acceptance/clients"
10+
"github.com/gophercloud/gophercloud/acceptance/tools"
11+
th "github.com/gophercloud/gophercloud/testhelper"
12+
)
13+
14+
func TestShareAccessRulesGet(t *testing.T) {
15+
client, err := clients.NewSharedFileSystemV2Client()
16+
if err != nil {
17+
t.Fatalf("Unable to create a shared file system client: %v", err)
18+
}
19+
20+
client.Microversion = "2.49"
21+
22+
share, err := CreateShare(t, client)
23+
if err != nil {
24+
t.Fatalf("Unable to create a share: %v", err)
25+
}
26+
27+
defer DeleteShare(t, client, share)
28+
29+
shareAccessRight, err := GrantAccess(t, client, share)
30+
if err != nil {
31+
t.Fatalf("Unable to grant access to share %s: %v", share.ID, err)
32+
}
33+
34+
accessRule, err := ShareAccessRuleGet(t, client, shareAccessRight.ID)
35+
if err != nil {
36+
t.Logf("Unable to get share access rule for share %s: %v", share.ID, err)
37+
}
38+
39+
tools.PrintResource(t, accessRule)
40+
41+
th.AssertEquals(t, shareAccessRight.ID, accessRule.ID)
42+
th.AssertEquals(t, shareAccessRight.ShareID, accessRule.ShareID)
43+
th.AssertEquals(t, shareAccessRight.AccessType, accessRule.AccessType)
44+
th.AssertEquals(t, shareAccessRight.AccessLevel, accessRule.AccessLevel)
45+
th.AssertEquals(t, shareAccessRight.AccessTo, accessRule.AccessTo)
46+
th.AssertEquals(t, shareAccessRight.AccessKey, accessRule.AccessKey)
47+
th.AssertEquals(t, shareAccessRight.State, accessRule.State)
48+
}
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package shareaccessrules
2+
3+
import (
4+
"github.com/gophercloud/gophercloud"
5+
)
6+
7+
// Get retrieves details about a share access rule.
8+
func Get(client *gophercloud.ServiceClient, accessID string) (r GetResult) {
9+
resp, err := client.Get(getURL(client, accessID), &r.Body, nil)
10+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
11+
return
12+
}
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package shareaccessrules
2+
3+
import (
4+
"encoding/json"
5+
"time"
6+
7+
"github.com/gophercloud/gophercloud"
8+
)
9+
10+
// ShareAccess contains information associated with an OpenStack share access rule.
11+
type ShareAccess struct {
12+
// The UUID of the share to which you are granted or denied access.
13+
ShareID string `json:"share_id"`
14+
// The date and time stamp when the resource was created within the service’s database.
15+
CreatedAt time.Time `json:"-"`
16+
// The date and time stamp when the resource was last updated within the service’s database.
17+
UpdatedAt time.Time `json:"-"`
18+
// The access rule type.
19+
AccessType string `json:"access_type"`
20+
// The value that defines the access. The back end grants or denies the access to it.
21+
AccessTo string `json:"access_to"`
22+
// The access credential of the entity granted share access.
23+
AccessKey string `json:"access_key"`
24+
// The state of the access rule.
25+
State string `json:"state"`
26+
// The access level to the share.
27+
AccessLevel string `json:"access_level"`
28+
// The access rule ID.
29+
ID string `json:"id"`
30+
// Access rule metadata.
31+
Metadata map[string]interface{} `json:"metadata"`
32+
}
33+
34+
func (r *ShareAccess) UnmarshalJSON(b []byte) error {
35+
type tmp ShareAccess
36+
var s struct {
37+
tmp
38+
CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
39+
UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
40+
}
41+
err := json.Unmarshal(b, &s)
42+
if err != nil {
43+
return err
44+
}
45+
*r = ShareAccess(s.tmp)
46+
47+
r.CreatedAt = time.Time(s.CreatedAt)
48+
r.UpdatedAt = time.Time(s.UpdatedAt)
49+
50+
return nil
51+
}
52+
53+
// GetResult contains the response body and error from a Get request.
54+
type GetResult struct {
55+
gophercloud.Result
56+
}
57+
58+
// Extract will get the ShareAccess object from the GetResult.
59+
func (r GetResult) Extract() (*ShareAccess, error) {
60+
var s struct {
61+
ShareAccess *ShareAccess `json:"access"`
62+
}
63+
err := r.ExtractInto(&s)
64+
return s.ShareAccess, err
65+
}
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package testing
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
th "github.com/gophercloud/gophercloud/testhelper"
9+
fake "github.com/gophercloud/gophercloud/testhelper/client"
10+
)
11+
12+
const (
13+
shareAccessRulesEndpoint = "/share-access-rules"
14+
shareAccessRuleID = "507bf114-36f2-4f56-8cf4-857985ca87c1"
15+
shareID = "fb213952-2352-41b4-ad7b-2c4c69d13eef"
16+
)
17+
18+
var getResponse = `{
19+
"access": {
20+
"access_level": "rw",
21+
"state": "error",
22+
"id": "507bf114-36f2-4f56-8cf4-857985ca87c1",
23+
"share_id": "fb213952-2352-41b4-ad7b-2c4c69d13eef",
24+
"access_type": "cert",
25+
"access_to": "example.com",
26+
"access_key": null,
27+
"created_at": "2018-07-17T02:01:04.000000",
28+
"updated_at": "2018-07-17T02:01:04.000000",
29+
"metadata": {
30+
"key1": "value1",
31+
"key2": "value2"
32+
}
33+
}
34+
}`
35+
36+
func MockGetResponse(t *testing.T) {
37+
th.Mux.HandleFunc(shareAccessRulesEndpoint+"/"+shareAccessRuleID, func(w http.ResponseWriter, r *http.Request) {
38+
th.TestMethod(t, r, "GET")
39+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
40+
th.TestHeader(t, r, "Accept", "application/json")
41+
w.Header().Add("Content-Type", "application/json")
42+
w.WriteHeader(http.StatusOK)
43+
fmt.Fprintf(w, getResponse)
44+
})
45+
}
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package testing
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/shareaccessrules"
8+
th "github.com/gophercloud/gophercloud/testhelper"
9+
"github.com/gophercloud/gophercloud/testhelper/client"
10+
)
11+
12+
func TestGet(t *testing.T) {
13+
th.SetupHTTP()
14+
defer th.TeardownHTTP()
15+
16+
MockGetResponse(t)
17+
18+
resp := shareaccessrules.Get(client.ServiceClient(), "507bf114-36f2-4f56-8cf4-857985ca87c1")
19+
th.AssertNoErr(t, resp.Err)
20+
21+
accessRule, err := resp.Extract()
22+
th.AssertNoErr(t, err)
23+
24+
th.AssertDeepEquals(t, &shareaccessrules.ShareAccess{
25+
ShareID: "fb213952-2352-41b4-ad7b-2c4c69d13eef",
26+
CreatedAt: time.Date(2018, 7, 17, 2, 1, 4, 0, time.UTC),
27+
UpdatedAt: time.Date(2018, 7, 17, 2, 1, 4, 0, time.UTC),
28+
AccessType: "cert",
29+
AccessTo: "example.com",
30+
AccessKey: "",
31+
State: "error",
32+
AccessLevel: "rw",
33+
ID: "507bf114-36f2-4f56-8cf4-857985ca87c1",
34+
Metadata: map[string]interface{}{
35+
"key1": "value1",
36+
"key2": "value2",
37+
},
38+
}, accessRule)
39+
}
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package shareaccessrules
2+
3+
import (
4+
"github.com/gophercloud/gophercloud"
5+
)
6+
7+
const shareAccessRulesEndpoint = "share-access-rules"
8+
9+
func getURL(c *gophercloud.ServiceClient, accessID string) string {
10+
return c.ServiceURL(shareAccessRulesEndpoint, accessID)
11+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.