| 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 { | 
|  | 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 Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 16 |  | 
|  | 17 | // EnabledState represents whether the user is enabled or not. | 
|  | 18 | type EnabledState *bool | 
|  | 19 |  | 
|  | 20 | // Useful variables to use when creating or updating users. | 
|  | 21 | var ( | 
|  | 22 | iTrue  = true | 
|  | 23 | iFalse = false | 
|  | 24 |  | 
|  | 25 | Enabled  EnabledState = &iTrue | 
|  | 26 | Disabled EnabledState = &iFalse | 
|  | 27 | ) | 
|  | 28 |  | 
| Alex Gaynor | d86dfd5 | 2014-10-31 10:59:48 -0700 | [diff] [blame] | 29 | // CommonOpts are the parameters that are shared between CreateOpts and | 
|  | 30 | // UpdateOpts | 
| Alex Gaynor | 19ff380 | 2014-10-31 10:36:03 -0700 | [diff] [blame] | 31 | type CommonOpts struct { | 
| Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 32 | // 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 Gaynor | 19ff380 | 2014-10-31 10:36:03 -0700 | [diff] [blame] | 47 | // CreateOpts represents the options needed when creating new users. | 
|  | 48 | type CreateOpts CommonOpts | 
|  | 49 |  | 
| Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 50 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. | 
|  | 51 | type CreateOptsBuilder interface { | 
|  | 52 | ToUserCreateMap() (map[string]interface{}, error) | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | // ToUserCreateMap assembles a request body based on the contents of a CreateOpts. | 
|  | 56 | func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) { | 
|  | 57 | m := make(map[string]interface{}) | 
|  | 58 |  | 
|  | 59 | if opts.Name == "" && opts.Username == "" { | 
| Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 60 | 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 Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 65 | } | 
|  | 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 Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 79 | if opts.TenantID != "" { | 
|  | 80 | m["tenant_id"] = opts.TenantID | 
|  | 81 | } | 
| Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 82 |  | 
| Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 83 | return map[string]interface{}{"user": m}, nil | 
| Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
|  | 86 | // Create is the operation responsible for creating new users. | 
|  | 87 | func 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 Hannaford | 562a7d5 | 2015-03-24 16:20:16 +0100 | [diff] [blame] | 96 | _, res.Err = client.Post(rootURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{ | 
|  | 97 | OkCodes: []int{200, 201}, | 
| Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 98 | }) | 
|  | 99 |  | 
|  | 100 | return res | 
|  | 101 | } | 
| Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 102 |  | 
| Jamie Hannaford | 69c1fe9 | 2014-10-29 13:28:58 +0100 | [diff] [blame] | 103 | // Get requests details on a single user, either by ID. | 
|  | 104 | func Get(client *gophercloud.ServiceClient, id string) GetResult { | 
| Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 105 | var result GetResult | 
| Jamie Hannaford | 562a7d5 | 2015-03-24 16:20:16 +0100 | [diff] [blame] | 106 | _, result.Err = client.Get(ResourceURL(client, id), &result.Body, nil) | 
| Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 107 | return result | 
|  | 108 | } | 
| Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 109 |  | 
| Alex Gaynor | c6cc18f | 2014-10-31 13:48:58 -0700 | [diff] [blame] | 110 | // UpdateOptsBuilder allows extensions to add additional attributes to the Update request. | 
| Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 111 | type UpdateOptsBuilder interface { | 
|  | 112 | ToUserUpdateMap() map[string]interface{} | 
|  | 113 | } | 
|  | 114 |  | 
| Alex Gaynor | 19ff380 | 2014-10-31 10:36:03 -0700 | [diff] [blame] | 115 | // UpdateOpts specifies the base attributes that may be updated on an existing server. | 
|  | 116 | type UpdateOpts CommonOpts | 
| Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 117 |  | 
|  | 118 | // ToUserUpdateMap formats an UpdateOpts structure into a request body. | 
|  | 119 | func (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. | 
|  | 142 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { | 
|  | 143 | var result UpdateResult | 
| Jamie Hannaford | 562a7d5 | 2015-03-24 16:20:16 +0100 | [diff] [blame] | 144 | reqBody := opts.ToUserUpdateMap() | 
|  | 145 | _, result.Err = client.Put(ResourceURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{ | 
|  | 146 | OkCodes: []int{200}, | 
| Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 147 | }) | 
| Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 148 | return result | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | // Delete is the operation responsible for permanently deleting an API user. | 
|  | 152 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { | 
|  | 153 | var result DeleteResult | 
| Jamie Hannaford | 562a7d5 | 2015-03-24 16:20:16 +0100 | [diff] [blame] | 154 | _, result.Err = client.Delete(ResourceURL(client, id), nil) | 
| Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 155 | return result | 
|  | 156 | } | 
| Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 157 |  | 
| Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 158 | // ListRoles lists the existing roles that can be assigned to users. | 
| Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 159 | func 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 | } |