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

     1  package users
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  )
     6  
     7  // CreateOptsBuilder allows extensions to add additional parameters to
     8  // the Create request.
     9  type CreateOptsBuilder interface {
    10  	ToUserCreateMap() (map[string]interface{}, error)
    11  }
    12  
    13  // CreateOpts provides options used to create a user.
    14  type CreateOpts struct {
    15  	// Name is the name of the new user.
    16  	Name string `json:"name" required:"true"`
    17  
    18  	// DomainID is the ID of the domain the user belongs to.
    19  	DomainID string `json:"domain_id" required:"true"`
    20  
    21  	// Password is the password of the new user.
    22  	Password string `json:"password,omitempty"`
    23  
    24  	// Email address with a maximum of 255 characters
    25  	Email string `json:"email,omitempty"`
    26  
    27  	// AreaCode is a country code, must be used together with Phone.
    28  	AreaCode string `json:"areacode,omitempty"`
    29  
    30  	// Phone is a mobile number with a maximum of 32 digits, must be used together with AreaCode.
    31  	Phone string `json:"phone,omitempty"`
    32  
    33  	// Description is a description of the user.
    34  	Description string `json:"description,omitempty"`
    35  
    36  	// AccessMode is the access type for IAM user
    37  	AccessMode string `json:"access_mode,omitempty"`
    38  
    39  	// Enabled sets the user status to enabled or disabled.
    40  	Enabled *bool `json:"enabled,omitempty"`
    41  
    42  	// PasswordReset Indicates whether password reset is required at the first login.
    43  	// By default, password reset is true.
    44  	PasswordReset *bool `json:"pwd_status,omitempty"`
    45  }
    46  
    47  // ToUserCreateMap formats a CreateOpts into a create request.
    48  func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
    49  	b, err := golangsdk.BuildRequestBody(opts, "user")
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	return b, nil
    55  }
    56  
    57  // Create creates a new User.
    58  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    59  	b, err := opts.ToUserCreateMap()
    60  	if err != nil {
    61  		r.Err = err
    62  		return
    63  	}
    64  	_, r.Err = client.Post(createURL(client), &b, &r.Body, nil)
    65  	return
    66  }
    67  
    68  // UpdateOptsBuilder allows extensions to add additional parameters to
    69  // the Update request.
    70  type UpdateOptsBuilder interface {
    71  	ToUserUpdateMap() (map[string]interface{}, error)
    72  }
    73  
    74  // UpdateOpts provides options for updating a user account.
    75  type UpdateOpts struct {
    76  	// Name is the name of the new user.
    77  	Name string `json:"name,omitempty"`
    78  
    79  	// Password is the password of the new user.
    80  	Password string `json:"password,omitempty"`
    81  
    82  	// Email address with a maximum of 255 characters
    83  	Email string `json:"email,omitempty"`
    84  
    85  	// AreaCode is a country code, must be used together with Phone.
    86  	AreaCode string `json:"areacode,omitempty"`
    87  
    88  	// Phone is a mobile number with a maximum of 32 digits. must be used together with AreaCode.
    89  	Phone string `json:"phone,omitempty"`
    90  
    91  	// Description is a description of the user.
    92  	Description string `json:"description,omitempty"`
    93  
    94  	// AccessMode is the access type for IAM user
    95  	AccessMode string `json:"access_mode,omitempty"`
    96  
    97  	// Enabled sets the user status to enabled or disabled.
    98  	Enabled *bool `json:"enabled,omitempty"`
    99  
   100  	// PasswordReset Indicates whether password reset is required
   101  	PasswordReset *bool `json:"pwd_status,omitempty"`
   102  }
   103  
   104  // ToUserUpdateMap formats a UpdateOpts into an update request.
   105  func (opts UpdateOpts) ToUserUpdateMap() (map[string]interface{}, error) {
   106  	b, err := golangsdk.BuildRequestBody(opts, "user")
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  
   111  	return b, nil
   112  }
   113  
   114  // Update updates an existing User.
   115  func Update(client *golangsdk.ServiceClient, userID string, opts UpdateOptsBuilder) (r UpdateResult) {
   116  	b, err := opts.ToUserUpdateMap()
   117  	if err != nil {
   118  		r.Err = err
   119  		return
   120  	}
   121  	_, r.Err = client.Put(updateURL(client, userID), &b, &r.Body, &golangsdk.RequestOpts{
   122  		OkCodes: []int{200},
   123  	})
   124  	return
   125  }
   126  
   127  // Get retrieves details on a single user, by ID.
   128  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
   129  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
   130  	return
   131  }