blob: 4ce395f2dd387caff1d59b8612230ed211433740 [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
94 _, res.Err = perigee.Request("POST", rootURL(client), perigee.Options{
95 Results: &res.Body,
96 ReqBody: reqBody,
97 MoreHeaders: client.AuthenticatedHeaders(),
98 OkCodes: []int{200, 201},
99 })
100
101 return res
102}
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100103
Jamie Hannaford69c1fe92014-10-29 13:28:58 +0100104// Get requests details on a single user, either by ID.
105func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100106 var result GetResult
107
Jamie Hannaford6e4d7952014-10-29 16:18:29 +0100108 _, result.Err = perigee.Request("GET", ResourceURL(client, id), perigee.Options{
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100109 Results: &result.Body,
110 MoreHeaders: client.AuthenticatedHeaders(),
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100111 OkCodes: []int{200},
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100112 })
113
114 return result
115}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100116
Alex Gaynorc6cc18f2014-10-31 13:48:58 -0700117// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100118type UpdateOptsBuilder interface {
119 ToUserUpdateMap() map[string]interface{}
120}
121
Alex Gaynor19ff3802014-10-31 10:36:03 -0700122// UpdateOpts specifies the base attributes that may be updated on an existing server.
123type UpdateOpts CommonOpts
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100124
125// ToUserUpdateMap formats an UpdateOpts structure into a request body.
126func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} {
127 m := make(map[string]interface{})
128
129 if opts.Name != "" {
130 m["name"] = opts.Name
131 }
132 if opts.Username != "" {
133 m["username"] = opts.Username
134 }
135 if opts.Enabled != nil {
136 m["enabled"] = &opts.Enabled
137 }
138 if opts.Email != "" {
139 m["email"] = opts.Email
140 }
141 if opts.TenantID != "" {
142 m["tenant_id"] = opts.TenantID
143 }
144
145 return map[string]interface{}{"user": m}
146}
147
148// Update is the operation responsible for updating exist users by their UUID.
149func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
150 var result UpdateResult
151
Jamie Hannaford6e4d7952014-10-29 16:18:29 +0100152 _, result.Err = perigee.Request("PUT", ResourceURL(client, id), perigee.Options{
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100153 Results: &result.Body,
154 ReqBody: opts.ToUserUpdateMap(),
155 MoreHeaders: client.AuthenticatedHeaders(),
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100156 OkCodes: []int{200},
157 })
158
159 return result
160}
161
162// Delete is the operation responsible for permanently deleting an API user.
163func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
164 var result DeleteResult
165
Jamie Hannaford6e4d7952014-10-29 16:18:29 +0100166 _, result.Err = perigee.Request("DELETE", ResourceURL(client, id), perigee.Options{
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100167 MoreHeaders: client.AuthenticatedHeaders(),
168 OkCodes: []int{204},
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100169 })
170
171 return result
172}
Jamie Hannaforde680e422014-10-29 14:55:57 +0100173
174func ListRoles(client *gophercloud.ServiceClient, tenantID, userID string) pagination.Pager {
175 createPage := func(r pagination.PageResult) pagination.Page {
176 return RolePage{pagination.SinglePageBase(r)}
177 }
178
179 return pagination.NewPager(client, listRolesURL(client, tenantID, userID), createPage)
180}