Skip to content

Commit

Permalink
clean code: fix lll linter errors with a var to map
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremmfr committed Apr 26, 2021
1 parent 4e7ab7d commit 9acc399
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 105 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
ENHANCEMENTS:
* resource/`junos_security`: add `policies` argument with `policy_rematch` and `policy_rematch_extensive` arguments inside (Fixes #185) Thanks @Sha-San-P
* clean code: remove override of the lists of 1 map to handle directly the map
* clean code: fix lll linter errors with a var to map

BUG FIXES:

Expand Down
48 changes: 22 additions & 26 deletions junos/resource_interface_physical.go
Original file line number Diff line number Diff line change
Expand Up @@ -1392,78 +1392,74 @@ func readInterfacePhysicalParentEtherOpts(confRead *interfacePhysicalOptions, it
"version": "",
})
}
parentEtherOptsBFDLiveDetect := confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]
itemTrimBfdLiveDet := strings.TrimPrefix(itemTrim, "bfd-liveness-detection ")
switch {
case strings.HasPrefix(itemTrimBfdLiveDet, "local-address "):
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["local_address"] =
strings.TrimPrefix(itemTrimBfdLiveDet, "local-address ")
parentEtherOptsBFDLiveDetect["local_address"] = strings.TrimPrefix(itemTrimBfdLiveDet, "local-address ")
case strings.HasPrefix(itemTrimBfdLiveDet, "authentication algorithm "):
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["authentication_algorithm"] =
parentEtherOptsBFDLiveDetect["authentication_algorithm"] =
strings.TrimPrefix(itemTrimBfdLiveDet, "authentication algorithm ")
case strings.HasPrefix(itemTrimBfdLiveDet, "authentication key-chain "):
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["authentication_key_chain"] =
parentEtherOptsBFDLiveDetect["authentication_key_chain"] =
strings.TrimPrefix(itemTrimBfdLiveDet, "authentication key-chain ")
case itemTrimBfdLiveDet == "authentication loose-check":
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["authentication_loose_check"] =
true
parentEtherOptsBFDLiveDetect["authentication_loose_check"] = true
case strings.HasPrefix(itemTrimBfdLiveDet, "detection-time threshold "):
var err error
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["detection_time_threshold"],
err = strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "detection-time threshold "))
parentEtherOptsBFDLiveDetect["detection_time_threshold"], err =
strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "detection-time threshold "))
if err != nil {
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrimBfdLiveDet, "holddown-interval "):
var err error
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["holddown_interval"],
err = strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "holddown-interval "))
parentEtherOptsBFDLiveDetect["holddown_interval"], err =
strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "holddown-interval "))
if err != nil {
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrimBfdLiveDet, "minimum-interval "):
var err error
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["minimum_interval"],
err = strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "minimum-interval "))
parentEtherOptsBFDLiveDetect["minimum_interval"], err =
strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "minimum-interval "))
if err != nil {
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrimBfdLiveDet, "minimum-receive-interval "):
var err error
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["minimum_receive_interval"],
err = strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "minimum-receive-interval "))
parentEtherOptsBFDLiveDetect["minimum_receive_interval"], err =
strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "minimum-receive-interval "))
if err != nil {
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrimBfdLiveDet, "multiplier "):
var err error
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["multiplier"],
err = strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "multiplier "))
parentEtherOptsBFDLiveDetect["multiplier"], err =
strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "multiplier "))
if err != nil {
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrimBfdLiveDet, "neighbor "):
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["neighbor"] =
strings.TrimPrefix(itemTrimBfdLiveDet, "neighbor ")
parentEtherOptsBFDLiveDetect["neighbor"] = strings.TrimPrefix(itemTrimBfdLiveDet, "neighbor ")
case itemTrimBfdLiveDet == "no-adaptation":
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["no_adaptation"] =
true
parentEtherOptsBFDLiveDetect["no_adaptation"] = true
case strings.HasPrefix(itemTrimBfdLiveDet, "transmit-interval minimum-interval "):
var err error
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["transmit_interval_minimum_interval"], // nolint: lll
err = strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "transmit-interval minimum-interval "))
parentEtherOptsBFDLiveDetect["transmit_interval_minimum_interval"], err =
strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "transmit-interval minimum-interval "))
if err != nil {
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrimBfdLiveDet, "transmit-interval threshold "):
var err error
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["transmit_interval_threshold"],
err = strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "transmit-interval threshold "))
parentEtherOptsBFDLiveDetect["transmit_interval_threshold"], err =
strconv.Atoi(strings.TrimPrefix(itemTrimBfdLiveDet, "transmit-interval threshold "))
if err != nil {
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrimBfdLiveDet, "version "):
confRead.parentEtherOpts[0]["bfd_liveness_detection"].([]map[string]interface{})[0]["version"] =
strings.TrimPrefix(itemTrimBfdLiveDet, "version ")
parentEtherOptsBFDLiveDetect["version"] = strings.TrimPrefix(itemTrimBfdLiveDet, "version ")
}
case itemTrim == flowControlWords:
confRead.parentEtherOpts[0]["flow_control"] = true
Expand Down
60 changes: 29 additions & 31 deletions junos/resource_security.go
Original file line number Diff line number Diff line change
Expand Up @@ -1609,23 +1609,23 @@ func readSecurityFlow(confRead *securityOptions, itemTrimFlow string) error {
"no_packet_flooding": make([]map[string]interface{}, 0),
})
}
flowEthernetSwitching := confRead.flow[0]["ethernet_switching"].([]map[string]interface{})[0]
switch {
case itemTrim == "ethernet-switching block-non-ip-all":
confRead.flow[0]["ethernet_switching"].([]map[string]interface{})[0]["block_non_ip_all"] = true
flowEthernetSwitching["block_non_ip_all"] = true
case itemTrim == "ethernet-switching bypass-non-ip-unicast":
confRead.flow[0]["ethernet_switching"].([]map[string]interface{})[0]["bypass_non_ip_unicast"] = true
flowEthernetSwitching["bypass_non_ip_unicast"] = true
case itemTrim == "ethernet-switching bpdu-vlan-flooding":
confRead.flow[0]["ethernet_switching"].([]map[string]interface{})[0]["bpdu_vlan_flooding"] = true
flowEthernetSwitching["bpdu_vlan_flooding"] = true
case strings.HasPrefix(itemTrim, "ethernet-switching no-packet-flooding"):
if len(confRead.flow[0]["ethernet_switching"].([]map[string]interface{})[0]["no_packet_flooding"].([]map[string]interface{})) == 0 { // nolint: lll
confRead.flow[0]["ethernet_switching"].([]map[string]interface{})[0]["no_packet_flooding"] = append(
confRead.flow[0]["ethernet_switching"].([]map[string]interface{})[0]["no_packet_flooding"].([]map[string]interface{}), // nolint: lll
map[string]interface{}{
if len(flowEthernetSwitching["no_packet_flooding"].([]map[string]interface{})) == 0 {
flowEthernetSwitching["no_packet_flooding"] = append(
flowEthernetSwitching["no_packet_flooding"].([]map[string]interface{}), map[string]interface{}{
"no_trace_route": false,
})
}
if itemTrim == "ethernet-switching no-packet-flooding no-trace-route" {
confRead.flow[0]["ethernet_switching"].([]map[string]interface{})[0]["no_packet_flooding"].([]map[string]interface{})[0]["no_trace_route"] = true // nolint: lll
flowEthernetSwitching["no_packet_flooding"].([]map[string]interface{})[0]["no_trace_route"] = true
}
}
case itemTrim == "force-ip-reassembly":
Expand Down Expand Up @@ -1732,35 +1732,34 @@ func readSecurityFlow(confRead *securityOptions, itemTrimFlow string) error {
"time_wait_state": make([]map[string]interface{}, 0),
})
}
flowTCPSession := confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]
switch {
case itemTrim == "tcp-session fin-invalidate-session":
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["fin_invalidate_session"] = true
flowTCPSession["fin_invalidate_session"] = true
case strings.HasPrefix(itemTrim, "tcp-session maximum-window "):
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["maximum_window"] =
strings.TrimPrefix(itemTrim, "tcp-session maximum-window ")
flowTCPSession["maximum_window"] = strings.TrimPrefix(itemTrim, "tcp-session maximum-window ")
case itemTrim == "tcp-session no-sequence-check":
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["no_sequence_check"] = true
flowTCPSession["no_sequence_check"] = true
case itemTrim == "tcp-session no-syn-check":
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["no_syn_check"] = true
flowTCPSession["no_syn_check"] = true
case itemTrim == "tcp-session no-syn-check-in-tunnel":
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["no_syn_check_in_tunnel"] = true
flowTCPSession["no_syn_check_in_tunnel"] = true
case itemTrim == "tcp-session rst-invalidate-session":
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["rst_invalidate_session"] = true
flowTCPSession["rst_invalidate_session"] = true
case itemTrim == "tcp-session rst-sequence-check":
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["rst_sequence_check"] = true
flowTCPSession["rst_sequence_check"] = true
case itemTrim == "tcp-session strict-syn-check":
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["strict_syn_check"] = true
flowTCPSession["strict_syn_check"] = true
case strings.HasPrefix(itemTrim, "tcp-session tcp-initial-timeout "):
var err error
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["tcp_initial_timeout"], err =
flowTCPSession["tcp_initial_timeout"], err =
strconv.Atoi(strings.TrimPrefix(itemTrim, "tcp-session tcp-initial-timeout "))
if err != nil {
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "tcp-session time-wait-state"):
if len(confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["time_wait_state"].([]map[string]interface{})) == 0 { // nolint: lll
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["time_wait_state"] = append(
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["time_wait_state"].([]map[string]interface{}),
if len(flowTCPSession["time_wait_state"].([]map[string]interface{})) == 0 {
flowTCPSession["time_wait_state"] = append(flowTCPSession["time_wait_state"].([]map[string]interface{}),
map[string]interface{}{
"apply_to_half_close_state": false,
"session_ageout": false,
Expand All @@ -1769,12 +1768,12 @@ func readSecurityFlow(confRead *securityOptions, itemTrimFlow string) error {
}
switch {
case itemTrim == "tcp-session time-wait-state apply-to-half-close-state":
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["time_wait_state"].([]map[string]interface{})[0]["apply_to_half_close_state"] = true // nolint: lll
flowTCPSession["time_wait_state"].([]map[string]interface{})[0]["apply_to_half_close_state"] = true
case itemTrim == "tcp-session time-wait-state session-ageout":
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["time_wait_state"].([]map[string]interface{})[0]["session_ageout"] = true // nolint: lll
flowTCPSession["time_wait_state"].([]map[string]interface{})[0]["session_ageout"] = true
case strings.HasPrefix(itemTrim, "tcp-session time-wait-state session-timeout "):
var err error
confRead.flow[0]["tcp_session"].([]map[string]interface{})[0]["time_wait_state"].([]map[string]interface{})[0]["session_timeout"], err = // nolint: lll
flowTCPSession["time_wait_state"].([]map[string]interface{})[0]["session_timeout"], err =
strconv.Atoi(strings.TrimPrefix(itemTrim, "tcp-session time-wait-state session-timeout "))
if err != nil {
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
Expand Down Expand Up @@ -2006,23 +2005,22 @@ func readSecurityUtm(confRead *securityOptions, itemTrimUtm string) error {
})
}
itemTrimServer := strings.TrimPrefix(itemTrimUtm, "utm feature-profile web-filtering juniper-enhanced server")
utmFeatProfWebFiltJunEnhServer :=
confRead.utm[0]["feature_profile_web_filtering_juniper_enhanced_server"].([]map[string]interface{})[0]
switch {
case strings.HasPrefix(itemTrimServer, " host "):
confRead.utm[0]["feature_profile_web_filtering_juniper_enhanced_server"].([]map[string]interface{})[0]["host"] =
strings.TrimPrefix(itemTrimServer, " host ")
utmFeatProfWebFiltJunEnhServer["host"] = strings.TrimPrefix(itemTrimServer, " host ")
case strings.HasPrefix(itemTrimServer, " port "):
var err error
confRead.utm[0]["feature_profile_web_filtering_juniper_enhanced_server"].([]map[string]interface{})[0]["port"], err =
strconv.Atoi(strings.TrimPrefix(itemTrimServer, " port "))
utmFeatProfWebFiltJunEnhServer["port"], err = strconv.Atoi(strings.TrimPrefix(itemTrimServer, " port "))
if err != nil {
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrimUtm, err)
}
case strings.HasPrefix(itemTrimServer, " proxy-profile "):
confRead.utm[0]["feature_profile_web_filtering_juniper_enhanced_server"].([]map[string]interface{})[0]["proxy_profile"] = //nolint: lll
utmFeatProfWebFiltJunEnhServer["proxy_profile"] =
strings.Trim(strings.TrimPrefix(itemTrimServer, " proxy-profile "), "\"")
case strings.HasPrefix(itemTrimServer, " routing-instance "):
confRead.utm[0]["feature_profile_web_filtering_juniper_enhanced_server"].([]map[string]interface{})[0]["routing_instance"] = //nolint: lll
strings.TrimPrefix(itemTrimServer, " routing-instance ")
utmFeatProfWebFiltJunEnhServer["routing_instance"] = strings.TrimPrefix(itemTrimServer, " routing-instance ")
}
}

Expand Down
Loading

0 comments on commit 9acc399

Please sign in to comment.