blob: 6135bec101a49df43cb1eb9d42014e22af31a258 [file] [log] [blame]
Jamie Hannaford6e4d7952014-10-29 16:18:29 +01001package users
2
3import (
4 "errors"
5
Jamie Hannaford6e4d7952014-10-29 16:18:29 +01006 "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.
12func List(client *gophercloud.ServiceClient) pagination.Pager {
13 return os.List(client)
14}
15
Alex Gaynor7b017d12014-10-31 11:13:41 -070016// CommonOpts are the options which are shared between CreateOpts and
17// UpdateOpts
18type CommonOpts struct {
Jamie Hannaford6e4d7952014-10-29 16:18:29 +010019 // 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 Gaynor7b017d12014-10-31 11:13:41 -070047type CreateOpts CommonOpts
Jamie Hannaford6e4d7952014-10-29 16:18:29 +010048
49// ToUserCreateMap assembles a request body based on the contents of a CreateOpts.
50func (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.
80func 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.
85func Get(client *gophercloud.ServiceClient, id string) GetResult {
86 return GetResult{os.Get(client, id)}
87}
88
Alex Gaynorc6cc18f2014-10-31 13:48:58 -070089// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jamie Hannaford6e4d7952014-10-29 16:18:29 +010090type UpdateOptsBuilder interface {
91 ToUserUpdateMap() map[string]interface{}
92}
93
94// UpdateOpts specifies the base attributes that may be updated on an existing server.
Alex Gaynor7b017d12014-10-31 11:13:41 -070095type UpdateOpts CommonOpts
Jamie Hannaford6e4d7952014-10-29 16:18:29 +010096
97// ToUserUpdateMap formats an UpdateOpts structure into a request body.
98func (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.
115func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
116 var result UpdateResult
117
Ash Wilson59fb6c42015-02-12 16:21:13 -0500118 _, result.Err = client.Request("POST", os.ResourceURL(client, id), gophercloud.RequestOpts{
119 JSONResponse: &result.Body,
120 JSONBody: opts.ToUserUpdateMap(),
121 OkCodes: []int{200},
Jamie Hannaford6e4d7952014-10-29 16:18:29 +0100122 })
123
124 return result
125}
126
127// Delete is the operation responsible for permanently deleting an API user.
128func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult {
129 return os.Delete(client, id)
130}
Paul Quernafdc369a2014-10-31 11:50:20 -0700131
132// ResetAPIKey resets the User's API key.
133func ResetAPIKey(client *gophercloud.ServiceClient, id string) ResetAPIKeyResult {
134 var result ResetAPIKeyResult
135
Ash Wilson59fb6c42015-02-12 16:21:13 -0500136 _, result.Err = client.Request("POST", resetAPIKeyURL(client, id), gophercloud.RequestOpts{
137 JSONResponse: &result.Body,
138 OkCodes: []int{200},
Paul Quernafdc369a2014-10-31 11:50:20 -0700139 })
140
141 return result
142}