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 | |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 31 | // Role assigns specific responsibilities to users, allowing them to accomplish |
| 32 | // certain API operations whilst scoped to a service. |
| 33 | type Role struct { |
| 34 | // UUID of the role |
| 35 | ID string |
| 36 | |
| 37 | // Name of the role |
| 38 | Name string |
| 39 | } |
| 40 | |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 41 | // UserPage is a single page of a User collection. |
| 42 | type UserPage struct { |
| 43 | pagination.SinglePageBase |
| 44 | } |
| 45 | |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 46 | // RolePage is a single page of a user Role collection. |
| 47 | type RolePage struct { |
| 48 | pagination.SinglePageBase |
| 49 | } |
| 50 | |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 51 | // IsEmpty determines whether or not a page of Tenants contains any results. |
| 52 | func (page UserPage) IsEmpty() (bool, error) { |
| 53 | users, err := ExtractUsers(page) |
| 54 | if err != nil { |
| 55 | return false, err |
| 56 | } |
| 57 | return len(users) == 0, nil |
| 58 | } |
| 59 | |
| 60 | // ExtractUsers returns a slice of Tenants contained in a single page of results. |
| 61 | func ExtractUsers(page pagination.Page) ([]User, error) { |
| 62 | casted := page.(UserPage).Body |
| 63 | var response struct { |
| 64 | Users []User `mapstructure:"users"` |
| 65 | } |
| 66 | |
| 67 | err := mapstructure.Decode(casted, &response) |
| 68 | return response.Users, err |
| 69 | } |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 70 | |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 71 | // IsEmpty determines whether or not a page of Tenants contains any results. |
| 72 | func (page RolePage) IsEmpty() (bool, error) { |
| 73 | users, err := ExtractRoles(page) |
| 74 | if err != nil { |
| 75 | return false, err |
| 76 | } |
| 77 | return len(users) == 0, nil |
| 78 | } |
| 79 | |
| 80 | // ExtractRoles returns a slice of Roles contained in a single page of results. |
| 81 | func ExtractRoles(page pagination.Page) ([]Role, error) { |
| 82 | casted := page.(RolePage).Body |
| 83 | var response struct { |
| 84 | Roles []Role `mapstructure:"roles"` |
| 85 | } |
| 86 | |
| 87 | err := mapstructure.Decode(casted, &response) |
| 88 | return response.Roles, err |
| 89 | } |
| 90 | |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 91 | type commonResult struct { |
| 92 | gophercloud.Result |
| 93 | } |
| 94 | |
| 95 | // Extract interprets any commonResult as a User, if possible. |
| 96 | func (r commonResult) Extract() (*User, error) { |
| 97 | if r.Err != nil { |
| 98 | return nil, r.Err |
| 99 | } |
| 100 | |
| 101 | var response struct { |
| 102 | User User `mapstructure:"user"` |
| 103 | } |
| 104 | |
| 105 | err := mapstructure.Decode(r.Body, &response) |
| 106 | |
| 107 | return &response.User, err |
| 108 | } |
| 109 | |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 110 | // CreateResult represents the result of a Create operation |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 111 | type CreateResult struct { |
| 112 | commonResult |
| 113 | } |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 114 | |
| 115 | // GetResult represents the result of a Get operation |
| 116 | type GetResult struct { |
| 117 | commonResult |
| 118 | } |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 119 | |
| 120 | // UpdateResult represents the result of an Update operation |
| 121 | type UpdateResult struct { |
| 122 | commonResult |
| 123 | } |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 124 | |
| 125 | // DeleteResult represents the result of a Delete operation |
| 126 | type DeleteResult struct { |
| 127 | commonResult |
| 128 | } |