diff --git a/openstack/identity/v3/roles/requests.go b/openstack/identity/v3/roles/requests.go index 6e3b33420..9656ff874 100644 --- a/openstack/identity/v3/roles/requests.go +++ b/openstack/identity/v3/roles/requests.go @@ -2,7 +2,6 @@ package roles import ( "github.com/huaweicloud/golangsdk" - "github.com/huaweicloud/golangsdk/openstack" "github.com/huaweicloud/golangsdk/pagination" ) @@ -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. @@ -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. @@ -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"` @@ -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}} }) diff --git a/openstack/identity/v3/roles/testing/fixtures.go b/openstack/identity/v3/roles/testing/fixtures.go index ce80c1344..86d4ebb78 100644 --- a/openstack/identity/v3/roles/testing/fixtures.go +++ b/openstack/identity/v3/roles/testing/fixtures.go @@ -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) diff --git a/openstack/identity/v3/roles/testing/requests_test.go b/openstack/identity/v3/roles/testing/requests_test.go index 318d36883..084f16ed8 100644 --- a/openstack/identity/v3/roles/testing/requests_test.go +++ b/openstack/identity/v3/roles/testing/requests_test.go @@ -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() diff --git a/openstack/identity/v3/roles/urls.go b/openstack/identity/v3/roles/urls.go index e7975d8f9..45fa074f7 100644 --- a/openstack/identity/v3/roles/urls.go +++ b/openstack/identity/v3/roles/urls.go @@ -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 {