Add a filter for device.primary_ip4 and primary_ip6 to the REST API / graphQL #7341

Closed
opened 2025-12-29 20:22:06 +01:00 by adam · 3 comments
Owner

Originally created by @michaelarnauts on GitHub (Dec 9, 2022).

Originally assigned to: @michaelarnauts on GitHub.

NetBox version

v3.3.9

Feature type

Change to existing functionality

Proposed functionality

I would like to be able to fetch a device based on it's primary ipv4 (and probably also ipv6). I understand IP addresses aren't unique (you can them in multiple VRF's), but it should still be posible to get the device based on the ID of the IP Address.

Related to https://github.com/netbox-community/netbox/issues/10058

Mentioned here https://groups.google.com/g/netbox-discuss/c/Di5VMTtK5lg

Use case

I'm pushing devices to netbox trough a script with the API, and it happens that a device already exists with the same primary IP address. I want to handle these cases (probably by removing the old device).

When adding the device, I currently only get the error Device with this Primary IPv4 already exists, but there is no way to find this actual device.

I assign the IP address to an interface of the new device before I can configure the primary IP, but somehow while doing this, the IP address is still linked to an old device, causing that device to have a primary IP4 that isn't linked to any of its interfaces.

Once this happens, it's not possible anymore to find the actual device to remove it.

Database changes

No response

External dependencies

No response

Originally created by @michaelarnauts on GitHub (Dec 9, 2022). Originally assigned to: @michaelarnauts on GitHub. ### NetBox version v3.3.9 ### Feature type Change to existing functionality ### Proposed functionality I would like to be able to fetch a device based on it's primary ipv4 (and probably also ipv6). I understand IP addresses aren't unique (you can them in multiple VRF's), but it should still be posible to get the device based on the ID of the IP Address. Related to https://github.com/netbox-community/netbox/issues/10058 Mentioned here https://groups.google.com/g/netbox-discuss/c/Di5VMTtK5lg ### Use case I'm pushing devices to netbox trough a script with the API, and it happens that a device already exists with the same primary IP address. I want to handle these cases (probably by removing the old device). When adding the device, I currently only get the error `Device with this Primary IPv4 already exists`, but there is no way to find this actual device. I assign the IP address to an interface of the new device before I can configure the primary IP, but somehow while doing this, the IP address is still linked to an old device, causing that device to have a primary IP4 that isn't linked to any of its interfaces. Once this happens, it's not possible anymore to find the actual device to remove it. ### Database changes _No response_ ### External dependencies _No response_
adam added the status: acceptedtype: feature labels 2025-12-29 20:22:06 +01:00
adam closed this issue 2025-12-29 20:22:07 +01:00
Author
Owner

@dsobon commented on GitHub (Dec 9, 2022):

I've had to do this in my bulk import python code, when a device gets renamed from the source:

fix_nb_device = [nb_device for nb_device in nb_devices if str(nb_device) == nb_address.assigned_object.device.name]
fix_nb_device[0].update({ 'primary_ip4': None })
nb_address.assigned_object_type = None
nb_address.assigned_object_id = None
nb_address.save()

This presumes nb_devices contains all netbox device objects, and nb_address is the "Primary IPv4 already exists" netbox address object.

However, I also do a cleanup of devices in netbox that do not exist any more in the source... (if done in the start, rather than at the end, then the above logic is not required)

@dsobon commented on GitHub (Dec 9, 2022): I've had to do this in my bulk import python code, when a device gets renamed from the source: ```python fix_nb_device = [nb_device for nb_device in nb_devices if str(nb_device) == nb_address.assigned_object.device.name] fix_nb_device[0].update({ 'primary_ip4': None }) nb_address.assigned_object_type = None nb_address.assigned_object_id = None nb_address.save() ``` This presumes nb_devices contains all netbox device objects, and nb_address is the "Primary IPv4 already exists" netbox address object. However, I also do a cleanup of devices in netbox that do not exist any more in the source... (if done in the start, rather than at the end, then the above logic is not required)
Author
Owner

@michaelarnauts commented on GitHub (Jan 4, 2023):

@jeremystretch i'm willing to take a look add adding this filter, doesn't seem that hard.

@michaelarnauts commented on GitHub (Jan 4, 2023): @jeremystretch i'm willing to take a look add adding this filter, doesn't seem that hard.
Author
Owner

@kkthxbye-code commented on GitHub (Jan 4, 2023):

@michaelarnauts - All yours :)

@kkthxbye-code commented on GitHub (Jan 4, 2023): @michaelarnauts - All yours :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7341