Adding support for updating users
diff --git a/openstack/identity/v2/users/requests.go b/openstack/identity/v2/users/requests.go
index 7a37836..896afb2 100644
--- a/openstack/identity/v2/users/requests.go
+++ b/openstack/identity/v2/users/requests.go
@@ -28,8 +28,7 @@
Disabled EnabledState = &iFalse
)
-// CreateOpts represents the options needed when creating new users.
-type CreateOpts struct {
+type commonOpts struct {
// Either a name or username is required. When provided, the value must be
// unique or a 409 conflict error will be returned. If you provide a name but
// omit a username, the latter will be set to the former; and vice versa.
@@ -45,6 +44,9 @@
Email string
}
+// CreateOpts represents the options needed when creating new users.
+type CreateOpts commonOpts
+
// CreateOptsBuilder describes struct types that can be accepted by the Create call.
type CreateOptsBuilder interface {
ToUserCreateMap() (map[string]interface{}, error)
@@ -70,8 +72,11 @@
if opts.Email != "" {
m["email"] = opts.Email
}
+ if opts.TenantID != "" {
+ m["tenant_id"] = opts.TenantID
+ }
- return m, nil
+ return map[string]interface{}{"user": m}, nil
}
// Create is the operation responsible for creating new users.
@@ -105,3 +110,47 @@
return result
}
+
+// UpdateOptsBuilder allows extentions to add additional attributes to the Update request.
+type UpdateOptsBuilder interface {
+ ToUserUpdateMap() map[string]interface{}
+}
+
+// UpdateOpts specifies the base attributes that may be updated on an existing server.
+type UpdateOpts commonOpts
+
+// ToUserUpdateMap formats an UpdateOpts structure into a request body.
+func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} {
+ m := make(map[string]interface{})
+
+ if opts.Name != "" {
+ m["name"] = opts.Name
+ }
+ if opts.Username != "" {
+ m["username"] = opts.Username
+ }
+ if opts.Enabled != nil {
+ m["enabled"] = &opts.Enabled
+ }
+ if opts.Email != "" {
+ m["email"] = opts.Email
+ }
+ if opts.TenantID != "" {
+ m["tenant_id"] = opts.TenantID
+ }
+
+ return map[string]interface{}{"user": m}
+}
+
+// Update is the operation responsible for updating exist users by their UUID.
+func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
+ var result UpdateResult
+
+ _, result.Err = perigee.Request("PUT", resourceURL(client, id), perigee.Options{
+ Results: &result.Body,
+ ReqBody: opts.ToUserUpdateMap(),
+ MoreHeaders: client.AuthenticatedHeaders(),
+ })
+
+ return result
+}