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" |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame^] | 5 | |
| 6 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
| 10 | // User represents a user resource that exists on the API. |
| 11 | type User struct { |
| 12 | // The UUID for this user. |
| 13 | ID string |
| 14 | |
| 15 | // The human name for this user. |
| 16 | Name string |
| 17 | |
| 18 | // The username for this user. |
| 19 | Username string |
| 20 | |
| 21 | // Indicates whether the user is enabled (true) or disabled (false). |
| 22 | Enabled bool |
| 23 | |
| 24 | // The email address for this user. |
| 25 | Email string |
| 26 | |
| 27 | // The ID of the tenant to which this user belongs. |
| 28 | TenantID string `mapstructure:"tenant_id"` |
| 29 | } |
| 30 | |
| 31 | // UserPage is a single page of a User collection. |
| 32 | type UserPage struct { |
| 33 | pagination.SinglePageBase |
| 34 | } |
| 35 | |
| 36 | // IsEmpty determines whether or not a page of Tenants contains any results. |
| 37 | func (page UserPage) IsEmpty() (bool, error) { |
| 38 | users, err := ExtractUsers(page) |
| 39 | if err != nil { |
| 40 | return false, err |
| 41 | } |
| 42 | return len(users) == 0, nil |
| 43 | } |
| 44 | |
| 45 | // ExtractUsers returns a slice of Tenants contained in a single page of results. |
| 46 | func ExtractUsers(page pagination.Page) ([]User, error) { |
| 47 | casted := page.(UserPage).Body |
| 48 | var response struct { |
| 49 | Users []User `mapstructure:"users"` |
| 50 | } |
| 51 | |
| 52 | err := mapstructure.Decode(casted, &response) |
| 53 | return response.Users, err |
| 54 | } |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame^] | 55 | |
| 56 | type commonResult struct { |
| 57 | gophercloud.Result |
| 58 | } |
| 59 | |
| 60 | // Extract interprets any commonResult as a User, if possible. |
| 61 | func (r commonResult) Extract() (*User, error) { |
| 62 | if r.Err != nil { |
| 63 | return nil, r.Err |
| 64 | } |
| 65 | |
| 66 | var response struct { |
| 67 | User User `mapstructure:"user"` |
| 68 | } |
| 69 | |
| 70 | err := mapstructure.Decode(r.Body, &response) |
| 71 | |
| 72 | return &response.User, err |
| 73 | } |
| 74 | |
| 75 | type CreateResult struct { |
| 76 | commonResult |
| 77 | } |