blob: 88be45ecc01877e165dff4adb5ef1ff4ed58aee9 [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
Jamie Hannaford562a7d52015-03-24 16:20:16 +010093 _, res.Err = client.Post(rootURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{
94 OkCodes: []int{200, 201},
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010095 })
96
97 return res
98}
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010099
Jamie Hannaford69c1fe92014-10-29 13:28:58 +0100100// Get requests details on a single user, either by ID.
101func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100102 var result GetResult
Jamie Hannaford562a7d52015-03-24 16:20:16 +0100103 _, result.Err = client.Get(ResourceURL(client, id), &result.Body, nil)
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100104 return result
105}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100106
Alex Gaynorc6cc18f2014-10-31 13:48:58 -0700107// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100108type UpdateOptsBuilder interface {
109 ToUserUpdateMap() map[string]interface{}
110}
111
Alex Gaynor19ff3802014-10-31 10:36:03 -0700112// UpdateOpts specifies the base attributes that may be updated on an existing server.
113type UpdateOpts CommonOpts
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100114
115// ToUserUpdateMap formats an UpdateOpts structure into a request body.
116func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} {
117 m := make(map[string]interface{})
118
119 if opts.Name != "" {
120 m["name"] = opts.Name
121 }
122 if opts.Username != "" {
123 m["username"] = opts.Username
124 }
125 if opts.Enabled != nil {
126 m["enabled"] = &opts.Enabled
127 }
128 if opts.Email != "" {
129 m["email"] = opts.Email
130 }
131 if opts.TenantID != "" {
132 m["tenant_id"] = opts.TenantID
133 }
134
135 return map[string]interface{}{"user": m}
136}
137
138// Update is the operation responsible for updating exist users by their UUID.
139func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
140 var result UpdateResult
Jamie Hannaford562a7d52015-03-24 16:20:16 +0100141 reqBody := opts.ToUserUpdateMap()
142 _, result.Err = client.Put(ResourceURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{
143 OkCodes: []int{200},
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100144 })
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100145 return result
146}
147
148// Delete is the operation responsible for permanently deleting an API user.
149func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
150 var result DeleteResult
Jamie Hannaford562a7d52015-03-24 16:20:16 +0100151 _, result.Err = client.Delete(ResourceURL(client, id), nil)
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100152 return result
153}
Jamie Hannaforde680e422014-10-29 14:55:57 +0100154
155func ListRoles(client *gophercloud.ServiceClient, tenantID, userID string) pagination.Pager {
156 createPage := func(r pagination.PageResult) pagination.Page {
157 return RolePage{pagination.SinglePageBase(r)}
158 }
159
160 return pagination.NewPager(client, listRolesURL(client, tenantID, userID), createPage)
161}