Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CleanCode: fix lll linter errors and rm override lists of 1 map #195

Merged
merged 2 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## upcoming release
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
117 changes: 36 additions & 81 deletions junos/func_resource_bgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,27 +492,8 @@ func setBgpOptsBfd(setPrefix string, bfdLivenessDetection []interface{},
return nil
}

func readBgpOptsBfd(item string, bfdOpts []map[string]interface{}) ([]map[string]interface{}, error) {
func readBgpOptsBfd(item string, bfdRead map[string]interface{}) error {
itemTrim := strings.TrimPrefix(item, "bfd-liveness-detection ")
bfdRead := map[string]interface{}{
"authentication_algorithm": "",
"authentication_key_chain": "",
"authentication_loose_check": false,
"detection_time_threshold": 0,
"holddown_interval": 0,
"minimum_interval": 0,
"minimum_receive_interval": 0,
"multiplier": 0,
"session_mode": "",
"transmit_interval_minimum_interval": 0,
"transmit_interval_threshold": 0,
"version": "",
}
if len(bfdOpts) > 0 {
for k, v := range bfdOpts[0] {
bfdRead[k] = v
}
}
switch {
case strings.HasPrefix(itemTrim, "authentication algorithm "):
bfdRead["authentication_algorithm"] = strings.TrimPrefix(itemTrim, "authentication algorithm ")
Expand All @@ -524,36 +505,31 @@ func readBgpOptsBfd(item string, bfdOpts []map[string]interface{}) ([]map[string
var err error
bfdRead["detection_time_threshold"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "detection-time threshold "))
if err != nil {
return []map[string]interface{}{bfdRead},
fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "holddown-interval "):
var err error
bfdRead["holddown_interval"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "holddown-interval "))
if err != nil {
return []map[string]interface{}{bfdRead},
fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "minimum-interval "):
var err error
bfdRead["minimum_interval"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "minimum-interval "))
if err != nil {
return []map[string]interface{}{bfdRead},
fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "minimum-receive-interval "):
var err error
bfdRead["minimum_receive_interval"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "minimum-receive-interval "))
if err != nil {
return []map[string]interface{}{bfdRead},
fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "multiplier "):
var err error
bfdRead["multiplier"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "multiplier "))
if err != nil {
return []map[string]interface{}{bfdRead},
fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "session-mode "):
bfdRead["session_mode"] = strings.TrimPrefix(itemTrim, "session-mode ")
Expand All @@ -562,23 +538,20 @@ func readBgpOptsBfd(item string, bfdOpts []map[string]interface{}) ([]map[string
bfdRead["transmit_interval_threshold"], err = strconv.Atoi(
strings.TrimPrefix(itemTrim, "transmit-interval threshold "))
if err != nil {
return []map[string]interface{}{bfdRead},
fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "transmit-interval minimum-interval "):
var err error
bfdRead["transmit_interval_minimum_interval"], err = strconv.Atoi(
strings.TrimPrefix(itemTrim, "transmit-interval minimum-interval "))
if err != nil {
return []map[string]interface{}{bfdRead},
fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "version "):
bfdRead["version"] = strings.TrimPrefix(itemTrim, "version ")
}

// override (maxItem = 1)
return []map[string]interface{}{bfdRead}, nil
return nil
}

func setBgpOptsFamily(setPrefix, familyType string, familyOptsList []interface{},
Expand Down Expand Up @@ -651,8 +624,8 @@ func setBgpOptsFamilyPrefixLimit(setPrefix string, prefixLimit map[string]interf
func readBgpOptsFamily(item, familyType string, opts []map[string]interface{}) ([]map[string]interface{}, error) {
readOpts := map[string]interface{}{
"nlri_type": "",
"accepted_prefix_limit": make([]map[string]interface{}, 0, 1),
"prefix_limit": make([]map[string]interface{}, 0, 1),
"accepted_prefix_limit": make([]map[string]interface{}, 0),
"prefix_limit": make([]map[string]interface{}, 0),
}
setPrefix := "family "
switch familyType {
Expand All @@ -669,17 +642,16 @@ func readBgpOptsFamily(item, familyType string, opts []map[string]interface{}) (
itemTrim := strings.TrimPrefix(item, setPrefix+readOpts["nlri_type"].(string)+" ")
switch {
case strings.HasPrefix(itemTrim, "accepted-prefix-limit "):
readOptsPL := map[string]interface{}{
"maximum": 0,
"teardown": 0,
"teardown_idle_timeout": 0,
"teardown_idle_timeout_forever": false,
}
if len(readOpts["accepted_prefix_limit"].([]map[string]interface{})) > 0 {
for k, v := range readOpts["accepted_prefix_limit"].([]map[string]interface{})[0] {
readOptsPL[k] = v
}
}
if len(readOpts["accepted_prefix_limit"].([]map[string]interface{})) == 0 {
readOpts["accepted_prefix_limit"] = append(readOpts["accepted_prefix_limit"].([]map[string]interface{}),
map[string]interface{}{
"maximum": 0,
"teardown": 0,
"teardown_idle_timeout": 0,
"teardown_idle_timeout_forever": false,
})
}
readOptsPL := readOpts["accepted_prefix_limit"].([]map[string]interface{})[0]
switch {
case strings.HasPrefix(itemTrim, "accepted-prefix-limit maximum"):
var err error
Expand All @@ -706,20 +678,17 @@ func readBgpOptsFamily(item, familyType string, opts []map[string]interface{}) (
return append(opts, readOpts), fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
}
// override (maxItem = 1)
readOpts["accepted_prefix_limit"] = []map[string]interface{}{readOptsPL}
case strings.HasPrefix(itemTrim, "prefix-limit "):
readOptsPL := map[string]interface{}{
"maximum": 0,
"teardown": 0,
"teardown_idle_timeout": 0,
"teardown_idle_timeout_forever": false,
}
if len(readOpts["prefix_limit"].([]map[string]interface{})) > 0 {
for k, v := range readOpts["prefix_limit"].([]map[string]interface{})[0] {
readOptsPL[k] = v
}
}
if len(readOpts["prefix_limit"].([]map[string]interface{})) == 0 {
readOpts["prefix_limit"] = append(readOpts["prefix_limit"].([]map[string]interface{}),
map[string]interface{}{
"maximum": 0,
"teardown": 0,
"teardown_idle_timeout": 0,
"teardown_idle_timeout_forever": false,
})
}
readOptsPL := readOpts["prefix_limit"].([]map[string]interface{})[0]
switch {
case strings.HasPrefix(itemTrim, "prefix-limit maximum "):
var err error
Expand All @@ -745,8 +714,6 @@ func readBgpOptsFamily(item, familyType string, opts []map[string]interface{}) (
return append(opts, readOpts), fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
}
// override (maxItem = 1)
readOpts["prefix_limit"] = []map[string]interface{}{readOptsPL}
}

return append(opts, readOpts), nil
Expand Down Expand Up @@ -783,36 +750,24 @@ func setBgpOptsGrafefulRestart(setPrefix string, gracefulRestarts []interface{},
return nil
}

func readBgpOptsGracefulRestart(item string, grOpts []map[string]interface{}) ([]map[string]interface{}, error) {
func readBgpOptsGracefulRestart(item string, grRead map[string]interface{}) error {
itemTrim := strings.TrimPrefix(item, "graceful-restart ")
grRead := map[string]interface{}{
"disable": false,
"restart_time": 0,
"stale_route_time": 0,
}
if len(grOpts) > 0 {
for k, v := range grOpts[0] {
grRead[k] = v
}
}
switch {
case itemTrim == disableW:
grRead["disable"] = true
case strings.HasPrefix(itemTrim, "restart-time "):
var err error
grRead["restart_time"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "restart-time "))
if err != nil {
return []map[string]interface{}{grRead},
fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
case strings.HasPrefix(itemTrim, "stale-routes-time "):
var err error
grRead["stale_route_time"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "stale-routes-time "))
if err != nil {
return []map[string]interface{}{grRead},
fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
return fmt.Errorf("failed to convert value from '%s' to integer : %w", itemTrim, err)
}
}
// override (maxItem = 1)
return []map[string]interface{}{grRead}, nil

return nil
}
30 changes: 26 additions & 4 deletions junos/resource_bgp_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,8 +878,24 @@ func readBgpGroup(bgpGroup, instance string, m interface{}, jnprSess *NetconfObj
itemTrim := strings.TrimPrefix(item, setLineStart)
switch {
case strings.HasPrefix(itemTrim, "bfd-liveness-detection "):
confRead.bfdLivenessDetection, err = readBgpOptsBfd(itemTrim, confRead.bfdLivenessDetection)
if err != nil {
if len(confRead.bfdLivenessDetection) == 0 {
confRead.bfdLivenessDetection = append(confRead.bfdLivenessDetection,
map[string]interface{}{
"authentication_algorithm": "",
"authentication_key_chain": "",
"authentication_loose_check": false,
"detection_time_threshold": 0,
"holddown_interval": 0,
"minimum_interval": 0,
"minimum_receive_interval": 0,
"multiplier": 0,
"session_mode": "",
"transmit_interval_minimum_interval": 0,
"transmit_interval_threshold": 0,
"version": "",
})
}
if err := readBgpOptsBfd(itemTrim, confRead.bfdLivenessDetection[0]); err != nil {
return confRead, err
}
case strings.HasPrefix(itemTrim, "family evpn "):
Expand All @@ -898,8 +914,14 @@ func readBgpGroup(bgpGroup, instance string, m interface{}, jnprSess *NetconfObj
return confRead, err
}
case strings.HasPrefix(itemTrim, "graceful-restart "):
confRead.gracefulRestart, err = readBgpOptsGracefulRestart(itemTrim, confRead.gracefulRestart)
if err != nil {
if len(confRead.gracefulRestart) == 0 {
confRead.gracefulRestart = append(confRead.gracefulRestart, map[string]interface{}{
"disable": false,
"restart_time": 0,
"stale_route_time": 0,
})
}
if err := readBgpOptsGracefulRestart(itemTrim, confRead.gracefulRestart[0]); err != nil {
return confRead, err
}
default:
Expand Down
30 changes: 26 additions & 4 deletions junos/resource_bgp_neighbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,13 +898,35 @@ func readBgpNeighbor(ip, instance, group string, m interface{}, jnprSess *Netcon
return confRead, err
}
case strings.HasPrefix(itemTrim, "bfd-liveness-detection "):
confRead.bfdLivenessDetection, err = readBgpOptsBfd(itemTrim, confRead.bfdLivenessDetection)
if err != nil {
if len(confRead.bfdLivenessDetection) == 0 {
confRead.bfdLivenessDetection = append(confRead.bfdLivenessDetection,
map[string]interface{}{
"authentication_algorithm": "",
"authentication_key_chain": "",
"authentication_loose_check": false,
"detection_time_threshold": 0,
"holddown_interval": 0,
"minimum_interval": 0,
"minimum_receive_interval": 0,
"multiplier": 0,
"session_mode": "",
"transmit_interval_minimum_interval": 0,
"transmit_interval_threshold": 0,
"version": "",
})
}
if err := readBgpOptsBfd(itemTrim, confRead.bfdLivenessDetection[0]); err != nil {
return confRead, err
}
case strings.HasPrefix(itemTrim, "graceful-restart "):
confRead.gracefulRestart, err = readBgpOptsGracefulRestart(itemTrim, confRead.gracefulRestart)
if err != nil {
if len(confRead.gracefulRestart) == 0 {
confRead.gracefulRestart = append(confRead.gracefulRestart, map[string]interface{}{
"disable": false,
"restart_time": 0,
"stale_route_time": 0,
})
}
if err := readBgpOptsGracefulRestart(itemTrim, confRead.gracefulRestart[0]); err != nil {
return confRead, err
}
default:
Expand Down
39 changes: 14 additions & 25 deletions junos/resource_firewall_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,11 +521,19 @@ func readFirewallFilter(filter, family string, m interface{}, jnprSess *NetconfO
case strings.HasPrefix(itemTrimTerm, "filter "):
termOptions["filter"] = strings.TrimPrefix(itemTrimTerm, "filter ")
case strings.HasPrefix(itemTrimTerm, "from "):
termOptions["from"] = readFirewallFilterOptsFrom(strings.TrimPrefix(itemTrimTerm, "from "),
termOptions["from"].([]map[string]interface{}))
if len(termOptions["from"].([]map[string]interface{})) == 0 {
termOptions["from"] = append(termOptions["from"].([]map[string]interface{}),
genMapFirewallFilterOptsFrom())
}
readFirewallFilterOptsFrom(strings.TrimPrefix(itemTrimTerm, "from "),
termOptions["from"].([]map[string]interface{})[0])
case strings.HasPrefix(itemTrimTerm, "then "):
termOptions["then"] = readFirewallFilterOptsThen(strings.TrimPrefix(itemTrimTerm, "then "),
termOptions["then"].([]map[string]interface{}))
if len(termOptions["then"].([]map[string]interface{})) == 0 {
termOptions["then"] = append(termOptions["then"].([]map[string]interface{}),
genMapFirewallFilterOptsThen())
}
readFirewallFilterOptsThen(strings.TrimPrefix(itemTrimTerm, "then "),
termOptions["then"].([]map[string]interface{})[0])
}
confRead.term = append(confRead.term, termOptions)
}
Expand Down Expand Up @@ -735,14 +743,7 @@ func setFirewallFilterOptsThen(setPrefixTermThen string, configSet []string, the
return configSet
}

func readFirewallFilterOptsFrom(item string,
confReadElement []map[string]interface{}) []map[string]interface{} {
fromMap := genMapFirewallFilterOptsFrom()
if len(confReadElement) > 0 {
for k, v := range confReadElement[0] {
fromMap[k] = v
}
}
func readFirewallFilterOptsFrom(item string, fromMap map[string]interface{}) {
switch {
case strings.HasPrefix(item, "address "):
if strings.HasSuffix(item, " except") {
Expand Down Expand Up @@ -840,19 +841,9 @@ func readFirewallFilterOptsFrom(item string,
case item == "tcp-initial":
fromMap["tcp_initial"] = true
}

// override (maxItem = 1)
return []map[string]interface{}{fromMap}
}

func readFirewallFilterOptsThen(item string,
confReadElement []map[string]interface{}) []map[string]interface{} {
thenMap := genMapFirewallFilterOptsThen()
if len(confReadElement) > 0 {
for k, v := range confReadElement[0] {
thenMap[k] = v
}
}
func readFirewallFilterOptsThen(item string, thenMap map[string]interface{}) {
switch {
case item == "accept",
item == "reject",
Expand All @@ -876,8 +867,6 @@ func readFirewallFilterOptsThen(item string,
case item == "syslog":
thenMap["syslog"] = true
}
// override (maxItem = 1)
return []map[string]interface{}{thenMap}
}

func genMapFirewallFilterOptsFrom() map[string]interface{} {
Expand Down
Loading