netbox-community / pynetbox

Python API client library for Netbox.
Apache License 2.0
556 stars 168 forks source link

Updating an IP created by available_ips.create() will try to PATCH the IP to the Prefix API #288

Closed jqueuniet closed 3 years ago

jqueuniet commented 3 years ago

When I try to update an IP object I just created with available_ips, it seems pynetbox tries to send the PATCH request to the prefixes endpoint.

Tested with Netbox 2.8.8, pynetbox 5.0.8 and Python 3.8

ip = prefix.available_ips.create({'interface': interface.id})
ip.description = 'test'
ip.save()
DEBUG:urllib3.connectionpool:https://netbox.xxx.zzz:443 "POST /api/ipam/prefixes/1322/available-ips/ HTTP/1.1" 201 675
DEBUG:urllib3.connectionpool:https://netbox.xxx.zzz:443 "PATCH /api/ipam/prefixes/55372/ HTTP/1.1" 404 23
Traceback (most recent call last):
  File "./myscript.py", line 283, in <module>
    main()
  File "./myscript.py", line 99, in allocate_ip
    ip.save()
  File "/home/johann/.local/lib/python3.8/site-packages/pynetbox/core/response.py", line 391, in save
    if req.patch({i: serialized[i] for i in diff}):
  File "/home/johann/.local/lib/python3.8/site-packages/pynetbox/core/query.py", line 409, in patch
    return self._make_call(verb="patch", data=data)
  File "/home/johann/.local/lib/python3.8/site-packages/pynetbox/core/query.py", line 274, in _make_call
    raise RequestError(req)
pynetbox.core.query.RequestError: The requested url: https://netbox.xxx.zzz/api/ipam/prefixes/55372/ could not be found.

I cheked the object type, is is ndeed pynetbox.models.ipam.IpAddresses

zachmoody commented 3 years ago

hrm, seems to work for me on latest pynetbox and NetBox 2.9.11

In [22]: x = nb.ipam.prefixes.get(1)
send: b'GET /api/ipam/prefixes/1/ HTTP/1.1\r\nHost: localhost:8000\r\nUser-Agent: python-requests/2.24.0\r\nAccept-Encoding: gzip, deflate\r\naccept: application/json;\r\nConnection: keep-alive\r\nauthorization: Token 0123456789abcdef0123456789abcdef01234567\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Server: nginx
header: Date: Tue, 29 Dec 2020 23:59:22 GMT
header: Content-Type: application/json
header: Content-Length: 354
header: Connection: keep-alive
header: Vary: Accept, Cookie, Origin
header: Allow: GET, PUT, PATCH, DELETE, HEAD, OPTIONS
header: API-Version: 2.9
header: X-Content-Type-Options: nosniff
header: Referrer-Policy: same-origin
header: X-Frame-Options: SAMEORIGIN
header: P3P: CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"

In [23]: ip = x.available_ips.create()
send: b'POST /api/ipam/prefixes/1/available-ips/ HTTP/1.1\r\nHost: localhost:8000\r\nUser-Agent: python-requests/2.24.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Type: application/json;\r\nauthorization: Token 0123456789abcdef0123456789abcdef01234567\r\nContent-Length: 2\r\n\r\n'
send: b'{}'
reply: 'HTTP/1.1 201 Created\r\n'
header: Server: nginx
header: Date: Tue, 29 Dec 2020 23:59:43 GMT
header: Content-Type: application/json
header: Content-Length: 447
header: Connection: keep-alive
header: Vary: Accept, Cookie, Origin
header: Allow: GET, POST, HEAD, OPTIONS
header: API-Version: 2.9
header: X-Content-Type-Options: nosniff
header: Referrer-Policy: same-origin
header: X-Frame-Options: SAMEORIGIN
header: P3P: CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"

In [24]: ip.description = 'testing'

In [25]: ip.save()
send: b'PATCH /api/ipam/ip-addresses/4/ HTTP/1.1\r\nHost: localhost:8000\r\nUser-Agent: python-requests/2.24.0\r\nAccept-Encoding: gzip, deflate\r\naccept: application/json;\r\nConnection: keep-alive\r\nauthorization: Token 0123456789abcdef0123456789abcdef01234567\r\nContent-Length: 26\r\nContent-Type: application/json\r\n\r\n'
send: b'{"description": "testing"}'
reply: 'HTTP/1.1 200 OK\r\n'
header: Server: nginx
header: Date: Tue, 29 Dec 2020 23:59:51 GMT
header: Content-Type: application/json
header: Content-Length: 454
header: Connection: keep-alive
header: Vary: Accept, Cookie, Origin
header: Allow: GET, PUT, PATCH, DELETE, HEAD, OPTIONS
header: API-Version: 2.9
header: X-Content-Type-Options: nosniff
header: Referrer-Policy: same-origin
header: X-Frame-Options: SAMEORIGIN
header: P3P: CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"
Out[25]: True