blob: ef77d397745c69c2530f6a639388b4c3df0d0bbd [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.
Jon Perritt3860b512016-03-29 12:01:48 -050051func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
52 b, err := opts.ToUserCreateMap()
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010053 if err != nil {
Jon Perritt3860b512016-03-29 12:01:48 -050054 r.Err = err
55 return
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010056 }
Jon Perritt3860b512016-03-29 12:01:48 -050057 _, r.Err = client.Post(rootURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford562a7d52015-03-24 16:20:16 +010058 OkCodes: []int{200, 201},
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010059 })
jrperritt29ae6b32016-04-13 12:59:37 -050060 return
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010061}
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010062
Jamie Hannaford69c1fe92014-10-29 13:28:58 +010063// Get requests details on a single user, either by ID.
Jon Perritt3860b512016-03-29 12:01:48 -050064func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
65 _, r.Err = client.Get(ResourceURL(client, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050066 return
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010067}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010068
Alex Gaynorc6cc18f2014-10-31 13:48:58 -070069// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010070type UpdateOptsBuilder interface {
Jon Perrittdb0ae142016-03-13 00:33:41 -060071 ToUserUpdateMap() (map[string]interface{}, error)
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010072}
73
Alex Gaynor19ff3802014-10-31 10:36:03 -070074// UpdateOpts specifies the base attributes that may be updated on an existing server.
75type UpdateOpts CommonOpts
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010076
77// ToUserUpdateMap formats an UpdateOpts structure into a request body.
Jon Perrittdb0ae142016-03-13 00:33:41 -060078func (opts UpdateOpts) ToUserUpdateMap() (map[string]interface{}, error) {
79 return gophercloud.BuildRequestBody(opts, "user")
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010080}
81
82// Update is the operation responsible for updating exist users by their UUID.
Jon Perritt3860b512016-03-29 12:01:48 -050083func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060084 b, err := opts.ToUserUpdateMap()
85 if err != nil {
86 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050087 return
Jon Perrittdb0ae142016-03-13 00:33:41 -060088 }
89 _, r.Err = client.Put(ResourceURL(client, id), &b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford562a7d52015-03-24 16:20:16 +010090 OkCodes: []int{200},
Jamie Hannaford8b9a8002014-10-29 14:20:24 +010091 })
jrperritt29ae6b32016-04-13 12:59:37 -050092 return
Jamie Hannaford8b9a8002014-10-29 14:20:24 +010093}
94
95// Delete is the operation responsible for permanently deleting an API user.
Jon Perritt3860b512016-03-29 12:01:48 -050096func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060097 _, r.Err = client.Delete(ResourceURL(client, id), nil)
jrperritt29ae6b32016-04-13 12:59:37 -050098 return
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010099}
Jamie Hannaforde680e422014-10-29 14:55:57 +0100100
Jon Perritta3302e12016-03-07 03:48:59 -0600101// ListRoles lists the existing roles that can be assigned to users.
Jamie Hannaforde680e422014-10-29 14:55:57 +0100102func ListRoles(client *gophercloud.ServiceClient, tenantID, userID string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600103 return pagination.NewPager(client, listRolesURL(client, tenantID, userID), func(r pagination.PageResult) pagination.Page {
Jamie Hannaforde680e422014-10-29 14:55:57 +0100104 return RolePage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600105 })
Jamie Hannaforde680e422014-10-29 14:55:57 +0100106}