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 | |
| 94 | _, res.Err = perigee.Request("POST", rootURL(client), perigee.Options{ |
| 95 | Results: &res.Body, |
| 96 | ReqBody: reqBody, |
| 97 | MoreHeaders: client.AuthenticatedHeaders(), |
| 98 | OkCodes: []int{200, 201}, |
| 99 | }) |
| 100 | |
| 101 | return res |
| 102 | } |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 103 | |
Jamie Hannaford | 69c1fe9 | 2014-10-29 13:28:58 +0100 | [diff] [blame] | 104 | // Get requests details on a single user, either by ID. |
| 105 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 106 | var result GetResult |
| 107 | |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 108 | _, result.Err = perigee.Request("GET", ResourceURL(client, id), perigee.Options{ |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 109 | Results: &result.Body, |
| 110 | MoreHeaders: client.AuthenticatedHeaders(), |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 111 | OkCodes: []int{200}, |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 112 | }) |
| 113 | |
| 114 | return result |
| 115 | } |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 116 | |
Alex Gaynor | c6cc18f | 2014-10-31 13:48:58 -0700 | [diff] [blame] | 117 | // UpdateOptsBuilder allows extensions to add additional attributes to the Update request. |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 118 | type UpdateOptsBuilder interface { |
| 119 | ToUserUpdateMap() map[string]interface{} |
| 120 | } |
| 121 | |
Alex Gaynor | 19ff380 | 2014-10-31 10:36:03 -0700 | [diff] [blame] | 122 | // UpdateOpts specifies the base attributes that may be updated on an existing server. |
| 123 | type UpdateOpts CommonOpts |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 124 | |
| 125 | // ToUserUpdateMap formats an UpdateOpts structure into a request body. |
| 126 | func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} { |
| 127 | m := make(map[string]interface{}) |
| 128 | |
| 129 | if opts.Name != "" { |
| 130 | m["name"] = opts.Name |
| 131 | } |
| 132 | if opts.Username != "" { |
| 133 | m["username"] = opts.Username |
| 134 | } |
| 135 | if opts.Enabled != nil { |
| 136 | m["enabled"] = &opts.Enabled |
| 137 | } |
| 138 | if opts.Email != "" { |
| 139 | m["email"] = opts.Email |
| 140 | } |
| 141 | if opts.TenantID != "" { |
| 142 | m["tenant_id"] = opts.TenantID |
| 143 | } |
| 144 | |
| 145 | return map[string]interface{}{"user": m} |
| 146 | } |
| 147 | |
| 148 | // Update is the operation responsible for updating exist users by their UUID. |
| 149 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { |
| 150 | var result UpdateResult |
| 151 | |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 152 | _, result.Err = perigee.Request("PUT", ResourceURL(client, id), perigee.Options{ |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 153 | Results: &result.Body, |
| 154 | ReqBody: opts.ToUserUpdateMap(), |
| 155 | MoreHeaders: client.AuthenticatedHeaders(), |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 156 | OkCodes: []int{200}, |
| 157 | }) |
| 158 | |
| 159 | return result |
| 160 | } |
| 161 | |
| 162 | // Delete is the operation responsible for permanently deleting an API user. |
| 163 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
| 164 | var result DeleteResult |
| 165 | |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 166 | _, result.Err = perigee.Request("DELETE", ResourceURL(client, id), perigee.Options{ |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 167 | MoreHeaders: client.AuthenticatedHeaders(), |
| 168 | OkCodes: []int{204}, |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 169 | }) |
| 170 | |
| 171 | return result |
| 172 | } |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 173 | |
| 174 | func ListRoles(client *gophercloud.ServiceClient, tenantID, userID string) pagination.Pager { |
| 175 | createPage := func(r pagination.PageResult) pagination.Page { |
| 176 | return RolePage{pagination.SinglePageBase(r)} |
| 177 | } |
| 178 | |
| 179 | return pagination.NewPager(client, listRolesURL(client, tenantID, userID), createPage) |
| 180 | } |