github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/dds/v3/users/requests.go (about)

     1  package users
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/openstack/dds/v3/roles"
     6  	"github.com/chnsz/golangsdk/pagination"
     7  )
     8  
     9  var requestOpts = golangsdk.RequestOpts{
    10  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    11  }
    12  
    13  // CreateOpts is the structure required by the Create method to create a new database user.
    14  type CreateOpts struct {
    15  	// User name.
    16  	// The length is 1~64 bits and can contain letters, numbers, hyphens, underscores and dots.
    17  	Name string `json:"user_name" required:"true"`
    18  	// Database user password.
    19  	// The length is 8~32 digits, and must be uppercase letters (A~Z), lowercase letters (a~z), numbers (0~9), special
    20  	// characters ~!@#%^*-_=+? The combination.
    21  	// It is recommended that you enter a strong password to improve security and prevent security risks such as
    22  	// password cracking by brute force.
    23  	Password string `json:"user_pwd" required:"true"`
    24  	// List of roles inherited by the newly created role.
    25  	Roles []roles.Role `json:"roles" required:"true"`
    26  	// The name of the database where the user is located.
    27  	// The length is 1~64 bits and can contain letters, numbers and underscores.
    28  	DbName string `json:"db_name,omitempty"`
    29  }
    30  
    31  // Create is a method to create a new database user using given parameters.
    32  func Create(c *golangsdk.ServiceClient, instanceId string, opts CreateOpts) error {
    33  	b, err := golangsdk.BuildRequestBody(opts, "")
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	_, err = c.Post(rootURL(c, instanceId), b, nil, &golangsdk.RequestOpts{
    39  		MoreHeaders: requestOpts.MoreHeaders,
    40  	})
    41  	return err
    42  }
    43  
    44  // ListOpts allows to filter list data using given parameters.
    45  type ListOpts struct {
    46  	// User name.
    47  	// The length is 1~64 bits and can contain letters, numbers, hyphens, underscores and dots.
    48  	Name string `q:"user_name"`
    49  	// The name of the database where the user is located.
    50  	// The length is 1~64 bits and can contain letters, numbers and underscores.
    51  	DbName string `q:"db_name"`
    52  	// The offset number.
    53  	// Default value: 0.
    54  	Offset int `q:"offset"`
    55  	// Number of records to be queried.
    56  	// Value range: 0–100.
    57  	// Default value: 100, indicating that a maximum of 1000 records can be queried and all records are displayed on
    58  	// the same page.
    59  	Limit int `q:"limit"`
    60  }
    61  
    62  // List is a method to query the list of the users using given opts.
    63  func List(c *golangsdk.ServiceClient, instanceId string, opts ListOpts) ([]UserResp, error) {
    64  	url := resourceURL(c, instanceId)
    65  	query, err := golangsdk.BuildQueryString(opts)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	url += query.String()
    70  
    71  	var result []UserResp
    72  	err = pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    73  		p := UserPage{pagination.OffsetPageBase{PageResult: r}}
    74  		return p
    75  	}).EachPage(func(page pagination.Page) (bool, error) {
    76  		resp, err := ExtractUsers(page)
    77  		if err != nil {
    78  			return false, err
    79  		}
    80  		if len(resp) == 0 {
    81  			return false, nil
    82  		}
    83  		result = append(result, resp...)
    84  		return true, nil
    85  	})
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	return result, err
    90  }
    91  
    92  // PwdResetOpts is the structure required by the ResetPassword method to reset the database user password.
    93  type PwdResetOpts struct {
    94  	// New user password for reset.
    95  	// The length is 8~32 digits, and must be uppercase letters (A~Z), lowercase letters (a~z), numbers (0~9), special
    96  	// characters ~!@#%^*-_=+? The combination.
    97  	// It is recommended that you enter a strong password to improve security and prevent security risks such as
    98  	// password cracking by brute force.
    99  	Password string `json:"user_pwd" required:"true"`
   100  	// User name. Defaults to "rwuser".
   101  	// The length is 1~64 bits and can contain letters, numbers, hyphens, underscores and dots.
   102  	Name string `json:"user_name,omitempty"`
   103  	// The name of the database where the user is located.
   104  	// The length is 1~64 bits and can contain letters, numbers and underscores.
   105  	DbName string `json:"db_name,omitempty"`
   106  }
   107  
   108  // ResetPassword is a method to reset the database user password.
   109  func ResetPassword(c *golangsdk.ServiceClient, instanceId string, opts PwdResetOpts) error {
   110  	b, err := golangsdk.BuildRequestBody(opts, "")
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	_, err = c.Put(pwdResetURL(c, instanceId), b, nil, &golangsdk.RequestOpts{
   116  		MoreHeaders: requestOpts.MoreHeaders,
   117  	})
   118  	return err
   119  }
   120  
   121  // DeleteOpts is the structure required by the Delete method to remove an existing database user.
   122  type DeleteOpts struct {
   123  	// User name.
   124  	// The length is 1~64 bits and can contain letters, numbers, hyphens, underscores and dots.
   125  	Name string `json:"user_name" required:"true"`
   126  	// The name of the database where the user is located.
   127  	// The length is 1~64 bits and can contain letters, numbers and underscores.
   128  	DbName string `json:"db_name" required:"true"`
   129  }
   130  
   131  // Delete is a method to remove an existing database user.
   132  func Delete(c *golangsdk.ServiceClient, instanceId string, opts DeleteOpts) error {
   133  	url := rootURL(c, instanceId)
   134  	b, err := golangsdk.BuildRequestBody(opts, "")
   135  	if err != nil {
   136  		return err
   137  	}
   138  
   139  	_, err = c.DeleteWithBody(url, b, &golangsdk.RequestOpts{
   140  		MoreHeaders: requestOpts.MoreHeaders,
   141  	})
   142  	return err
   143  }