blob: bcf86c59e784f0733ab7a0353e6b7c23d9f6cf2d [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
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010031type commonOpts struct {
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010032 // Either a name or username is required. When provided, the value must be
33 // unique or a 409 conflict error will be returned. If you provide a name but
34 // omit a username, the latter will be set to the former; and vice versa.
35 Name, Username string
36
37 // The ID of the tenant to which you want to assign this user.
38 TenantID string
39
40 // Indicates whether this user is enabled or not.
41 Enabled EnabledState
42
43 // The email address of this user.
44 Email string
45}
46
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010047// CreateOpts represents the options needed when creating new users.
48type CreateOpts commonOpts
49
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010050// CreateOptsBuilder describes struct types that can be accepted by the Create call.
51type CreateOptsBuilder interface {
52 ToUserCreateMap() (map[string]interface{}, error)
53}
54
55// ToUserCreateMap assembles a request body based on the contents of a CreateOpts.
56func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
57 m := make(map[string]interface{})
58
59 if opts.Name == "" && opts.Username == "" {
60 return m, errors.New("Either a Name or Username must be provided")
61 }
62
63 if opts.Name != "" {
64 m["name"] = opts.Name
65 }
66 if opts.Username != "" {
67 m["username"] = opts.Username
68 }
69 if opts.Enabled != nil {
70 m["enabled"] = &opts.Enabled
71 }
72 if opts.Email != "" {
73 m["email"] = opts.Email
74 }
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010075 if opts.TenantID != "" {
76 m["tenant_id"] = opts.TenantID
77 }
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010078
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010079 return map[string]interface{}{"user": m}, nil
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010080}
81
82// Create is the operation responsible for creating new users.
83func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
84 var res CreateResult
85
86 reqBody, err := opts.ToUserCreateMap()
87 if err != nil {
88 res.Err = err
89 return res
90 }
91
92 _, res.Err = perigee.Request("POST", rootURL(client), perigee.Options{
93 Results: &res.Body,
94 ReqBody: reqBody,
95 MoreHeaders: client.AuthenticatedHeaders(),
96 OkCodes: []int{200, 201},
97 })
98
99 return res
100}
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100101
Jamie Hannaford69c1fe92014-10-29 13:28:58 +0100102// Get requests details on a single user, either by ID.
103func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100104 var result GetResult
105
Jamie Hannaford69c1fe92014-10-29 13:28:58 +0100106 _, result.Err = perigee.Request("GET", resourceURL(client, id), perigee.Options{
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100107 Results: &result.Body,
108 MoreHeaders: client.AuthenticatedHeaders(),
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100109 OkCodes: []int{200},
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100110 })
111
112 return result
113}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100114
115// UpdateOptsBuilder allows extentions to add additional attributes to the Update request.
116type UpdateOptsBuilder interface {
117 ToUserUpdateMap() map[string]interface{}
118}
119
120// UpdateOpts specifies the base attributes that may be updated on an existing server.
121type UpdateOpts commonOpts
122
123// ToUserUpdateMap formats an UpdateOpts structure into a request body.
124func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} {
125 m := make(map[string]interface{})
126
127 if opts.Name != "" {
128 m["name"] = opts.Name
129 }
130 if opts.Username != "" {
131 m["username"] = opts.Username
132 }
133 if opts.Enabled != nil {
134 m["enabled"] = &opts.Enabled
135 }
136 if opts.Email != "" {
137 m["email"] = opts.Email
138 }
139 if opts.TenantID != "" {
140 m["tenant_id"] = opts.TenantID
141 }
142
143 return map[string]interface{}{"user": m}
144}
145
146// Update is the operation responsible for updating exist users by their UUID.
147func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
148 var result UpdateResult
149
150 _, result.Err = perigee.Request("PUT", resourceURL(client, id), perigee.Options{
151 Results: &result.Body,
152 ReqBody: opts.ToUserUpdateMap(),
153 MoreHeaders: client.AuthenticatedHeaders(),
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100154 OkCodes: []int{200},
155 })
156
157 return result
158}
159
160// Delete is the operation responsible for permanently deleting an API user.
161func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
162 var result DeleteResult
163
164 _, result.Err = perigee.Request("DELETE", resourceURL(client, id), perigee.Options{
165 MoreHeaders: client.AuthenticatedHeaders(),
166 OkCodes: []int{204},
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100167 })
168
169 return result
170}