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