Solvik / netbox-agent

Netbox agent to run on your infrastructure's servers
Apache License 2.0
273 stars 67 forks source link

Interfaces in virtual machines need to use different endpoint #241

Open Natureshadow opened 1 year ago

Natureshadow commented 1 year ago

Describe the bug

When updating interfaces, netbox-agent always uses the endpoint /api/dcim/interfaces. This is wrong for interfaces of virtual machines – in that regard, this is a critical bug because it can cause data loss (interface ID 35 in DCIM is something else that interface ID 35 in Virtualization).

If lucky, this results in a failing sanity check, like here:

DEBUG:urllib3.connectionpool:https://*******:443 "PATCH /api/dcim/interfaces/35/ HTTP/1.1" 400 61
Traceback (most recent call last):
  File "/opt/netbox-agent/./bin/netbox_agent", line 8, in <module>
    sys.exit(main())
  File "/opt/netbox-agent/lib/python3.9/site-packages/netbox_agent/cli.py", line 50, in main
    return run(config)
  File "/opt/netbox-agent/lib/python3.9/site-packages/netbox_agent/cli.py", line 43, in run
    server.netbox_create_or_update(config)
  File "/opt/netbox-agent/lib/python3.9/site-packages/netbox_agent/virtualmachine.py", line 109, in netbox_create_or_update
    self.network.create_or_update_netbox_network_cards()
  File "/opt/netbox-agent/lib/python3.9/site-packages/netbox_agent/network.py", line 478, in create_or_update_netbox_network_cards
    interface.save()
  File "/opt/netbox-agent/lib/python3.9/site-packages/pynetbox/core/response.py", line 460, in save
    if req.patch({i: serialized[i] for i in diff}):
  File "/opt/netbox-agent/lib/python3.9/site-packages/pynetbox/core/query.py", line 413, in patch
    return self._make_call(verb="patch", data=data)
  File "/opt/netbox-agent/lib/python3.9/site-packages/pynetbox/core/query.py", line 282, in _make_call
    raise RequestError(req)
pynetbox.core.query.RequestError: The request failed with code 400 Bad Request: {'type': ['Virtual interfaces cannot have a cable attached.']}

(Attention: The exception is not the bug, iit is a follow-up issue because, as explained above, interface 35 in DCIM is a virtual interface on an entirely different machine. If it hadn't been virtual in our setup, the netbox-agent PATCH request would have invalidated its data.)

It seems that the interface is actually resolved using the correct endpoint, and then when working with the discovered interface record, things fall apart:

DEBUG:urllib3.connectionpool:https://******:443 "GET /api/virtualization/interfaces/?mac_address=b6%3Ad1%3A9b%3A47%3A85%3A59&name=ens18&virtual_machine_i
d=48&limit=0 HTTP/1.1" 200 667
DEBUG:urllib3.connectionpool:https://******:443 "GET /api/dcim/interfaces/?id=35&limit=0 HTTP/1.1" 200 1768
INFO:root:Interface type is wrong, resetting

Expected behavior

If the machine type is detected as virtual, the endpoint /api/virtualization/interfaces should be used instead.

Environment:

KivraChristoffer commented 1 year ago

would this be solved by #232

Natureshadow commented 1 year ago

I see no way how this could be solved by the MR.

KivraChristoffer commented 1 year ago

have you tested with the latest release ?