blob: 07c7c97ab00e84619eb775c6e815aaf87fd5e9ed [file] [log] [blame]
Jamie Hannaford6e4d7952014-10-29 16:18:29 +01001package users
2
3import (
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.
13func List(client *gophercloud.ServiceClient) pagination.Pager {
14 return os.List(client)
15}
16
17type commonOpts struct {
18 // Required. The username to assign to the user. When provided, the username
19 // must:
20 // - start with an alphabetical (A-Za-z) character
21 // - have a minimum length of 1 character
22 //
23 // The username may contain upper and lowercase characters, as well as any of
24 // the following special character: . - @ _
25 Username string
26
27 // Required. Email address for the user account.
28 Email string
29
30 // Required. Indicates whether the user can authenticate after the user
31 // account is created. If no value is specified, the default value is true.
32 Enabled os.EnabledState
33
34 // Optional. The password to assign to the user. If provided, the password
35 // must:
36 // - start with an alphabetical (A-Za-z) character
37 // - have a minimum length of 8 characters
38 // - contain at least one uppercase character, one lowercase character, and
39 // one numeric character.
40 //
41 // The password may contain any of the following special characters: . - @ _
42 Password string
43}
44
45// CreateOpts represents the options needed when creating new users.
46type CreateOpts commonOpts
47
48// ToUserCreateMap assembles a request body based on the contents of a CreateOpts.
49func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
50 m := make(map[string]interface{})
51
52 if opts.Username == "" {
53 return m, errors.New("Username is a required field")
54 }
55 if opts.Enabled == nil {
56 return m, errors.New("Enabled is a required field")
57 }
58 if opts.Email == "" {
59 return m, errors.New("Email is a required field")
60 }
61
62 if opts.Username != "" {
63 m["username"] = opts.Username
64 }
65 if opts.Email != "" {
66 m["email"] = opts.Email
67 }
68 if opts.Enabled != nil {
69 m["enabled"] = opts.Enabled
70 }
71 if opts.Password != "" {
72 m["OS-KSADM:password"] = opts.Password
73 }
74
75 return map[string]interface{}{"user": m}, nil
76}
77
78// Create is the operation responsible for creating new users.
79func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult {
80 return CreateResult{os.Create(client, opts)}
81}
82
83// Get requests details on a single user, either by ID.
84func Get(client *gophercloud.ServiceClient, id string) GetResult {
85 return GetResult{os.Get(client, id)}
86}
87
88// UpdateOptsBuilder allows extentions to add additional attributes to the Update request.
89type UpdateOptsBuilder interface {
90 ToUserUpdateMap() map[string]interface{}
91}
92
93// UpdateOpts specifies the base attributes that may be updated on an existing server.
94type UpdateOpts commonOpts
95
96// ToUserUpdateMap formats an UpdateOpts structure into a request body.
97func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} {
98 m := make(map[string]interface{})
99
100 if opts.Username != "" {
101 m["username"] = opts.Username
102 }
103 if opts.Enabled != nil {
104 m["enabled"] = &opts.Enabled
105 }
106 if opts.Email != "" {
107 m["email"] = opts.Email
108 }
109
110 return map[string]interface{}{"user": m}
111}
112
113// Update is the operation responsible for updating exist users by their UUID.
114func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
115 var result UpdateResult
116
117 _, result.Err = perigee.Request("POST", os.ResourceURL(client, id), perigee.Options{
118 Results: &result.Body,
119 ReqBody: opts.ToUserUpdateMap(),
120 MoreHeaders: client.AuthenticatedHeaders(),
121 OkCodes: []int{200},
122 })
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}