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

EOS: Add a JSON option when running a cli command #1637

Merged
merged 11 commits into from
Jun 3, 2022
4 changes: 3 additions & 1 deletion napalm/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,9 @@ def get_bgp_config(
"""
raise NotImplementedError

def cli(self, commands: List[str]) -> Dict[str, Union[str, Dict[str, Any]]]:
def cli(
self, commands: List[str], encoding: str = "text"
) -> Dict[str, Union[str, Dict[str, Any]]]:

"""
Will execute a list of commands and return the output in a dictionary format.
Expand Down
4 changes: 3 additions & 1 deletion napalm/base/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ def close(self) -> None:
def is_alive(self) -> models.AliveDict:
return {"is_alive": self.opened}

def cli(self, commands: List[str]) -> Dict[str, Union[str, Dict[str, Any]]]:
def cli(
self, commands: List[str], encoding: str = "text"
) -> Dict[str, Union[str, Dict[str, Any]]]:
count = self._count_calls("cli")
result = {}
regexp = re.compile("[^a-zA-Z0-9]+")
Expand Down
12 changes: 8 additions & 4 deletions napalm/eos/eos.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,17 +891,21 @@ def get_lldp_neighbors_detail(self, interface=""):
)
return lldp_neighbors_out

def cli(self, commands):
def cli(self, commands, encoding="text"):
if encoding not in ("text", "json"):
raise NotImplementedError("%s is not a supported encoding" % encoding)
cli_output = {}

if type(commands) is not list:
raise TypeError("Please enter a valid list of commands!")

for command in commands:
try:
cli_output[str(command)] = self._run_commands(
[command], encoding="text"
)[0].get("output")
result = self._run_commands([command], encoding=encoding)
if encoding == "text":
cli_output[str(command)] = result[0]["output"]
else:
cli_output[str(command)] = result[0]
# not quite fair to not exploit rum_commands
# but at least can have better control to point to wrong command in case of failure
except pyeapi.eapilib.CommandError:
Expand Down
4 changes: 3 additions & 1 deletion napalm/ios/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -2362,7 +2362,7 @@ def get_arp_table(self, vrf=""):
arp_table.append(entry)
return arp_table

def cli(self, commands):
def cli(self, commands, encoding="text"):
"""
Execute a list of commands and return the output in a dictionary format using the command
as the key.
Expand All @@ -2375,6 +2375,8 @@ def cli(self, commands):
'show clock': u'*22:01:51.165 UTC Thu Feb 18 2016'}

"""
if encoding not in ("text",):
raise NotImplementedError("%s is not a supported encoding" % encoding)
cli_output = dict()
if type(commands) is not list:
raise TypeError("Please enter a valid list of commands!")
Expand Down
4 changes: 3 additions & 1 deletion napalm/iosxr/iosxr.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,9 @@ def get_lldp_neighbors_detail(self, interface=""):

return lldp_neighbors

def cli(self, commands):
def cli(self, commands, encoding="text"):
if encoding not in ("text",):
raise NotImplementedError("%s is not a supported encoding" % encoding)

cli_output = {}

Expand Down
2 changes: 1 addition & 1 deletion napalm/iosxr_netconf/iosxr_netconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@ def get_lldp_neighbors_detail(self, interface=""):

return lldp_neighbors_detail

def cli(self, commands):
def cli(self, commands, encoding="text"):
"""Execute raw CLI commands and returns their output."""
return NotImplementedError

Expand Down
4 changes: 3 additions & 1 deletion napalm/junos/junos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,10 @@ def get_lldp_neighbors_detail(self, interface=""):

return lldp_neighbors

def cli(self, commands):
def cli(self, commands, encoding="text"):
"""Execute raw CLI commands and returns their output."""
if encoding not in ("text",):
raise NotImplementedError("%s is not a supported encoding" % encoding)
cli_output = {}

def _count(txt, none): # Second arg for consistency only. noqa
Expand Down
6 changes: 5 additions & 1 deletion napalm/nxos/nxos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,11 @@ def get_bgp_neighbors(self) -> Dict[str, models.BGPStateNeighborsPerVRFDict]:
results[vrf_name] = result_vrf_dict
return results

def cli(self, commands: List[str]) -> Dict[str, Union[str, Dict[str, Any]]]:
def cli(
self, commands: List[str], encoding: str = "text"
) -> Dict[str, Union[str, Dict[str, Any]]]:
if encoding not in ("text",):
raise NotImplementedError("%s is not a supported encoding" % encoding)
cli_output: Dict[str, Union[str, Dict[str, Any]]] = {}
if type(commands) is not list:
raise TypeError("Please enter a valid list of commands!")
Expand Down
4 changes: 3 additions & 1 deletion napalm/nxos_ssh/nxos_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,9 @@ def get_bgp_neighbors(self):
# FIX -- need to merge IPv6 and IPv4 AFI for same neighbor
return bgp_dict

def cli(self, commands):
def cli(self, commands, encoding="text"):
if encoding not in ("text",):
raise NotImplementedError("%s is not a supported encoding" % encoding)
cli_output = {}
if type(commands) is not list:
raise TypeError("Please enter a valid list of commands!")
Expand Down
2 changes: 1 addition & 1 deletion test/junos/TestJunOSDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def read_txt_file(self, filename):
with open(filename) as data_file:
return data_file.read()

def cli(self, command=""):
def cli(self, command="", encoding="text"):
return self.read_txt_file(
"junos/mock_data/{parsed_command}.txt".format(
parsed_command=command.replace(" ", "_")
Expand Down
2 changes: 1 addition & 1 deletion test/junos/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def close(self):
def bind(*args, **kvargs):
pass

def cli(self, command=""):
def cli(self, command="", encoding="text"):
filename = "{safe_command}.txt".format(safe_command=self.sanitize_text(command))
fielpath = self.find_file(filename)
return self.read_txt_file(fielpath)
Expand Down