-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
resource_security_nat_destination_pool.go
370 lines (328 loc) · 11.6 KB
/
resource_security_nat_destination_pool.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
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"
)
type natDestinationPoolOptions struct {
addressPort int
name string
address string
addressTo string
description string
routingInstance string
}
func resourceSecurityNatDestinationPool() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceSecurityNatDestinationPoolCreate,
ReadWithoutTimeout: resourceSecurityNatDestinationPoolRead,
UpdateWithoutTimeout: resourceSecurityNatDestinationPoolUpdate,
DeleteWithoutTimeout: resourceSecurityNatDestinationPoolDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceSecurityNatDestinationPoolImport,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 32, formatDefault),
},
"address": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateIPMaskFunc(),
},
"address_port": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 65535),
ConflictsWith: []string{"address_to"},
},
"address_to": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateIPMaskFunc(),
ConflictsWith: []string{"address_port"},
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"routing_instance": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault),
},
},
}
}
func resourceSecurityNatDestinationPoolCreate(ctx context.Context, d *schema.ResourceData, m interface{},
) diag.Diagnostics {
clt := m.(*Client)
if clt.fakeCreateSetFile != "" {
if err := setSecurityNatDestinationPool(d, clt, nil); err != nil {
return diag.FromErr(err)
}
d.SetId(d.Get("name").(string))
return nil
}
junSess, err := clt.startNewSession(ctx)
if err != nil {
return diag.FromErr(err)
}
defer clt.closeSession(junSess)
if !checkCompatibilitySecurity(junSess) {
return diag.FromErr(fmt.Errorf("security nat destination pool not compatible with Junos device %s",
junSess.SystemInformation.HardwareModel))
}
if err := clt.configLock(ctx, junSess); err != nil {
return diag.FromErr(err)
}
var diagWarns diag.Diagnostics
securityNatDestinationPoolExists, err := checkSecurityNatDestinationPoolExists(d.Get("name").(string), clt, junSess)
if err != nil {
appendDiagWarns(&diagWarns, clt.configClear(junSess))
return append(diagWarns, diag.FromErr(err)...)
}
if securityNatDestinationPoolExists {
appendDiagWarns(&diagWarns, clt.configClear(junSess))
return append(diagWarns,
diag.FromErr(fmt.Errorf("security nat destination pool %v already exists", d.Get("name").(string)))...)
}
if err := setSecurityNatDestinationPool(d, clt, junSess); err != nil {
appendDiagWarns(&diagWarns, clt.configClear(junSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := clt.commitConf("create resource junos_security_nat_destination_pool", junSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, clt.configClear(junSess))
return append(diagWarns, diag.FromErr(err)...)
}
securityNatDestinationPoolExists, err = checkSecurityNatDestinationPoolExists(d.Get("name").(string), clt, junSess)
if err != nil {
return append(diagWarns, diag.FromErr(err)...)
}
if securityNatDestinationPoolExists {
d.SetId(d.Get("name").(string))
} else {
return append(diagWarns, diag.FromErr(fmt.Errorf("security nat destination pool %v not exists after commit "+
"=> check your config", d.Get("name").(string)))...)
}
return append(diagWarns, resourceSecurityNatDestinationPoolReadWJunSess(d, clt, junSess)...)
}
func resourceSecurityNatDestinationPoolRead(ctx context.Context, d *schema.ResourceData, m interface{},
) diag.Diagnostics {
clt := m.(*Client)
junSess, err := clt.startNewSession(ctx)
if err != nil {
return diag.FromErr(err)
}
defer clt.closeSession(junSess)
return resourceSecurityNatDestinationPoolReadWJunSess(d, clt, junSess)
}
func resourceSecurityNatDestinationPoolReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession,
) diag.Diagnostics {
mutex.Lock()
natDestinationPoolOptions, err := readSecurityNatDestinationPool(d.Get("name").(string), clt, junSess)
mutex.Unlock()
if err != nil {
return diag.FromErr(err)
}
if natDestinationPoolOptions.name == "" {
d.SetId("")
} else {
fillSecurityNatDestinationPoolData(d, natDestinationPoolOptions)
}
return nil
}
func resourceSecurityNatDestinationPoolUpdate(ctx context.Context, d *schema.ResourceData, m interface{},
) diag.Diagnostics {
d.Partial(true)
clt := m.(*Client)
if clt.fakeUpdateAlso {
if err := delSecurityNatDestinationPool(d.Get("name").(string), clt, nil); err != nil {
return diag.FromErr(err)
}
if err := setSecurityNatDestinationPool(d, clt, nil); err != nil {
return diag.FromErr(err)
}
d.Partial(false)
return nil
}
junSess, err := clt.startNewSession(ctx)
if err != nil {
return diag.FromErr(err)
}
defer clt.closeSession(junSess)
if err := clt.configLock(ctx, junSess); err != nil {
return diag.FromErr(err)
}
var diagWarns diag.Diagnostics
if err := delSecurityNatDestinationPool(d.Get("name").(string), clt, junSess); err != nil {
appendDiagWarns(&diagWarns, clt.configClear(junSess))
return append(diagWarns, diag.FromErr(err)...)
}
if err := setSecurityNatDestinationPool(d, clt, junSess); err != nil {
appendDiagWarns(&diagWarns, clt.configClear(junSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := clt.commitConf("update resource junos_security_nat_destination_pool", junSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, clt.configClear(junSess))
return append(diagWarns, diag.FromErr(err)...)
}
d.Partial(false)
return append(diagWarns, resourceSecurityNatDestinationPoolReadWJunSess(d, clt, junSess)...)
}
func resourceSecurityNatDestinationPoolDelete(ctx context.Context, d *schema.ResourceData, m interface{},
) diag.Diagnostics {
clt := m.(*Client)
if clt.fakeDeleteAlso {
if err := delSecurityNatDestinationPool(d.Get("name").(string), clt, nil); err != nil {
return diag.FromErr(err)
}
return nil
}
junSess, err := clt.startNewSession(ctx)
if err != nil {
return diag.FromErr(err)
}
defer clt.closeSession(junSess)
if err := clt.configLock(ctx, junSess); err != nil {
return diag.FromErr(err)
}
var diagWarns diag.Diagnostics
if err := delSecurityNatDestinationPool(d.Get("name").(string), clt, junSess); err != nil {
appendDiagWarns(&diagWarns, clt.configClear(junSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := clt.commitConf("delete resource junos_security_nat_destination_pool", junSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, clt.configClear(junSess))
return append(diagWarns, diag.FromErr(err)...)
}
return diagWarns
}
func resourceSecurityNatDestinationPoolImport(ctx context.Context, d *schema.ResourceData, m interface{},
) ([]*schema.ResourceData, error) {
clt := m.(*Client)
junSess, err := clt.startNewSession(ctx)
if err != nil {
return nil, err
}
defer clt.closeSession(junSess)
result := make([]*schema.ResourceData, 1)
securityNatDestinationPoolExists, err := checkSecurityNatDestinationPoolExists(d.Id(), clt, junSess)
if err != nil {
return nil, err
}
if !securityNatDestinationPoolExists {
return nil, fmt.Errorf("don't find nat destination pool with id '%v' (id must be <name>)", d.Id())
}
natDestinationPoolOptions, err := readSecurityNatDestinationPool(d.Id(), clt, junSess)
if err != nil {
return nil, err
}
fillSecurityNatDestinationPoolData(d, natDestinationPoolOptions)
result[0] = d
return result, nil
}
func checkSecurityNatDestinationPoolExists(name string, clt *Client, junSess *junosSession) (bool, error) {
showConfig, err := clt.command(cmdShowConfig+
"security nat destination pool "+name+pipeDisplaySet, junSess)
if err != nil {
return false, err
}
if showConfig == emptyW {
return false, nil
}
return true, nil
}
func setSecurityNatDestinationPool(d *schema.ResourceData, clt *Client, junSess *junosSession) error {
configSet := make([]string, 0)
setPrefix := "set security nat destination pool " + d.Get("name").(string)
configSet = append(configSet, setPrefix+" address "+d.Get("address").(string))
if d.Get("address_port").(int) != 0 {
configSet = append(configSet, setPrefix+" address port "+strconv.Itoa(d.Get("address_port").(int)))
}
if d.Get("address_to").(string) != "" {
configSet = append(configSet, setPrefix+" address to "+d.Get("address_to").(string))
}
if v := d.Get("description").(string); v != "" {
configSet = append(configSet, setPrefix+" description \""+v+"\"")
}
if d.Get("routing_instance").(string) != "" {
configSet = append(configSet, setPrefix+" routing-instance "+d.Get("routing_instance").(string))
}
return clt.configSet(configSet, junSess)
}
func readSecurityNatDestinationPool(name string, clt *Client, junSess *junosSession,
) (natDestinationPoolOptions, error) {
var confRead natDestinationPoolOptions
showConfig, err := clt.command(cmdShowConfig+
"security nat destination pool "+name+pipeDisplaySetRelative, junSess)
if err != nil {
return confRead, err
}
if showConfig != emptyW {
confRead.name = name
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, "address port"):
confRead.addressPort, err = strconv.Atoi(strings.TrimPrefix(itemTrim, "address port "))
if err != nil {
return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err)
}
case strings.HasPrefix(itemTrim, "address to"):
confRead.addressTo = strings.TrimPrefix(itemTrim, "address to ")
case strings.HasPrefix(itemTrim, "address "):
confRead.address = strings.TrimPrefix(itemTrim, "address ")
case strings.HasPrefix(itemTrim, "description "):
confRead.description = strings.Trim(strings.TrimPrefix(itemTrim, "description "), "\"")
case strings.HasPrefix(itemTrim, "routing-instance "):
confRead.routingInstance = strings.TrimPrefix(itemTrim, "routing-instance ")
}
}
}
return confRead, nil
}
func delSecurityNatDestinationPool(natDestinationPool string, clt *Client, junSess *junosSession) error {
configSet := make([]string, 0, 1)
configSet = append(configSet, "delete security nat destination pool "+natDestinationPool)
return clt.configSet(configSet, junSess)
}
func fillSecurityNatDestinationPoolData(d *schema.ResourceData, natDestinationPoolOptions natDestinationPoolOptions) {
if tfErr := d.Set("name", natDestinationPoolOptions.name); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("address", natDestinationPoolOptions.address); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("address_port", natDestinationPoolOptions.addressPort); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("address_to", natDestinationPoolOptions.addressTo); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("description", natDestinationPoolOptions.description); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("routing_instance", natDestinationPoolOptions.routingInstance); tfErr != nil {
panic(tfErr)
}
}