blob: 7fa5fc3415d79263e3304e648f2697122803a6e8 [file] [log] [blame]
Jamie Hannaford2a130242014-10-28 11:19:46 +01001package users
Jamie Hannaford929bd002014-10-29 11:14:25 +01002
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford929bd002014-10-29 11:14:25 +01006)
7
Jon Perritta3302e12016-03-07 03:48:59 -06008// List lists the existing users.
Jamie Hannaford929bd002014-10-29 11:14:25 +01009func List(client *gophercloud.ServiceClient) pagination.Pager {
10 createPage := func(r pagination.PageResult) pagination.Page {
11 return UserPage{pagination.SinglePageBase(r)}
12 }
13
14 return pagination.NewPager(client, rootURL(client), createPage)
15}
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010016
17// EnabledState represents whether the user is enabled or not.
18type EnabledState *bool
19
20// Useful variables to use when creating or updating users.
21var (
22 iTrue = true
23 iFalse = false
24
25 Enabled EnabledState = &iTrue
26 Disabled EnabledState = &iFalse
27)
28
Alex Gaynord86dfd52014-10-31 10:59:48 -070029// CommonOpts are the parameters that are shared between CreateOpts and
30// UpdateOpts
Alex Gaynor19ff3802014-10-31 10:36:03 -070031type 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
Alex Gaynor19ff3802014-10-31 10:36:03 -070047// 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 == "" {
Jon Perritta3302e12016-03-07 03:48:59 -060060 err := gophercloud.ErrMissingInput{}
61 err.Function = "users.ToUserCreateMap"
62 err.Argument = "users.CreateOpts.Name/users.CreateOpts.Username"
63 err.Info = "Either a Name or Username must be provided"
64 return m, err
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010065 }
66
67 if opts.Name != "" {
68 m["name"] = opts.Name
69 }
70 if opts.Username != "" {
71 m["username"] = opts.Username
72 }
73 if opts.Enabled != nil {
74 m["enabled"] = &opts.Enabled
75 }
76 if opts.Email != "" {
77 m["email"] = opts.Email
78 }
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010079 if opts.TenantID != "" {
80 m["tenant_id"] = opts.TenantID
81 }
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010082
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010083 return map[string]interface{}{"user": m}, nil
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010084}
85
86// Create is the operation responsible for creating new users.
87func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
88 var res CreateResult
89
90 reqBody, err := opts.ToUserCreateMap()
91 if err != nil {
92 res.Err = err
93 return res
94 }
95
Jamie Hannaford562a7d52015-03-24 16:20:16 +010096 _, res.Err = client.Post(rootURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{
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
Jamie Hannaford562a7d52015-03-24 16:20:16 +0100106 _, result.Err = client.Get(ResourceURL(client, id), &result.Body, nil)
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100107 return result
108}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100109
Alex Gaynorc6cc18f2014-10-31 13:48:58 -0700110// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100111type UpdateOptsBuilder interface {
112 ToUserUpdateMap() map[string]interface{}
113}
114
Alex Gaynor19ff3802014-10-31 10:36:03 -0700115// UpdateOpts specifies the base attributes that may be updated on an existing server.
116type UpdateOpts CommonOpts
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100117
118// ToUserUpdateMap formats an UpdateOpts structure into a request body.
119func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} {
120 m := make(map[string]interface{})
121
122 if opts.Name != "" {
123 m["name"] = opts.Name
124 }
125 if opts.Username != "" {
126 m["username"] = opts.Username
127 }
128 if opts.Enabled != nil {
129 m["enabled"] = &opts.Enabled
130 }
131 if opts.Email != "" {
132 m["email"] = opts.Email
133 }
134 if opts.TenantID != "" {
135 m["tenant_id"] = opts.TenantID
136 }
137
138 return map[string]interface{}{"user": m}
139}
140
141// Update is the operation responsible for updating exist users by their UUID.
142func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
143 var result UpdateResult
Jamie Hannaford562a7d52015-03-24 16:20:16 +0100144 reqBody := opts.ToUserUpdateMap()
145 _, result.Err = client.Put(ResourceURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{
146 OkCodes: []int{200},
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100147 })
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100148 return result
149}
150
151// Delete is the operation responsible for permanently deleting an API user.
152func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
153 var result DeleteResult
Jamie Hannaford562a7d52015-03-24 16:20:16 +0100154 _, result.Err = client.Delete(ResourceURL(client, id), nil)
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100155 return result
156}
Jamie Hannaforde680e422014-10-29 14:55:57 +0100157
Jon Perritta3302e12016-03-07 03:48:59 -0600158// ListRoles lists the existing roles that can be assigned to users.
Jamie Hannaforde680e422014-10-29 14:55:57 +0100159func ListRoles(client *gophercloud.ServiceClient, tenantID, userID string) pagination.Pager {
160 createPage := func(r pagination.PageResult) pagination.Page {
161 return RolePage{pagination.SinglePageBase(r)}
162 }
163
164 return pagination.NewPager(client, listRolesURL(client, tenantID, userID), createPage)
165}