dsl struct tags; wip
diff --git a/openstack/identity/v2/users/requests.go b/openstack/identity/v2/users/requests.go
index 7fa5fc3..f62f979 100644
--- a/openstack/identity/v2/users/requests.go
+++ b/openstack/identity/v2/users/requests.go
@@ -7,41 +7,25 @@
// List lists the existing users.
func List(client *gophercloud.ServiceClient) pagination.Pager {
- createPage := func(r pagination.PageResult) pagination.Page {
+ return pagination.NewPager(client, rootURL(client), func(r pagination.PageResult) pagination.Page {
return UserPage{pagination.SinglePageBase(r)}
- }
-
- return pagination.NewPager(client, rootURL(client), createPage)
+ })
}
-// EnabledState represents whether the user is enabled or not.
-type EnabledState *bool
-
-// Useful variables to use when creating or updating users.
-var (
- iTrue = true
- iFalse = false
-
- Enabled EnabledState = &iTrue
- Disabled EnabledState = &iFalse
-)
-
// CommonOpts are the parameters that are shared between CreateOpts and
// UpdateOpts
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.
- Name, Username string
-
+ Name string `json:"name,omitempty"`
+ Username string `json:"username,omitempty"`
// The ID of the tenant to which you want to assign this user.
- TenantID string
-
+ TenantID string `json:"tenant_id,omitempty"`
// Indicates whether this user is enabled or not.
- Enabled EnabledState
-
+ Enabled *bool `json:"enabled,omitempty"`
// The email address of this user.
- Email string
+ Email string `json:"email,omitempty"`
}
// CreateOpts represents the options needed when creating new users.
@@ -54,33 +38,13 @@
// ToUserCreateMap assembles a request body based on the contents of a CreateOpts.
func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
- m := make(map[string]interface{})
-
if opts.Name == "" && opts.Username == "" {
err := gophercloud.ErrMissingInput{}
- err.Function = "users.ToUserCreateMap"
err.Argument = "users.CreateOpts.Name/users.CreateOpts.Username"
err.Info = "Either a Name or Username must be provided"
- return m, err
+ return nil, err
}
-
- 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}, nil
+ return gophercloud.BuildRequestBody(opts, "user")
}
// Create is the operation responsible for creating new users.
@@ -109,57 +73,41 @@
// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
type UpdateOptsBuilder interface {
- ToUserUpdateMap() map[string]interface{}
+ ToUserUpdateMap() (map[string]interface{}, error)
}
// 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}
+func (opts UpdateOpts) ToUserUpdateMap() (map[string]interface{}, error) {
+ return gophercloud.BuildRequestBody(opts, "user")
}
// 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
- reqBody := opts.ToUserUpdateMap()
- _, result.Err = client.Put(ResourceURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{
+ var r UpdateResult
+ b, err := opts.ToUserUpdateMap()
+ if err != nil {
+ r.Err = err
+ return r
+ }
+ _, r.Err = client.Put(ResourceURL(client, id), &b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
- return result
+ return r
}
// Delete is the operation responsible for permanently deleting an API user.
func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
- var result DeleteResult
- _, result.Err = client.Delete(ResourceURL(client, id), nil)
- return result
+ var r DeleteResult
+ _, r.Err = client.Delete(ResourceURL(client, id), nil)
+ return r
}
// ListRoles lists the existing roles that can be assigned to users.
func ListRoles(client *gophercloud.ServiceClient, tenantID, userID string) pagination.Pager {
- createPage := func(r pagination.PageResult) pagination.Page {
+ return pagination.NewPager(client, listRolesURL(client, tenantID, userID), func(r pagination.PageResult) pagination.Page {
return RolePage{pagination.SinglePageBase(r)}
- }
-
- return pagination.NewPager(client, listRolesURL(client, tenantID, userID), createPage)
+ })
}