Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 1cc1c84

Browse files
committed
code styling, paging fix
1 parent 44e3b54 commit 1cc1c84

File tree

5 files changed

+61
-25
lines changed

5 files changed

+61
-25
lines changed

openstack/identity/v3/roles/requests.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ import (
55
"github.com/rackspace/gophercloud/pagination"
66
)
77

8-
// RoleAssignmentsOpts allows you to query the RoleAssignments method.
9-
type RoleAssignmentsOpts struct {
8+
// ListAssignmentsOptsBuilder allows extensions to add additional parameters to
9+
// the ListAssignments request.
10+
type ListAssignmentsOptsBuilder interface {
11+
ToRolesListAssignmentsQuery() (string, error)
12+
}
13+
14+
// ListAssignmentsOpts allows you to query the ListAssignments method.
15+
// Specify one of or a combination of GroupId, RoleId, ScopeDomainId, ScopeProjectId,
16+
// and/or UserId to search for roles assigned to corresponding entities.
17+
// Effective lists effective assignments at the user, project, and domain level,
18+
// allowing for the effects of group membership.
19+
type ListAssignmentsOpts struct {
1020
GroupId string `q:"group.id"`
1121
RoleId string `q:"role.id"`
1222
ScopeDomainId string `q:"scope.domain.id"`
@@ -15,17 +25,26 @@ type RoleAssignmentsOpts struct {
1525
Effective bool `q:"effective"`
1626
}
1727

18-
// RoleAssignments enumerates the roles assigned to a specified resource.
19-
func RoleAssignments(client *gophercloud.ServiceClient, opts RoleAssignmentsOpts) pagination.Pager {
20-
u := roleAssignmentsURL(client)
28+
// ToRolesListAssignmentsQuery formats a ListAssignmentsOpts into a query string.
29+
func (opts ListAssignmentsOpts) ToRolesListAssignmentsQuery() (string, error) {
2130
q, err := gophercloud.BuildQueryString(opts)
31+
if err != nil {
32+
return "", err
33+
}
34+
return q.String(), nil
35+
}
36+
37+
// ListAssignments enumerates the roles assigned to a specified resource.
38+
func ListAssignments(client *gophercloud.ServiceClient, opts ListAssignmentsOptsBuilder) pagination.Pager {
39+
url := listAssignmentsURL(client)
40+
query, err := opts.ToRolesListAssignmentsQuery()
2241
if err != nil {
2342
return pagination.Pager{Err: err}
2443
}
25-
u += q.String()
44+
url += query
2645
createPage := func(r pagination.PageResult) pagination.Page {
2746
return RoleAssignmentsPage{pagination.LinkedPageBase{PageResult: r}}
2847
}
2948

30-
return pagination.NewPager(client, u, createPage)
49+
return pagination.NewPager(client, url, createPage)
3150
}

openstack/identity/v3/roles/requests_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestListSinglePage(t *testing.T) {
6767
})
6868

6969
count := 0
70-
err := RoleAssignments(client.ServiceClient(), RoleAssignmentsOpts{}).EachPage(func(page pagination.Page) (bool, error) {
70+
err := ListAssignments(client.ServiceClient(), ListAssignmentsOpts{}).EachPage(func(page pagination.Page) (bool, error) {
7171
count++
7272
actual, err := ExtractRoleAssignments(page)
7373
if err != nil {
@@ -76,16 +76,16 @@ func TestListSinglePage(t *testing.T) {
7676

7777
expected := []RoleAssignment{
7878
RoleAssignment{
79-
Role: &Role{ID: "123456"},
80-
Scope: &Scope{Domain: &Domain{ID: "161718"}},
81-
User: &User{ID: "313233"},
82-
Group: nil,
79+
Role: Role{ID: "123456"},
80+
Scope: Scope{Domain: Domain{ID: "161718"}},
81+
User: User{ID: "313233"},
82+
Group: Group{},
8383
},
8484
RoleAssignment{
85-
Role: &Role{ID: "123456"},
86-
Scope: &Scope{Project: &Project{ID: "456789"}},
87-
User: &User{ID: "313233"},
88-
Group: nil,
85+
Role: Role{ID: "123456"},
86+
Scope: Scope{Project: Project{ID: "456789"}},
87+
User: User{ID: "313233"},
88+
Group: Group{},
8989
},
9090
}
9191

openstack/identity/v3/roles/results.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ import (
88

99
// RoleAssignment is the result of a role assignments query.
1010
type RoleAssignment struct {
11-
Role *Role `json:"role,omitempty"`
12-
Scope *Scope `json:"scope,omitempty"`
13-
User *User `json:"user,omitempty"`
14-
Group *Group `json:"group,omitempty"`
11+
Role Role `json:"role,omitempty"`
12+
Scope Scope `json:"scope,omitempty"`
13+
User User `json:"user,omitempty"`
14+
Group Group `json:"group,omitempty"`
1515
}
1616

1717
type Role struct {
1818
ID string `json:"id,omitempty"`
1919
}
2020

2121
type Scope struct {
22-
Domain *Domain `json:"domain,omitempty"`
23-
Project *Project `json:"domain,omitempty"`
22+
Domain Domain `json:"domain,omitempty"`
23+
Project Project `json:"domain,omitempty"`
2424
}
2525

2626
type Domain struct {
@@ -53,6 +53,23 @@ func (p RoleAssignmentsPage) IsEmpty() (bool, error) {
5353
return len(roleAssignments) == 0, nil
5454
}
5555

56+
// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
57+
func (page RoleAssignmentsPage) NextPageURL() (string, error) {
58+
type resp struct {
59+
Links struct {
60+
Next string `mapstructure:"next"`
61+
} `mapstructure:"links"`
62+
}
63+
64+
var r resp
65+
err := mapstructure.Decode(page.Body, &r)
66+
if err != nil {
67+
return "", err
68+
}
69+
70+
return r.Links.Next, nil
71+
}
72+
5673
// ExtractRoleAssignments extracts a slice of RoleAssignments from a Collection acquired from List.
5774
func ExtractRoleAssignments(page pagination.Page) ([]RoleAssignment, error) {
5875
var response struct {

openstack/identity/v3/roles/urls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package roles
22

33
import "github.com/rackspace/gophercloud"
44

5-
func roleAssignmentsURL(client *gophercloud.ServiceClient) string {
5+
func listAssignmentsURL(client *gophercloud.ServiceClient) string {
66
return client.ServiceURL("role_assignments")
77
}

openstack/identity/v3/roles/urls_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"github.com/rackspace/gophercloud"
77
)
88

9-
func TestRoleAssignmentsURL(t *testing.T) {
9+
func TestListAssignmentsURL(t *testing.T) {
1010
client := gophercloud.ServiceClient{Endpoint: "http://localhost:5000/v3/"}
11-
url := roleAssignmentsURL(&client)
11+
url := listAssignmentsURL(&client)
1212
if url != "http://localhost:5000/v3/role_assignments" {
1313
t.Errorf("Unexpected list URL generated: [%s]", url)
1414
}

0 commit comments

Comments
 (0)