Skip to content

Commit

Permalink
FIX: fix vrf detection when no vrf is defined (#938)
Browse files Browse the repository at this point in the history
* 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 <frederic.brin@exane.com>
  • Loading branch information
ExaneServerTeam and ExaneServerTeam authored Mar 6, 2020
1 parent fdf91d9 commit c35d503
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 6 deletions.
30 changes: 24 additions & 6 deletions napalm/ios/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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]
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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": {}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Empty file.

0 comments on commit c35d503

Please sign in to comment.