Jamie Hannaford | 2a13024 | 2014-10-28 11:19:46 +0100 | [diff] [blame] | 1 | package users |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 6 | ) |
| 7 | |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 8 | // List lists the existing users. |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 9 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 10 | return pagination.NewPager(client, rootURL(client), func(r pagination.PageResult) pagination.Page { |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 11 | return UserPage{pagination.SinglePageBase(r)} |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 12 | }) |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 13 | } |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 14 | |
Alex Gaynor | d86dfd5 | 2014-10-31 10:59:48 -0700 | [diff] [blame] | 15 | // CommonOpts are the parameters that are shared between CreateOpts and |
| 16 | // UpdateOpts |
Alex Gaynor | 19ff380 | 2014-10-31 10:36:03 -0700 | [diff] [blame] | 17 | type CommonOpts struct { |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 18 | // 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 Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 21 | Name string `json:"name,omitempty"` |
| 22 | Username string `json:"username,omitempty"` |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 23 | // The ID of the tenant to which you want to assign this user. |
dbaumgarten | 700ce7d | 2017-01-19 02:04:45 +0100 | [diff] [blame] | 24 | TenantID string `json:"tenantId,omitempty"` |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 25 | // Indicates whether this user is enabled or not. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 26 | Enabled *bool `json:"enabled,omitempty"` |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 27 | // The email address of this user. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 28 | Email string `json:"email,omitempty"` |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 29 | } |
| 30 | |
Alex Gaynor | 19ff380 | 2014-10-31 10:36:03 -0700 | [diff] [blame] | 31 | // CreateOpts represents the options needed when creating new users. |
| 32 | type CreateOpts CommonOpts |
| 33 | |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 34 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. |
| 35 | type CreateOptsBuilder interface { |
| 36 | ToUserCreateMap() (map[string]interface{}, error) |
| 37 | } |
| 38 | |
| 39 | // ToUserCreateMap assembles a request body based on the contents of a CreateOpts. |
| 40 | func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) { |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 41 | if opts.Name == "" && opts.Username == "" { |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 42 | err := gophercloud.ErrMissingInput{} |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 43 | err.Argument = "users.CreateOpts.Name/users.CreateOpts.Username" |
| 44 | err.Info = "Either a Name or Username must be provided" |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 45 | return nil, err |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 46 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 47 | return gophercloud.BuildRequestBody(opts, "user") |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Create is the operation responsible for creating new users. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 51 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
| 52 | b, err := opts.ToUserCreateMap() |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 53 | if err != nil { |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 54 | r.Err = err |
| 55 | return |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 56 | } |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 57 | _, r.Err = client.Post(rootURL(client), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 562a7d5 | 2015-03-24 16:20:16 +0100 | [diff] [blame] | 58 | OkCodes: []int{200, 201}, |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 59 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 60 | return |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 61 | } |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 62 | |
Jamie Hannaford | 69c1fe9 | 2014-10-29 13:28:58 +0100 | [diff] [blame] | 63 | // Get requests details on a single user, either by ID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 64 | func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { |
| 65 | _, r.Err = client.Get(ResourceURL(client, id), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 66 | return |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 67 | } |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 68 | |
Alex Gaynor | c6cc18f | 2014-10-31 13:48:58 -0700 | [diff] [blame] | 69 | // UpdateOptsBuilder allows extensions to add additional attributes to the Update request. |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 70 | type UpdateOptsBuilder interface { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 71 | ToUserUpdateMap() (map[string]interface{}, error) |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Alex Gaynor | 19ff380 | 2014-10-31 10:36:03 -0700 | [diff] [blame] | 74 | // UpdateOpts specifies the base attributes that may be updated on an existing server. |
| 75 | type UpdateOpts CommonOpts |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 76 | |
| 77 | // ToUserUpdateMap formats an UpdateOpts structure into a request body. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 78 | func (opts UpdateOpts) ToUserUpdateMap() (map[string]interface{}, error) { |
| 79 | return gophercloud.BuildRequestBody(opts, "user") |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // Update is the operation responsible for updating exist users by their UUID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 83 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 84 | b, err := opts.ToUserUpdateMap() |
| 85 | if err != nil { |
| 86 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 87 | return |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 88 | } |
| 89 | _, r.Err = client.Put(ResourceURL(client, id), &b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 562a7d5 | 2015-03-24 16:20:16 +0100 | [diff] [blame] | 90 | OkCodes: []int{200}, |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 91 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 92 | return |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Delete is the operation responsible for permanently deleting an API user. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 96 | func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 97 | _, r.Err = client.Delete(ResourceURL(client, id), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 98 | return |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 99 | } |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 100 | |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 101 | // ListRoles lists the existing roles that can be assigned to users. |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 102 | func ListRoles(client *gophercloud.ServiceClient, tenantID, userID string) pagination.Pager { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 103 | return pagination.NewPager(client, listRolesURL(client, tenantID, userID), func(r pagination.PageResult) pagination.Page { |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 104 | return RolePage{pagination.SinglePageBase(r)} |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 105 | }) |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 106 | } |