blob: d25abd25d773b0f9182175838362d14c1c0034fa [file] [log] [blame]
Daniel Speichert44e3b542015-08-26 20:55:58 -04001package roles
2
3import (
4 "github.com/rackspace/gophercloud/pagination"
5
6 "github.com/mitchellh/mapstructure"
7)
8
9// RoleAssignment is the result of a role assignments query.
10type RoleAssignment struct {
Daniel Speichert1cc1c842015-09-15 23:19:13 -040011 Role Role `json:"role,omitempty"`
12 Scope Scope `json:"scope,omitempty"`
13 User User `json:"user,omitempty"`
14 Group Group `json:"group,omitempty"`
Daniel Speichert44e3b542015-08-26 20:55:58 -040015}
16
17type Role struct {
18 ID string `json:"id,omitempty"`
19}
20
21type Scope struct {
Daniel Speichert1cc1c842015-09-15 23:19:13 -040022 Domain Domain `json:"domain,omitempty"`
23 Project Project `json:"domain,omitempty"`
Daniel Speichert44e3b542015-08-26 20:55:58 -040024}
25
26type Domain struct {
27 ID string `json:"id,omitempty"`
28}
29
30type Project struct {
31 ID string `json:"id,omitempty"`
32}
33
34type User struct {
35 ID string `json:"id,omitempty"`
36}
37
38type Group struct {
39 ID string `json:"id,omitempty"`
40}
41
42// RoleAssignmentsPage is a single page of RoleAssignments results.
43type RoleAssignmentsPage struct {
44 pagination.LinkedPageBase
45}
46
47// IsEmpty returns true if the page contains no results.
48func (p RoleAssignmentsPage) IsEmpty() (bool, error) {
49 roleAssignments, err := ExtractRoleAssignments(p)
50 if err != nil {
51 return true, err
52 }
53 return len(roleAssignments) == 0, nil
54}
55
Daniel Speichert1cc1c842015-09-15 23:19:13 -040056// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
57func (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
Daniel Speichert44e3b542015-08-26 20:55:58 -040073// ExtractRoleAssignments extracts a slice of RoleAssignments from a Collection acquired from List.
74func ExtractRoleAssignments(page pagination.Page) ([]RoleAssignment, error) {
75 var response struct {
76 RoleAssignments []RoleAssignment `mapstructure:"role_assignments"`
77 }
78
79 err := mapstructure.Decode(page.(RoleAssignmentsPage).Body, &response)
80 return response.RoleAssignments, err
81}