blob: fe62ada600545ffe8145d4a6d00d4cbe799923df [file] [log] [blame]
Jamie Hannaford2a130242014-10-28 11:19:46 +01001package users
Jamie Hannaford929bd002014-10-29 11:14:25 +01002
3import (
4 "github.com/mitchellh/mapstructure"
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +01005
6 "github.com/rackspace/gophercloud"
Jamie Hannaford929bd002014-10-29 11:14:25 +01007 "github.com/rackspace/gophercloud/pagination"
8)
9
10// User represents a user resource that exists on the API.
11type 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.
32type UserPage struct {
33 pagination.SinglePageBase
34}
35
36// IsEmpty determines whether or not a page of Tenants contains any results.
37func (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.
46func 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 Hannaford9c7bb8e2014-10-29 11:47:34 +010055
56type commonResult struct {
57 gophercloud.Result
58}
59
60// Extract interprets any commonResult as a User, if possible.
61func (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
75type CreateResult struct {
76 commonResult
77}