github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/workspace/v2/users/requests.go (about) 1 package users 2 3 import "github.com/chnsz/golangsdk" 4 5 // CreateOpts is the structure required by the Create method to create a new user. 6 type CreateOpts struct { 7 // User name. 8 Name string `json:"user_name" required:"true"` 9 // User email. The value can contain from 1 to 64 characters. 10 Email string `json:"user_email" required:"true"` 11 // The expires time of Workspace user. The format is "yyyy-MM-ddTHH:mm:ss.000 Z". 12 // 0 means it will never expire. 13 AccountExpires string `json:"account_expires,omitempty"` 14 // Whether to allow password modification. 15 EnableChangePassword *bool `json:"enable_change_password,omitempty"` 16 // Whether the next login requires a password reset. 17 NextLoginChangePassword *bool `json:"next_login_change_password,omitempty"` 18 // Group ID list. 19 GroupIds []string `json:"group_ids,omitempty"` 20 // User description. 21 Description string `json:"description,omitempty"` 22 // Alias name. 23 Alias string `json:"alias_name,omitempty"` 24 } 25 26 var requestOpts = golangsdk.RequestOpts{ 27 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 28 } 29 30 // Create is a method to create a user using given parameters. 31 func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*CreateResp, error) { 32 b, err := golangsdk.BuildRequestBody(opts, "") 33 if err != nil { 34 return nil, err 35 } 36 37 var r CreateResp 38 _, err = c.Post(rootURL(c), b, &r, &golangsdk.RequestOpts{ 39 MoreHeaders: requestOpts.MoreHeaders, 40 }) 41 return &r, err 42 } 43 44 // Get is a method to obtain the user detail by its ID. 45 func Get(c *golangsdk.ServiceClient, userId string) (*UserDetail, error) { 46 var r GetResp 47 _, err := c.Get(resourceURL(c, userId), &r, &golangsdk.RequestOpts{ 48 MoreHeaders: requestOpts.MoreHeaders, 49 }) 50 return &r.UserDetail, err 51 } 52 53 // ListOpts is the structure required by the List method to query user list. 54 type ListOpts struct { 55 // User name. 56 Name string `q:"user_name"` 57 // User description, support fuzzing match. 58 Description string `q:"description"` 59 // Number of records to be queried. 60 // If omited, return all user's information. 61 Limit int `q:"limit"` 62 // The offset number. 63 Offset int `q:"offset"` 64 } 65 66 // List is a method to query the job details using given parameters. 67 func List(c *golangsdk.ServiceClient, opts ListOpts) (*QueryResp, error) { 68 url := rootURL(c) 69 query, err := golangsdk.BuildQueryString(opts) 70 if err != nil { 71 return nil, err 72 } 73 url += query.String() 74 75 var r QueryResp 76 _, err = c.Get(url, &r, &golangsdk.RequestOpts{ 77 MoreHeaders: requestOpts.MoreHeaders, 78 }) 79 return &r, err 80 } 81 82 // UpdateOpts is the structure required by the Update method to change user information. 83 type UpdateOpts struct { 84 // User email. The value can contain from 1 to 64 characters. 85 Email string `json:"user_email" required:"true"` 86 // User description. 87 Description *string `json:"description,omitempty"` 88 // The expires time of Workspace user. The format is "yyyy-MM-ddTHH:mm:ss.000Z". 89 // 0 means it will never expire. 90 AccountExpires string `json:"account_expires,omitempty"` 91 // Whether to allow password modification. 92 EnableChangePassword *bool `json:"enable_change_password,omitempty"` 93 // Whether the next login requires a password reset. 94 NextLoginChangePassword *bool `json:"next_login_change_password,omitempty"` 95 // Whether the password will never expires. 96 PasswordNeverExpires *bool `json:"password_never_expired,omitempty"` 97 // Whether the account is disabled. 98 Disabled *bool `json:"disabled,omitempty"` 99 } 100 101 // Update is a method to change user informaion using givin parameters. 102 func Update(c *golangsdk.ServiceClient, userId string, opts UpdateOpts) (*UpdateResp, error) { 103 b, err := golangsdk.BuildRequestBody(opts, "") 104 if err != nil { 105 return nil, err 106 } 107 108 var r UpdateResp 109 _, err = c.Put(resourceURL(c, userId), b, &r, &golangsdk.RequestOpts{ 110 MoreHeaders: requestOpts.MoreHeaders, 111 }) 112 return &r, err 113 } 114 115 // Delete is a method to remove an existing user using given parameters. 116 func Delete(c *golangsdk.ServiceClient, userId string) error { 117 _, err := c.Delete(resourceURL(c, userId), &golangsdk.RequestOpts{ 118 MoreHeaders: requestOpts.MoreHeaders, 119 }) 120 return err 121 }