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

     1  package users
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/openstack/identity/v3/groups"
     6  	"github.com/huaweicloud/golangsdk/openstack/identity/v3/projects"
     7  	"github.com/huaweicloud/golangsdk/pagination"
     8  )
     9  
    10  // Option is a specific option defined at the API to enable features
    11  // on a user account.
    12  type Option string
    13  
    14  const (
    15  	IgnoreChangePasswordUponFirstUse Option = "ignore_change_password_upon_first_use"
    16  	IgnorePasswordExpiry             Option = "ignore_password_expiry"
    17  	IgnoreLockoutFailureAttempts     Option = "ignore_lockout_failure_attempts"
    18  	MultiFactorAuthRules             Option = "multi_factor_auth_rules"
    19  	MultiFactorAuthEnabled           Option = "multi_factor_auth_enabled"
    20  )
    21  
    22  // ListOptsBuilder allows extensions to add additional parameters to
    23  // the List request
    24  type ListOptsBuilder interface {
    25  	ToUserListQuery() (string, error)
    26  }
    27  
    28  // ListOpts provides options to filter the List results.
    29  type ListOpts struct {
    30  	// DomainID filters the response by a domain ID.
    31  	DomainID string `q:"domain_id"`
    32  
    33  	// Enabled filters the response by enabled users.
    34  	Enabled *bool `q:"enabled"`
    35  
    36  	// Name filters the response by username.
    37  	Name string `q:"name"`
    38  }
    39  
    40  // ToUserListQuery formats a ListOpts into a query string.
    41  func (opts ListOpts) ToUserListQuery() (string, error) {
    42  	q, err := golangsdk.BuildQueryString(opts)
    43  	return q.String(), err
    44  }
    45  
    46  // List enumerates the Users to which the current token has access.
    47  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    48  	url := listURL(client)
    49  	if opts != nil {
    50  		query, err := opts.ToUserListQuery()
    51  		if err != nil {
    52  			return pagination.Pager{Err: err}
    53  		}
    54  		url += query
    55  	}
    56  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    57  		return UserPage{pagination.LinkedPageBase{PageResult: r}}
    58  	})
    59  }
    60  
    61  // Get retrieves details on a single user, by ID.
    62  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    63  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
    64  	return
    65  }
    66  
    67  // CreateOptsBuilder allows extensions to add additional parameters to
    68  // the Create request.
    69  type CreateOptsBuilder interface {
    70  	ToUserCreateMap() (map[string]interface{}, error)
    71  }
    72  
    73  // CreateOpts provides options used to create a user.
    74  type CreateOpts struct {
    75  	// Name is the name of the new user.
    76  	Name string `json:"name" required:"true"`
    77  
    78  	// DefaultProjectID is the ID of the default project of the user.
    79  	DefaultProjectID string `json:"default_project_id,omitempty"`
    80  
    81  	// DomainID is the ID of the domain the user belongs to.
    82  	DomainID string `json:"domain_id,omitempty"`
    83  
    84  	// Enabled sets the user status to enabled or disabled.
    85  	Enabled *bool `json:"enabled,omitempty"`
    86  
    87  	// Password is the password of the new user.
    88  	Password string `json:"password,omitempty"`
    89  
    90  	// Description is a description of the user.
    91  	Description string `json:"description,omitempty"`
    92  }
    93  
    94  // ToUserCreateMap formats a CreateOpts into a create request.
    95  func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
    96  	b, err := golangsdk.BuildRequestBody(opts, "user")
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  
   101  	return b, nil
   102  }
   103  
   104  // Create creates a new User.
   105  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   106  	b, err := opts.ToUserCreateMap()
   107  	if err != nil {
   108  		r.Err = err
   109  		return
   110  	}
   111  	_, r.Err = client.Post(createURL(client), &b, &r.Body, &golangsdk.RequestOpts{
   112  		OkCodes: []int{201},
   113  	})
   114  	return
   115  }
   116  
   117  // UpdateOptsBuilder allows extensions to add additional parameters to
   118  // the Update request.
   119  type UpdateOptsBuilder interface {
   120  	ToUserUpdateMap() (map[string]interface{}, error)
   121  }
   122  
   123  // UpdateOpts provides options for updating a user account.
   124  type UpdateOpts struct {
   125  	// Name is the name of the new user.
   126  	Name string `json:"name,omitempty"`
   127  
   128  	// DefaultProjectID is the ID of the default project of the user.
   129  	DefaultProjectID string `json:"default_project_id,omitempty"`
   130  
   131  	// DomainID is the ID of the domain the user belongs to.
   132  	DomainID string `json:"domain_id,omitempty"`
   133  
   134  	// Enabled sets the user status to enabled or disabled.
   135  	Enabled *bool `json:"enabled,omitempty"`
   136  
   137  	// Password is the password of the new user.
   138  	Password string `json:"password,omitempty"`
   139  
   140  	// Description is a description of the user.
   141  	Description string `json:"description,omitempty"`
   142  }
   143  
   144  // ToUserUpdateMap formats a UpdateOpts into an update request.
   145  func (opts UpdateOpts) ToUserUpdateMap() (map[string]interface{}, error) {
   146  	b, err := golangsdk.BuildRequestBody(opts, "user")
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	return b, nil
   152  }
   153  
   154  // Update updates an existing User.
   155  func Update(client *golangsdk.ServiceClient, userID string, opts UpdateOptsBuilder) (r UpdateResult) {
   156  	b, err := opts.ToUserUpdateMap()
   157  	if err != nil {
   158  		r.Err = err
   159  		return
   160  	}
   161  	_, r.Err = client.Patch(updateURL(client, userID), &b, &r.Body, &golangsdk.RequestOpts{
   162  		OkCodes: []int{200},
   163  	})
   164  	return
   165  }
   166  
   167  // Delete deletes a user.
   168  func Delete(client *golangsdk.ServiceClient, userID string) (r DeleteResult) {
   169  	_, r.Err = client.Delete(deleteURL(client, userID), nil)
   170  	return
   171  }
   172  
   173  // ListGroups enumerates groups user belongs to.
   174  func ListGroups(client *golangsdk.ServiceClient, userID string) pagination.Pager {
   175  	url := listGroupsURL(client, userID)
   176  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   177  		return groups.GroupPage{LinkedPageBase: pagination.LinkedPageBase{PageResult: r}}
   178  	})
   179  }
   180  
   181  // ListProjects enumerates groups user belongs to.
   182  func ListProjects(client *golangsdk.ServiceClient, userID string) pagination.Pager {
   183  	url := listProjectsURL(client, userID)
   184  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   185  		return projects.ProjectPage{LinkedPageBase: pagination.LinkedPageBase{PageResult: r}}
   186  	})
   187  }
   188  
   189  // ListInGroup enumerates users that belong to a group.
   190  func ListInGroup(client *golangsdk.ServiceClient, groupID string, opts ListOptsBuilder) pagination.Pager {
   191  	url := listInGroupURL(client, groupID)
   192  	if opts != nil {
   193  		query, err := opts.ToUserListQuery()
   194  		if err != nil {
   195  			return pagination.Pager{Err: err}
   196  		}
   197  		url += query
   198  	}
   199  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   200  		return UserPage{pagination.LinkedPageBase{PageResult: r}}
   201  	})
   202  }
   203  
   204  // Add a user into one group
   205  func AddToGroup(client *golangsdk.ServiceClient, groupID string, userID string) (r AddMembershipResult) {
   206  	_, r.Err = client.Put(membershipURL(client, groupID, userID), nil, nil, &golangsdk.RequestOpts{
   207  		OkCodes: []int{204},
   208  	})
   209  	return
   210  }
   211  
   212  // Remove user from group
   213  func RemoveFromGroup(client *golangsdk.ServiceClient, groupID string, userID string) (r DeleteResult) {
   214  	_, r.Err = client.Delete(membershipURL(client, groupID, userID), nil)
   215  	return
   216  }