-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Description
NetBox version
v3.0.7
Python version
3.9
Steps to Reproduce
Create a new IPAM prefix which is already full like this: 192.168.101.0/32
Then note down the ID of the created prefix. It can be found in the URL bar of the browser.
- For example:
https://demo.netbox.dev/ipam/prefixes/78/-78is the ID.
Next create an API token for your user.
Now call the available-ips API and insert the prefix ID and token values:
token="<token>"
id="<id>"
for i in {0..10}; do
curl -v -XPOST -H "Content-Type: application/json" \
-H "Authorization: Token $token" \
"https://demo.netbox.dev/api/ipam/prefixes/$id/available-ips/" 2>&1 \
| grep -q "Excess found:" && echo "found" || echo "not found"
done
Output:
found
not found
found
found
found
not found
not found
found
not found
found
not found
Explanation:
The above curl command calls the same API 10 times.
When curl notices a resoponse body for a HTTP status code which does normally not include a response body (like status code 204) it reports this via Excess found:.
Now normally there shouldn't be an Excess found: and thus the output should show not found 10 times.
For every found in the output the netbox API returned a response body containing this:
{"detail":"An insufficient number of IP addresses are available within 192.168.101.0/32 (1 requested, 0 available)"}
Now I don't know why this only happens sometimes (maybe this has somthing to do with how the request is built in django) but in any case there should be no response body at all for HTTP 204.
Expected Behavior
More information on the HTTP status code 204:
The code causing this:
netbox/netbox/ipam/api/mixins.py
Lines 127 to 134 in e05fa5c
| if available_ips.size < len(requested_ips): | |
| return Response( | |
| { | |
| "detail": f"An insufficient number of IP addresses are available within {parent} " | |
| f"({len(requested_ips)} requested, {len(available_ips)} available)" | |
| }, | |
| status=status.HTTP_204_NO_CONTENT | |
| ) |
A fix would be rather simple:
- Return an empty response and
HTTP_204_NO_CONTENT - or return a response, but change the status to somthing else that fits the case.
Observed Behavior
I think most HTTP clients expect no response body when presented with a HTTP 204 No Content.
The funny thing is sometimes there is no body returned and sometimes there is.
So for example to golang http client logs these violations to the console:
2021/10/10 13:13:42 Unsolicited response received on idle HTTP channel starting with "{\"detail\":\"An insufficient number of IP addresses are available within 192.168.101.0/32 (1 requested, 0 available)\"}"; err=<nil>