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 ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
| 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. |
| 26 | TenantID string `mapstructure:"tenant_id"` |
| 27 | } |
| 28 | |
| 29 | // UserPage is a single page of a User collection. |
| 30 | type UserPage struct { |
| 31 | pagination.SinglePageBase |
| 32 | } |
| 33 | |
| 34 | // IsEmpty determines whether or not a page of Tenants contains any results. |
| 35 | func (page UserPage) IsEmpty() (bool, error) { |
| 36 | users, err := ExtractUsers(page) |
| 37 | if err != nil { |
| 38 | return false, err |
| 39 | } |
| 40 | return len(users) == 0, nil |
| 41 | } |
| 42 | |
| 43 | // ExtractUsers returns a slice of Tenants contained in a single page of results. |
| 44 | func ExtractUsers(page pagination.Page) ([]User, error) { |
| 45 | casted := page.(UserPage).Body |
| 46 | var response struct { |
| 47 | Users []User `mapstructure:"users"` |
| 48 | } |
| 49 | |
| 50 | err := mapstructure.Decode(casted, &response) |
| 51 | return response.Users, err |
| 52 | } |