From 52b2f70bb88c75bd34b37fc2c0bfb3b60f63f9bb Mon Sep 17 00:00:00 2001 From: Diego Asturias Date: Thu, 12 May 2022 11:29:04 -0700 Subject: [PATCH 1/6] EOS: Add a JSON option when running a cli command pyeapi supports JSON as an option for the cli commands. However right now the JSON option isn't exposed to users of this API. This exposes the JSON option as well. --- napalm/eos/eos.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/napalm/eos/eos.py b/napalm/eos/eos.py index a14031cf7..7686d83ee 100644 --- a/napalm/eos/eos.py +++ b/napalm/eos/eos.py @@ -803,7 +803,7 @@ def get_lldp_neighbors_detail(self, interface=""): ) return lldp_neighbors_out - def cli(self, commands): + def cli(self, commands, encoding="text"): cli_output = {} if type(commands) is not list: @@ -811,9 +811,13 @@ def cli(self, commands): for command in commands: try: - cli_output[str(command)] = self.device.run_commands( - [command], encoding="text" - )[0].get("output") + result = self.device.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: From 0d92c2341b194fb6a04a036c8f59a3bdcbaf76d3 Mon Sep 17 00:00:00 2001 From: Diego Asturias Date: Thu, 12 May 2022 11:29:04 -0700 Subject: [PATCH 2/6] EOS: Add a JSON option when running a cli command pyeapi supports JSON as an option for the cli commands. However right now the JSON option isn't exposed to users of this API. This exposes the JSON option as well. --- napalm/eos/eos.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/napalm/eos/eos.py b/napalm/eos/eos.py index 7686d83ee..e35ac03ee 100644 --- a/napalm/eos/eos.py +++ b/napalm/eos/eos.py @@ -811,11 +811,9 @@ def cli(self, commands, encoding="text"): for command in commands: try: - result = self.device.run_commands( - [command], encoding=encoding - ) + result = self.device.run_commands([command], encoding=encoding) if encoding == "text": - cli_output[str(command)] = result[0]['output'] + cli_output[str(command)] = result[0]["output"] else: cli_output[str(command)] = result[0] # not quite fair to not exploit rum_commands From 29a957a63fe7424d3eb4b31cb08cad417b1acc35 Mon Sep 17 00:00:00 2001 From: Diego Asturias Date: Thu, 12 May 2022 11:29:04 -0700 Subject: [PATCH 3/6] EOS: Add a JSON option when running a cli command pyeapi supports JSON as an option for the cli commands. However right now the JSON option isn't exposed to users of this API. This exposes the JSON option as well. --- napalm/base/base.py | 2 +- napalm/eos/eos.py | 2 ++ napalm/ios/ios.py | 4 +++- napalm/iosxr/iosxr.py | 4 +++- napalm/iosxr_netconf/iosxr_netconf.py | 2 +- napalm/junos/junos.py | 4 +++- napalm/nxos/nxos.py | 4 +++- napalm/nxos_ssh/nxos_ssh.py | 4 +++- test/junos/TestJunOSDriver.py | 2 +- test/junos/conftest.py | 2 +- 10 files changed, 21 insertions(+), 9 deletions(-) diff --git a/napalm/base/base.py b/napalm/base/base.py index f93927293..474df008f 100644 --- a/napalm/base/base.py +++ b/napalm/base/base.py @@ -737,7 +737,7 @@ 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. diff --git a/napalm/eos/eos.py b/napalm/eos/eos.py index e35ac03ee..7199eaadb 100644 --- a/napalm/eos/eos.py +++ b/napalm/eos/eos.py @@ -804,6 +804,8 @@ def get_lldp_neighbors_detail(self, interface=""): return lldp_neighbors_out def cli(self, commands, encoding="text"): + if encoding not in ("text", "json"): + raise ValueError("%s is not a supported encoding" % encoding) cli_output = {} if type(commands) is not list: diff --git a/napalm/ios/ios.py b/napalm/ios/ios.py index 70b5d871a..b0c6cb5fa 100644 --- a/napalm/ios/ios.py +++ b/napalm/ios/ios.py @@ -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. @@ -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 ValueError("%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!") diff --git a/napalm/iosxr/iosxr.py b/napalm/iosxr/iosxr.py index bcad6d6bb..677daae55 100644 --- a/napalm/iosxr/iosxr.py +++ b/napalm/iosxr/iosxr.py @@ -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 ValueError("%s is not a supported encoding" % encoding) cli_output = {} diff --git a/napalm/iosxr_netconf/iosxr_netconf.py b/napalm/iosxr_netconf/iosxr_netconf.py index 8f64151ac..7de8f5574 100644 --- a/napalm/iosxr_netconf/iosxr_netconf.py +++ b/napalm/iosxr_netconf/iosxr_netconf.py @@ -1329,7 +1329,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 diff --git a/napalm/junos/junos.py b/napalm/junos/junos.py index a2de18ad5..910241141 100644 --- a/napalm/junos/junos.py +++ b/napalm/junos/junos.py @@ -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 ValueError("%s is not a supported encoding" % encoding) cli_output = {} def _count(txt, none): # Second arg for consistency only. noqa diff --git a/napalm/nxos/nxos.py b/napalm/nxos/nxos.py index 5ab7f8134..cff810f85 100644 --- a/napalm/nxos/nxos.py +++ b/napalm/nxos/nxos.py @@ -1127,7 +1127,9 @@ 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 ValueError("%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!") diff --git a/napalm/nxos_ssh/nxos_ssh.py b/napalm/nxos_ssh/nxos_ssh.py index d050553a5..43dab9f03 100644 --- a/napalm/nxos_ssh/nxos_ssh.py +++ b/napalm/nxos_ssh/nxos_ssh.py @@ -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 ValueError("%s is not a supported encoding" % encoding) cli_output = {} if type(commands) is not list: raise TypeError("Please enter a valid list of commands!") diff --git a/test/junos/TestJunOSDriver.py b/test/junos/TestJunOSDriver.py index 2f8d5e0ae..c477bc541 100644 --- a/test/junos/TestJunOSDriver.py +++ b/test/junos/TestJunOSDriver.py @@ -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(" ", "_") diff --git a/test/junos/conftest.py b/test/junos/conftest.py index a0a057328..dc21dce60 100644 --- a/test/junos/conftest.py +++ b/test/junos/conftest.py @@ -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) From 78238ca6242f5275f61cbfe384ade125729f53aa Mon Sep 17 00:00:00 2001 From: Diego Asturias Date: Fri, 20 May 2022 11:01:46 -0700 Subject: [PATCH 4/6] Format file correctly --- napalm/base/base.py | 4 +++- napalm/ios/ios.py | 2 +- napalm/iosxr/iosxr.py | 2 +- napalm/junos/junos.py | 2 +- napalm/nxos/nxos.py | 6 ++++-- napalm/nxos_ssh/nxos_ssh.py | 2 +- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/napalm/base/base.py b/napalm/base/base.py index 474df008f..fdcbc9a8a 100644 --- a/napalm/base/base.py +++ b/napalm/base/base.py @@ -737,7 +737,9 @@ def get_bgp_config( """ raise NotImplementedError - def cli(self, commands: List[str], encoding: str="text") -> 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. diff --git a/napalm/ios/ios.py b/napalm/ios/ios.py index b0c6cb5fa..b1acd27a3 100644 --- a/napalm/ios/ios.py +++ b/napalm/ios/ios.py @@ -2375,7 +2375,7 @@ def cli(self, commands, encoding="text"): 'show clock': u'*22:01:51.165 UTC Thu Feb 18 2016'} """ - if encoding not in ("text", ): + if encoding not in ("text",): raise ValueError("%s is not a supported encoding" % encoding) cli_output = dict() if type(commands) is not list: diff --git a/napalm/iosxr/iosxr.py b/napalm/iosxr/iosxr.py index 677daae55..c081a56e1 100644 --- a/napalm/iosxr/iosxr.py +++ b/napalm/iosxr/iosxr.py @@ -886,7 +886,7 @@ def get_lldp_neighbors_detail(self, interface=""): return lldp_neighbors def cli(self, commands, encoding="text"): - if encoding not in ("text", ): + if encoding not in ("text",): raise ValueError("%s is not a supported encoding" % encoding) cli_output = {} diff --git a/napalm/junos/junos.py b/napalm/junos/junos.py index 84b768631..585f1e097 100644 --- a/napalm/junos/junos.py +++ b/napalm/junos/junos.py @@ -1014,7 +1014,7 @@ def get_lldp_neighbors_detail(self, interface=""): def cli(self, commands, encoding="text"): """Execute raw CLI commands and returns their output.""" - if encoding not in ("text", ): + if encoding not in ("text",): raise ValueError("%s is not a supported encoding" % encoding) cli_output = {} diff --git a/napalm/nxos/nxos.py b/napalm/nxos/nxos.py index cff810f85..6cbc15f04 100644 --- a/napalm/nxos/nxos.py +++ b/napalm/nxos/nxos.py @@ -1127,8 +1127,10 @@ def get_bgp_neighbors(self) -> Dict[str, models.BGPStateNeighborsPerVRFDict]: results[vrf_name] = result_vrf_dict return results - def cli(self, commands: List[str], encoding: str="text") -> Dict[str, Union[str, Dict[str, Any]]]: - if encoding not in ("text", ): + def cli( + self, commands: List[str], encoding: str = "text" + ) -> Dict[str, Union[str, Dict[str, Any]]]: + if encoding not in ("text",): raise ValueError("%s is not a supported encoding" % encoding) cli_output: Dict[str, Union[str, Dict[str, Any]]] = {} if type(commands) is not list: diff --git a/napalm/nxos_ssh/nxos_ssh.py b/napalm/nxos_ssh/nxos_ssh.py index 43dab9f03..94144ca49 100644 --- a/napalm/nxos_ssh/nxos_ssh.py +++ b/napalm/nxos_ssh/nxos_ssh.py @@ -788,7 +788,7 @@ def get_bgp_neighbors(self): return bgp_dict def cli(self, commands, encoding="text"): - if encoding not in ("text", ): + if encoding not in ("text",): raise ValueError("%s is not a supported encoding" % encoding) cli_output = {} if type(commands) is not list: From 487f317403012811274a563239454cc97725b315 Mon Sep 17 00:00:00 2001 From: Diego Asturias Date: Fri, 20 May 2022 11:05:09 -0700 Subject: [PATCH 5/6] Add param to mock base --- napalm/base/mock.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/napalm/base/mock.py b/napalm/base/mock.py index e785528fd..bff491e20 100644 --- a/napalm/base/mock.py +++ b/napalm/base/mock.py @@ -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]+") From 3b0906a8ef1da9141baa031cbf0284c07cf67e62 Mon Sep 17 00:00:00 2001 From: Diego Asturias Date: Mon, 23 May 2022 09:00:50 -0700 Subject: [PATCH 6/6] Change ValueError to NotImplementedError --- napalm/eos/eos.py | 2 +- napalm/ios/ios.py | 2 +- napalm/iosxr/iosxr.py | 2 +- napalm/junos/junos.py | 2 +- napalm/nxos/nxos.py | 2 +- napalm/nxos_ssh/nxos_ssh.py | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/napalm/eos/eos.py b/napalm/eos/eos.py index 39c6cacfe..dc864f0b5 100644 --- a/napalm/eos/eos.py +++ b/napalm/eos/eos.py @@ -887,7 +887,7 @@ def get_lldp_neighbors_detail(self, interface=""): def cli(self, commands, encoding="text"): if encoding not in ("text", "json"): - raise ValueError("%s is not a supported encoding" % encoding) + raise NotImplementedError("%s is not a supported encoding" % encoding) cli_output = {} if type(commands) is not list: diff --git a/napalm/ios/ios.py b/napalm/ios/ios.py index b1acd27a3..87cf14538 100644 --- a/napalm/ios/ios.py +++ b/napalm/ios/ios.py @@ -2376,7 +2376,7 @@ def cli(self, commands, encoding="text"): """ if encoding not in ("text",): - raise ValueError("%s is not a supported encoding" % encoding) + 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!") diff --git a/napalm/iosxr/iosxr.py b/napalm/iosxr/iosxr.py index c081a56e1..d56df0da6 100644 --- a/napalm/iosxr/iosxr.py +++ b/napalm/iosxr/iosxr.py @@ -887,7 +887,7 @@ def get_lldp_neighbors_detail(self, interface=""): def cli(self, commands, encoding="text"): if encoding not in ("text",): - raise ValueError("%s is not a supported encoding" % encoding) + raise NotImplementedError("%s is not a supported encoding" % encoding) cli_output = {} diff --git a/napalm/junos/junos.py b/napalm/junos/junos.py index 585f1e097..52cd7fd7f 100644 --- a/napalm/junos/junos.py +++ b/napalm/junos/junos.py @@ -1015,7 +1015,7 @@ def get_lldp_neighbors_detail(self, interface=""): def cli(self, commands, encoding="text"): """Execute raw CLI commands and returns their output.""" if encoding not in ("text",): - raise ValueError("%s is not a supported encoding" % encoding) + raise NotImplementedError("%s is not a supported encoding" % encoding) cli_output = {} def _count(txt, none): # Second arg for consistency only. noqa diff --git a/napalm/nxos/nxos.py b/napalm/nxos/nxos.py index 6cbc15f04..63625a168 100644 --- a/napalm/nxos/nxos.py +++ b/napalm/nxos/nxos.py @@ -1131,7 +1131,7 @@ def cli( self, commands: List[str], encoding: str = "text" ) -> Dict[str, Union[str, Dict[str, Any]]]: if encoding not in ("text",): - raise ValueError("%s is not a supported encoding" % encoding) + 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!") diff --git a/napalm/nxos_ssh/nxos_ssh.py b/napalm/nxos_ssh/nxos_ssh.py index 94144ca49..01ae1da83 100644 --- a/napalm/nxos_ssh/nxos_ssh.py +++ b/napalm/nxos_ssh/nxos_ssh.py @@ -789,7 +789,7 @@ def get_bgp_neighbors(self): def cli(self, commands, encoding="text"): if encoding not in ("text",): - raise ValueError("%s is not a supported encoding" % encoding) + 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!")