-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
resource_services_user_identification_ad_access_domain.go
527 lines (486 loc) · 18.3 KB
/
resource_services_user_identification_ad_access_domain.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package junos
import (
"context"
"fmt"
"strconv"
"strings"
"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"
bchk "github.com/jeremmfr/go-utils/basiccheck"
jdecode "github.com/jeremmfr/junosdecode"
)
type svcUserIdentAdAccessDomainOptions struct {
name string
userName string
userPassword string
domainController []map[string]interface{}
ipUserMappingDiscoveryWmi []map[string]interface{}
userGroupMappingLdap []map[string]interface{}
}
func resourceServicesUserIdentAdAccessDomain() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceServicesUserIdentAdAccessDomainCreate,
ReadWithoutTimeout: resourceServicesUserIdentAdAccessDomainRead,
UpdateWithoutTimeout: resourceServicesUserIdentAdAccessDomainUpdate,
DeleteWithoutTimeout: resourceServicesUserIdentAdAccessDomainDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceServicesUserIdentAdAccessDomainImport,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefAndDots),
},
"user_name": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefAndDots),
},
"user_password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"domain_controller": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefAndDots),
},
"address": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsIPAddress,
},
},
},
},
"ip_user_mapping_discovery_wmi": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"event_log_scanning_interval": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(5, 60),
},
"initial_event_log_timespan": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 168),
},
},
},
},
"user_group_mapping_ldap": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"base": {
Type: schema.TypeString,
Required: true,
},
"address": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"auth_algo_simple": {
Type: schema.TypeBool,
Optional: true,
},
"ssl": {
Type: schema.TypeBool,
Optional: true,
},
"user_name": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefAndDots),
},
"user_password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
},
},
},
},
}
}
func resourceServicesUserIdentAdAccessDomainCreate(ctx context.Context, d *schema.ResourceData, m interface{},
) diag.Diagnostics {
sess := m.(*Session)
if sess.junosFakeCreateSetFile != "" {
if err := setServicesUserIdentAdAccessDomain(d, m, nil); err != nil {
return diag.FromErr(err)
}
d.SetId(d.Get("name").(string))
return nil
}
jnprSess, err := sess.startNewSession(ctx)
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
if err := sess.configLock(ctx, jnprSess); err != nil {
return diag.FromErr(err)
}
var diagWarns diag.Diagnostics
svcUserIdentAdAccessDomainExists, err := checkServicesUserIdentAdAccessDomainExists(
d.Get("name").(string), m, jnprSess)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if svcUserIdentAdAccessDomainExists {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns,
diag.FromErr(fmt.Errorf(
"services user-identification active-directory-access domain %v already exists", d.Get("name").(string)))...)
}
if err := setServicesUserIdentAdAccessDomain(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("create resource junos_services_user_identification_ad_access_domain", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
svcUserIdentAdAccessDomainExists, err = checkServicesUserIdentAdAccessDomainExists(d.Get("name").(string), m, jnprSess)
if err != nil {
return append(diagWarns, diag.FromErr(err)...)
}
if svcUserIdentAdAccessDomainExists {
d.SetId(d.Get("name").(string))
} else {
return append(diagWarns, diag.FromErr(fmt.Errorf(
"services user-identification active-directory-access domain %v "+
"not exists after commit => check your config", d.Get("name").(string)))...)
}
return append(diagWarns, resourceServicesUserIdentAdAccessDomainReadWJnprSess(d, m, jnprSess)...)
}
func resourceServicesUserIdentAdAccessDomainRead(ctx context.Context, d *schema.ResourceData, m interface{},
) diag.Diagnostics {
sess := m.(*Session)
jnprSess, err := sess.startNewSession(ctx)
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
return resourceServicesUserIdentAdAccessDomainReadWJnprSess(d, m, jnprSess)
}
func resourceServicesUserIdentAdAccessDomainReadWJnprSess(
d *schema.ResourceData, m interface{}, jnprSess *NetconfObject,
) diag.Diagnostics {
mutex.Lock()
svcUserIdentAdAccessDomainOptions, err := readServicesUserIdentAdAccessDomain(d.Get("name").(string), m, jnprSess)
mutex.Unlock()
if err != nil {
return diag.FromErr(err)
}
if svcUserIdentAdAccessDomainOptions.name == "" {
d.SetId("")
} else {
fillServicesUserIdentAdAccessDomainData(d, svcUserIdentAdAccessDomainOptions)
}
return nil
}
func resourceServicesUserIdentAdAccessDomainUpdate(ctx context.Context, d *schema.ResourceData, m interface{},
) diag.Diagnostics {
d.Partial(true)
sess := m.(*Session)
if sess.junosFakeUpdateAlso {
if err := delServicesUserIdentAdAccessDomain(d.Get("name").(string), m, nil); err != nil {
return diag.FromErr(err)
}
if err := setServicesUserIdentAdAccessDomain(d, m, nil); err != nil {
return diag.FromErr(err)
}
d.Partial(false)
return nil
}
jnprSess, err := sess.startNewSession(ctx)
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
if err := sess.configLock(ctx, jnprSess); err != nil {
return diag.FromErr(err)
}
var diagWarns diag.Diagnostics
if err := delServicesUserIdentAdAccessDomain(d.Get("name").(string), m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if err := setServicesUserIdentAdAccessDomain(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("update resource junos_services_user_identification_ad_access_domain", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
d.Partial(false)
return append(diagWarns, resourceServicesUserIdentAdAccessDomainReadWJnprSess(d, m, jnprSess)...)
}
func resourceServicesUserIdentAdAccessDomainDelete(ctx context.Context, d *schema.ResourceData, m interface{},
) diag.Diagnostics {
sess := m.(*Session)
if sess.junosFakeDeleteAlso {
if err := delServicesUserIdentAdAccessDomain(d.Get("name").(string), m, nil); err != nil {
return diag.FromErr(err)
}
return nil
}
jnprSess, err := sess.startNewSession(ctx)
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
if err := sess.configLock(ctx, jnprSess); err != nil {
return diag.FromErr(err)
}
var diagWarns diag.Diagnostics
if err := delServicesUserIdentAdAccessDomain(d.Get("name").(string), m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("delete resource junos_services_user_identification_ad_access_domain", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
return diagWarns
}
func resourceServicesUserIdentAdAccessDomainImport(ctx context.Context, d *schema.ResourceData, m interface{},
) ([]*schema.ResourceData, error) {
sess := m.(*Session)
jnprSess, err := sess.startNewSession(ctx)
if err != nil {
return nil, err
}
defer sess.closeSession(jnprSess)
result := make([]*schema.ResourceData, 1)
svcUserIdentAdAccessDomainExists, err := checkServicesUserIdentAdAccessDomainExists(d.Id(), m, jnprSess)
if err != nil {
return nil, err
}
if !svcUserIdentAdAccessDomainExists {
return nil, fmt.Errorf("don't find services user-identification "+
"active-directory-access domain with id '%v' (id must be <name>)", d.Id())
}
svcUserIdentAdAccessDomainOptions, err := readServicesUserIdentAdAccessDomain(d.Id(), m, jnprSess)
if err != nil {
return nil, err
}
fillServicesUserIdentAdAccessDomainData(d, svcUserIdentAdAccessDomainOptions)
result[0] = d
return result, nil
}
func checkServicesUserIdentAdAccessDomainExists(domain string, m interface{}, jnprSess *NetconfObject,
) (bool, error) {
sess := m.(*Session)
showConfig, err := sess.command(cmdShowConfig+
"services user-identification active-directory-access domain "+domain+pipeDisplaySet, jnprSess)
if err != nil {
return false, err
}
if showConfig == emptyW {
return false, nil
}
return true, nil
}
func setServicesUserIdentAdAccessDomain(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
configSet := make([]string, 0)
setPrefix := "set services user-identification active-directory-access domain " + d.Get("name").(string) + " "
configSet = append(configSet, setPrefix+"user "+d.Get("user_name").(string))
configSet = append(configSet, setPrefix+"user password \""+d.Get("user_password").(string)+"\"")
domainControllerNameList := make([]string, 0)
for _, v := range d.Get("domain_controller").([]interface{}) {
domainController := v.(map[string]interface{})
if bchk.StringInSlice(domainController["name"].(string), domainControllerNameList) {
return fmt.Errorf("multiple blocks domain_controller with the same name %s", domainController["name"].(string))
}
domainControllerNameList = append(domainControllerNameList, domainController["name"].(string))
configSet = append(configSet, setPrefix+"domain-controller "+domainController["name"].(string)+
" address "+domainController["address"].(string))
}
for _, v := range d.Get("ip_user_mapping_discovery_wmi").([]interface{}) {
configSet = append(configSet, setPrefix+"ip-user-mapping discovery-method wmi")
if v != nil {
wmi := v.(map[string]interface{})
if v2 := wmi["event_log_scanning_interval"].(int); v2 != 0 {
configSet = append(configSet,
setPrefix+"ip-user-mapping discovery-method wmi event-log-scanning-interval "+strconv.Itoa(v2))
}
if v2 := wmi["initial_event_log_timespan"].(int); v2 != 0 {
configSet = append(configSet,
setPrefix+"ip-user-mapping discovery-method wmi initial-event-log-timespan "+strconv.Itoa(v2))
}
}
}
for _, v := range d.Get("user_group_mapping_ldap").([]interface{}) {
ldap := v.(map[string]interface{})
configSet = append(configSet, setPrefix+"user-group-mapping ldap base \""+ldap["base"].(string)+"\"")
for _, v2 := range ldap["address"].([]interface{}) {
configSet = append(configSet, setPrefix+"user-group-mapping ldap address "+v2.(string))
}
if ldap["auth_algo_simple"].(bool) {
configSet = append(configSet, setPrefix+"user-group-mapping ldap authentication-algorithm simple")
}
if ldap["ssl"].(bool) {
configSet = append(configSet, setPrefix+"user-group-mapping ldap ssl")
}
if v2 := ldap["user_name"].(string); v2 != "" {
configSet = append(configSet, setPrefix+"user-group-mapping ldap user "+v2)
}
if v2 := ldap["user_password"].(string); v2 != "" {
configSet = append(configSet, setPrefix+"user-group-mapping ldap user password \""+v2+"\"")
}
}
return sess.configSet(configSet, jnprSess)
}
func readServicesUserIdentAdAccessDomain(domain string, m interface{}, jnprSess *NetconfObject,
) (svcUserIdentAdAccessDomainOptions, error) {
sess := m.(*Session)
var confRead svcUserIdentAdAccessDomainOptions
showConfig, err := sess.command(cmdShowConfig+
"services user-identification active-directory-access domain "+domain+pipeDisplaySetRelative, jnprSess)
if err != nil {
return confRead, err
}
if showConfig != emptyW {
confRead.name = domain
for _, item := range strings.Split(showConfig, "\n") {
if strings.Contains(item, xmlStartTagConfigOut) {
continue
}
if strings.Contains(item, xmlEndTagConfigOut) {
break
}
itemTrim := strings.TrimPrefix(item, setLS)
switch {
case strings.HasPrefix(itemTrim, "user password "):
var err error
confRead.userPassword, err = jdecode.Decode(strings.Trim(strings.TrimPrefix(itemTrim, "user password "), "\""))
if err != nil {
return confRead, fmt.Errorf("failed to decode user password: %w", err)
}
case strings.HasPrefix(itemTrim, "user "):
confRead.userName = strings.TrimPrefix(itemTrim, "user ")
case strings.HasPrefix(itemTrim, "domain-controller ") &&
strings.Contains(itemTrim, " address "):
itemTrimSplit := strings.Split(itemTrim, " ")
confRead.domainController = append(confRead.domainController, map[string]interface{}{
"name": itemTrimSplit[1],
"address": itemTrimSplit[3],
})
case strings.HasPrefix(itemTrim, "ip-user-mapping discovery-method wmi"):
if len(confRead.ipUserMappingDiscoveryWmi) == 0 {
confRead.ipUserMappingDiscoveryWmi = append(confRead.ipUserMappingDiscoveryWmi, map[string]interface{}{
"event_log_scanning_interval": 0,
"initial_event_log_timespan": 0,
})
}
switch {
case strings.HasPrefix(itemTrim, "ip-user-mapping discovery-method wmi event-log-scanning-interval "):
var err error
confRead.ipUserMappingDiscoveryWmi[0]["event_log_scanning_interval"], err = strconv.Atoi(strings.TrimPrefix(
itemTrim, "ip-user-mapping discovery-method wmi event-log-scanning-interval "))
if err != nil {
return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err)
}
case strings.HasPrefix(itemTrim, "ip-user-mapping discovery-method wmi initial-event-log-timespan "):
var err error
confRead.ipUserMappingDiscoveryWmi[0]["initial_event_log_timespan"], err = strconv.Atoi(strings.TrimPrefix(
itemTrim, "ip-user-mapping discovery-method wmi initial-event-log-timespan "))
if err != nil {
return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err)
}
}
case strings.HasPrefix(itemTrim, "user-group-mapping ldap "):
if len(confRead.userGroupMappingLdap) == 0 {
confRead.userGroupMappingLdap = append(confRead.userGroupMappingLdap, map[string]interface{}{
"base": "",
"address": make([]string, 0),
"auth_algo_simple": false,
"ssl": false,
"user_name": "",
"user_password": "",
})
}
switch {
case strings.HasPrefix(itemTrim, "user-group-mapping ldap base "):
confRead.userGroupMappingLdap[0]["base"] = strings.Trim(strings.TrimPrefix(
itemTrim, "user-group-mapping ldap base "), "\"")
case strings.HasPrefix(itemTrim, "user-group-mapping ldap address "):
confRead.userGroupMappingLdap[0]["address"] = append(confRead.userGroupMappingLdap[0]["address"].([]string),
strings.TrimPrefix(itemTrim, "user-group-mapping ldap address "))
case itemTrim == "user-group-mapping ldap authentication-algorithm simple":
confRead.userGroupMappingLdap[0]["auth_algo_simple"] = true
case itemTrim == "user-group-mapping ldap ssl":
confRead.userGroupMappingLdap[0]["ssl"] = true
case strings.HasPrefix(itemTrim, "user-group-mapping ldap user password "):
var err error
confRead.userGroupMappingLdap[0]["user_password"], err = jdecode.Decode(strings.Trim(strings.TrimPrefix(
itemTrim, "user-group-mapping ldap user password "), "\""))
if err != nil {
return confRead, fmt.Errorf("failed to decode user password: %w", err)
}
case strings.HasPrefix(itemTrim, "user-group-mapping ldap user "):
confRead.userGroupMappingLdap[0]["user_name"] = strings.TrimPrefix(itemTrim, "user-group-mapping ldap user ")
}
}
}
}
return confRead, nil
}
func delServicesUserIdentAdAccessDomain(domain string, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
configSet := []string{
"delete services user-identification active-directory-access domain " + domain,
}
return sess.configSet(configSet, jnprSess)
}
func fillServicesUserIdentAdAccessDomainData(
d *schema.ResourceData, svcUserIdentAdAccessDomainOptions svcUserIdentAdAccessDomainOptions,
) {
if tfErr := d.Set("name", svcUserIdentAdAccessDomainOptions.name); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("user_name", svcUserIdentAdAccessDomainOptions.userName); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("user_password", svcUserIdentAdAccessDomainOptions.userPassword); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("domain_controller", svcUserIdentAdAccessDomainOptions.domainController); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("ip_user_mapping_discovery_wmi",
svcUserIdentAdAccessDomainOptions.ipUserMappingDiscoveryWmi); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("user_group_mapping_ldap", svcUserIdentAdAccessDomainOptions.userGroupMappingLdap); tfErr != nil {
panic(tfErr)
}
}