github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/workspace/v2/groups/requests.go (about)

     1  package groups
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is the structure required by the Create method to create a new user group.
     9  type CreateOpts struct {
    10  	// The name of user group.
    11  	Name string `json:"group_name" required:"true"`
    12  	// The type of user group. The valid types are as following:
    13  	//   AD: AD domain user group
    14  	//   LOCAL: Local liteAs user group
    15  	Type string `json:"platform_type" required:"true"`
    16  	// The description of user group.
    17  	Description string `json:"description,omitempty"`
    18  }
    19  
    20  var requestOpts = golangsdk.RequestOpts{
    21  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    22  }
    23  
    24  // Create is a method to create a user group using given parameters.
    25  func Create(c *golangsdk.ServiceClient, opts CreateOpts) error {
    26  	b, err := golangsdk.BuildRequestBody(opts, "")
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	_, err = c.Post(rootURL(c), b, nil, &golangsdk.RequestOpts{
    32  		MoreHeaders: requestOpts.MoreHeaders,
    33  	})
    34  	return err
    35  }
    36  
    37  // ListOpts is the structure required by the List method to query user group list.
    38  type ListOpts struct {
    39  	// Search keywords used to match user groups. For example, fuzzy queries based on user group names.
    40  	Keyword string `q:"keyword"`
    41  	// Number of records to be queried.
    42  	// Valid range is 0-100.
    43  	Limit int `q:"limit"`
    44  	// The offset number.
    45  	Offset int `q:"offset"`
    46  }
    47  
    48  // List is a method to query the user group details using given parameters.
    49  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]UserGroup, error) {
    50  	url := rootURL(c)
    51  	query, err := golangsdk.BuildQueryString(opts)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	url += query.String()
    56  
    57  	pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    58  		p := UserGroupPage{pagination.OffsetPageBase{PageResult: r}}
    59  		return p
    60  	})
    61  
    62  	pager.Headers = requestOpts.MoreHeaders
    63  	pages, err := pager.AllPages()
    64  
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	return ExtractUserGroupPages(pages)
    69  }
    70  
    71  // UpdateOpts is the structure required by the Update method to change user group information.
    72  type UpdateOpts struct {
    73  	// The ID of user group.
    74  	GroupID string `json:"-" required:"true"`
    75  	// The name of user group.
    76  	Name string `json:"group_name,omitempty"`
    77  	// The description of user group.
    78  	Description *string `json:"description,omitempty"`
    79  }
    80  
    81  // Update is a method to change user group information using given parameters.
    82  func Update(c *golangsdk.ServiceClient, opts UpdateOpts) error {
    83  	b, err := golangsdk.BuildRequestBody(opts, "")
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	_, err = c.Put(resourceURL(c, opts.GroupID), b, nil, &golangsdk.RequestOpts{
    89  		MoreHeaders: requestOpts.MoreHeaders,
    90  	})
    91  	return err
    92  }
    93  
    94  // Delete is a method to remove an existing user group using given parameters.
    95  func Delete(c *golangsdk.ServiceClient, groupId string) error {
    96  	_, err := c.Delete(resourceURL(c, groupId), &golangsdk.RequestOpts{
    97  		MoreHeaders: requestOpts.MoreHeaders,
    98  	})
    99  	return err
   100  }
   101  
   102  // ListUserOpts is the structure required by the ListUser method to query all user information under a user group.
   103  type ListUserOpts struct {
   104  	// The ID of user.
   105  	Name string `q:"user_name"`
   106  	// The description of user.
   107  	Description string `q:"description"`
   108  	// The activation type of user. The valid types are as following:
   109  	//   USER_ACTIVATE: User activation
   110  	//   ADMIN_ACTIVATE: Administrator activation
   111  	Type string `q:"active_type"`
   112  	// Number of records to be queried.
   113  	// Valid range is 0-2000.
   114  	Limit int `q:"limit"`
   115  	// The offset number.
   116  	Offset int `q:"offset"`
   117  }
   118  
   119  // ListUser is a method to query all user information under a user group using given parameters.
   120  func ListUser(c *golangsdk.ServiceClient, groupId string, opts ListUserOpts) ([]User, error) {
   121  	url := userURL(c, groupId)
   122  	query, err := golangsdk.BuildQueryString(opts)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  	url += query.String()
   127  
   128  	var r listUserResp
   129  	_, err = c.Get(url, &r, &golangsdk.RequestOpts{
   130  		MoreHeaders: requestOpts.MoreHeaders,
   131  	})
   132  	return r.Users, err
   133  }
   134  
   135  // ActionOpts is the structure required by the DoAction method of add users to or remove users from user group.
   136  type ActionOpts struct {
   137  	// The user ID to be added or removed from the user group.
   138  	UserIDs []string `json:"user_ids" required:"true"`
   139  	// The operation type. The valid types are as following:
   140  	//   ADD: Add user
   141  	//   DELETE: Delete user
   142  	Type string `json:"op_type" required:"true"`
   143  }
   144  
   145  // DoAction is a method of add users to or remove users from user group using given parameters.
   146  func DoAction(client *golangsdk.ServiceClient, groupId string, opts ActionOpts) error {
   147  	b, err := golangsdk.BuildRequestBody(opts, "")
   148  	if err != nil {
   149  		return err
   150  	}
   151  	_, err = client.Post(actionURL(client, groupId), &b, nil, &golangsdk.RequestOpts{
   152  		MoreHeaders: requestOpts.MoreHeaders,
   153  		OkCodes:     []int{204},
   154  	})
   155  	return err
   156  }