Skip to content

Commit f11d02d

Browse files
committed
Simplify _get_commands_to_run method
1 parent 4d3713b commit f11d02d

1 file changed

Lines changed: 29 additions & 44 deletions

File tree

nautobot_device_onboarding/nornir_plays/command_getter.py

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -61,56 +61,41 @@ def deduplicate_command_list(data):
6161

6262

6363
def _get_commands_to_run(yaml_parsed_info, sync_vlans, sync_vrfs, sync_cables, sync_software_version):
64-
"""Using merged command mapper info and look up all commands that need to be run."""
64+
"""Return a deduplicated list of commands to run based on YAML info and sync flags."""
6565
all_commands = []
66+
67+
# Define which keys to skip based on sync flags
68+
skip_conditions = {
69+
"interfaces__tagged_vlans": not sync_vlans,
70+
"interfaces__untagged_vlan": not sync_vlans,
71+
"interfaces__vrf": not sync_vrfs,
72+
"cables": not sync_cables,
73+
"software_version": not sync_software_version,
74+
}
75+
6676
for key, value in yaml_parsed_info.items():
77+
# Handle pre_processor section separately
6778
if key == "pre_processor":
6879
for pre_processor, v in value.items():
6980
if not sync_vlans and pre_processor == "vlan_map":
7081
continue
71-
current_root_key = v.get("commands")
72-
if isinstance(current_root_key, list):
73-
# Means their is any "nested" structures. e.g multiple commands
74-
for command in v["commands"]:
75-
all_commands.append(command)
76-
else:
77-
if isinstance(current_root_key, dict):
78-
all_commands.append(current_root_key)
79-
else:
80-
# Deduplicate commands + parser key
81-
current_root_key = value.get("commands")
82-
if isinstance(current_root_key, list):
83-
# Means there is a "nested" structures. e.g. multiple commands
84-
for command in value["commands"]:
85-
# If syncing vlans isn't in scope don't run the unneeded commands.
86-
if not sync_vlans and key in ["interfaces__tagged_vlans", "interfaces__untagged_vlan"]:
87-
continue
88-
# If syncing vrfs isn't in scope remove the unneeded commands.
89-
if not sync_vrfs and key == "interfaces__vrf":
90-
continue
91-
# If syncing cables isn't in scope remove the unneeded commands.
92-
if not sync_cables and key == "cables":
93-
continue
94-
# If syncing software_versions isn't in scope remove the unneeded commands.
95-
if not sync_software_version and key == "software_version":
96-
continue
97-
all_commands.append(command)
98-
else:
99-
if isinstance(current_root_key, dict):
100-
# Means there isn't a "nested" structures. e.g. 1 command
101-
# If syncing vlans isn't in scope don't run the unneeded commands.
102-
if not sync_vlans and key in ["interfaces__tagged_vlans", "interfaces__untagged_vlan"]:
103-
continue
104-
# If syncing vrfs isn't in scope remove the unneeded commands.
105-
if not sync_vrfs and key == "interfaces__vrf":
106-
continue
107-
# If syncing cables isn't in scope remove the unneeded commands.
108-
if not sync_cables and key == "cables":
109-
continue
110-
# If syncing software_versions isn't in scope remove the unneeded commands.
111-
if not sync_software_version and key == "software_version":
112-
continue
113-
all_commands.append(current_root_key)
82+
commands = v.get("commands", [])
83+
if isinstance(commands, dict):
84+
all_commands.append(commands)
85+
elif isinstance(commands, list):
86+
all_commands.extend(commands)
87+
continue # move to next key
88+
89+
# Skip if this key shouldn't be synced
90+
if skip_conditions.get(key, False):
91+
continue
92+
93+
commands = value.get("commands", [])
94+
if isinstance(commands, dict):
95+
all_commands.append(commands)
96+
elif isinstance(commands, list):
97+
all_commands.extend(commands)
98+
11499
return deduplicate_command_list(all_commands)
115100

116101

0 commit comments

Comments
 (0)