-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
resource_system_syslog_file.go
758 lines (717 loc) · 26 KB
/
resource_system_syslog_file.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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
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"
jdecode "github.com/jeremmfr/junosdecode"
)
type syslogFileOptions struct {
allowDuplicates bool
explicitPriority bool
filename string
match string
anySeverity string
authorizationSeverity string
changelogSeverity string
conflictlogSeverity string
daemonSeverity string
dfcSeverity string
externalSeverity string
firewallSeverity string
ftpSeverity string
interactivecommandsSeverity string
kernelSeverity string
ntpSeverity string
pfeSeverity string
securitySeverity string
userSeverity string
matchStrings []string
archive []map[string]interface{}
structuredData []map[string]interface{}
}
func resourceSystemSyslogFile() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSystemSyslogFileCreate,
ReadContext: resourceSystemSyslogFileRead,
UpdateContext: resourceSystemSyslogFileUpdate,
DeleteContext: resourceSystemSyslogFileDelete,
Importer: &schema.ResourceImporter{
State: resourceSystemSyslogFileImport,
},
Schema: map[string]*schema.Schema{
"filename": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault),
},
"allow_duplicates": {
Type: schema.TypeBool,
Optional: true,
},
"explicit_priority": {
Type: schema.TypeBool,
Optional: true,
},
"match": {
Type: schema.TypeString,
Optional: true,
},
"match_strings": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"structured_data": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"brief": {
Type: schema.TypeBool,
Optional: true,
},
},
},
},
"any_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"authorization_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"changelog_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"conflictlog_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"daemon_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"dfc_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"external_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"firewall_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"ftp_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"interactivecommands_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"kernel_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"ntp_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"pfe_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"security_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"user_severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(listOfSyslogSeverity(), false),
},
"archive": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"sites": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Required: true,
},
"password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"routing_instance": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"binary_data": {
Type: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"archive.0.no_binary_data"},
},
"no_binary_data": {
Type: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"archive.0.binary_data"},
},
"world_readable": {
Type: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"archive.0.no_world_readable"},
},
"no_world_readable": {
Type: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"archive.0.world_readable"},
},
"files": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 1000),
},
"size": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(65536, 1073741824),
},
"start_time": {
Type: schema.TypeString,
Optional: true,
},
"transfer_interval": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(5, 2880),
},
},
},
},
},
}
}
func resourceSystemSyslogFileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
if sess.junosFakeCreateSetFile != "" {
if err := setSystemSyslogFile(d, m, nil); err != nil {
return diag.FromErr(err)
}
d.SetId(d.Get("filename").(string))
return nil
}
jnprSess, err := sess.startNewSession()
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
sess.configLock(jnprSess)
var diagWarns diag.Diagnostics
syslogFileExists, err := checkSystemSyslogFileExists(d.Get("filename").(string), m, jnprSess)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if syslogFileExists {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns,
diag.FromErr(fmt.Errorf("system syslog file %v already exists", d.Get("filename").(string)))...)
}
if err := setSystemSyslogFile(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("create resource junos_system_syslog_file", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
syslogFileExists, err = checkSystemSyslogFileExists(d.Get("filename").(string), m, jnprSess)
if err != nil {
return append(diagWarns, diag.FromErr(err)...)
}
if syslogFileExists {
d.SetId(d.Get("filename").(string))
} else {
return append(diagWarns, diag.FromErr(fmt.Errorf("system syslog file %v not exists after commit "+
"=> check your config", d.Get("filename").(string)))...)
}
return append(diagWarns, resourceSystemSyslogFileReadWJnprSess(d, m, jnprSess)...)
}
func resourceSystemSyslogFileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
jnprSess, err := sess.startNewSession()
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
return resourceSystemSyslogFileReadWJnprSess(d, m, jnprSess)
}
func resourceSystemSyslogFileReadWJnprSess(
d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics {
mutex.Lock()
syslogFileOptions, err := readSystemSyslogFile(d.Get("filename").(string), m, jnprSess)
mutex.Unlock()
if err != nil {
return diag.FromErr(err)
}
if syslogFileOptions.filename == "" {
d.SetId("")
} else {
fillSystemSyslogFileData(d, syslogFileOptions)
}
return nil
}
func resourceSystemSyslogFileUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
d.Partial(true)
sess := m.(*Session)
jnprSess, err := sess.startNewSession()
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
sess.configLock(jnprSess)
var diagWarns diag.Diagnostics
if err := delSystemSyslogFile(d.Get("filename").(string), m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if err := setSystemSyslogFile(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("update resource junos_system_syslog_file", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
d.Partial(false)
return append(diagWarns, resourceSystemSyslogFileReadWJnprSess(d, m, jnprSess)...)
}
func resourceSystemSyslogFileDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
jnprSess, err := sess.startNewSession()
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
sess.configLock(jnprSess)
var diagWarns diag.Diagnostics
if err := delSystemSyslogFile(d.Get("filename").(string), m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("delete resource junos_system_syslog_file", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
return diagWarns
}
func resourceSystemSyslogFileImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
sess := m.(*Session)
jnprSess, err := sess.startNewSession()
if err != nil {
return nil, err
}
defer sess.closeSession(jnprSess)
result := make([]*schema.ResourceData, 1)
syslogFileExists, err := checkSystemSyslogFileExists(d.Id(), m, jnprSess)
if err != nil {
return nil, err
}
if !syslogFileExists {
return nil, fmt.Errorf("don't find system syslog file with id '%v' (id must be <filename>)", d.Id())
}
syslogFileOptions, err := readSystemSyslogFile(d.Id(), m, jnprSess)
if err != nil {
return nil, err
}
fillSystemSyslogFileData(d, syslogFileOptions)
result[0] = d
return result, nil
}
func checkSystemSyslogFileExists(filename string, m interface{}, jnprSess *NetconfObject) (bool, error) {
sess := m.(*Session)
syslogFileConfig, err := sess.command("show configuration"+
" system syslog file "+filename+" | display set", jnprSess)
if err != nil {
return false, err
}
if syslogFileConfig == emptyWord {
return false, nil
}
return true, nil
}
func setSystemSyslogFile(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
setPrefix := "set system syslog file " + d.Get("filename").(string)
configSet := make([]string, 0)
if d.Get("allow_duplicates").(bool) {
configSet = append(configSet, setPrefix+" allow-duplicates")
}
if d.Get("explicit_priority").(bool) {
configSet = append(configSet, setPrefix+" explicit-priority")
}
if d.Get("match").(string) != "" {
configSet = append(configSet, setPrefix+" match \""+d.Get("match").(string)+"\"")
}
for _, v := range d.Get("match_strings").([]interface{}) {
configSet = append(configSet, setPrefix+" match-strings \""+v.(string)+"\"")
}
for _, v := range d.Get("structured_data").([]interface{}) {
configSet = append(configSet, setPrefix+" structured-data")
if v != nil {
ma := v.(map[string]interface{})
if ma["brief"].(bool) {
configSet = append(configSet, setPrefix+" structured-data brief")
}
}
}
if d.Get("any_severity").(string) != "" {
configSet = append(configSet, setPrefix+" any "+d.Get("any_severity").(string))
}
if d.Get("authorization_severity").(string) != "" {
configSet = append(configSet, setPrefix+" authorization "+d.Get("authorization_severity").(string))
}
if d.Get("changelog_severity").(string) != "" {
configSet = append(configSet, setPrefix+" change-log "+d.Get("changelog_severity").(string))
}
if d.Get("conflictlog_severity").(string) != "" {
configSet = append(configSet, setPrefix+" conflict-log "+d.Get("conflictlog_severity").(string))
}
if d.Get("daemon_severity").(string) != "" {
configSet = append(configSet, setPrefix+" daemon "+d.Get("daemon_severity").(string))
}
if d.Get("dfc_severity").(string) != "" {
configSet = append(configSet, setPrefix+" dfc "+d.Get("dfc_severity").(string))
}
if d.Get("external_severity").(string) != "" {
configSet = append(configSet, setPrefix+" external "+d.Get("external_severity").(string))
}
if d.Get("firewall_severity").(string) != "" {
configSet = append(configSet, setPrefix+" firewall "+d.Get("firewall_severity").(string))
}
if d.Get("ftp_severity").(string) != "" {
configSet = append(configSet, setPrefix+" ftp "+d.Get("ftp_severity").(string))
}
if d.Get("interactivecommands_severity").(string) != "" {
configSet = append(configSet, setPrefix+" interactive-commands "+d.Get("interactivecommands_severity").(string))
}
if d.Get("kernel_severity").(string) != "" {
configSet = append(configSet, setPrefix+" kernel "+d.Get("kernel_severity").(string))
}
if d.Get("ntp_severity").(string) != "" {
configSet = append(configSet, setPrefix+" ntp "+d.Get("ntp_severity").(string))
}
if d.Get("pfe_severity").(string) != "" {
configSet = append(configSet, setPrefix+" pfe "+d.Get("pfe_severity").(string))
}
if d.Get("security_severity").(string) != "" {
configSet = append(configSet, setPrefix+" security "+d.Get("security_severity").(string))
}
if d.Get("user_severity").(string) != "" {
configSet = append(configSet, setPrefix+" user "+d.Get("user_severity").(string))
}
for _, v := range d.Get("archive").([]interface{}) {
setPrefixArchive := setPrefix + " archive"
configSet = append(configSet, setPrefixArchive)
if v != nil {
archive := v.(map[string]interface{})
for _, v2 := range archive["sites"].([]interface{}) {
sites := v2.(map[string]interface{})
setPrefixArchiveSite := setPrefixArchive + " archive-sites " + sites["url"].(string)
configSet = append(configSet, setPrefixArchiveSite)
if sites["password"].(string) != "" {
configSet = append(configSet, setPrefixArchiveSite+" password \""+
sites["password"].(string)+"\"")
}
if sites["routing_instance"].(string) != "" {
configSet = append(configSet, setPrefixArchiveSite+" routing-instance "+
sites["routing_instance"].(string))
}
}
if archive["binary_data"].(bool) {
configSet = append(configSet, setPrefixArchive+" binary-data")
}
if archive["no_binary_data"].(bool) {
configSet = append(configSet, setPrefixArchive+" no-binary-data")
}
if archive["world_readable"].(bool) {
configSet = append(configSet, setPrefixArchive+" world-readable")
}
if archive["no_world_readable"].(bool) {
configSet = append(configSet, setPrefixArchive+" no-world-readable")
}
if archive["files"].(int) != 0 {
configSet = append(configSet, setPrefixArchive+" files "+
strconv.Itoa(archive["files"].(int)))
}
if archive["size"].(int) != 0 {
configSet = append(configSet, setPrefixArchive+" size "+
strconv.Itoa(archive["size"].(int)))
}
if archive["start_time"].(string) != "" {
configSet = append(configSet, setPrefixArchive+" start-time "+
archive["start_time"].(string))
}
if archive["transfer_interval"].(int) != 0 {
configSet = append(configSet, setPrefixArchive+" transfer-interval "+
strconv.Itoa(archive["transfer_interval"].(int)))
}
}
}
return sess.configSet(configSet, jnprSess)
}
func readSystemSyslogFile(filename string, m interface{}, jnprSess *NetconfObject) (syslogFileOptions, error) {
sess := m.(*Session)
var confRead syslogFileOptions
syslogFileConfig, err := sess.command("show configuration"+
" system syslog file "+filename+" | display set relative", jnprSess)
if err != nil {
return confRead, err
}
if syslogFileConfig != emptyWord {
confRead.filename = filename
for _, item := range strings.Split(syslogFileConfig, "\n") {
if strings.Contains(item, "<configuration-output>") {
continue
}
if strings.Contains(item, "</configuration-output>") {
break
}
itemTrim := strings.TrimPrefix(item, setLineStart)
switch {
case itemTrim == "allow-duplicates":
confRead.allowDuplicates = true
case itemTrim == "explicit-priority":
confRead.explicitPriority = true
case strings.HasPrefix(itemTrim, "match "):
confRead.match = strings.Trim(strings.TrimPrefix(itemTrim, "match "), "\"")
case strings.HasPrefix(itemTrim, "match-strings "):
confRead.matchStrings = append(confRead.matchStrings,
strings.Trim(strings.TrimPrefix(itemTrim, "match-strings "), "\""))
case strings.HasPrefix(itemTrim, "structured-data"):
if len(confRead.structuredData) == 0 {
confRead.structuredData = append(confRead.structuredData, map[string]interface{}{
"brief": false,
})
}
if itemTrim == "structured-data brief" {
confRead.structuredData[0]["brief"] = true
}
case strings.HasPrefix(itemTrim, "any "):
confRead.anySeverity = strings.TrimPrefix(itemTrim, "any ")
case strings.HasPrefix(itemTrim, "authorization "):
confRead.authorizationSeverity = strings.TrimPrefix(itemTrim, "authorization ")
case strings.HasPrefix(itemTrim, "change-log "):
confRead.changelogSeverity = strings.TrimPrefix(itemTrim, "change-log ")
case strings.HasPrefix(itemTrim, "conflict-log "):
confRead.conflictlogSeverity = strings.TrimPrefix(itemTrim, "conflict-log ")
case strings.HasPrefix(itemTrim, "daemon "):
confRead.daemonSeverity = strings.TrimPrefix(itemTrim, "daemon ")
case strings.HasPrefix(itemTrim, "dfc "):
confRead.dfcSeverity = strings.TrimPrefix(itemTrim, "dfc ")
case strings.HasPrefix(itemTrim, "external "):
confRead.externalSeverity = strings.TrimPrefix(itemTrim, "external ")
case strings.HasPrefix(itemTrim, "firewall "):
confRead.firewallSeverity = strings.TrimPrefix(itemTrim, "firewall ")
case strings.HasPrefix(itemTrim, "ftp "):
confRead.ftpSeverity = strings.TrimPrefix(itemTrim, "ftp ")
case strings.HasPrefix(itemTrim, "interactive-commands "):
confRead.interactivecommandsSeverity = strings.TrimPrefix(itemTrim, "interactive-commands ")
case strings.HasPrefix(itemTrim, "kernel "):
confRead.kernelSeverity = strings.TrimPrefix(itemTrim, "kernel ")
case strings.HasPrefix(itemTrim, "ntp "):
confRead.ntpSeverity = strings.TrimPrefix(itemTrim, "ntp ")
case strings.HasPrefix(itemTrim, "pfe "):
confRead.pfeSeverity = strings.TrimPrefix(itemTrim, "pfe ")
case strings.HasPrefix(itemTrim, "security "):
confRead.securitySeverity = strings.TrimPrefix(itemTrim, "security ")
case strings.HasPrefix(itemTrim, "user "):
confRead.userSeverity = strings.TrimPrefix(itemTrim, "user ")
case strings.HasPrefix(itemTrim, "archive"):
if len(confRead.archive) == 0 {
confRead.archive = append(confRead.archive, map[string]interface{}{
"sites": make([]map[string]interface{}, 0),
"binary_data": false,
"no_binary_data": false,
"world_readable": false,
"no_world_readable": false,
"files": 0,
"size": 0,
"transfer_interval": 0,
"start_time": "",
})
}
switch {
case itemTrim == "archive binary-data":
confRead.archive[0]["binary_data"] = true
case itemTrim == "archive no-binary-data":
confRead.archive[0]["no_binary_data"] = true
case itemTrim == "archive world-readable":
confRead.archive[0]["world_readable"] = true
case itemTrim == "archive no-world-readable":
confRead.archive[0]["no_world_readable"] = true
case strings.HasPrefix(itemTrim, "archive files "):
var err error
confRead.archive[0]["files"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "archive files "))
if err != nil {
return confRead, fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "archive size "):
var err error
confRead.archive[0]["size"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "archive size "))
if err != nil {
return confRead, fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "archive transfer-interval "):
var err error
confRead.archive[0]["transfer_interval"], err =
strconv.Atoi(strings.TrimPrefix(itemTrim, "archive transfer-interval "))
if err != nil {
return confRead, fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "archive start-time "):
confRead.archive[0]["start_time"] = strings.TrimPrefix(itemTrim, "archive start-time ")
case strings.HasPrefix(itemTrim, "archive archive-sites "):
itemTrimArchSitesSplit := strings.Split(
strings.TrimPrefix(itemTrim, "archive archive-sites "), " ")
itemTrimArchSites := strings.TrimPrefix(itemTrim, "archive archive-sites "+itemTrimArchSitesSplit[0]+" ")
sitesOptions := map[string]interface{}{
"url": itemTrimArchSitesSplit[0],
"password": "",
"routing_instance": "",
}
confRead.archive[0]["sites"] = copyAndRemoveItemMapList("url", sitesOptions,
confRead.archive[0]["sites"].([]map[string]interface{}))
switch {
case strings.HasPrefix(itemTrimArchSites, "password "):
var err error
sitesOptions["password"], err = jdecode.Decode(strings.Trim(strings.TrimPrefix(
itemTrimArchSites, "password "), "\""))
if err != nil {
return confRead, fmt.Errorf("failed to decode password : %w", err)
}
case strings.HasPrefix(itemTrimArchSites, "routing-instance "):
sitesOptions["routing_instance"] = strings.TrimPrefix(itemTrimArchSites, "routing-instance ")
}
confRead.archive[0]["sites"] = append(confRead.archive[0]["sites"].([]map[string]interface{}), sitesOptions)
}
}
}
}
return confRead, nil
}
func delSystemSyslogFile(filename string, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
configSet := make([]string, 0, 1)
configSet = append(configSet, "delete system syslog file "+filename)
return sess.configSet(configSet, jnprSess)
}
func fillSystemSyslogFileData(d *schema.ResourceData, syslogFileOptions syslogFileOptions) {
if tfErr := d.Set("filename", syslogFileOptions.filename); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("allow_duplicates", syslogFileOptions.allowDuplicates); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("explicit_priority", syslogFileOptions.explicitPriority); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("match", syslogFileOptions.match); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("match_strings", syslogFileOptions.matchStrings); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("structured_data", syslogFileOptions.structuredData); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("any_severity", syslogFileOptions.anySeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("authorization_severity", syslogFileOptions.authorizationSeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("changelog_severity", syslogFileOptions.changelogSeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("conflictlog_severity", syslogFileOptions.conflictlogSeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("daemon_severity", syslogFileOptions.daemonSeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("dfc_severity", syslogFileOptions.dfcSeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("external_severity", syslogFileOptions.externalSeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("firewall_severity", syslogFileOptions.firewallSeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("ftp_severity", syslogFileOptions.ftpSeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("interactivecommands_severity", syslogFileOptions.interactivecommandsSeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("kernel_severity", syslogFileOptions.kernelSeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("ntp_severity", syslogFileOptions.ntpSeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("pfe_severity", syslogFileOptions.pfeSeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("security_severity", syslogFileOptions.securitySeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("user_severity", syslogFileOptions.userSeverity); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("archive", syslogFileOptions.archive); tfErr != nil {
panic(tfErr)
}
}