-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
resource_security_utm_profile_web_filtering_juniper_local.go
401 lines (362 loc) · 13.5 KB
/
resource_security_utm_profile_web_filtering_juniper_local.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
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 utmProfileWebFilteringLocalOptions struct {
timeout int
name string
defaultAction string
customBlockMessage string
fallbackSettings []map[string]interface{}
}
func resourceSecurityUtmProfileWebFilteringLocal() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSecurityUtmProfileWebFilteringLocalCreate,
ReadContext: resourceSecurityUtmProfileWebFilteringLocalRead,
UpdateContext: resourceSecurityUtmProfileWebFilteringLocalUpdate,
DeleteContext: resourceSecurityUtmProfileWebFilteringLocalDelete,
Importer: &schema.ResourceImporter{
State: resourceSecurityUtmProfileWebFilteringLocalImport,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"custom_block_message": {
Type: schema.TypeString,
Optional: true,
},
"default_action": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"block", "log-and-permit", permitWord}, false),
},
"fallback_settings": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"default": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"block", "log-and-permit"}, false),
},
"server_connectivity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"block", "log-and-permit"}, false),
},
"timeout": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"block", "log-and-permit"}, false),
},
"too_many_requests": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"block", "log-and-permit"}, false),
},
},
},
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 1800),
},
},
}
}
func resourceSecurityUtmProfileWebFilteringLocalCreate(
ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
if sess.junosFakeCreateSetFile != "" {
if err := setUtmProfileWebFLocal(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 utm feature-profile web-filtering juniper-local "+
"not compatible with Junos device %s", jnprSess.SystemInformation.HardwareModel))
}
sess.configLock(jnprSess)
var diagWarns diag.Diagnostics
utmProfileWebFLocalExists, err := checkUtmProfileWebFLocalExists(d.Get("name").(string), m, jnprSess)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if utmProfileWebFLocalExists {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(fmt.Errorf("security utm feature-profile web-filtering juniper-local "+
"%v already exists", d.Get("name").(string)))...)
}
if err := setUtmProfileWebFLocal(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("create resource junos_security_utm_profile_web_filtering_juniper_local", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
utmProfileWebFLocalExists, err = checkUtmProfileWebFLocalExists(d.Get("name").(string), m, jnprSess)
if err != nil {
return append(diagWarns, diag.FromErr(err)...)
}
if utmProfileWebFLocalExists {
d.SetId(d.Get("name").(string))
} else {
return append(diagWarns, diag.FromErr(fmt.Errorf("security utm feature-profile web-filtering juniper-local %v "+
"not exists after commit => check your config", d.Get("name").(string)))...)
}
return append(diagWarns, resourceSecurityUtmProfileWebFilteringLocalReadWJnprSess(d, m, jnprSess)...)
}
func resourceSecurityUtmProfileWebFilteringLocalRead(
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 resourceSecurityUtmProfileWebFilteringLocalReadWJnprSess(d, m, jnprSess)
}
func resourceSecurityUtmProfileWebFilteringLocalReadWJnprSess(
d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics {
mutex.Lock()
utmProfileWebFLocalOptions, err := readUtmProfileWebFLocal(d.Get("name").(string), m, jnprSess)
mutex.Unlock()
if err != nil {
return diag.FromErr(err)
}
if utmProfileWebFLocalOptions.name == "" {
d.SetId("")
} else {
fillUtmProfileWebFLocalData(d, utmProfileWebFLocalOptions)
}
return nil
}
func resourceSecurityUtmProfileWebFilteringLocalUpdate(
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 := delUtmProfileWebFLocal(d.Get("name").(string), m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if err := setUtmProfileWebFLocal(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("update resource junos_security_utm_profile_web_filtering_juniper_local", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
d.Partial(false)
return append(diagWarns, resourceSecurityUtmProfileWebFilteringLocalReadWJnprSess(d, m, jnprSess)...)
}
func resourceSecurityUtmProfileWebFilteringLocalDelete(
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 := delUtmProfileWebFLocal(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_utm_profile_web_filtering_juniper_local", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
return diagWarns
}
func resourceSecurityUtmProfileWebFilteringLocalImport(
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)
utmProfileWebFLocalExists, err := checkUtmProfileWebFLocalExists(d.Id(), m, jnprSess)
if err != nil {
return nil, err
}
if !utmProfileWebFLocalExists {
return nil, fmt.Errorf("don't find security utm feature-profile web-filtering juniper-local with id "+
"'%v' (id must be <name>)", d.Id())
}
utmProfileWebFLocalOptions, err := readUtmProfileWebFLocal(d.Id(), m, jnprSess)
if err != nil {
return nil, err
}
fillUtmProfileWebFLocalData(d, utmProfileWebFLocalOptions)
result[0] = d
return result, nil
}
func checkUtmProfileWebFLocalExists(profile string, m interface{}, jnprSess *NetconfObject) (bool, error) {
sess := m.(*Session)
showConfig, err := sess.command("show configuration"+
" security utm feature-profile web-filtering juniper-local profile \""+profile+"\" | display set", jnprSess)
if err != nil {
return false, err
}
if showConfig == emptyWord {
return false, nil
}
return true, nil
}
func setUtmProfileWebFLocal(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
configSet := make([]string, 0)
setPrefix := "set security utm feature-profile web-filtering juniper-local " +
"profile \"" + d.Get("name").(string) + "\" "
if d.Get("custom_block_message").(string) != "" {
configSet = append(configSet, setPrefix+"custom-block-message \""+d.Get("custom_block_message").(string)+"\"")
}
if d.Get("default_action").(string) != "" {
configSet = append(configSet, setPrefix+"default "+d.Get("default_action").(string))
}
for _, v := range d.Get("fallback_settings").([]interface{}) {
if v != nil {
fSettings := v.(map[string]interface{})
if fSettings["default"].(string) != "" {
configSet = append(configSet, setPrefix+"fallback-settings default "+
fSettings["default"].(string))
}
if fSettings["server_connectivity"].(string) != "" {
configSet = append(configSet, setPrefix+"fallback-settings server-connectivity "+
fSettings["server_connectivity"].(string))
}
if fSettings["timeout"].(string) != "" {
configSet = append(configSet, setPrefix+"fallback-settings timeout "+
fSettings["timeout"].(string))
}
if fSettings["too_many_requests"].(string) != "" {
configSet = append(configSet, setPrefix+"fallback-settings too-many-requests "+
fSettings["too_many_requests"].(string))
}
} else {
configSet = append(configSet, setPrefix+"fallback-settings")
}
}
if d.Get("timeout").(int) != 0 {
configSet = append(configSet, setPrefix+"timeout "+strconv.Itoa(d.Get("timeout").(int)))
}
return sess.configSet(configSet, jnprSess)
}
func readUtmProfileWebFLocal(profile string, m interface{}, jnprSess *NetconfObject) (
utmProfileWebFilteringLocalOptions, error) {
sess := m.(*Session)
var confRead utmProfileWebFilteringLocalOptions
showConfig, err := sess.command("show configuration"+
" security utm feature-profile web-filtering juniper-local profile \""+profile+"\" | display set relative", jnprSess)
if err != nil {
return confRead, err
}
if showConfig != emptyWord {
confRead.name = profile
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, "custom-block-message "):
confRead.customBlockMessage = strings.Trim(strings.TrimPrefix(itemTrim, "custom-block-message "), "\"")
case strings.HasPrefix(itemTrim, "default "):
confRead.defaultAction = strings.TrimPrefix(itemTrim, "default ")
case strings.HasPrefix(itemTrim, "fallback-settings"):
if len(confRead.fallbackSettings) == 0 {
confRead.fallbackSettings = append(confRead.fallbackSettings, map[string]interface{}{
"default": "",
"server_connectivity": "",
"timeout": "",
"too_many_requests": "",
})
}
itemTrimFallback := strings.TrimPrefix(itemTrim, "fallback-settings ")
switch {
case strings.HasPrefix(itemTrimFallback, "default "):
confRead.fallbackSettings[0]["default"] = strings.TrimPrefix(itemTrimFallback, "default ")
case strings.HasPrefix(itemTrimFallback, "server-connectivity "):
confRead.fallbackSettings[0]["server_connectivity"] = strings.TrimPrefix(itemTrimFallback, "server-connectivity ")
case strings.HasPrefix(itemTrimFallback, "timeout "):
confRead.fallbackSettings[0]["timeout"] = strings.TrimPrefix(itemTrimFallback, "timeout ")
case strings.HasPrefix(itemTrimFallback, "too-many-requests "):
confRead.fallbackSettings[0]["too_many_requests"] = strings.TrimPrefix(itemTrimFallback, "too-many-requests ")
}
case strings.HasPrefix(itemTrim, "timeout "):
var err error
confRead.timeout, err = strconv.Atoi(strings.TrimPrefix(itemTrim, "timeout "))
if err != nil {
return confRead, fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
}
}
}
return confRead, nil
}
func delUtmProfileWebFLocal(profile string, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
configSet := make([]string, 0, 1)
configSet = append(configSet, "delete security utm feature-profile web-filtering juniper-local "+
"profile \""+profile+"\"")
return sess.configSet(configSet, jnprSess)
}
func fillUtmProfileWebFLocalData(d *schema.ResourceData,
utmProfileWebFLocalOptions utmProfileWebFilteringLocalOptions) {
if tfErr := d.Set("name", utmProfileWebFLocalOptions.name); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("custom_block_message", utmProfileWebFLocalOptions.customBlockMessage); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("default_action", utmProfileWebFLocalOptions.defaultAction); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("fallback_settings", utmProfileWebFLocalOptions.fallbackSettings); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("timeout", utmProfileWebFLocalOptions.timeout); tfErr != nil {
panic(tfErr)
}
}