Daniel Speichert | 44e3b54 | 2015-08-26 20:55:58 -0400 | [diff] [blame] | 1 | package roles |
| 2 | |
| 3 | import ( |
| 4 | "github.com/rackspace/gophercloud/pagination" |
| 5 | |
| 6 | "github.com/mitchellh/mapstructure" |
| 7 | ) |
| 8 | |
| 9 | // RoleAssignment is the result of a role assignments query. |
| 10 | type RoleAssignment struct { |
Daniel Speichert | 1cc1c84 | 2015-09-15 23:19:13 -0400 | [diff] [blame] | 11 | Role Role `json:"role,omitempty"` |
| 12 | Scope Scope `json:"scope,omitempty"` |
| 13 | User User `json:"user,omitempty"` |
| 14 | Group Group `json:"group,omitempty"` |
Daniel Speichert | 44e3b54 | 2015-08-26 20:55:58 -0400 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | type Role struct { |
| 18 | ID string `json:"id,omitempty"` |
| 19 | } |
| 20 | |
| 21 | type Scope struct { |
Daniel Speichert | 1cc1c84 | 2015-09-15 23:19:13 -0400 | [diff] [blame] | 22 | Domain Domain `json:"domain,omitempty"` |
| 23 | Project Project `json:"domain,omitempty"` |
Daniel Speichert | 44e3b54 | 2015-08-26 20:55:58 -0400 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | type Domain struct { |
| 27 | ID string `json:"id,omitempty"` |
| 28 | } |
| 29 | |
| 30 | type Project struct { |
| 31 | ID string `json:"id,omitempty"` |
| 32 | } |
| 33 | |
| 34 | type User struct { |
| 35 | ID string `json:"id,omitempty"` |
| 36 | } |
| 37 | |
| 38 | type Group struct { |
| 39 | ID string `json:"id,omitempty"` |
| 40 | } |
| 41 | |
| 42 | // RoleAssignmentsPage is a single page of RoleAssignments results. |
| 43 | type RoleAssignmentsPage struct { |
| 44 | pagination.LinkedPageBase |
| 45 | } |
| 46 | |
| 47 | // IsEmpty returns true if the page contains no results. |
| 48 | func (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 Speichert | 1cc1c84 | 2015-09-15 23:19:13 -0400 | [diff] [blame] | 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 | |
Daniel Speichert | 44e3b54 | 2015-08-26 20:55:58 -0400 | [diff] [blame] | 73 | // ExtractRoleAssignments extracts a slice of RoleAssignments from a Collection acquired from List. |
| 74 | func 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 | } |