-
Notifications
You must be signed in to change notification settings - Fork 154
/
resource_huaweicloud_identity_user.go
351 lines (307 loc) · 9.66 KB
/
resource_huaweicloud_identity_user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package iam
import (
"context"
"fmt"
"log"
"regexp"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/chnsz/golangsdk"
"github.com/chnsz/golangsdk/openstack/identity/v3.0/users"
identity_users "github.com/chnsz/golangsdk/openstack/identity/v3/users"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API IAM POST /v3.0/OS-USER/users
// @API IAM GET /v3.0/OS-USER/users/{user_id}
// @API IAM PUT /v3.0/OS-USER/users/{user_id}
// @API IAM DELETE /v3/users/{user_id}
// @API IAM PUT /v3.0/OS-USER/users/{user_id}/login-protect
// @API IAM GET /v3.0/OS-USER/users/{user_id}/login-protect
func ResourceIdentityUser() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIdentityUserCreate,
ReadContext: resourceIdentityUserRead,
UpdateContext: resourceIdentityUserUpdate,
DeleteContext: resourceIdentityUserDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"email": {
Type: schema.TypeString,
Optional: true,
},
"phone": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"country_code"},
ValidateFunc: validation.StringMatch(regexp.MustCompile("^[0-9]{0,32}$"),
"the phone number must have a maximum of 32 digits"),
},
"country_code": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"phone"},
},
"external_identity_id": {
Type: schema.TypeString,
Optional: true,
},
"external_identity_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
RequiredWith: []string{"external_identity_id"},
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"pwd_reset": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"access_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"default", "programmatic", "console",
}, false),
},
"login_protect_verification_method": {
Type: schema.TypeString,
Optional: true,
},
"password_strength": {
Type: schema.TypeString,
Computed: true,
},
"create_time": {
Type: schema.TypeString,
Computed: true,
},
"last_login": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func buildExternalIdentityType(d *schema.ResourceData) string {
// external_identity_type is valid only when external_identity_id is specified.
if _, ok := d.GetOk("external_identity_id"); !ok {
return ""
}
if v, ok := d.GetOk("external_identity_type"); ok {
return v.(string)
}
// the default value of external_identity_type is TenantIdp
return "TenantIdp"
}
func resourceIdentityUserCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
iamClient, err := cfg.IAMV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating IAM client: %s", err)
}
if cfg.DomainID == "" {
return diag.Errorf("the domain_id must be specified in the provider configuration")
}
enabled := d.Get("enabled").(bool)
reset := d.Get("pwd_reset").(bool)
createOpts := users.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Email: d.Get("email").(string),
Phone: d.Get("phone").(string),
AreaCode: d.Get("country_code").(string),
AccessMode: d.Get("access_type").(string),
XUserID: d.Get("external_identity_id").(string),
XUserType: buildExternalIdentityType(d),
Enabled: &enabled,
PasswordReset: &reset,
DomainID: cfg.DomainID,
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
// Add password here so it wouldn't go in the above log entry
createOpts.Password = d.Get("password").(string)
user, err := users.Create(iamClient, createOpts).Extract()
if err != nil {
return diag.Errorf("error creating IAM user: %s", err)
}
d.SetId(user.ID)
// if login_protect_verification_method is not empty, update login protect
if val, ok := d.GetOk("login_protect_verification_method"); ok {
err := updateLoginProtect(iamClient, user.ID, val.(string))
if err != nil {
return diag.FromErr(err)
}
}
return resourceIdentityUserRead(ctx, d, meta)
}
func updateLoginProtect(client *golangsdk.ServiceClient, userID, method string) error {
// if method empty, disable login protect
ok := true
if method == "" {
ok = false
// the method is required, and must be one of "sms", "email" and "vmfa"
method = "sms"
}
updateOpts := users.UpdateLoginProtectOpts{
Enabled: &ok,
VerificationMethod: method,
}
_, err := users.UpdateLoginProtect(client, userID, updateOpts).ExtractLoginProtect()
if err != nil {
return fmt.Errorf("error updating IAM user login protect: %s", err)
}
return nil
}
func resourceIdentityUserRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
iamClient, err := cfg.IAMV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating IAM client: %s", err)
}
user, err := users.Get(iamClient, d.Id()).Extract()
if err != nil {
return common.CheckDeletedDiag(d, err, "user")
}
method := ""
loginProtect, err := users.GetLoginProtect(iamClient, d.Id()).ExtractLoginProtect()
if err != nil {
log.Printf("[WARN] failed to get the login verification method of user (%s): %s", d.Id(), err)
} else if loginProtect.VerificationMethod != "none" {
method = loginProtect.VerificationMethod
}
log.Printf("[DEBUG] Retrieved IAM user: %#v", user)
mErr := multierror.Append(nil,
d.Set("enabled", user.Enabled),
d.Set("name", user.Name),
d.Set("description", user.Description),
d.Set("email", user.Email),
d.Set("phone", normalizePhoneNumber(user.Phone)),
d.Set("country_code", user.AreaCode),
d.Set("access_type", user.AccessMode),
d.Set("password_strength", user.PasswordStrength),
d.Set("pwd_reset", user.PasswordStatus),
d.Set("create_time", user.CreateAt),
d.Set("last_login", user.LastLogin),
d.Set("external_identity_id", user.XUserID),
d.Set("external_identity_type", user.XUserType),
d.Set("login_protect_verification_method", method),
)
if err = mErr.ErrorOrNil(); err != nil {
return diag.Errorf("error setting IAM user fields: %s", err)
}
return nil
}
func normalizePhoneNumber(raw string) string {
phone := raw
rawList := strings.Split(raw, "-")
if len(rawList) > 1 {
phone = rawList[1]
}
return phone
}
func resourceIdentityUserUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
iamClient, err := cfg.IAMV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating IAM client: %s", err)
}
updateChanges := []string{
"name",
"description",
"email",
"country_code",
"phone",
"external_identity_id",
"external_identity_type",
"access_type",
"enabled",
"pwd_reset",
"password",
}
if d.HasChanges(updateChanges...) {
var updateOpts users.UpdateOpts
if d.HasChange("name") {
updateOpts.Name = d.Get("name").(string)
}
if d.HasChange("description") {
updateOpts.Description = utils.String(d.Get("description").(string))
}
if d.HasChange("email") {
updateOpts.Email = d.Get("email").(string)
}
if d.HasChanges("country_code", "phone") {
updateOpts.AreaCode = d.Get("country_code").(string)
updateOpts.Phone = d.Get("phone").(string)
}
if d.HasChanges("external_identity_id", "external_identity_type") {
updateOpts.XUserID = utils.String(d.Get("external_identity_id").(string))
updateOpts.XUserType = utils.String(buildExternalIdentityType(d))
}
if d.HasChange("access_type") {
updateOpts.AccessMode = d.Get("access_type").(string)
}
if d.HasChange("enabled") {
enabled := d.Get("enabled").(bool)
updateOpts.Enabled = &enabled
}
if d.HasChange("pwd_reset") {
reset := d.Get("pwd_reset").(bool)
updateOpts.PasswordReset = &reset
}
log.Printf("[DEBUG] Update Options: %#v", updateOpts)
// Add password here so it wouldn't go in the above log entry
if d.HasChange("password") {
updateOpts.Password = d.Get("password").(string)
}
_, err = users.Update(iamClient, d.Id(), updateOpts).Extract()
if err != nil {
return diag.Errorf("error updating IAM user: %s", err)
}
}
// update login protect
if d.HasChange("login_protect_verification_method") {
err = updateLoginProtect(iamClient, d.Id(), d.Get("login_protect_verification_method").(string))
if err != nil {
return diag.FromErr(err)
}
}
return resourceIdentityUserRead(ctx, d, meta)
}
func resourceIdentityUserDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
identityClient, err := cfg.IdentityV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating IAM client: %s", err)
}
err = identity_users.Delete(identityClient, d.Id()).ExtractErr()
if err != nil {
return diag.Errorf("error deleting IAM user: %s", err)
}
return nil
}