netbox-community / pynetbox

Python API client library for Netbox.
Apache License 2.0
538 stars 165 forks source link

virtual-device-context save() is using wrong API endpoint = missing support for VDCs in v7.0.1? #558

Closed empusas closed 11 months ago

empusas commented 11 months ago

with Pynetbox v7.0.1 I ran into a issue today.

vdc = nb.dcim.virtual_device_contexts.get(id=1059)
print(vdc)
vdc.comments = "test"
vdc.save()

pynetbox.core.query.RequestError: The requested url: https://x.x.x.x/api/dcim/devices/1059/ could not be found.

Retrieving the information works fine with get and I get all details. But the save obviously tries to use the "devices" endpoint instead of the "virtual-device-contexts" for the save().

Looking into the code I could not find any support for VDCs. So I assume it has not been implemented yet. But strange that the "get" seems to work anyway.

empusas commented 11 months ago

I have found the issue after some more reading of the pynetbox code and understanding how it works. And it is known bug in the net box API that was fixed in a more recent version. with netbox version 3.5.1 the API response for VDC`s looked like this

HTTP 200 OK
Allow: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "count": 1149,
    "next": "https://xxxxxt/api/dcim/virtual-device-contexts/?limit=50&offset=50",
    "previous": null,
    "results": [
        {
            "id": 1059,
            "url": "https://xxxx/api/dcim/device/1059/",
            "display": "xxxx",
            "name": "xxxx",
            "device": {
                "id": 1178,
                "url": "https://xxxxxx/api/dcim/devices/1178/",

As you can see the wrong URL was given back in the API that pointed to "devices" not "virtual-device-contexts". After update ing net box 3.5.7 the API response looked like this. All updates with pynetbox worked then as well.

HTTP 200 OK
Allow: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "count": 1149,
    "next": "https://xxxxxt/api/dcim/virtual-device-contexts/?limit=50&offset=50",
    "previous": null,
    "results": [
        {
            "id": 1059,
            "url": "https://xxxx/api/dcim/virtual-device-contexts/1059/",
            "display": "xxxx",
            "name": "xxxx",
            "device": {
                "id": 1178,
                "url": "https://xxxxxx/api/dcim/devices/1178/",