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

Open
illes wants to merge 2 commits from illes/patch-1 into main
illes commented 2022-04-26 12:25:47 +02:00 (Migrated from github.com)
lldp.eth0.vlan.vlan-id=300
lldp.eth0.vlan.pvid=yes
lldp.eth0.vlan=VLAN300
Version: lldpd 1.0.11
Traceback (most recent call last):
  File "/root/.local/bin/netbox_agent", line 8, in <module>
    sys.exit(main())
  File "/root/.local/lib/python3.10/site-packages/netbox_agent/cli.py", line 44, in main
    return run(config)
  File "/root/.local/lib/python3.10/site-packages/netbox_agent/cli.py", line 39, in run
    server.netbox_create_or_update(config)
  File "/root/.local/lib/python3.10/site-packages/netbox_agent/server.py", line 292, in netbox_create_or_update
    self.network.create_or_update_netbox_network_cards()
  File "/root/.local/lib/python3.10/site-packages/netbox_agent/network.py", line 417, in create_or_update_netbox_network_cards
    ret, interface = self.reset_vlan_on_interface(nic, interface)
  File "/root/.local/lib/python3.10/site-packages/netbox_agent/network.py", line 234, in reset_vlan_on_interface
    pvid_vlan = [key for (key, value) in lldp_vlan.items() if value['pvid']]
  File "/root/.local/lib/python3.10/site-packages/netbox_agent/network.py", line 234, in <listcomp>
    pvid_vlan = [key for (key, value) in lldp_vlan.items() if value['pvid']]
KeyError: 'pvid'
lldp_vlan := {'300': {'pvid': True}, 'VLAN300': {}}
``` lldp.eth0.vlan.vlan-id=300 lldp.eth0.vlan.pvid=yes lldp.eth0.vlan=VLAN300 ``` ``` Version: lldpd 1.0.11 ``` ``` Traceback (most recent call last): File "/root/.local/bin/netbox_agent", line 8, in <module> sys.exit(main()) File "/root/.local/lib/python3.10/site-packages/netbox_agent/cli.py", line 44, in main return run(config) File "/root/.local/lib/python3.10/site-packages/netbox_agent/cli.py", line 39, in run server.netbox_create_or_update(config) File "/root/.local/lib/python3.10/site-packages/netbox_agent/server.py", line 292, in netbox_create_or_update self.network.create_or_update_netbox_network_cards() File "/root/.local/lib/python3.10/site-packages/netbox_agent/network.py", line 417, in create_or_update_netbox_network_cards ret, interface = self.reset_vlan_on_interface(nic, interface) File "/root/.local/lib/python3.10/site-packages/netbox_agent/network.py", line 234, in reset_vlan_on_interface pvid_vlan = [key for (key, value) in lldp_vlan.items() if value['pvid']] File "/root/.local/lib/python3.10/site-packages/netbox_agent/network.py", line 234, in <listcomp> pvid_vlan = [key for (key, value) in lldp_vlan.items() if value['pvid']] KeyError: 'pvid' ``` ``` lldp_vlan := {'300': {'pvid': True}, 'VLAN300': {}} ```
rosberen commented 2022-04-26 22:48:45 +02:00 (Migrated from github.com)

Fix KeyError: 'pvid' in lldp.py #223
vid = value.replace('vlan-', '').replace('VLAN', '')

It still gives an error

Traceback (most recent call last):
File "/usr/lib64/python3.6/runpy.py", line 193, in _run_module_as_main
"main", mod_spec)
File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/opt/netbox_agent/netbox_agent/cli.py", line 54, in
main()
File "/opt/netbox_agent/netbox_agent/cli.py", line 50, in main
return run(config)
File "/opt/netbox_agent/netbox_agent/cli.py", line 43, in run
server.netbox_create_or_update(config)
File "/opt/netbox_agent/netbox_agent/server.py", line 413, in netbox_create_or_update
self.network.create_or_update_netbox_network_cards()
File "/opt/netbox_agent/netbox_agent/network.py", line 443, in create_or_update_netbox_network_cards
ret, interface = self.reset_vlan_on_interface(nic, interface)
File "/opt/netbox_agent/netbox_agent/network.py", line 248, in reset_vlan_on_interface
pvid_vlan = [key for (key, value) in lldp_vlan.items() if value['pvid']]
File "/opt/netbox_agent/netbox_agent/network.py", line 248, in
pvid_vlan = [key for (key, value) in lldp_vlan.items() if value['pvid']]
KeyError: 'pvid'

Fix KeyError: 'pvid' in lldp.py #223 vid = value.replace('vlan-', '').replace('VLAN', '') It still gives an error Traceback (most recent call last): File "/usr/lib64/python3.6/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code exec(code, run_globals) File "/opt/netbox_agent/netbox_agent/cli.py", line 54, in <module> main() File "/opt/netbox_agent/netbox_agent/cli.py", line 50, in main return run(config) File "/opt/netbox_agent/netbox_agent/cli.py", line 43, in run server.netbox_create_or_update(config) File "/opt/netbox_agent/netbox_agent/server.py", line 413, in netbox_create_or_update self.network.create_or_update_netbox_network_cards() File "/opt/netbox_agent/netbox_agent/network.py", line 443, in create_or_update_netbox_network_cards ret, interface = self.reset_vlan_on_interface(nic, interface) File "/opt/netbox_agent/netbox_agent/network.py", line 248, in reset_vlan_on_interface pvid_vlan = [key for (key, value) in lldp_vlan.items() if value['pvid']] File "/opt/netbox_agent/netbox_agent/network.py", line 248, in <listcomp> pvid_vlan = [key for (key, value) in lldp_vlan.items() if value['pvid']] KeyError: 'pvid'
illes commented 2022-04-29 09:43:21 +02:00 (Migrated from github.com)

You are right that KeyError: 'pvid' can still happen, though not with the minimal example I provided. To catch that case, I added an example and tests.

You are right that `KeyError: 'pvid'` can still happen, though not with the minimal example I provided. To catch that case, I added an example and tests.
cyrinux commented 2022-11-14 10:10:29 +01:00 (Migrated from github.com)

@illes @rosberen sorry for the delay, is this PR ready for you ?

@illes @rosberen sorry for the delay, is this PR ready for you ?
This pull request has changes conflicting with the target branch.
  • netbox_agent/lldp.py
  • netbox_agent/network.py
  • tests/network.py
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin illes/patch-1:illes/patch-1
git checkout illes/patch-1

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git checkout main
git merge --no-ff illes/patch-1
git checkout illes/patch-1
git rebase main
git checkout main
git merge --ff-only illes/patch-1
git checkout illes/patch-1
git rebase main
git checkout main
git merge --no-ff illes/patch-1
git checkout main
git merge --squash illes/patch-1
git checkout main
git merge --ff-only illes/patch-1
git checkout main
git merge illes/patch-1
git push origin main
Sign in to join this conversation.
No description provided.