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