blob: f62f979bafb364ab99d1116efc128ff73f0352c4 [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 {
Jon Perrittdb0ae142016-03-13 00:33:41 -060010 return pagination.NewPager(client, rootURL(client), func(r pagination.PageResult) pagination.Page {
Jamie Hannaford929bd002014-10-29 11:14:25 +010011 return UserPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -060012 })
Jamie Hannaford929bd002014-10-29 11:14:25 +010013}
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010014
Alex Gaynord86dfd52014-10-31 10:59:48 -070015// CommonOpts are the parameters that are shared between CreateOpts and
16// UpdateOpts
Alex Gaynor19ff3802014-10-31 10:36:03 -070017type CommonOpts struct {
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010018 // Either a name or username is required. When provided, the value must be
19 // unique or a 409 conflict error will be returned. If you provide a name but
20 // omit a username, the latter will be set to the former; and vice versa.
Jon Perrittdb0ae142016-03-13 00:33:41 -060021 Name string `json:"name,omitempty"`
22 Username string `json:"username,omitempty"`
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010023 // The ID of the tenant to which you want to assign this user.
Jon Perrittdb0ae142016-03-13 00:33:41 -060024 TenantID string `json:"tenant_id,omitempty"`
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010025 // Indicates whether this user is enabled or not.
Jon Perrittdb0ae142016-03-13 00:33:41 -060026 Enabled *bool `json:"enabled,omitempty"`
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010027 // The email address of this user.
Jon Perrittdb0ae142016-03-13 00:33:41 -060028 Email string `json:"email,omitempty"`
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010029}
30
Alex Gaynor19ff3802014-10-31 10:36:03 -070031// CreateOpts represents the options needed when creating new users.
32type CreateOpts CommonOpts
33
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010034// CreateOptsBuilder describes struct types that can be accepted by the Create call.
35type CreateOptsBuilder interface {
36 ToUserCreateMap() (map[string]interface{}, error)
37}
38
39// ToUserCreateMap assembles a request body based on the contents of a CreateOpts.
40func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010041 if opts.Name == "" && opts.Username == "" {
Jon Perritta3302e12016-03-07 03:48:59 -060042 err := gophercloud.ErrMissingInput{}
Jon Perritta3302e12016-03-07 03:48:59 -060043 err.Argument = "users.CreateOpts.Name/users.CreateOpts.Username"
44 err.Info = "Either a Name or Username must be provided"
Jon Perrittdb0ae142016-03-13 00:33:41 -060045 return nil, err
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010046 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060047 return gophercloud.BuildRequestBody(opts, "user")
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010048}
49
50// Create is the operation responsible for creating new users.
51func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
52 var res CreateResult
53
54 reqBody, err := opts.ToUserCreateMap()
55 if err != nil {
56 res.Err = err
57 return res
58 }
59
Jamie Hannaford562a7d52015-03-24 16:20:16 +010060 _, res.Err = client.Post(rootURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{
61 OkCodes: []int{200, 201},
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010062 })
63
64 return res
65}
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010066
Jamie Hannaford69c1fe92014-10-29 13:28:58 +010067// Get requests details on a single user, either by ID.
68func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010069 var result GetResult
Jamie Hannaford562a7d52015-03-24 16:20:16 +010070 _, result.Err = client.Get(ResourceURL(client, id), &result.Body, nil)
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010071 return result
72}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010073
Alex Gaynorc6cc18f2014-10-31 13:48:58 -070074// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010075type UpdateOptsBuilder interface {
Jon Perrittdb0ae142016-03-13 00:33:41 -060076 ToUserUpdateMap() (map[string]interface{}, error)
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010077}
78
Alex Gaynor19ff3802014-10-31 10:36:03 -070079// UpdateOpts specifies the base attributes that may be updated on an existing server.
80type UpdateOpts CommonOpts
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010081
82// ToUserUpdateMap formats an UpdateOpts structure into a request body.
Jon Perrittdb0ae142016-03-13 00:33:41 -060083func (opts UpdateOpts) ToUserUpdateMap() (map[string]interface{}, error) {
84 return gophercloud.BuildRequestBody(opts, "user")
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010085}
86
87// Update is the operation responsible for updating exist users by their UUID.
88func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060089 var r UpdateResult
90 b, err := opts.ToUserUpdateMap()
91 if err != nil {
92 r.Err = err
93 return r
94 }
95 _, r.Err = client.Put(ResourceURL(client, id), &b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford562a7d52015-03-24 16:20:16 +010096 OkCodes: []int{200},
Jamie Hannaford8b9a8002014-10-29 14:20:24 +010097 })
Jon Perrittdb0ae142016-03-13 00:33:41 -060098 return r
Jamie Hannaford8b9a8002014-10-29 14:20:24 +010099}
100
101// Delete is the operation responsible for permanently deleting an API user.
102func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600103 var r DeleteResult
104 _, r.Err = client.Delete(ResourceURL(client, id), nil)
105 return r
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100106}
Jamie Hannaforde680e422014-10-29 14:55:57 +0100107
Jon Perritta3302e12016-03-07 03:48:59 -0600108// ListRoles lists the existing roles that can be assigned to users.
Jamie Hannaforde680e422014-10-29 14:55:57 +0100109func ListRoles(client *gophercloud.ServiceClient, tenantID, userID string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600110 return pagination.NewPager(client, listRolesURL(client, tenantID, userID), func(r pagination.PageResult) pagination.Page {
Jamie Hannaforde680e422014-10-29 14:55:57 +0100111 return RolePage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600112 })
Jamie Hannaforde680e422014-10-29 14:55:57 +0100113}