github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v2/users/requests.go (about)

     1  package users
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // List lists the existing users.
     9  func List(client *golangsdk.ServiceClient) pagination.Pager {
    10  	return pagination.NewPager(client, rootURL(client), func(r pagination.PageResult) pagination.Page {
    11  		return UserPage{pagination.SinglePageBase(r)}
    12  	})
    13  }
    14  
    15  // CommonOpts are the parameters that are shared between CreateOpts and
    16  // UpdateOpts
    17  type CommonOpts struct {
    18  	// Either a name or username is required. When provided, the value must be
    19  	// unique or a 409 conflict error will be returned. If you provide a name but
    20  	// omit a username, the latter will be set to the former; and vice versa.
    21  	Name     string `json:"name,omitempty"`
    22  	Username string `json:"username,omitempty"`
    23  
    24  	// TenantID is the ID of the tenant to which you want to assign this user.
    25  	TenantID string `json:"tenantId,omitempty"`
    26  
    27  	// Enabled indicates whether this user is enabled or not.
    28  	Enabled *bool `json:"enabled,omitempty"`
    29  
    30  	// Email is the email address of this user.
    31  	Email string `json:"email,omitempty"`
    32  }
    33  
    34  // CreateOpts represents the options needed when creating new users.
    35  type CreateOpts CommonOpts
    36  
    37  // CreateOptsBuilder allows extensions to add additional parameters to the
    38  // Create request.
    39  type CreateOptsBuilder interface {
    40  	ToUserCreateMap() (map[string]interface{}, error)
    41  }
    42  
    43  // ToUserCreateMap assembles a request body based on the contents of a
    44  // CreateOpts.
    45  func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
    46  	if opts.Name == "" && opts.Username == "" {
    47  		err := golangsdk.ErrMissingInput{}
    48  		err.Argument = "users.CreateOpts.Name/users.CreateOpts.Username"
    49  		err.Info = "Either a Name or Username must be provided"
    50  		return nil, err
    51  	}
    52  	return golangsdk.BuildRequestBody(opts, "user")
    53  }
    54  
    55  // Create is the operation responsible for creating new users.
    56  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    57  	b, err := opts.ToUserCreateMap()
    58  	if err != nil {
    59  		r.Err = err
    60  		return
    61  	}
    62  	_, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{
    63  		OkCodes: []int{200, 201},
    64  	})
    65  	return
    66  }
    67  
    68  // Get requests details on a single user, either by ID or Name.
    69  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    70  	_, r.Err = client.Get(ResourceURL(client, id), &r.Body, nil)
    71  	return
    72  }
    73  
    74  // UpdateOptsBuilder allows extensions to add additional parameters to the
    75  // Update request.
    76  type UpdateOptsBuilder interface {
    77  	ToUserUpdateMap() (map[string]interface{}, error)
    78  }
    79  
    80  // UpdateOpts specifies the base attributes that may be updated on an
    81  // existing server.
    82  type UpdateOpts CommonOpts
    83  
    84  // ToUserUpdateMap formats an UpdateOpts structure into a request body.
    85  func (opts UpdateOpts) ToUserUpdateMap() (map[string]interface{}, error) {
    86  	return golangsdk.BuildRequestBody(opts, "user")
    87  }
    88  
    89  // Update is the operation responsible for updating exist users by their ID.
    90  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
    91  	b, err := opts.ToUserUpdateMap()
    92  	if err != nil {
    93  		r.Err = err
    94  		return
    95  	}
    96  	_, r.Err = client.Put(ResourceURL(client, id), &b, &r.Body, &golangsdk.RequestOpts{
    97  		OkCodes: []int{200},
    98  	})
    99  	return
   100  }
   101  
   102  // Delete is the operation responsible for permanently deleting a User.
   103  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   104  	_, r.Err = client.Delete(ResourceURL(client, id), nil)
   105  	return
   106  }
   107  
   108  // ListRoles lists the existing roles that can be assigned to users.
   109  func ListRoles(client *golangsdk.ServiceClient, tenantID, userID string) pagination.Pager {
   110  	return pagination.NewPager(client, listRolesURL(client, tenantID, userID), func(r pagination.PageResult) pagination.Page {
   111  		return RolePage{pagination.SinglePageBase(r)}
   112  	})
   113  }