Include 'interface' name within API call to '/api/dcim/devices/' #1892

Closed
opened 2025-12-29 17:20:07 +01:00 by adam · 1 comment
Owner

Originally created by @bdlamprecht on GitHub (Aug 3, 2018).

Environment

  • Python version: 3.6
  • NetBox version: 2.3.7

Proposed Functionality

Currently, the primary_ip4 and primary_ip6 are returned along with other information about each device. However, one thing that is missing, in my opinion, is the name of the interface that that address is associated with.

Let me explain my rational for including this one additional detail...
I'm working on an inventory plugin for nornir to use NetBox as a source-of-truth so I can migrate away from the unnecessary complexity that is ansible.

This plugin basically returns all of the values from /api/dcim/devices/ as attributes of an object so that a "task" can be run against it (such as creating a config file from a jinja template and deploying it via napalm in one step).

Juniper network devices use the format source-address X.X.X.X in their configuration which works fine with the current implementation. However, Cisco network devices use the format source-interface [NAME] instead.

Yes, I know that this [NAME] can be retrieved using an additional query to /api/ipam/ip-addresses/?address=X.X.X.X, however, that is just another API call that could be avoided.

I'm definitely open to other ideas and input from others either for or against this.

Use Case

Explained above.

Database Changes

None

External Dependencies

None

Originally created by @bdlamprecht on GitHub (Aug 3, 2018). ### Environment * Python version: 3.6 * NetBox version: 2.3.7 ### Proposed Functionality Currently, the `primary_ip4` and `primary_ip6` are returned along with other information about each `device`. However, one thing that is missing, in my opinion, is the name of the `interface` that that `address` is associated with. Let me explain my rational for including this one additional detail... I'm working on an inventory plugin for `nornir` to use NetBox as a source-of-truth so I can migrate away from the unnecessary complexity that is `ansible`. This plugin basically returns all of the values from `/api/dcim/devices/` as attributes of an `object` so that a "task" can be run against it (such as creating a config file from a `jinja` template and deploying it via `napalm` in one step). Juniper network devices use the format `source-address X.X.X.X` in their configuration which works fine with the current implementation. However, Cisco network devices use the format `source-interface [NAME]` instead. Yes, I know that this `[NAME]` _can_ be retrieved using an additional query to `/api/ipam/ip-addresses/?address=X.X.X.X`, however, that is just another API call that _*could*_ be avoided. I'm definitely open to other ideas and input from others either for or against this. ### Use Case Explained above. ### Database Changes None ### External Dependencies None
adam closed this issue 2025-12-29 17:20:07 +01:00
Author
Owner

@bdlamprecht commented on GitHub (Aug 3, 2018):

As discussed in NetworkToCode on Slack from jstretch:

you'd have to do like Device.objects.select_related('primary_ip4', 'primary_ip6').prefetch_related('interfaces') and then manually set the Interface on the IPAddressSerializer

actually I'm sure just doing the individual query is cheaper

it's one of those trade-offs between few API queries vs fast API queries

I'll just end up doing another query for the name of the interface.
Cheers!

@bdlamprecht commented on GitHub (Aug 3, 2018): As discussed in NetworkToCode on Slack from `jstretch`: >you'd have to do like `Device.objects.select_related('primary_ip4', 'primary_ip6').prefetch_related('interfaces')` and then manually set the Interface on the IPAddressSerializer > actually I'm sure just doing the individual query is cheaper > it's one of those trade-offs between few API queries vs fast API queries I'll just end up doing another query for the `name` of the `interface`. Cheers!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#1892