From c35d503445f97aa040ed54ebc106e41dc7f9625e Mon Sep 17 00:00:00 2001 From: Exane Server Team Date: Fri, 6 Mar 2020 16:45:53 +0100 Subject: [PATCH] FIX: fix vrf detection when no vrf is defined (#938) * FIX: fix vrf detection when no vrf is defined Right now an exception is raized when no vrf is defined. It will now return the default instances list. Also catch a few unhandle errors when vrf name does not exists. A ValueError exception is now raised with an explaination. * Add test where novrf is defined Add a new test for ios to match the case where no vrf is setup on the equipment * Fix current black issue on ios.py * Narrow exception catching .. It makes pylama happy, and was not a recommanded usage. Co-authored-by: Frederic Brin --- napalm/ios/ios.py | 30 ++++++-- .../novrf/expected_result.json | 76 +++++++++++++++++++ .../novrf/show_ip_interface_brief.txt | 65 ++++++++++++++++ .../novrf/show_vrf_detail.txt | 0 4 files changed, 165 insertions(+), 6 deletions(-) create mode 100644 test/ios/mocked_data/test_get_network_instances/novrf/expected_result.json create mode 100644 test/ios/mocked_data/test_get_network_instances/novrf/show_ip_interface_brief.txt create mode 100644 test/ios/mocked_data/test_get_network_instances/novrf/show_vrf_detail.txt diff --git a/napalm/ios/ios.py b/napalm/ios/ios.py index a944bb1cc..13c3d0502 100644 --- a/napalm/ios/ios.py +++ b/napalm/ios/ios.py @@ -2358,9 +2358,17 @@ def get_ntp_stats(self): return [] elif len(line.split()) == 9: - address, ref_clock, st, when, poll, reach, delay, offset, disp = ( - line.split() - ) + ( + address, + ref_clock, + st, + when, + poll, + reach, + delay, + offset, + disp, + ) = line.split() address_regex = re.match(r"(\W*)([0-9.*]*)", address) try: ntp_stats.append( @@ -2961,8 +2969,8 @@ def get_route_to(self, destination="", protocol="", longer=False): destination, _vrf, nh, ip_version ) nh_line_found = ( - False - ) # for next RT entry processing ... + False # for next RT entry processing ... + ) routes[destination].append(route_entry) return routes @@ -3307,6 +3315,13 @@ def get_network_instances(self, name=""): "interfaces": {"interface": interface_dict}, } + # No vrf is defined return default one + if len(sh_vrf_detail) == 0: + if name: + raise ValueError("No vrf is setup on router") + else: + return instances + for vrf in sh_vrf_detail.split("\n\n"): first_part = vrf.split("Address family")[0] @@ -3329,7 +3344,10 @@ def get_network_instances(self, name=""): "state": {"route_distinguisher": RD}, "interfaces": {"interface": interfaces}, } - return instances if not name else instances[name] + try: + return instances if not name else instances[name] + except AttributeError: + raise ValueError("The vrf %s does not exist" % name) def get_config(self, retrieve="all", full=False): """Implementation of get_config for IOS. diff --git a/test/ios/mocked_data/test_get_network_instances/novrf/expected_result.json b/test/ios/mocked_data/test_get_network_instances/novrf/expected_result.json new file mode 100644 index 000000000..a336408ec --- /dev/null +++ b/test/ios/mocked_data/test_get_network_instances/novrf/expected_result.json @@ -0,0 +1,76 @@ +{ + "default": { + "name": "default", + "type": "DEFAULT_INSTANCE", + "state": { + "route_distinguisher": "" + }, + "interfaces": { + "interface": { + "Vlan1": {}, + "Vlan1980": {}, + "Vlan1981": {}, + "Vlan1982": {}, + "Vlan1983": {}, + "Vlan1984": {}, + "Vlan1985": {}, + "Vlan1986": {}, + "Vlan1987": {}, + "GigabitEthernet1/0/1": {}, + "GigabitEthernet1/0/2": {}, + "GigabitEthernet1/0/3": {}, + "GigabitEthernet1/0/4": {}, + "GigabitEthernet1/0/5": {}, + "GigabitEthernet1/0/6": {}, + "GigabitEthernet1/0/7": {}, + "GigabitEthernet1/0/8": {}, + "GigabitEthernet1/0/9": {}, + "GigabitEthernet1/0/10": {}, + "GigabitEthernet1/0/11": {}, + "GigabitEthernet1/0/12": {}, + "GigabitEthernet1/0/13": {}, + "GigabitEthernet1/0/14": {}, + "GigabitEthernet1/0/15": {}, + "GigabitEthernet1/0/16": {}, + "GigabitEthernet1/0/17": {}, + "GigabitEthernet1/0/18": {}, + "GigabitEthernet1/0/19": {}, + "GigabitEthernet1/0/20": {}, + "GigabitEthernet1/0/21": {}, + "GigabitEthernet1/0/22": {}, + "GigabitEthernet1/0/23": {}, + "GigabitEthernet1/0/24": {}, + "GigabitEthernet1/0/25": {}, + "GigabitEthernet1/0/26": {}, + "GigabitEthernet1/0/27": {}, + "GigabitEthernet1/0/28": {}, + "GigabitEthernet1/0/29": {}, + "GigabitEthernet1/0/30": {}, + "GigabitEthernet1/0/31": {}, + "GigabitEthernet1/0/32": {}, + "GigabitEthernet1/0/33": {}, + "GigabitEthernet1/0/34": {}, + "GigabitEthernet1/0/35": {}, + "GigabitEthernet1/0/36": {}, + "GigabitEthernet1/0/37": {}, + "GigabitEthernet1/0/38": {}, + "GigabitEthernet1/0/39": {}, + "GigabitEthernet1/0/40": {}, + "GigabitEthernet1/0/41": {}, + "GigabitEthernet1/0/42": {}, + "GigabitEthernet1/0/43": {}, + "GigabitEthernet1/0/44": {}, + "GigabitEthernet1/0/45": {}, + "GigabitEthernet1/0/46": {}, + "GigabitEthernet1/0/47": {}, + "GigabitEthernet1/0/48": {}, + "GigabitEthernet1/0/49": {}, + "GigabitEthernet1/0/50": {}, + "GigabitEthernet1/0/51": {}, + "GigabitEthernet1/0/52": {}, + "Loopback1987": {}, + "Tunnel100": {} + } + } + } +} diff --git a/test/ios/mocked_data/test_get_network_instances/novrf/show_ip_interface_brief.txt b/test/ios/mocked_data/test_get_network_instances/novrf/show_ip_interface_brief.txt new file mode 100644 index 000000000..668bb4cf6 --- /dev/null +++ b/test/ios/mocked_data/test_get_network_instances/novrf/show_ip_interface_brief.txt @@ -0,0 +1,65 @@ + +Interface IP-Address OK? Method Status Protocol +Vlan1 unassigned YES NVRAM administratively down down +Vlan1980 10.196.16.254 YES NVRAM up up +Vlan1981 10.196.17.254 YES NVRAM up up +Vlan1982 10.196.18.254 YES NVRAM up down +Vlan1983 10.196.19.254 YES NVRAM up up +Vlan1984 10.196.20.254 YES NVRAM up down +Vlan1985 10.196.21.254 YES NVRAM up down +Vlan1986 10.196.22.254 YES NVRAM up down +Vlan1987 unassigned YES unset administratively down down +GigabitEthernet1/0/1 unassigned YES unset down down +GigabitEthernet1/0/2 unassigned YES unset down down +GigabitEthernet1/0/3 unassigned YES unset up up +GigabitEthernet1/0/4 unassigned YES unset down down +GigabitEthernet1/0/5 unassigned YES unset down down +GigabitEthernet1/0/6 unassigned YES unset down down +GigabitEthernet1/0/7 unassigned YES unset up up +GigabitEthernet1/0/8 unassigned YES unset down down +GigabitEthernet1/0/9 unassigned YES unset down down +GigabitEthernet1/0/10 unassigned YES unset down down +GigabitEthernet1/0/11 unassigned YES unset down down +GigabitEthernet1/0/12 unassigned YES unset down down +GigabitEthernet1/0/13 unassigned YES unset down down +GigabitEthernet1/0/14 unassigned YES unset down down +GigabitEthernet1/0/15 unassigned YES unset down down +GigabitEthernet1/0/16 unassigned YES unset down down +GigabitEthernet1/0/17 unassigned YES unset down down +GigabitEthernet1/0/18 unassigned YES unset down down +GigabitEthernet1/0/19 unassigned YES unset down down +GigabitEthernet1/0/20 unassigned YES unset down down +GigabitEthernet1/0/21 unassigned YES unset down down +GigabitEthernet1/0/22 unassigned YES unset down down +GigabitEthernet1/0/23 unassigned YES unset down down +GigabitEthernet1/0/24 unassigned YES unset down down +GigabitEthernet1/0/25 unassigned YES unset down down +GigabitEthernet1/0/26 unassigned YES unset down down +GigabitEthernet1/0/27 unassigned YES unset down down +GigabitEthernet1/0/28 unassigned YES unset down down +GigabitEthernet1/0/29 unassigned YES unset down down +GigabitEthernet1/0/30 unassigned YES unset down down +GigabitEthernet1/0/31 unassigned YES unset down down +GigabitEthernet1/0/32 unassigned YES unset down down +GigabitEthernet1/0/33 unassigned YES unset administratively down down +GigabitEthernet1/0/34 unassigned YES unset administratively down down +GigabitEthernet1/0/35 unassigned YES unset administratively down down +GigabitEthernet1/0/36 unassigned YES unset administratively down down +GigabitEthernet1/0/37 unassigned YES unset up up +GigabitEthernet1/0/38 unassigned YES unset up up +GigabitEthernet1/0/39 unassigned YES unset up up +GigabitEthernet1/0/40 10.127.0.189 YES NVRAM up up +GigabitEthernet1/0/41 unassigned YES unset administratively down down +GigabitEthernet1/0/42 unassigned YES unset administratively down down +GigabitEthernet1/0/43 unassigned YES unset administratively down down +GigabitEthernet1/0/44 unassigned YES unset administratively down down +GigabitEthernet1/0/45 unassigned YES unset administratively down down +GigabitEthernet1/0/46 unassigned YES unset administratively down down +GigabitEthernet1/0/47 unassigned YES unset administratively down down +GigabitEthernet1/0/48 unassigned YES unset down down +GigabitEthernet1/0/49 unassigned YES unset administratively down down +GigabitEthernet1/0/50 unassigned YES unset administratively down down +GigabitEthernet1/0/51 unassigned YES unset administratively down down +GigabitEthernet1/0/52 10.235.48.16 YES NVRAM up up +Loopback1987 10.196.23.254 YES NVRAM up up +Tunnel100 10.127.0.181 YES NVRAM up up \ No newline at end of file diff --git a/test/ios/mocked_data/test_get_network_instances/novrf/show_vrf_detail.txt b/test/ios/mocked_data/test_get_network_instances/novrf/show_vrf_detail.txt new file mode 100644 index 000000000..e69de29bb