blob: 2afe62a6134029eed3d73b308c5939f1b59d88db [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
Jamie Hannaford929bd002014-10-29 11:14:25 +01006 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8)
9
10func List(client *gophercloud.ServiceClient) pagination.Pager {
11 createPage := func(r pagination.PageResult) pagination.Page {
12 return UserPage{pagination.SinglePageBase(r)}
13 }
14
15 return pagination.NewPager(client, rootURL(client), createPage)
16}
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010017
18// EnabledState represents whether the user is enabled or not.
19type EnabledState *bool
20
21// Useful variables to use when creating or updating users.
22var (
23 iTrue = true
24 iFalse = false
25
26 Enabled EnabledState = &iTrue
27 Disabled EnabledState = &iFalse
28)
29
Alex Gaynord86dfd52014-10-31 10:59:48 -070030// CommonOpts are the parameters that are shared between CreateOpts and
31// UpdateOpts
Alex Gaynor19ff3802014-10-31 10:36:03 -070032type CommonOpts struct {
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010033 // Either a name or username is required. When provided, the value must be
34 // unique or a 409 conflict error will be returned. If you provide a name but
35 // omit a username, the latter will be set to the former; and vice versa.
36 Name, Username string
37
38 // The ID of the tenant to which you want to assign this user.
39 TenantID string
40
41 // Indicates whether this user is enabled or not.
42 Enabled EnabledState
43
44 // The email address of this user.
45 Email string
46}
47
Alex Gaynor19ff3802014-10-31 10:36:03 -070048// CreateOpts represents the options needed when creating new users.
49type CreateOpts CommonOpts
50
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010051// CreateOptsBuilder describes struct types that can be accepted by the Create call.
52type CreateOptsBuilder interface {
53 ToUserCreateMap() (map[string]interface{}, error)
54}
55
56// ToUserCreateMap assembles a request body based on the contents of a CreateOpts.
57func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
58 m := make(map[string]interface{})
59
60 if opts.Name == "" && opts.Username == "" {
61 return m, errors.New("Either a Name or Username must be provided")
62 }
63
64 if opts.Name != "" {
65 m["name"] = opts.Name
66 }
67 if opts.Username != "" {
68 m["username"] = opts.Username
69 }
70 if opts.Enabled != nil {
71 m["enabled"] = &opts.Enabled
72 }
73 if opts.Email != "" {
74 m["email"] = opts.Email
75 }
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010076 if opts.TenantID != "" {
77 m["tenant_id"] = opts.TenantID
78 }
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010079
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010080 return map[string]interface{}{"user": m}, nil
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010081}
82
83// Create is the operation responsible for creating new users.
84func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
85 var res CreateResult
86
87 reqBody, err := opts.ToUserCreateMap()
88 if err != nil {
89 res.Err = err
90 return res
91 }
92
Ash Wilson4bf41a32015-02-12 15:52:44 -050093 _, res.Err = client.Request("POST", rootURL(client), gophercloud.RequestOpts{
94 JSONResponse: &res.Body,
95 JSONBody: reqBody,
96 OkCodes: []int{200, 201},
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010097 })
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
Ash Wilson59fb6c42015-02-12 16:21:13 -0500106 _, result.Err = client.Request("GET", ResourceURL(client, id), gophercloud.RequestOpts{
107 JSONResponse: &result.Body,
108 OkCodes: []int{200},
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100109 })
110
111 return result
112}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100113
Alex Gaynorc6cc18f2014-10-31 13:48:58 -0700114// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100115type UpdateOptsBuilder interface {
116 ToUserUpdateMap() map[string]interface{}
117}
118
Alex Gaynor19ff3802014-10-31 10:36:03 -0700119// UpdateOpts specifies the base attributes that may be updated on an existing server.
120type UpdateOpts CommonOpts
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100121
122// ToUserUpdateMap formats an UpdateOpts structure into a request body.
123func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} {
124 m := make(map[string]interface{})
125
126 if opts.Name != "" {
127 m["name"] = opts.Name
128 }
129 if opts.Username != "" {
130 m["username"] = opts.Username
131 }
132 if opts.Enabled != nil {
133 m["enabled"] = &opts.Enabled
134 }
135 if opts.Email != "" {
136 m["email"] = opts.Email
137 }
138 if opts.TenantID != "" {
139 m["tenant_id"] = opts.TenantID
140 }
141
142 return map[string]interface{}{"user": m}
143}
144
145// Update is the operation responsible for updating exist users by their UUID.
146func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
147 var result UpdateResult
148
Ash Wilson59fb6c42015-02-12 16:21:13 -0500149 _, result.Err = client.Request("PUT", ResourceURL(client, id), gophercloud.RequestOpts{
150 JSONResponse: &result.Body,
151 JSONBody: opts.ToUserUpdateMap(),
152 OkCodes: []int{200},
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100153 })
154
155 return result
156}
157
158// Delete is the operation responsible for permanently deleting an API user.
159func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
160 var result DeleteResult
161
Ash Wilson59fb6c42015-02-12 16:21:13 -0500162 _, result.Err = client.Request("DELETE", ResourceURL(client, id), gophercloud.RequestOpts{
163 OkCodes: []int{204},
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100164 })
165
166 return result
167}
Jamie Hannaforde680e422014-10-29 14:55:57 +0100168
169func ListRoles(client *gophercloud.ServiceClient, tenantID, userID string) pagination.Pager {
170 createPage := func(r pagination.PageResult) pagination.Page {
171 return RolePage{pagination.SinglePageBase(r)}
172 }
173
174 return pagination.NewPager(client, listRolesURL(client, tenantID, userID), createPage)
175}