netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
15.77k stars 2.54k forks source link

Add Device filtering by rack name #7286

Closed pspdbx closed 2 years ago

pspdbx commented 2 years ago

NetBox version

2.11.2

Feature type

New functionality

Proposed functionality

To iterate over the Devices in a rack, the only way to do it currently is nb.dcim.devices.filter(role="server", rack_id=<rack id integer>) , where nb is an instance of pynetbox client.

It requires tools that interact with Netbox make an additional call to check if the rack exists, and if it does, get its ID.

Proposed functionality: nb.dcim.devices.filter(role="server", rack=<rack name string>) is far easier to read and adopt.

Use case

In many of datacenter automation workflows (think provisioning, dns/dhcp config gen, ip allocation etc.), it is common to iterate over racks. While I'm very glad there is a model in Netbox for Rack, it would be a huge benefit to be able to filter on Device based on rack name, without making additional calls to Netbox to fetch the rack ID.

Database changes

None required.

External dependencies

None.

jeremystretch commented 2 years ago

Filtering by rack name is not supported because rack names need not be unique. For example, filtering by ?rack=R101 would return devices belonging to any rack named R101, at any site. You could also limit the query by assigned site, but at that point it's more efficient to specify the rack by ID anyway.

pspdbx commented 2 years ago

That makes sense, thank you @jeremystretch . Would adding a field for fqrack to the Rack model make sense? A fully qualified rack name that is guaranteed to be unique across all Sites.

jeremystretch commented 2 years ago

No, that wouldn't make any sense.

pspdbx commented 2 years ago

Ack, thanks for reviewing the request!