github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/identity/v3.0/users/requests.go (about) 1 package users 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 ) 6 7 // CreateOptsBuilder allows extensions to add additional parameters to 8 // the Create request. 9 type CreateOptsBuilder interface { 10 ToUserCreateMap() (map[string]interface{}, error) 11 } 12 13 // CreateOpts provides options used to create a user. 14 type CreateOpts struct { 15 // Name is the name of the new user. 16 Name string `json:"name" required:"true"` 17 18 // DomainID is the ID of the domain the user belongs to. 19 DomainID string `json:"domain_id" required:"true"` 20 21 // Password is the password of the new user. 22 Password string `json:"password,omitempty"` 23 24 // Email address with a maximum of 255 characters 25 Email string `json:"email,omitempty"` 26 27 // AreaCode is a country code, must be used together with Phone. 28 AreaCode string `json:"areacode,omitempty"` 29 30 // Phone is a mobile number with a maximum of 32 digits, must be used together with AreaCode. 31 Phone string `json:"phone,omitempty"` 32 33 // Description is a description of the user. 34 Description string `json:"description,omitempty"` 35 36 // AccessMode is the access type for IAM user 37 AccessMode string `json:"access_mode,omitempty"` 38 39 // XUserID is the user ID in the external system, it must be used together with xuser_type. 40 XUserID string `json:"xuser_id,omitempty"` 41 42 // XUserType is the user type in the external system. Currently, it can only be "TenantIdp". 43 XUserType string `json:"xuser_type,omitempty"` 44 45 // Enabled sets the user status to enabled or disabled. 46 Enabled *bool `json:"enabled,omitempty"` 47 48 // PasswordReset Indicates whether password reset is required at the first login. 49 // By default, password reset is true. 50 PasswordReset *bool `json:"pwd_status,omitempty"` 51 } 52 53 // ToUserCreateMap formats a CreateOpts into a create request. 54 func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) { 55 b, err := golangsdk.BuildRequestBody(opts, "user") 56 if err != nil { 57 return nil, err 58 } 59 60 return b, nil 61 } 62 63 // Create creates a new User. 64 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 65 b, err := opts.ToUserCreateMap() 66 if err != nil { 67 r.Err = err 68 return 69 } 70 _, r.Err = client.Post(createURL(client), &b, &r.Body, nil) 71 return 72 } 73 74 // UpdateOptsBuilder allows extensions to add additional parameters to 75 // the Update request. 76 type UpdateOptsBuilder interface { 77 ToUserUpdateMap() (map[string]interface{}, error) 78 } 79 80 // UpdateOpts provides options for updating a user account. 81 type UpdateOpts struct { 82 // Name is the name of the new user. 83 Name string `json:"name,omitempty"` 84 85 // Password is the password of the new user. 86 Password string `json:"password,omitempty"` 87 88 // Email address with a maximum of 255 characters 89 Email string `json:"email,omitempty"` 90 91 // AreaCode is a country code, must be used together with Phone. 92 AreaCode string `json:"areacode,omitempty"` 93 94 // Phone is a mobile number with a maximum of 32 digits. must be used together with AreaCode. 95 Phone string `json:"phone,omitempty"` 96 97 // Description is a description of the user. 98 Description *string `json:"description,omitempty"` 99 100 // AccessMode is the access type for IAM user 101 AccessMode string `json:"access_mode,omitempty"` 102 103 // XUserID is the user ID in the external system, it must be used together with xuser_type. 104 XUserID *string `json:"xuser_id,omitempty"` 105 106 // XUserType is the user type in the external system, currently, it can only be "TenantIdp". 107 XUserType *string `json:"xuser_type,omitempty"` 108 109 // Enabled sets the user status to enabled or disabled. 110 Enabled *bool `json:"enabled,omitempty"` 111 112 // PasswordReset Indicates whether password reset is required 113 PasswordReset *bool `json:"pwd_status,omitempty"` 114 } 115 116 // ToUserUpdateMap formats a UpdateOpts into an update request. 117 func (opts UpdateOpts) ToUserUpdateMap() (map[string]interface{}, error) { 118 b, err := golangsdk.BuildRequestBody(opts, "user") 119 if err != nil { 120 return nil, err 121 } 122 123 return b, nil 124 } 125 126 // Update updates an existing User. 127 func Update(client *golangsdk.ServiceClient, userID string, opts UpdateOptsBuilder) (r UpdateResult) { 128 b, err := opts.ToUserUpdateMap() 129 if err != nil { 130 r.Err = err 131 return 132 } 133 _, r.Err = client.Put(updateURL(client, userID), &b, &r.Body, &golangsdk.RequestOpts{ 134 OkCodes: []int{200}, 135 }) 136 return 137 } 138 139 // Get retrieves details on a single user, by ID. 140 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 141 _, r.Err = client.Get(getURL(client, id), &r.Body, nil) 142 return 143 } 144 145 // UpdateLoginProtectOpts provides options for updating a user account login protect. 146 type UpdateLoginProtectOpts struct { 147 Enabled *bool `json:"enabled" required:"true"` 148 VerificationMethod string `json:"verification_method" required:"true"` 149 } 150 151 // UpdateLoginProtectOptsBuilder allows extensions to add additional parameters to 152 // the Update request. 153 type UpdateLoginProtectOptsBuilder interface { 154 ToLoginProtectUpdateMap() (map[string]interface{}, error) 155 } 156 157 // ToLoginProtectUpdateMap formats a UpdateLoginProtectOpts into an update request. 158 func (opts UpdateLoginProtectOpts) ToLoginProtectUpdateMap() (map[string]interface{}, error) { 159 b, err := golangsdk.BuildRequestBody(opts, "login_protect") 160 if err != nil { 161 return nil, err 162 } 163 164 return b, nil 165 } 166 167 // UpdateLoginProtect updates login protect for user account. 168 func UpdateLoginProtect(client *golangsdk.ServiceClient, userID string, 169 opts UpdateLoginProtectOpts) (r UpdateLoginProtectResult) { 170 b, err := opts.ToLoginProtectUpdateMap() 171 if err != nil { 172 r.Err = err 173 return 174 } 175 _, r.Err = client.Put(loginProtectURL(client, userID), &b, &r.Body, &golangsdk.RequestOpts{}) 176 return 177 } 178 179 // Get retrieves details on a single user login protect, by user ID. 180 func GetLoginProtect(client *golangsdk.ServiceClient, id string) (r GetLoginProtectResult) { 181 _, r.Err = client.Get(loginProtectURL(client, id), &r.Body, nil) 182 return 183 }