Fix KeyError: 'pvid' in lldp.py #223

Open
illes wants to merge 2 commits from illes/patch-1 into main
3 changed files with 18 additions and 1 deletions
Showing only changes of commit 5346e0fdf9 - Show all commits

View file

@ -245,7 +245,7 @@ class Network(object):
interface.untagged_vlan = None interface.untagged_vlan = None
# Finally if LLDP reports a vlan-id with the pvid attribute # Finally if LLDP reports a vlan-id with the pvid attribute
elif lldp_vlan: elif lldp_vlan:
pvid_vlan = [key for (key, value) in lldp_vlan.items() if value['pvid']] pvid_vlan = [key for (key, value) in lldp_vlan.items() if value.get('pvid')]
if len(pvid_vlan) > 0 and ( if len(pvid_vlan) > 0 and (
interface.mode is None or interface.mode is None or
interface.mode.value != self.dcim_choices['interface:mode']['Access'] or interface.mode.value != self.dcim_choices['interface:mode']['Access'] or

7
tests/fixtures/lldp/223.txt vendored Normal file
View file

@ -0,0 +1,7 @@
lldp.eth0.vlan.vlan-id=300
lldp.eth0.vlan.pvid=yes
lldp.eth0.vlan=VLAN300
# PVID is optional
lldp.eth1.vlan.vlan-id=300
lldp.eth1.vlan=VLAN300

View file

@ -18,3 +18,13 @@ def test_lldp_parse_with_port_desc(fixture):
def test_lldp_parse_without_ifname(fixture): def test_lldp_parse_without_ifname(fixture):
lldp = LLDP(fixture) lldp = LLDP(fixture)
assert lldp.get_switch_port('eth0') == 'xe-0/0/1' assert lldp.get_switch_port('eth0') == 'xe-0/0/1'
@parametrize_with_fixtures(
'lldp/', only_filenames=[
'223.txt',
])
def test_lldp_parse_with_vlan(fixture):
lldp = LLDP(fixture)
assert lldp.get_switch_vlan('eth0') == {'300': {'pvid': True}}
assert lldp.get_switch_vlan('eth1') == {'300': {}}