Netbox: Device primary_ip not being removed when removing address from interface

Created on 28 Aug 2019  路  7Comments  路  Source: netbox-community/netbox

Environment

  • Python version: 3.5.3
  • NetBox version: 2.6.2

Steps to Reproduce

  1. Add an interface (let's call it 'I1') to a device ('D1'), and then add an IP address ('A1') to that interface 'I1'
  2. Set the tuple (Interface-I1, IP-A1) as the primary_ip for the device 'D1'
  3. Assign that IP address to a new interface ('I2') owned by a different device ('D2')
  4. Try to set that new tuple (Interface-I2, IP-A1) as the primary IP for the device 'D2'

Expected Behavior

As far as I can reassign the IP address to a new interface (which is completely logical), I should be able to set it as primary_ip on the new device.

Observed Behavior

Response from the UI (device edit): Device with this Primary IPv4 already exists.
Response from the API (PATCH /api/dcim/devices//):

HTTP 500:

duplicate key value violates unique constraint "dcim_device_primary_ip4_id_key"
DETAIL:  Key (primary_ip4_id)=(<IP-A1_id>) already exists.
accepted bug

All 7 comments

  1. Assign that IP address to a new interface ('I2') owned by a different device ('D2')

How are you making this change? Via the API?

Hello @jeremystretch

Yes, I'm performing that change (and the others) via the API

Are you telling me that this behaviour only happens when using the API? Is the UI the one triggering the release? :(

juan-vg,

You are removing the IP from the interface but otherwise leaving it in the database correct? The current setup is a foreign key on the Device table pointing to the primary ip.

Instead of re-assigning to a new device, you should delete the IP and re-create it on a new device. The UI can handle this gracefully as the UI generally requires more logic in the controllers whereas for the most part, with the API we run off of serializers so it doesn't take into account secondary events such as assigning an IP to a different device then wanting to set that IP as the primary.

If you want to do this, you should:

  1. Delete the IP
  2. Recreate it on the new device

or

  1. Unassign the IP as primary
  2. Attach it to the new device
  3. Assign it as primary

I am going to look into possible ways to clean this up however.

Hey @DanSheps! Thank you for your answer and for the workarounds :)

Sadly the workarounds are not perfectly/efficiently solving my current situation.

I have several scripts gathering real-time info from my infrastructure and then populating NetBox.

  • On the one hand I'm setting info on the IP addresses so if I delete them I will loose (meta)data.
  • On the other hand, I have a script that checks some network interfaces and updates the IP addresses they currently have (DHCP with long leases, but DHCP at all). I would need several queries to know first to who was assigned, then unassign it, and then assign it again.

I believe that this update must be done by the API because if not the DB will end up in an inconsistent state

I am going to look into possible ways to clean this up however.

I guess that a SQL trigger could be a quick solution working for the API and the UI at the same time :)

  • On the one hand I'm setting info on the IP addresses so if I delete them I will loose (meta)data.

It sounds like option #2 will solve this for you

  • On the other hand, I have a script that checks some network interfaces and updates the IP addresses they currently have (DHCP with long leases, but DHCP at all). I would need several queries to know first to who was assigned, then unassign it, and then assign it again.

I don't see what this has to do with either of the workarounds presented.

If you need further assistance with the workarounds specifically, I would suggest you go to the mailing list for additional support there.

To be clear, it is intended behavior to not let an IP be moved from device A to device B while being assigned as the primary IP for device A. The parent device/VM needs to be modified to remove the assignment as primary IP before the IP can be reassigned. However, there is currently no validation in place to prevent this (via the API), so I'm calling it a bug.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

markve-sa picture markve-sa  路  4Comments

hellerve picture hellerve  路  3Comments

markve-sa picture markve-sa  路  4Comments

robbagithub picture robbagithub  路  3Comments

mrfroggg picture mrfroggg  路  3Comments