-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
resource_security_zone_book_address.go
427 lines (385 loc) · 13.7 KB
/
resource_security_zone_book_address.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
package junos
import (
"context"
"fmt"
"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"
)
type zoneBookAddressOptions struct {
dnsIPv4Only bool
dnsIPv6Only bool
cidr string
description string
dnsName string
name string
rangeFrom string
rangeTo string
wildcard string
zone string
}
func resourceSecurityZoneBookAddress() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSecurityZoneBookAddressCreate,
ReadContext: resourceSecurityZoneBookAddressRead,
UpdateContext: resourceSecurityZoneBookAddressUpdate,
DeleteContext: resourceSecurityZoneBookAddressDelete,
Importer: &schema.ResourceImporter{
State: resourceSecurityZoneBookAddressImport,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatAddressName),
},
"zone": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault),
},
"cidr": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsCIDRNetwork(0, 128),
ExactlyOneOf: []string{"cidr", "dns_name", "range_from", "wildcard"},
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"dns_ipv4_only": {
Type: schema.TypeBool,
Optional: true,
RequiredWith: []string{"dns_name"},
ConflictsWith: []string{"dns_ipv6_only"},
},
"dns_ipv6_only": {
Type: schema.TypeBool,
Optional: true,
RequiredWith: []string{"dns_name"},
ConflictsWith: []string{"dns_ipv4_only"},
},
"dns_name": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"cidr", "dns_name", "range_from", "wildcard"},
},
"range_from": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsIPAddress,
RequiredWith: []string{"range_to"},
ExactlyOneOf: []string{"cidr", "dns_name", "range_from", "wildcard"},
},
"range_to": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsIPAddress,
RequiredWith: []string{"range_from"},
},
"wildcard": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateWildcardFunc(),
ExactlyOneOf: []string{"cidr", "dns_name", "range_from", "wildcard"},
},
},
}
}
func resourceSecurityZoneBookAddressCreate(ctx context.Context,
d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
if sess.junosFakeCreateSetFile != "" {
if err := setSecurityZoneBookAddress(d, m, nil); err != nil {
return diag.FromErr(err)
}
d.SetId(d.Get("zone").(string) + idSeparator + 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 zone address-book address not compatible with Junos device %s",
jnprSess.SystemInformation.HardwareModel))
}
sess.configLock(jnprSess)
var diagWarns diag.Diagnostics
zonesExists, err := checkSecurityZonesExists(d.Get("zone").(string), m, jnprSess)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if !zonesExists {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns,
diag.FromErr(fmt.Errorf("security zone %v doesn't exist", d.Get("zone").(string)))...)
}
securityZoneBookAddressExists, err := checkSecurityZoneBookAddresssExists(
d.Get("zone").(string), d.Get("name").(string), m, jnprSess)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if securityZoneBookAddressExists {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(fmt.Errorf(
"security zone address-book address %v already exists in zone %s",
d.Get("name").(string), d.Get("zone").(string)))...)
}
if err := setSecurityZoneBookAddress(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("create resource junos_security_zone_book_address", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
securityZoneBookAddressExists, err =
checkSecurityZoneBookAddresssExists(d.Get("zone").(string), d.Get("name").(string), m, jnprSess)
if err != nil {
return append(diagWarns, diag.FromErr(err)...)
}
if securityZoneBookAddressExists {
d.SetId(d.Get("zone").(string) + idSeparator + d.Get("name").(string))
} else {
return append(diagWarns, diag.FromErr(fmt.Errorf(
"security zone address-book address %v not exists in zone %s after commit "+
"=> check your config", d.Get("name").(string), d.Get("zone").(string)))...)
}
return append(diagWarns, resourceSecurityZoneBookAddressReadWJnprSess(d, m, jnprSess)...)
}
func resourceSecurityZoneBookAddressRead(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 resourceSecurityZoneBookAddressReadWJnprSess(d, m, jnprSess)
}
func resourceSecurityZoneBookAddressReadWJnprSess(
d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics {
mutex.Lock()
zoneBookAddressOptions, err := readSecurityZoneBookAddress(d.Get("zone").(string), d.Get("name").(string), m, jnprSess)
mutex.Unlock()
if err != nil {
return diag.FromErr(err)
}
if zoneBookAddressOptions.name == "" {
d.SetId("")
} else {
fillSecurityZoneBookAddressData(d, zoneBookAddressOptions)
}
return nil
}
func resourceSecurityZoneBookAddressUpdate(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 := delSecurityZoneBookAddress(d.Get("zone").(string), d.Get("name").(string), m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if err := setSecurityZoneBookAddress(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("update resource junos_security_zone_book_address", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
d.Partial(false)
return append(diagWarns, resourceSecurityZoneBookAddressReadWJnprSess(d, m, jnprSess)...)
}
func resourceSecurityZoneBookAddressDelete(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 := delSecurityZoneBookAddress(d.Get("zone").(string), 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_zone_book_address", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
return diagWarns
}
func resourceSecurityZoneBookAddressImport(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)
if len(idList) < 2 {
return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator)
}
securityZoneBookAddressExists, err := checkSecurityZoneBookAddresssExists(idList[0], idList[1], m, jnprSess)
if err != nil {
return nil, err
}
if !securityZoneBookAddressExists {
return nil, fmt.Errorf(
"don't find zone address-book address with id '%v' (id must be <zone>"+idSeparator+"<name>)", d.Id())
}
zoneBookAddressOptions, err := readSecurityZoneBookAddress(idList[0], idList[1], m, jnprSess)
if err != nil {
return nil, err
}
fillSecurityZoneBookAddressData(d, zoneBookAddressOptions)
result[0] = d
return result, nil
}
func checkSecurityZoneBookAddresssExists(zone, address string, m interface{}, jnprSess *NetconfObject) (bool, error) {
sess := m.(*Session)
showConfig, err := sess.command("show configuration"+
" security zones security-zone "+zone+" address-book address "+address+" | display set", jnprSess)
if err != nil {
return false, err
}
if showConfig == emptyWord {
return false, nil
}
return true, nil
}
func setSecurityZoneBookAddress(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
configSet := make([]string, 0)
setPrefix := "set security zones security-zone " +
d.Get("zone").(string) + " address-book address " + d.Get("name").(string) + " "
if v := d.Get("cidr").(string); v != "" {
configSet = append(configSet, setPrefix+v)
}
if v := d.Get("description").(string); v != "" {
configSet = append(configSet, setPrefix+"description \""+v+"\"")
}
if v := d.Get("dns_name").(string); v != "" {
configSet = append(configSet, setPrefix+"dns-name "+v)
if d.Get("dns_ipv4_only").(bool) {
configSet = append(configSet, setPrefix+"dns-name "+v+" ipv4-only")
}
if d.Get("dns_ipv6_only").(bool) {
configSet = append(configSet, setPrefix+"dns-name "+v+" ipv6-only")
}
}
if v := d.Get("range_from").(string); v != "" {
configSet = append(configSet, setPrefix+"range-address "+v+" to "+d.Get("range_to").(string))
}
if v := d.Get("wildcard").(string); v != "" {
configSet = append(configSet, setPrefix+"wildcard-address "+v)
}
return sess.configSet(configSet, jnprSess)
}
func readSecurityZoneBookAddress(
zone, address string, m interface{}, jnprSess *NetconfObject) (zoneBookAddressOptions, error) {
sess := m.(*Session)
var confRead zoneBookAddressOptions
showConfig, err := sess.command("show configuration"+
" security zones security-zone "+zone+" address-book address "+address+" | display set relative", jnprSess)
if err != nil {
return confRead, err
}
if showConfig != emptyWord {
confRead.name = address
confRead.zone = zone
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, "description "):
confRead.description = strings.Trim(strings.TrimPrefix(itemTrim, "description "), "\"")
case strings.HasPrefix(itemTrim, "dns-name "):
dnsValue := strings.TrimPrefix(itemTrim, "dns-name ")
switch {
case strings.HasSuffix(itemTrim, " ipv4-only"):
confRead.dnsIPv4Only = true
dnsValue = strings.TrimSuffix(strings.TrimPrefix(itemTrim, "dns-name "), " ipv4-only")
case strings.HasSuffix(itemTrim, " ipv6-only"):
confRead.dnsIPv6Only = true
dnsValue = strings.TrimSuffix(strings.TrimPrefix(itemTrim, "dns-name "), " ipv6-only")
}
confRead.dnsName = dnsValue
case strings.HasPrefix(itemTrim, "range-address "):
itemTrimSplit := strings.Split(strings.TrimPrefix(itemTrim, "range-address "), " ")
confRead.rangeFrom = itemTrimSplit[0]
confRead.rangeTo = itemTrimSplit[2]
case strings.HasPrefix(itemTrim, "wildcard-address "):
confRead.wildcard = strings.TrimPrefix(itemTrim, "wildcard-address ")
case strings.Contains(itemTrim, "/"):
confRead.cidr = itemTrim
}
}
}
return confRead, nil
}
func delSecurityZoneBookAddress(zone, address string, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
configSet := []string{"delete security zones security-zone " + zone + " address-book address " + address}
return sess.configSet(configSet, jnprSess)
}
func fillSecurityZoneBookAddressData(d *schema.ResourceData, zoneBookAddressOptions zoneBookAddressOptions) {
if tfErr := d.Set("name", zoneBookAddressOptions.name); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("zone", zoneBookAddressOptions.zone); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("cidr", zoneBookAddressOptions.cidr); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("description", zoneBookAddressOptions.description); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("dns_ipv4_only", zoneBookAddressOptions.dnsIPv4Only); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("dns_ipv6_only", zoneBookAddressOptions.dnsIPv6Only); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("dns_name", zoneBookAddressOptions.dnsName); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("range_from", zoneBookAddressOptions.rangeFrom); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("range_to", zoneBookAddressOptions.rangeTo); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("wildcard", zoneBookAddressOptions.wildcard); tfErr != nil {
panic(tfErr)
}
}