github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/identity/v3/users/requests.go (about) 1 package users 2 3 import ( 4 "github.com/opentelekomcloud/gophertelekomcloud" 5 "github.com/opentelekomcloud/gophertelekomcloud/openstack/identity/v3/groups" 6 "github.com/opentelekomcloud/gophertelekomcloud/openstack/identity/v3/projects" 7 "github.com/opentelekomcloud/gophertelekomcloud/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 if err != nil { 44 return "", err 45 } 46 return q.String(), nil 47 } 48 49 // List enumerates the Users to which the current token has access. 50 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 51 url := listURL(client) 52 if opts != nil { 53 query, err := opts.ToUserListQuery() 54 if err != nil { 55 return pagination.Pager{Err: err} 56 } 57 url += query 58 } 59 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 60 return UserPage{pagination.LinkedPageBase{PageResult: r}} 61 }) 62 } 63 64 // Get retrieves details on a single user, by ID. 65 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 66 _, r.Err = client.Get(getURL(client, id), &r.Body, nil) 67 return 68 } 69 70 // CreateOptsBuilder allows extensions to add additional parameters to 71 // the Create request. 72 type CreateOptsBuilder interface { 73 ToUserCreateMap() (map[string]interface{}, error) 74 } 75 76 // CreateOpts provides options used to create a user. 77 type CreateOpts struct { 78 // Name is the name of the new user. 79 Name string `json:"name" required:"true"` 80 81 // DefaultProjectID is the ID of the default project of the user. 82 DefaultProjectID string `json:"default_project_id,omitempty"` 83 84 // DomainID is the ID of the domain the user belongs to. 85 DomainID string `json:"domain_id,omitempty"` 86 87 // Enabled sets the user status to enabled or disabled. 88 Enabled *bool `json:"enabled,omitempty"` 89 90 // Password is the password of the new user. 91 Password string `json:"password,omitempty"` 92 93 // Description is a description of the user. 94 Description string `json:"description,omitempty"` 95 96 // Email is the email of the user. 97 Email string `json:"email,omitempty"` 98 } 99 100 // ToUserCreateMap formats a CreateOpts into a create request. 101 func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) { 102 b, err := golangsdk.BuildRequestBody(opts, "user") 103 if err != nil { 104 return nil, err 105 } 106 107 return b, nil 108 } 109 110 // Create creates a new User. 111 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 112 b, err := opts.ToUserCreateMap() 113 if err != nil { 114 r.Err = err 115 return 116 } 117 _, r.Err = client.Post(createURL(client), &b, &r.Body, &golangsdk.RequestOpts{ 118 OkCodes: []int{201}, 119 }) 120 return 121 } 122 123 // UpdateOptsBuilder allows extensions to add additional parameters to 124 // the Update request. 125 type UpdateOptsBuilder interface { 126 ToUserUpdateMap() (map[string]interface{}, error) 127 } 128 129 // UpdateOpts provides options for updating a user account. 130 type UpdateOpts struct { 131 // Name is the name of the new user. 132 Name string `json:"name,omitempty"` 133 134 // DefaultProjectID is the ID of the default project of the user. 135 DefaultProjectID string `json:"default_project_id,omitempty"` 136 137 // DomainID is the ID of the domain the user belongs to. 138 DomainID string `json:"domain_id,omitempty"` 139 140 // Enabled sets the user status to enabled or disabled. 141 Enabled *bool `json:"enabled,omitempty"` 142 143 // Password is the password of the new user. 144 Password string `json:"password,omitempty"` 145 146 // Description is a description of the user. 147 Description *string `json:"description,omitempty"` 148 149 // Email is the email of the user. 150 Email string `json:"email,omitempty"` 151 } 152 153 // ToUserUpdateMap formats a UpdateOpts into an update request. 154 func (opts UpdateOpts) ToUserUpdateMap() (map[string]interface{}, error) { 155 b, err := golangsdk.BuildRequestBody(opts, "user") 156 if err != nil { 157 return nil, err 158 } 159 160 return b, nil 161 } 162 163 // Update updates an existing User. 164 func Update(client *golangsdk.ServiceClient, userID string, opts UpdateOptsBuilder) (r UpdateResult) { 165 b, err := opts.ToUserUpdateMap() 166 if err != nil { 167 r.Err = err 168 return 169 } 170 _, r.Err = client.Patch(updateURL(client, userID), &b, &r.Body, &golangsdk.RequestOpts{ 171 OkCodes: []int{200}, 172 }) 173 return 174 } 175 176 // ExtendedUpdateOptsBuilder allows extensions to add additional parameters to 177 // the ExtendedUpdate request. 178 type ExtendedUpdateOptsBuilder interface { 179 ToUserUpdateMap() (map[string]interface{}, error) 180 } 181 182 // ExtendedUpdateOpts allows modifying User information (including e-mail address and mobile number) 183 type ExtendedUpdateOpts struct { 184 185 // Name is the name of the user. 186 Name string `json:"name,omitempty"` 187 188 /*Password of the user. The password must meet the following requirements: 189 - Can contain 6 to 32 characters. The default minimum password length is 6 characters. 190 - Must contain at least two of the following character types: uppercase letters, lowercase letters, digits, and special characters. 191 - Must meet the requirements of the password policy configured on the account settings page. 192 - Must be different from the old password. 193 */ 194 Password string `json:"password,omitempty"` 195 196 // Enabled is whether or not the user is enabled. 197 Enabled *bool `json:"enabled,omitempty"` 198 199 // Description is a description of the user. 200 Description *string `json:"description,omitempty"` 201 202 // Email is the email of the user 203 Email string `json:"email,omitempty"` 204 205 // AreaCode is country code 206 AreaCode string `json:"areacode,omitempty"` 207 208 // Phone is mobile number, which can contain a maximum of 32 digits. 209 // The mobile number must be used together with a country code. 210 Phone string `json:"phone,omitempty"` 211 212 // Whether password reset is required at first login 213 PwdResetRequired *bool `json:"pwd_status,omitempty"` 214 215 // XUserType is Type of the IAM user in the external system. 216 XUserType string `json:"xuser_type,omitempty"` 217 218 // XUserID is ID of the IAM user in the external system. 219 XUserID string `json:"xuser_id,omitempty"` 220 } 221 222 func (opts ExtendedUpdateOpts) ToUserUpdateMap() (map[string]interface{}, error) { 223 b, err := golangsdk.BuildRequestBody(opts, "user") 224 if err != nil { 225 return nil, err 226 } 227 return b, nil 228 } 229 230 func ExtendedUpdate(client *golangsdk.ServiceClient, userID string, opts ExtendedUpdateOpts) (r UpdateExtendedResult) { 231 b, err := opts.ToUserUpdateMap() 232 if err != nil { 233 r.Err = err 234 return 235 } 236 _, r.Err = client.Put(updateExtendedURL(client, userID), &b, &r.Body, &golangsdk.RequestOpts{ 237 OkCodes: []int{200}, 238 }) 239 return 240 } 241 242 // Delete deletes a user. 243 func Delete(client *golangsdk.ServiceClient, userID string) (r DeleteResult) { 244 _, r.Err = client.Delete(deleteURL(client, userID), nil) 245 return 246 } 247 248 // ListGroups enumerates groups user belongs to. 249 func ListGroups(client *golangsdk.ServiceClient, userID string) pagination.Pager { 250 url := listGroupsURL(client, userID) 251 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 252 return groups.GroupPage{LinkedPageBase: pagination.LinkedPageBase{PageResult: r}} 253 }) 254 } 255 256 // ListProjects enumerates groups user belongs to. 257 func ListProjects(client *golangsdk.ServiceClient, userID string) pagination.Pager { 258 url := listProjectsURL(client, userID) 259 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 260 return projects.ProjectPage{LinkedPageBase: pagination.LinkedPageBase{PageResult: r}} 261 }) 262 } 263 264 // ListInGroup enumerates users that belong to a group. 265 func ListInGroup(client *golangsdk.ServiceClient, groupID string, opts ListOptsBuilder) pagination.Pager { 266 url := listInGroupURL(client, groupID) 267 if opts != nil { 268 query, err := opts.ToUserListQuery() 269 if err != nil { 270 return pagination.Pager{Err: err} 271 } 272 url += query 273 } 274 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 275 return UserPage{pagination.LinkedPageBase{PageResult: r}} 276 }) 277 } 278 279 // AddToGroup add a user into one group 280 func AddToGroup(client *golangsdk.ServiceClient, groupID string, userID string) (r AddMembershipResult) { 281 _, r.Err = client.Put(membershipURL(client, groupID, userID), nil, nil, &golangsdk.RequestOpts{ 282 OkCodes: []int{204}, 283 }) 284 return 285 } 286 287 // RemoveFromGroup remove user from group 288 func RemoveFromGroup(client *golangsdk.ServiceClient, groupID string, userID string) (r DeleteResult) { 289 _, r.Err = client.Delete(membershipURL(client, groupID, userID), nil) 290 return 291 } 292 293 // SendWelcomeEmail sends a welcome email to a user. 294 func SendWelcomeEmail(client *golangsdk.ServiceClient, userID string) (r WelcomeResult) { 295 _, r.Err = client.Post(welcomeExtendedURL(client, userID), nil, nil, &golangsdk.RequestOpts{ 296 OkCodes: []int{200, 201}, 297 }) 298 return 299 }