Skip to content

POST to available-ips API call sometimes returns a body for an exhausted prefix #7519

@ston1th

Description

@ston1th

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/ - 78 is 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:

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>

Metadata

Metadata

Assignees

Labels

status: acceptedThis issue has been accepted for implementationtype: bugA confirmed report of unexpected behavior in the application

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions