blob: 045d9b385e67646aba5f55a3c7a6757be3685b15 [file] [log] [blame]
Jamie Hannaford2a130242014-10-28 11:19:46 +01001package users
Jamie Hannaford929bd002014-10-29 11:14:25 +01002
3import (
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +01004 "errors"
5
6 "github.com/racker/perigee"
Jamie Hannaford929bd002014-10-29 11:14:25 +01007 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
11func List(client *gophercloud.ServiceClient) pagination.Pager {
12 createPage := func(r pagination.PageResult) pagination.Page {
13 return UserPage{pagination.SinglePageBase(r)}
14 }
15
16 return pagination.NewPager(client, rootURL(client), createPage)
17}
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010018
19// EnabledState represents whether the user is enabled or not.
20type EnabledState *bool
21
22// Useful variables to use when creating or updating users.
23var (
24 iTrue = true
25 iFalse = false
26
27 Enabled EnabledState = &iTrue
28 Disabled EnabledState = &iFalse
29)
30
Alex Gaynord86dfd52014-10-31 10:59:48 -070031// CommonOpts are the parameters that are shared between CreateOpts and
32// UpdateOpts
Alex Gaynor19ff3802014-10-31 10:36:03 -070033type CommonOpts struct {
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010034 // Either a name or username is required. When provided, the value must be
35 // unique or a 409 conflict error will be returned. If you provide a name but
36 // omit a username, the latter will be set to the former; and vice versa.
37 Name, Username string
38
39 // The ID of the tenant to which you want to assign this user.
40 TenantID string
41
42 // Indicates whether this user is enabled or not.
43 Enabled EnabledState
44
45 // The email address of this user.
46 Email string
47}
48
Alex Gaynor19ff3802014-10-31 10:36:03 -070049// CreateOpts represents the options needed when creating new users.
50type CreateOpts CommonOpts
51
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010052// CreateOptsBuilder describes struct types that can be accepted by the Create call.
53type CreateOptsBuilder interface {
54 ToUserCreateMap() (map[string]interface{}, error)
55}
56
57// ToUserCreateMap assembles a request body based on the contents of a CreateOpts.
58func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
59 m := make(map[string]interface{})
60
61 if opts.Name == "" && opts.Username == "" {
62 return m, errors.New("Either a Name or Username must be provided")
63 }
64
65 if opts.Name != "" {
66 m["name"] = opts.Name
67 }
68 if opts.Username != "" {
69 m["username"] = opts.Username
70 }
71 if opts.Enabled != nil {
72 m["enabled"] = &opts.Enabled
73 }
74 if opts.Email != "" {
75 m["email"] = opts.Email
76 }
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010077 if opts.TenantID != "" {
78 m["tenant_id"] = opts.TenantID
79 }
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010080
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010081 return map[string]interface{}{"user": m}, nil
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010082}
83
84// Create is the operation responsible for creating new users.
85func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
86 var res CreateResult
87
88 reqBody, err := opts.ToUserCreateMap()
89 if err != nil {
90 res.Err = err
91 return res
92 }
93
Ash Wilson4bf41a32015-02-12 15:52:44 -050094 _, res.Err = client.Request("POST", rootURL(client), gophercloud.RequestOpts{
95 JSONResponse: &res.Body,
96 JSONBody: reqBody,
97 OkCodes: []int{200, 201},
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010098 })
99
100 return res
101}
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100102
Jamie Hannaford69c1fe92014-10-29 13:28:58 +0100103// Get requests details on a single user, either by ID.
104func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100105 var result GetResult
106
Jamie Hannaford6e4d7952014-10-29 16:18:29 +0100107 _, result.Err = perigee.Request("GET", ResourceURL(client, id), perigee.Options{
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100108 Results: &result.Body,
109 MoreHeaders: client.AuthenticatedHeaders(),
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100110 OkCodes: []int{200},
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100111 })
112
113 return result
114}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100115
Alex Gaynorc6cc18f2014-10-31 13:48:58 -0700116// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100117type UpdateOptsBuilder interface {
118 ToUserUpdateMap() map[string]interface{}
119}
120
Alex Gaynor19ff3802014-10-31 10:36:03 -0700121// UpdateOpts specifies the base attributes that may be updated on an existing server.
122type UpdateOpts CommonOpts
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100123
124// ToUserUpdateMap formats an UpdateOpts structure into a request body.
125func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} {
126 m := make(map[string]interface{})
127
128 if opts.Name != "" {
129 m["name"] = opts.Name
130 }
131 if opts.Username != "" {
132 m["username"] = opts.Username
133 }
134 if opts.Enabled != nil {
135 m["enabled"] = &opts.Enabled
136 }
137 if opts.Email != "" {
138 m["email"] = opts.Email
139 }
140 if opts.TenantID != "" {
141 m["tenant_id"] = opts.TenantID
142 }
143
144 return map[string]interface{}{"user": m}
145}
146
147// Update is the operation responsible for updating exist users by their UUID.
148func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
149 var result UpdateResult
150
Jamie Hannaford6e4d7952014-10-29 16:18:29 +0100151 _, result.Err = perigee.Request("PUT", ResourceURL(client, id), perigee.Options{
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100152 Results: &result.Body,
153 ReqBody: opts.ToUserUpdateMap(),
154 MoreHeaders: client.AuthenticatedHeaders(),
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100155 OkCodes: []int{200},
156 })
157
158 return result
159}
160
161// Delete is the operation responsible for permanently deleting an API user.
162func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
163 var result DeleteResult
164
Jamie Hannaford6e4d7952014-10-29 16:18:29 +0100165 _, result.Err = perigee.Request("DELETE", ResourceURL(client, id), perigee.Options{
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100166 MoreHeaders: client.AuthenticatedHeaders(),
167 OkCodes: []int{204},
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100168 })
169
170 return result
171}
Jamie Hannaforde680e422014-10-29 14:55:57 +0100172
173func ListRoles(client *gophercloud.ServiceClient, tenantID, userID string) pagination.Pager {
174 createPage := func(r pagination.PageResult) pagination.Page {
175 return RolePage{pagination.SinglePageBase(r)}
176 }
177
178 return pagination.NewPager(client, listRolesURL(client, tenantID, userID), createPage)
179}