Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 1 | package users |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | os "github.com/rackspace/gophercloud/openstack/identity/v2/users" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
| 11 | // List returns a pager that allows traversal over a collection of users. |
| 12 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 13 | return os.List(client) |
| 14 | } |
| 15 | |
Alex Gaynor | 7b017d1 | 2014-10-31 11:13:41 -0700 | [diff] [blame] | 16 | // CommonOpts are the options which are shared between CreateOpts and |
| 17 | // UpdateOpts |
| 18 | type CommonOpts struct { |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 19 | // Required. The username to assign to the user. When provided, the username |
| 20 | // must: |
| 21 | // - start with an alphabetical (A-Za-z) character |
| 22 | // - have a minimum length of 1 character |
| 23 | // |
| 24 | // The username may contain upper and lowercase characters, as well as any of |
| 25 | // the following special character: . - @ _ |
| 26 | Username string |
| 27 | |
| 28 | // Required. Email address for the user account. |
| 29 | Email string |
| 30 | |
| 31 | // Required. Indicates whether the user can authenticate after the user |
| 32 | // account is created. If no value is specified, the default value is true. |
| 33 | Enabled os.EnabledState |
| 34 | |
| 35 | // Optional. The password to assign to the user. If provided, the password |
| 36 | // must: |
| 37 | // - start with an alphabetical (A-Za-z) character |
| 38 | // - have a minimum length of 8 characters |
| 39 | // - contain at least one uppercase character, one lowercase character, and |
| 40 | // one numeric character. |
| 41 | // |
| 42 | // The password may contain any of the following special characters: . - @ _ |
| 43 | Password string |
| 44 | } |
| 45 | |
| 46 | // CreateOpts represents the options needed when creating new users. |
Alex Gaynor | 7b017d1 | 2014-10-31 11:13:41 -0700 | [diff] [blame] | 47 | type CreateOpts CommonOpts |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 48 | |
| 49 | // ToUserCreateMap assembles a request body based on the contents of a CreateOpts. |
| 50 | func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) { |
| 51 | m := make(map[string]interface{}) |
| 52 | |
| 53 | if opts.Username == "" { |
| 54 | return m, errors.New("Username is a required field") |
| 55 | } |
| 56 | if opts.Enabled == nil { |
| 57 | return m, errors.New("Enabled is a required field") |
| 58 | } |
| 59 | if opts.Email == "" { |
| 60 | return m, errors.New("Email is a required field") |
| 61 | } |
| 62 | |
| 63 | if opts.Username != "" { |
| 64 | m["username"] = opts.Username |
| 65 | } |
| 66 | if opts.Email != "" { |
| 67 | m["email"] = opts.Email |
| 68 | } |
| 69 | if opts.Enabled != nil { |
| 70 | m["enabled"] = opts.Enabled |
| 71 | } |
| 72 | if opts.Password != "" { |
| 73 | m["OS-KSADM:password"] = opts.Password |
| 74 | } |
| 75 | |
| 76 | return map[string]interface{}{"user": m}, nil |
| 77 | } |
| 78 | |
| 79 | // Create is the operation responsible for creating new users. |
| 80 | func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult { |
| 81 | return CreateResult{os.Create(client, opts)} |
| 82 | } |
| 83 | |
| 84 | // Get requests details on a single user, either by ID. |
| 85 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 86 | return GetResult{os.Get(client, id)} |
| 87 | } |
| 88 | |
Alex Gaynor | c6cc18f | 2014-10-31 13:48:58 -0700 | [diff] [blame] | 89 | // UpdateOptsBuilder allows extensions to add additional attributes to the Update request. |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 90 | type UpdateOptsBuilder interface { |
| 91 | ToUserUpdateMap() map[string]interface{} |
| 92 | } |
| 93 | |
| 94 | // UpdateOpts specifies the base attributes that may be updated on an existing server. |
Alex Gaynor | 7b017d1 | 2014-10-31 11:13:41 -0700 | [diff] [blame] | 95 | type UpdateOpts CommonOpts |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 96 | |
| 97 | // ToUserUpdateMap formats an UpdateOpts structure into a request body. |
| 98 | func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} { |
| 99 | m := make(map[string]interface{}) |
| 100 | |
| 101 | if opts.Username != "" { |
| 102 | m["username"] = opts.Username |
| 103 | } |
| 104 | if opts.Enabled != nil { |
| 105 | m["enabled"] = &opts.Enabled |
| 106 | } |
| 107 | if opts.Email != "" { |
| 108 | m["email"] = opts.Email |
| 109 | } |
| 110 | |
| 111 | return map[string]interface{}{"user": m} |
| 112 | } |
| 113 | |
| 114 | // Update is the operation responsible for updating exist users by their UUID. |
| 115 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { |
| 116 | var result UpdateResult |
| 117 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 118 | _, result.Err = client.Request("POST", os.ResourceURL(client, id), gophercloud.RequestOpts{ |
| 119 | JSONResponse: &result.Body, |
| 120 | JSONBody: opts.ToUserUpdateMap(), |
| 121 | OkCodes: []int{200}, |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 122 | }) |
| 123 | |
| 124 | return result |
| 125 | } |
| 126 | |
| 127 | // Delete is the operation responsible for permanently deleting an API user. |
| 128 | func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult { |
| 129 | return os.Delete(client, id) |
| 130 | } |
Paul Querna | fdc369a | 2014-10-31 11:50:20 -0700 | [diff] [blame] | 131 | |
| 132 | // ResetAPIKey resets the User's API key. |
| 133 | func ResetAPIKey(client *gophercloud.ServiceClient, id string) ResetAPIKeyResult { |
| 134 | var result ResetAPIKeyResult |
| 135 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 136 | _, result.Err = client.Request("POST", resetAPIKeyURL(client, id), gophercloud.RequestOpts{ |
| 137 | JSONResponse: &result.Body, |
| 138 | OkCodes: []int{200}, |
Paul Querna | fdc369a | 2014-10-31 11:50:20 -0700 | [diff] [blame] | 139 | }) |
| 140 | |
| 141 | return result |
| 142 | } |