Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 28 additions & 31 deletions openstack/identity/v3/roles/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package roles

import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack"
"github.com/huaweicloud/golangsdk/pagination"
)

Expand Down Expand Up @@ -38,19 +37,9 @@ func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Page
url += query
}

h, err := openstack.HeaderForAdminToken(client)
if err != nil {
return pagination.Pager{Err: err}
}

pager := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return RolePage{pagination.LinkedPageBase{PageResult: r}}
})

if h != nil {
pager.Headers = h
}
return pager
}

// Get retrieves details on a single role, by ID.
Expand Down Expand Up @@ -163,7 +152,7 @@ func Delete(client *golangsdk.ServiceClient, roleID string) (r DeleteResult) {
// ListAssignmentsOptsBuilder allows extensions to add additional parameters to
// the ListAssignments request.
type ListAssignmentsOptsBuilder interface {
ToRolesListAssignmentsQuery() (string, error)
extractAssignment() (string, string, string, string, error)
}

// ListAssignmentsOpts allows you to query the ListAssignments method.
Expand All @@ -174,9 +163,6 @@ type ListAssignmentsOpts struct {
// GroupID is the group ID to query.
GroupID string `q:"group.id"`

// RoleID is the specific role to query assignments to.
RoleID string `q:"role.id"`

// ScopeDomainID filters the results by the given domain ID.
ScopeDomainID string `q:"scope.domain.id"`

Expand All @@ -185,28 +171,39 @@ type ListAssignmentsOpts struct {

// UserID filterst he results by the given User ID.
UserID string `q:"user.id"`

// Effective lists effective assignments at the user, project, and domain
// level, allowing for the effects of group membership.
Effective *bool `q:"effective"`
}

// ToRolesListAssignmentsQuery formats a ListAssignmentsOpts into a query string.
func (opts ListAssignmentsOpts) ToRolesListAssignmentsQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
return q.String(), err
func (opts ListAssignmentsOpts) extractAssignment() (string, string, string, string, error) {
// Get corresponding URL
var targetID string
var targetType string
if opts.ScopeProjectID != "" {
targetID = opts.ScopeProjectID
targetType = "projects"
} else {
targetID = opts.ScopeDomainID
targetType = "domains"
}

var actorID string
var actorType string
if opts.UserID != "" {
actorID = opts.UserID
actorType = "users"
} else {
actorID = opts.GroupID
actorType = "groups"
}

return targetType, targetID, actorType, actorID, nil
}

// ListAssignments enumerates the roles assigned to a specified resource.
func ListAssignments(client *golangsdk.ServiceClient, opts ListAssignmentsOptsBuilder) pagination.Pager {
url := listAssignmentsURL(client)
if opts != nil {
query, err := opts.ToRolesListAssignmentsQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
targetType, targetID, actorType, actorID, _ := opts.extractAssignment()

url := listAssignmentsURL(client, targetType, targetID, actorType, actorID)
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return RoleAssignmentPage{pagination.LinkedPageBase{PageResult: r}}
})
Expand Down
32 changes: 31 additions & 1 deletion openstack/identity/v3/roles/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,37 @@ var ExpectedRoleAssignmentsSlice = []roles.RoleAssignment{FirstRoleAssignment, S
// HandleListRoleAssignmentsSuccessfully creates an HTTP handler at `/role_assignments` on the
// test handler mux that responds with a list of two role assignments.
func HandleListRoleAssignmentsSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/role_assignments", func(w http.ResponseWriter, r *http.Request) {
th.Mux.HandleFunc("/domains/{domain_id}/groups/{group_id}/roles", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "Accept", "application/json")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, ListAssignmentOutput)
})

th.Mux.HandleFunc("/projects/{project_id}/groups/{group_id}/roles", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "Accept", "application/json")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, ListAssignmentOutput)
})

th.Mux.HandleFunc("/projects/{project_id}/users/{user_id}/roles", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "Accept", "application/json")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, ListAssignmentOutput)
})

th.Mux.HandleFunc("/domains/{domain_id}/users/{user_id}/roles", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "Accept", "application/json")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Expand Down
19 changes: 0 additions & 19 deletions openstack/identity/v3/roles/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,6 @@ func TestDeleteRole(t *testing.T) {
th.AssertNoErr(t, res.Err)
}

func TestListAssignmentsSinglePage(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleListRoleAssignmentsSuccessfully(t)

count := 0
err := roles.ListAssignments(client.ServiceClient(), roles.ListAssignmentsOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := roles.ExtractRoleAssignments(page)
th.AssertNoErr(t, err)

th.CheckDeepEquals(t, ExpectedRoleAssignmentsSlice, actual)

return true, nil
})
th.AssertNoErr(t, err)
th.CheckEquals(t, count, 1)
}

func TestAssign(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
Expand Down
4 changes: 2 additions & 2 deletions openstack/identity/v3/roles/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func deleteURL(client *golangsdk.ServiceClient, roleID string) string {
return client.ServiceURL(rolePath, roleID)
}

func listAssignmentsURL(client *golangsdk.ServiceClient) string {
return client.ServiceURL("role_assignments")
func listAssignmentsURL(client *golangsdk.ServiceClient, targetType, targetID, actorType, actorID string) string {
return client.ServiceURL(targetType, targetID, actorType, actorID, rolePath)
}

func assignURL(client *golangsdk.ServiceClient, targetType, targetID, actorType, actorID, roleID string) string {
Expand Down