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 ( |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 4 | "errors" |
| 5 | |
| 6 | "github.com/racker/perigee" |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
| 11 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 12 | createPage := func(r pagination.PageResult) pagination.Page { |
| 13 | return UserPage{pagination.SinglePageBase(r)} |
| 14 | } |
| 15 | |
| 16 | return pagination.NewPager(client, rootURL(client), createPage) |
| 17 | } |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 18 | |
| 19 | // EnabledState represents whether the user is enabled or not. |
| 20 | type EnabledState *bool |
| 21 | |
| 22 | // Useful variables to use when creating or updating users. |
| 23 | var ( |
| 24 | iTrue = true |
| 25 | iFalse = false |
| 26 | |
| 27 | Enabled EnabledState = &iTrue |
| 28 | Disabled EnabledState = &iFalse |
| 29 | ) |
| 30 | |
Alex Gaynor | d86dfd5 | 2014-10-31 10:59:48 -0700 | [diff] [blame] | 31 | // CommonOpts are the parameters that are shared between CreateOpts and |
| 32 | // UpdateOpts |
Alex Gaynor | 19ff380 | 2014-10-31 10:36:03 -0700 | [diff] [blame] | 33 | type CommonOpts struct { |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 34 | // Either a name or username is required. When provided, the value must be |
| 35 | // unique or a 409 conflict error will be returned. If you provide a name but |
| 36 | // omit a username, the latter will be set to the former; and vice versa. |
| 37 | Name, Username string |
| 38 | |
| 39 | // The ID of the tenant to which you want to assign this user. |
| 40 | TenantID string |
| 41 | |
| 42 | // Indicates whether this user is enabled or not. |
| 43 | Enabled EnabledState |
| 44 | |
| 45 | // The email address of this user. |
| 46 | Email string |
| 47 | } |
| 48 | |
Alex Gaynor | 19ff380 | 2014-10-31 10:36:03 -0700 | [diff] [blame] | 49 | // CreateOpts represents the options needed when creating new users. |
| 50 | type CreateOpts CommonOpts |
| 51 | |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 52 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. |
| 53 | type CreateOptsBuilder interface { |
| 54 | ToUserCreateMap() (map[string]interface{}, error) |
| 55 | } |
| 56 | |
| 57 | // ToUserCreateMap assembles a request body based on the contents of a CreateOpts. |
| 58 | func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) { |
| 59 | m := make(map[string]interface{}) |
| 60 | |
| 61 | if opts.Name == "" && opts.Username == "" { |
| 62 | return m, errors.New("Either a Name or Username must be provided") |
| 63 | } |
| 64 | |
| 65 | if opts.Name != "" { |
| 66 | m["name"] = opts.Name |
| 67 | } |
| 68 | if opts.Username != "" { |
| 69 | m["username"] = opts.Username |
| 70 | } |
| 71 | if opts.Enabled != nil { |
| 72 | m["enabled"] = &opts.Enabled |
| 73 | } |
| 74 | if opts.Email != "" { |
| 75 | m["email"] = opts.Email |
| 76 | } |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 77 | if opts.TenantID != "" { |
| 78 | m["tenant_id"] = opts.TenantID |
| 79 | } |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 80 | |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 81 | return map[string]interface{}{"user": m}, nil |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Create is the operation responsible for creating new users. |
| 85 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
| 86 | var res CreateResult |
| 87 | |
| 88 | reqBody, err := opts.ToUserCreateMap() |
| 89 | if err != nil { |
| 90 | res.Err = err |
| 91 | return res |
| 92 | } |
| 93 | |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 94 | _, res.Err = client.Request("POST", rootURL(client), gophercloud.RequestOpts{ |
| 95 | JSONResponse: &res.Body, |
| 96 | JSONBody: reqBody, |
| 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 |
| 106 | |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 107 | _, result.Err = perigee.Request("GET", ResourceURL(client, id), perigee.Options{ |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 108 | Results: &result.Body, |
| 109 | MoreHeaders: client.AuthenticatedHeaders(), |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 110 | OkCodes: []int{200}, |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 111 | }) |
| 112 | |
| 113 | return result |
| 114 | } |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 115 | |
Alex Gaynor | c6cc18f | 2014-10-31 13:48:58 -0700 | [diff] [blame] | 116 | // UpdateOptsBuilder allows extensions to add additional attributes to the Update request. |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 117 | type UpdateOptsBuilder interface { |
| 118 | ToUserUpdateMap() map[string]interface{} |
| 119 | } |
| 120 | |
Alex Gaynor | 19ff380 | 2014-10-31 10:36:03 -0700 | [diff] [blame] | 121 | // UpdateOpts specifies the base attributes that may be updated on an existing server. |
| 122 | type UpdateOpts CommonOpts |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 123 | |
| 124 | // ToUserUpdateMap formats an UpdateOpts structure into a request body. |
| 125 | func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} { |
| 126 | m := make(map[string]interface{}) |
| 127 | |
| 128 | if opts.Name != "" { |
| 129 | m["name"] = opts.Name |
| 130 | } |
| 131 | if opts.Username != "" { |
| 132 | m["username"] = opts.Username |
| 133 | } |
| 134 | if opts.Enabled != nil { |
| 135 | m["enabled"] = &opts.Enabled |
| 136 | } |
| 137 | if opts.Email != "" { |
| 138 | m["email"] = opts.Email |
| 139 | } |
| 140 | if opts.TenantID != "" { |
| 141 | m["tenant_id"] = opts.TenantID |
| 142 | } |
| 143 | |
| 144 | return map[string]interface{}{"user": m} |
| 145 | } |
| 146 | |
| 147 | // Update is the operation responsible for updating exist users by their UUID. |
| 148 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { |
| 149 | var result UpdateResult |
| 150 | |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 151 | _, result.Err = perigee.Request("PUT", ResourceURL(client, id), perigee.Options{ |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 152 | Results: &result.Body, |
| 153 | ReqBody: opts.ToUserUpdateMap(), |
| 154 | MoreHeaders: client.AuthenticatedHeaders(), |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 155 | OkCodes: []int{200}, |
| 156 | }) |
| 157 | |
| 158 | return result |
| 159 | } |
| 160 | |
| 161 | // Delete is the operation responsible for permanently deleting an API user. |
| 162 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
| 163 | var result DeleteResult |
| 164 | |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 165 | _, result.Err = perigee.Request("DELETE", ResourceURL(client, id), perigee.Options{ |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 166 | MoreHeaders: client.AuthenticatedHeaders(), |
| 167 | OkCodes: []int{204}, |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 168 | }) |
| 169 | |
| 170 | return result |
| 171 | } |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 172 | |
| 173 | func ListRoles(client *gophercloud.ServiceClient, tenantID, userID string) pagination.Pager { |
| 174 | createPage := func(r pagination.PageResult) pagination.Page { |
| 175 | return RolePage{pagination.SinglePageBase(r)} |
| 176 | } |
| 177 | |
| 178 | return pagination.NewPager(client, listRolesURL(client, tenantID, userID), createPage) |
| 179 | } |