API permit within to get all prefixes without parent prefix #2750

Closed
opened 2025-12-29 18:21:44 +01:00 by adam · 3 comments
Owner

Originally created by @Renji-FR on GitHub (Jul 22, 2019).

Environment

  • Python version: 3.6.9
  • NetBox version: 59139502ac7d (v2.6.1)

Proposed Functionality

For some application, we have to get prefixes which have not a parent prefix. These prefixes are root prefixes. We want use within filter with null value.

Use Case

  • We want to get all root prefixes which have site A (site_id = 1) as parent for example, so we have to filter prefixes with site field and within.
    curl -H "Accept: application/json; indent=4" -H "Authorization: Token 0123456789abcdef0123456789abcdef01234567" "http://localhost:32768/api/ipam/prefixes/?site_id=1&within=null"

  • We want to known if a prefix is a root prefix or not, so we have to filter prefixes with prefix field and within.

Gets all prefixes with prefix equal to 10.0.0.0/16 and without parent prefix:
curl -H "Accept: application/json; indent=4" -H "Authorization: Token 0123456789abcdef0123456789abcdef01234567" "http://localhost:32768/api/ipam/prefixes/?prefix=10.0.0.0/16&within=null"

Gets prefix with id=1 and filter "no parent prefix":
curl -H "Accept: application/json; indent=4" -H "Authorization: Token 0123456789abcdef0123456789abcdef01234567" "http://localhost:32768/api/ipam/prefixes/1/?within=null"

Gets all root prefixes:
curl -H "Accept: application/json; indent=4" -H "Authorization: Token 0123456789abcdef0123456789abcdef01234567" "http://localhost:32768/api/ipam/prefixes/?within=null"

Database Changes

Nothing change about database.

External Dependencies

Don't require any new dependency.

Originally created by @Renji-FR on GitHub (Jul 22, 2019). # Environment * Python version: 3.6.9 * NetBox version: 59139502ac7d (v2.6.1) ### Proposed Functionality For some application, we have to get prefixes which have not a parent prefix. These prefixes are root prefixes. We want use within filter with null value. ### Use Case - We want to get all root prefixes which have site A (site_id = 1) as parent for example, so we have to filter prefixes with site field and within. ` curl -H "Accept: application/json; indent=4" -H "Authorization: Token 0123456789abcdef0123456789abcdef01234567" "http://localhost:32768/api/ipam/prefixes/?site_id=1&within=null" ` - We want to known if a prefix is a root prefix or not, so we have to filter prefixes with prefix field and within. Gets all prefixes with prefix equal to 10.0.0.0/16 and without parent prefix: ` curl -H "Accept: application/json; indent=4" -H "Authorization: Token 0123456789abcdef0123456789abcdef01234567" "http://localhost:32768/api/ipam/prefixes/?prefix=10.0.0.0/16&within=null" ` Gets prefix with id=1 and filter "no parent prefix": ` curl -H "Accept: application/json; indent=4" -H "Authorization: Token 0123456789abcdef0123456789abcdef01234567" "http://localhost:32768/api/ipam/prefixes/1/?within=null" ` Gets all root prefixes: ` curl -H "Accept: application/json; indent=4" -H "Authorization: Token 0123456789abcdef0123456789abcdef01234567" "http://localhost:32768/api/ipam/prefixes/?within=null" ` ### Database Changes Nothing change about database. ### External Dependencies Don't require any new dependency.
adam closed this issue 2025-12-29 18:21:44 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jul 23, 2019):

The prefix hierarchy is not modeled explicitly within NetBox: there is no "parent" foreign key field relating one prefix to another. Constructing the hierarchy is a function of [address, mask length, VRF] provided inherently by IP itself. The parent filter is used to construct a SQL query comparing the address field of each row to a given prefix to determine at the database level whether the IP address falls within it. I don't believe PostgreSQL provides a way to explicitly filter for orphaned IPs (those without any parent objects), but I could be wrong.

@jeremystretch commented on GitHub (Jul 23, 2019): The prefix hierarchy is not modeled explicitly within NetBox: there is no "parent" foreign key field relating one prefix to another. Constructing the hierarchy is a function of [address, mask length, VRF] provided inherently by IP itself. The `parent` filter is used to construct a SQL query comparing the `address` field of each row to a given prefix to determine at the database level whether the IP address falls within it. I don't believe PostgreSQL provides a way to explicitly filter for orphaned IPs (those without any parent objects), but I could be wrong.
Author
Owner

@Renji-FR commented on GitHub (Jul 24, 2019):

My question was about prefixes and not addresses.

My application allow user to browse IPAM (NetBox or phpIPAM) in CLI.
I can browse correctly regions and sites because it is possible to retrieve root objects.

For prefixes, when user is in site, I would like retrieve root prefixes in this site.
The only solution is to get all prefixes and filter it client side.
It is not the best solution when there is a lot of prefixes.

It is possible to retrieve prefixes inside (whithin) another prefix so I was thinking it was possible to retrieve root prefixes.

I understand there is not relation between prefixes.
If my feature request is not possible, I will close this issue.

Thank you.

@Renji-FR commented on GitHub (Jul 24, 2019): My question was about prefixes and not addresses. My application allow user to browse IPAM (NetBox or phpIPAM) in CLI. I can browse correctly regions and sites because it is possible to retrieve root objects. For prefixes, when user is in site, I would like retrieve root prefixes in this site. The only solution is to get all prefixes and filter it client side. It is not the best solution when there is a lot of prefixes. It is possible to retrieve prefixes inside (whithin) another prefix so I was thinking it was possible to retrieve root prefixes. I understand there is not relation between prefixes. If my feature request is not possible, I will close this issue. Thank you.
Author
Owner

@jeremystretch commented on GitHub (Dec 13, 2019):

Closing due to inactivity.

@jeremystretch commented on GitHub (Dec 13, 2019): Closing due to inactivity.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#2750