Jamie Hannaford | 2a13024 | 2014-10-28 11:19:46 +0100 | [diff] [blame] | 1 | package users |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 6 | ) |
| 7 | |
| 8 | // User represents a user resource that exists on the API. |
| 9 | type User struct { |
| 10 | // The UUID for this user. |
| 11 | ID string |
| 12 | |
| 13 | // The human name for this user. |
| 14 | Name string |
| 15 | |
| 16 | // The username for this user. |
| 17 | Username string |
| 18 | |
| 19 | // Indicates whether the user is enabled (true) or disabled (false). |
| 20 | Enabled bool |
| 21 | |
| 22 | // The email address for this user. |
| 23 | Email string |
| 24 | |
| 25 | // The ID of the tenant to which this user belongs. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 26 | TenantID string `json:"tenant_id"` |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 27 | } |
| 28 | |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 29 | // Role assigns specific responsibilities to users, allowing them to accomplish |
| 30 | // certain API operations whilst scoped to a service. |
| 31 | type Role struct { |
| 32 | // UUID of the role |
| 33 | ID string |
| 34 | |
| 35 | // Name of the role |
| 36 | Name string |
| 37 | } |
| 38 | |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 39 | // UserPage is a single page of a User collection. |
| 40 | type UserPage struct { |
| 41 | pagination.SinglePageBase |
| 42 | } |
| 43 | |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 44 | // RolePage is a single page of a user Role collection. |
| 45 | type RolePage struct { |
| 46 | pagination.SinglePageBase |
| 47 | } |
| 48 | |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 49 | // IsEmpty determines whether or not a page of Tenants contains any results. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 50 | func (r UserPage) IsEmpty() (bool, error) { |
| 51 | users, err := ExtractUsers(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 52 | return len(users) == 0, err |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // ExtractUsers returns a slice of Tenants contained in a single page of results. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 56 | func ExtractUsers(r pagination.Page) ([]User, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 57 | var s struct { |
| 58 | Users []User `json:"users"` |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 59 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 60 | err := (r.(UserPage)).ExtractInto(&s) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 61 | return s.Users, err |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 62 | } |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 63 | |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 64 | // IsEmpty determines whether or not a page of Tenants contains any results. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 65 | func (r RolePage) IsEmpty() (bool, error) { |
| 66 | users, err := ExtractRoles(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 67 | return len(users) == 0, err |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // ExtractRoles returns a slice of Roles contained in a single page of results. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 71 | func ExtractRoles(r pagination.Page) ([]Role, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 72 | var s struct { |
| 73 | Roles []Role `json:"roles"` |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 74 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 75 | err := (r.(RolePage)).ExtractInto(&s) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 76 | return s.Roles, err |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 79 | type commonResult struct { |
| 80 | gophercloud.Result |
| 81 | } |
| 82 | |
| 83 | // Extract interprets any commonResult as a User, if possible. |
| 84 | func (r commonResult) Extract() (*User, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 85 | var s struct { |
| 86 | User *User `json:"user"` |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 87 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 88 | err := r.ExtractInto(&s) |
| 89 | return s.User, err |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 92 | // CreateResult represents the result of a Create operation |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 93 | type CreateResult struct { |
| 94 | commonResult |
| 95 | } |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 96 | |
| 97 | // GetResult represents the result of a Get operation |
| 98 | type GetResult struct { |
| 99 | commonResult |
| 100 | } |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 101 | |
| 102 | // UpdateResult represents the result of an Update operation |
| 103 | type UpdateResult struct { |
| 104 | commonResult |
| 105 | } |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 106 | |
| 107 | // DeleteResult represents the result of a Delete operation |
| 108 | type DeleteResult struct { |
| 109 | commonResult |
| 110 | } |