blob: 896afb21cf55a75e7fa3e8ed8722509c8c56ff89 [file] [log] [blame]
Jamie Hannaford2a130242014-10-28 11:19:46 +01001package users
Jamie Hannaford929bd002014-10-29 11:14:25 +01002
3import (
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +01004 "errors"
5
6 "github.com/racker/perigee"
Jamie Hannaford929bd002014-10-29 11:14:25 +01007 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
11func List(client *gophercloud.ServiceClient) pagination.Pager {
12 createPage := func(r pagination.PageResult) pagination.Page {
13 return UserPage{pagination.SinglePageBase(r)}
14 }
15
16 return pagination.NewPager(client, rootURL(client), createPage)
17}
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010018
19// EnabledState represents whether the user is enabled or not.
20type EnabledState *bool
21
22// Useful variables to use when creating or updating users.
23var (
24 iTrue = true
25 iFalse = false
26
27 Enabled EnabledState = &iTrue
28 Disabled EnabledState = &iFalse
29)
30
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010031type commonOpts struct {
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010032 // Either a name or username is required. When provided, the value must be
33 // unique or a 409 conflict error will be returned. If you provide a name but
34 // omit a username, the latter will be set to the former; and vice versa.
35 Name, Username string
36
37 // The ID of the tenant to which you want to assign this user.
38 TenantID string
39
40 // Indicates whether this user is enabled or not.
41 Enabled EnabledState
42
43 // The email address of this user.
44 Email string
45}
46
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010047// CreateOpts represents the options needed when creating new users.
48type CreateOpts commonOpts
49
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010050// CreateOptsBuilder describes struct types that can be accepted by the Create call.
51type CreateOptsBuilder interface {
52 ToUserCreateMap() (map[string]interface{}, error)
53}
54
55// ToUserCreateMap assembles a request body based on the contents of a CreateOpts.
56func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
57 m := make(map[string]interface{})
58
59 if opts.Name == "" && opts.Username == "" {
60 return m, errors.New("Either a Name or Username must be provided")
61 }
62
63 if opts.Name != "" {
64 m["name"] = opts.Name
65 }
66 if opts.Username != "" {
67 m["username"] = opts.Username
68 }
69 if opts.Enabled != nil {
70 m["enabled"] = &opts.Enabled
71 }
72 if opts.Email != "" {
73 m["email"] = opts.Email
74 }
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010075 if opts.TenantID != "" {
76 m["tenant_id"] = opts.TenantID
77 }
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010078
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010079 return map[string]interface{}{"user": m}, nil
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010080}
81
82// Create is the operation responsible for creating new users.
83func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
84 var res CreateResult
85
86 reqBody, err := opts.ToUserCreateMap()
87 if err != nil {
88 res.Err = err
89 return res
90 }
91
92 _, res.Err = perigee.Request("POST", rootURL(client), perigee.Options{
93 Results: &res.Body,
94 ReqBody: reqBody,
95 MoreHeaders: client.AuthenticatedHeaders(),
96 OkCodes: []int{200, 201},
97 })
98
99 return res
100}
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100101
Jamie Hannaford69c1fe92014-10-29 13:28:58 +0100102// Get requests details on a single user, either by ID.
103func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100104 var result GetResult
105
Jamie Hannaford69c1fe92014-10-29 13:28:58 +0100106 _, result.Err = perigee.Request("GET", resourceURL(client, id), perigee.Options{
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +0100107 Results: &result.Body,
108 MoreHeaders: client.AuthenticatedHeaders(),
109 })
110
111 return result
112}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100113
114// UpdateOptsBuilder allows extentions to add additional attributes to the Update request.
115type UpdateOptsBuilder interface {
116 ToUserUpdateMap() map[string]interface{}
117}
118
119// UpdateOpts specifies the base attributes that may be updated on an existing server.
120type UpdateOpts commonOpts
121
122// ToUserUpdateMap formats an UpdateOpts structure into a request body.
123func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} {
124 m := make(map[string]interface{})
125
126 if opts.Name != "" {
127 m["name"] = opts.Name
128 }
129 if opts.Username != "" {
130 m["username"] = opts.Username
131 }
132 if opts.Enabled != nil {
133 m["enabled"] = &opts.Enabled
134 }
135 if opts.Email != "" {
136 m["email"] = opts.Email
137 }
138 if opts.TenantID != "" {
139 m["tenant_id"] = opts.TenantID
140 }
141
142 return map[string]interface{}{"user": m}
143}
144
145// Update is the operation responsible for updating exist users by their UUID.
146func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
147 var result UpdateResult
148
149 _, result.Err = perigee.Request("PUT", resourceURL(client, id), perigee.Options{
150 Results: &result.Body,
151 ReqBody: opts.ToUserUpdateMap(),
152 MoreHeaders: client.AuthenticatedHeaders(),
153 })
154
155 return result
156}