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 { |
| 11 | Role *Role `json:"role,omitempty"` |
| 12 | Scope *Scope `json:"scope,omitempty"` |
| 13 | User *User `json:"user,omitempty"` |
| 14 | Group *Group `json:"group,omitempty"` |
| 15 | } |
| 16 | |
| 17 | type Role struct { |
| 18 | ID string `json:"id,omitempty"` |
| 19 | } |
| 20 | |
| 21 | type Scope struct { |
| 22 | Domain *Domain `json:"domain,omitempty"` |
| 23 | Project *Project `json:"domain,omitempty"` |
| 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 | |
| 56 | // ExtractRoleAssignments extracts a slice of RoleAssignments from a Collection acquired from List. |
| 57 | func ExtractRoleAssignments(page pagination.Page) ([]RoleAssignment, error) { |
| 58 | var response struct { |
| 59 | RoleAssignments []RoleAssignment `mapstructure:"role_assignments"` |
| 60 | } |
| 61 | |
| 62 | err := mapstructure.Decode(page.(RoleAssignmentsPage).Body, &response) |
| 63 | return response.RoleAssignments, err |
| 64 | } |