blob: f531d5d023a399b1f8165e691a7f5e927c1bc061 [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
Jamie Hannaforde680e422014-10-29 14:55:57 +010031// Role assigns specific responsibilities to users, allowing them to accomplish
32// certain API operations whilst scoped to a service.
33type Role struct {
34 // UUID of the role
35 ID string
36
37 // Name of the role
38 Name string
39}
40
Jamie Hannaford929bd002014-10-29 11:14:25 +010041// UserPage is a single page of a User collection.
42type UserPage struct {
43 pagination.SinglePageBase
44}
45
Jamie Hannaforde680e422014-10-29 14:55:57 +010046// RolePage is a single page of a user Role collection.
47type RolePage struct {
48 pagination.SinglePageBase
49}
50
Jamie Hannaford929bd002014-10-29 11:14:25 +010051// IsEmpty determines whether or not a page of Tenants contains any results.
52func (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.
61func 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 Hannaford9c7bb8e2014-10-29 11:47:34 +010070
Jamie Hannaforde680e422014-10-29 14:55:57 +010071// IsEmpty determines whether or not a page of Tenants contains any results.
72func (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.
81func 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 Hannaford9c7bb8e2014-10-29 11:47:34 +010091type commonResult struct {
92 gophercloud.Result
93}
94
95// Extract interprets any commonResult as a User, if possible.
96func (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 Hannaford2ad98bd2014-10-29 13:26:47 +0100110// CreateResult represents the result of a Create operation
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +0100111type CreateResult struct {
112 commonResult
113}
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100114
115// GetResult represents the result of a Get operation
116type GetResult struct {
117 commonResult
118}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100119
120// UpdateResult represents the result of an Update operation
121type UpdateResult struct {
122 commonResult
123}
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100124
125// DeleteResult represents the result of a Delete operation
126type DeleteResult struct {
127 commonResult
128}