-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
resource_security_nat_static.go
646 lines (604 loc) · 23.7 KB
/
resource_security_nat_static.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
package junos
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/hashicorp/go-cty/cty"
"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"
)
type natStaticOptions struct {
name string
description string
from []map[string]interface{}
rule []map[string]interface{}
}
func resourceSecurityNatStatic() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSecurityNatStaticCreate,
ReadContext: resourceSecurityNatStaticRead,
UpdateContext: resourceSecurityNatStaticUpdate,
DeleteContext: resourceSecurityNatStaticDelete,
Importer: &schema.ResourceImporter{
State: resourceSecurityNatStaticImport,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 32, formatDefault),
},
"from": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"interface", "routing-instance", "zone"}, false),
},
"value": {
Type: schema.TypeSet,
Required: true,
MinItems: 1,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"configure_rules_singly": {
Type: schema.TypeBool,
Optional: true,
ExactlyOneOf: []string{"configure_rules_singly", "rule"},
},
"rule": {
Type: schema.TypeList,
Optional: true,
ExactlyOneOf: []string{"rule", "configure_rules_singly"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 32, formatDefault),
},
"destination_address": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsCIDRNetwork(0, 128),
},
"destination_address_name": {
Type: schema.TypeString,
Optional: true,
},
"destination_port": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 65535),
},
"destination_port_to": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 65535),
},
"source_address": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"source_address_name": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"source_port": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"then": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{inetWord, prefixWord, prefixNameWord}, false),
},
"mapped_port": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 65535),
},
"mapped_port_to": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 65535),
},
"prefix": {
Type: schema.TypeString,
Optional: true,
},
"routing_instance": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault),
},
},
},
},
},
},
},
"description": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceSecurityNatStaticCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
if sess.junosFakeCreateSetFile != "" {
if err := setSecurityNatStatic(d, m, nil); err != nil {
return diag.FromErr(err)
}
d.SetId(d.Get("name").(string))
return nil
}
jnprSess, err := sess.startNewSession()
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
if !checkCompatibilitySecurity(jnprSess) {
return diag.FromErr(fmt.Errorf("security nat static not compatible with Junos device %s",
jnprSess.SystemInformation.HardwareModel))
}
sess.configLock(jnprSess)
var diagWarns diag.Diagnostics
securityNatStaticExists, err := checkSecurityNatStaticExists(d.Get("name").(string), m, jnprSess)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if securityNatStaticExists {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(fmt.Errorf("security nat static %v already exists", d.Get("name").(string)))...)
}
if err := setSecurityNatStatic(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("create resource junos_security_nat_static", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
securityNatStaticExists, err = checkSecurityNatStaticExists(d.Get("name").(string), m, jnprSess)
if err != nil {
return append(diagWarns, diag.FromErr(err)...)
}
if securityNatStaticExists {
d.SetId(d.Get("name").(string))
} else {
return append(diagWarns, diag.FromErr(fmt.Errorf("security nat static %v not exists after commit "+
"=> check your config", d.Get("name").(string)))...)
}
return append(diagWarns, resourceSecurityNatStaticReadWJnprSess(d, m, jnprSess)...)
}
func resourceSecurityNatStaticRead(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 resourceSecurityNatStaticReadWJnprSess(d, m, jnprSess)
}
func resourceSecurityNatStaticReadWJnprSess(
d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics {
mutex.Lock()
natStaticOptions, err := readSecurityNatStatic(d.Get("name").(string), m, jnprSess)
mutex.Unlock()
if err != nil {
return diag.FromErr(err)
}
if natStaticOptions.name == "" {
d.SetId("")
} else {
fillSecurityNatStaticData(d, natStaticOptions)
}
return nil
}
func resourceSecurityNatStaticUpdate(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
configureRulesSingly := d.Get("configure_rules_singly").(bool)
if d.HasChange("configure_rules_singly") {
if o, _ := d.GetChange("configure_rules_singly"); o.(bool) {
configureRulesSingly = o.(bool)
diagWarns = append(diagWarns, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Disable configure_rules_singly on resource already created doesn't " +
"delete rule(s) already configured.",
Detail: "So refresh resource after apply to detect rule(s) that need to be deleted",
AttributePath: cty.Path{cty.GetAttrStep{Name: "configure_rules_singly"}},
})
} else {
diagWarns = append(diagWarns, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Enable configure_rules_singly on resource already created doesn't " +
"delete rules already configured.",
Detail: "So import rule(s) in dedicated resource(s) to be able to manage them",
AttributePath: cty.Path{cty.GetAttrStep{Name: "configure_rules_singly"}},
})
}
}
if configureRulesSingly {
if err := delSecurityNatStaticWithoutRules(d.Get("name").(string), m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
} else {
if err := delSecurityNatStatic(d.Get("name").(string), m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
}
if err := setSecurityNatStatic(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("update resource junos_security_nat_static", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
d.Partial(false)
return append(diagWarns, resourceSecurityNatStaticReadWJnprSess(d, m, jnprSess)...)
}
func resourceSecurityNatStaticDelete(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 := delSecurityNatStatic(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_security_nat_static", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
return diagWarns
}
func resourceSecurityNatStaticImport(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)
idList := strings.Split(d.Id(), idSeparator)
securityNatStaticExists, err := checkSecurityNatStaticExists(idList[0], m, jnprSess)
if err != nil {
return nil, err
}
if !securityNatStaticExists {
return nil, fmt.Errorf("don't find nat static with id '%v' "+
"(id must be <name> or <name>%sno_rules)", idList[0], idSeparator)
}
natStaticOptions, err := readSecurityNatStatic(idList[0], m, jnprSess)
if err != nil {
return nil, err
}
if len(idList) > 1 && idList[1] == "no_rules" {
if tfErr := d.Set("configure_rules_singly", true); tfErr != nil {
panic(tfErr)
}
}
fillSecurityNatStaticData(d, natStaticOptions)
result[0] = d
return result, nil
}
func checkSecurityNatStaticExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) {
sess := m.(*Session)
showConfig, err := sess.command("show configuration"+
" security nat static rule-set "+name+" | display set", jnprSess)
if err != nil {
return false, err
}
if showConfig == emptyWord {
return false, nil
}
return true, nil
}
func setSecurityNatStatic(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
configSet := make([]string, 0)
regexpSourcePort := regexp.MustCompile(`^\d+( to \d+)?$`)
setPrefix := "set security nat static rule-set " + d.Get("name").(string)
for _, v := range d.Get("from").([]interface{}) {
from := v.(map[string]interface{})
for _, value := range sortSetOfString(from["value"].(*schema.Set).List()) {
configSet = append(configSet, setPrefix+" from "+from["type"].(string)+" "+value)
}
}
ruleNameList := make([]string, 0)
if !d.Get("configure_rules_singly").(bool) {
for _, v := range d.Get("rule").([]interface{}) {
rule := v.(map[string]interface{})
if bchk.StringInSlice(rule["name"].(string), ruleNameList) {
return fmt.Errorf("multiple blocks rule with the same name %s", rule["name"].(string))
}
ruleNameList = append(ruleNameList, rule["name"].(string))
setPrefixRule := setPrefix + " rule " + rule["name"].(string)
if rule["destination_address"].(string) == "" && rule["destination_address_name"].(string) == "" {
return fmt.Errorf("missing destination_address or destination_address_name in rule %s", rule["name"].(string))
}
if rule["destination_address"].(string) != "" && rule["destination_address_name"].(string) != "" {
return fmt.Errorf("destination_address and destination_address_name must not be set at the same time "+
"in rule %s", rule["name"].(string))
}
if vv := rule["destination_address"].(string); vv != "" {
configSet = append(configSet, setPrefixRule+" match destination-address "+vv)
}
if vv := rule["destination_address_name"].(string); vv != "" {
configSet = append(configSet, setPrefixRule+" match destination-address-name \""+vv+"\"")
}
if vv := rule["destination_port"].(int); vv != 0 {
configSet = append(configSet, setPrefixRule+" match destination-port "+strconv.Itoa(vv))
if vvTo := rule["destination_port_to"].(int); vvTo != 0 {
configSet = append(configSet, setPrefixRule+" match destination-port to "+strconv.Itoa(vvTo))
}
} else if rule["destination_port_to"].(int) != 0 {
return fmt.Errorf("destination_port need to be set with destination_port_to in rule %s", rule["name"].(string))
}
for _, vv := range sortSetOfString(rule["source_address"].(*schema.Set).List()) {
if err := validateCIDRNetwork(vv); err != nil {
return err
}
configSet = append(configSet, setPrefixRule+" match source-address "+vv)
}
for _, vv := range sortSetOfString(rule["source_address_name"].(*schema.Set).List()) {
configSet = append(configSet, setPrefixRule+" match source-address-name \""+vv+"\"")
}
for _, vv := range sortSetOfString(rule["source_port"].(*schema.Set).List()) {
if !regexpSourcePort.MatchString(vv) {
return fmt.Errorf("source_port need to have format `x` or `x to y` in rule %s", rule["name"].(string))
}
configSet = append(configSet, setPrefixRule+" match source-port "+vv)
}
for _, thenV := range rule[thenWord].([]interface{}) {
then := thenV.(map[string]interface{})
if then["type"].(string) == inetWord {
if then["routing_instance"].(string) == "" {
return fmt.Errorf("missing routing_instance in rule %s with type = inet", rule["name"].(string))
}
if then["prefix"].(string) != "" ||
then["mapped_port"].(int) != 0 ||
then["mapped_port_to"].(int) != 0 {
return fmt.Errorf("only routing_instance need to be set in rule %s with type = inet", rule["name"].(string))
}
configSet = append(configSet, setPrefixRule+" then static-nat inet routing-instance "+
then["routing_instance"].(string))
}
if then["type"].(string) == prefixWord || then["type"].(string) == prefixNameWord {
setPrefixRuleThenStaticNat := setPrefixRule + " then static-nat "
if then["type"].(string) == prefixWord {
setPrefixRuleThenStaticNat += "prefix "
if then["prefix"].(string) == "" {
return fmt.Errorf("missing prefix in rule %s with type = prefix", rule["name"].(string))
}
if err := validateCIDRNetwork(then["prefix"].(string)); err != nil {
return err
}
}
if then["type"].(string) == prefixNameWord {
setPrefixRuleThenStaticNat += "prefix-name "
if then["prefix"].(string) == "" {
return fmt.Errorf("missing prefix in rule %s with type = prefix-name", rule["name"].(string))
}
}
configSet = append(configSet, setPrefixRuleThenStaticNat+"\""+then["prefix"].(string)+"\"")
if vv := then["mapped_port"].(int); vv != 0 {
configSet = append(configSet, setPrefixRuleThenStaticNat+"mapped-port "+strconv.Itoa(vv))
if vvTo := then["mapped_port_to"].(int); vvTo != 0 {
configSet = append(configSet, setPrefixRuleThenStaticNat+"mapped-port to "+strconv.Itoa(vvTo))
}
} else if then["mapped_port_to"].(int) != 0 {
return fmt.Errorf("mapped_port need to set with mapped_port_to in rule %s", rule["name"].(string))
}
if vv := then["routing_instance"].(string); vv != "" {
configSet = append(configSet, setPrefixRuleThenStaticNat+"routing-instance "+vv)
}
}
}
}
}
if v := d.Get("description").(string); v != "" {
configSet = append(configSet, setPrefix+" description \""+v+"\"")
}
return sess.configSet(configSet, jnprSess)
}
func readSecurityNatStatic(name string, m interface{}, jnprSess *NetconfObject) (natStaticOptions, error) {
sess := m.(*Session)
var confRead natStaticOptions
showConfig, err := sess.command("show configuration"+
" security nat static rule-set "+name+" | display set relative", jnprSess)
if err != nil {
return confRead, err
}
if showConfig != emptyWord {
confRead.name = name
for _, item := range strings.Split(showConfig, "\n") {
if strings.Contains(item, "<configuration-output>") {
continue
}
if strings.Contains(item, "</configuration-output>") {
break
}
itemTrim := strings.TrimPrefix(item, setLineStart)
switch {
case strings.HasPrefix(itemTrim, "from "):
fromWords := strings.Split(strings.TrimPrefix(itemTrim, "from "), " ")
if len(confRead.from) == 0 {
confRead.from = append(confRead.from, map[string]interface{}{
"type": fromWords[0],
"value": make([]string, 0),
})
}
confRead.from[0]["value"] = append(confRead.from[0]["value"].([]string), fromWords[1])
case strings.HasPrefix(itemTrim, "rule "):
ruleConfig := strings.Split(strings.TrimPrefix(itemTrim, "rule "), " ")
ruleOptions := map[string]interface{}{
"name": ruleConfig[0],
"destination_address": "",
"destination_address_name": "",
"destination_port": 0,
"destination_port_to": 0,
"source_address": make([]string, 0),
"source_address_name": make([]string, 0),
"source_port": make([]string, 0),
"then": make([]map[string]interface{}, 0),
}
confRead.rule = copyAndRemoveItemMapList("name", ruleOptions, confRead.rule)
switch {
case strings.HasPrefix(itemTrim, "rule "+ruleConfig[0]+" match destination-address "):
ruleOptions["destination_address"] = strings.TrimPrefix(itemTrim,
"rule "+ruleConfig[0]+" match destination-address ")
case strings.HasPrefix(itemTrim, "rule "+ruleConfig[0]+" match destination-address-name "):
ruleOptions["destination_address_name"] = strings.Trim(strings.TrimPrefix(itemTrim,
"rule "+ruleConfig[0]+" match destination-address-name "), "\"")
case strings.HasPrefix(itemTrim, "rule "+ruleConfig[0]+" match destination-port to "):
var err error
ruleOptions["destination_port_to"], err = strconv.Atoi(strings.TrimPrefix(itemTrim,
"rule "+ruleConfig[0]+" match destination-port to "))
if err != nil {
return confRead, fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "rule "+ruleConfig[0]+" match destination-port "):
var err error
ruleOptions["destination_port"], err = strconv.Atoi(strings.TrimPrefix(itemTrim,
"rule "+ruleConfig[0]+" match destination-port "))
if err != nil {
return confRead, fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "rule "+ruleConfig[0]+" match source-address "):
ruleOptions["source_address"] = append(ruleOptions["source_address"].([]string),
strings.TrimPrefix(itemTrim, "rule "+ruleConfig[0]+" match source-address "))
case strings.HasPrefix(itemTrim, "rule "+ruleConfig[0]+" match source-address-name "):
ruleOptions["source_address_name"] = append(ruleOptions["source_address_name"].([]string),
strings.Trim(strings.TrimPrefix(itemTrim, "rule "+ruleConfig[0]+" match source-address-name "), "\""))
case strings.HasPrefix(itemTrim, "rule "+ruleConfig[0]+" match source-port "):
ruleOptions["source_port"] = append(ruleOptions["source_port"].([]string),
strings.TrimPrefix(itemTrim, "rule "+ruleConfig[0]+" match source-port "))
case strings.HasPrefix(itemTrim, "rule "+ruleConfig[0]+" then static-nat "):
itemThen := strings.TrimPrefix(itemTrim, "rule "+ruleConfig[0]+" then static-nat ")
if len(ruleOptions["then"].([]map[string]interface{})) == 0 {
ruleOptions["then"] = append(ruleOptions["then"].([]map[string]interface{}),
map[string]interface{}{
"type": "",
"mapped_port": 0,
"mapped_port_to": 0,
"prefix": "",
"routing_instance": "",
})
}
ruleThenOptions := ruleOptions["then"].([]map[string]interface{})[0]
switch {
case strings.HasPrefix(itemThen, "prefix ") || strings.HasPrefix(itemThen, "prefix-name "):
if strings.HasPrefix(itemThen, "prefix ") {
ruleThenOptions["type"] = prefixWord
itemThen = strings.TrimPrefix(itemThen, "prefix ")
}
if strings.HasPrefix(itemThen, "prefix-name ") {
ruleThenOptions["type"] = prefixNameWord
itemThen = strings.TrimPrefix(itemThen, "prefix-name ")
}
switch {
case strings.HasPrefix(itemThen, "routing-instance "):
ruleThenOptions["routing_instance"] = strings.TrimPrefix(itemThen, "routing-instance ")
case strings.HasPrefix(itemThen, "mapped-port to "):
var err error
ruleThenOptions["mapped_port_to"], err = strconv.Atoi(strings.TrimPrefix(itemThen, "mapped-port to "))
if err != nil {
return confRead, fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemThen, "mapped-port "):
var err error
ruleThenOptions["mapped_port"], err = strconv.Atoi(strings.TrimPrefix(itemThen, "mapped-port "))
if err != nil {
return confRead, fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
default:
ruleThenOptions["prefix"] = strings.Trim(itemThen, "\"")
}
case strings.HasPrefix(itemThen, "inet "):
ruleThenOptions["type"] = inetWord
ruleThenOptions["routing_instance"] = strings.TrimPrefix(itemThen, "inet routing-instance ")
}
}
confRead.rule = append(confRead.rule, ruleOptions)
case strings.HasPrefix(itemTrim, "description "):
confRead.description = strings.Trim(strings.TrimPrefix(itemTrim, "description "), "\"")
}
}
}
return confRead, nil
}
func delSecurityNatStatic(natStatic string, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
configSet := make([]string, 0, 1)
configSet = append(configSet, "delete security nat static rule-set "+natStatic)
return sess.configSet(configSet, jnprSess)
}
func delSecurityNatStaticWithoutRules(natStatic string, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
configSet := make([]string, 0, 1)
configSet = append(configSet, "delete security nat static rule-set "+natStatic+" description")
configSet = append(configSet, "delete security nat static rule-set "+natStatic+" from")
return sess.configSet(configSet, jnprSess)
}
func fillSecurityNatStaticData(d *schema.ResourceData, natStaticOptions natStaticOptions) {
if tfErr := d.Set("name", natStaticOptions.name); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("from", natStaticOptions.from); tfErr != nil {
panic(tfErr)
}
if !d.Get("configure_rules_singly").(bool) {
if tfErr := d.Set("rule", natStaticOptions.rule); tfErr != nil {
panic(tfErr)
}
}
if tfErr := d.Set("description", natStaticOptions.description); tfErr != nil {
panic(tfErr)
}
}