netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Public demo: https://demo.netbox.dev
http://netboxlabs.com/oss/netbox/
Apache License 2.0
15.38k stars 2.49k forks source link

Unable to place another device in rack non-racked via REST API #16613

Open FestplattenSchnitzel opened 3 weeks ago

FestplattenSchnitzel commented 3 weeks ago

Deployment Type

Self-hosted

NetBox Version

4.0.5

Python Version

3.11

Steps to Reproduce

  1. Ensure some site exists
  2. Ensure some rack exists in the site (id=1 in example)
  3. Create first device in rack non-racked
  4. Create second device in the site (id=2 in example)
  5. Try placing the the device in the rack non-racked via the API

curl -X PATCH -H "authorization: Token TOKEN" -H 'Content-Type: application/json' -d '{"rack":1}' https://netbox.example.org/api/dcim/devices/2/

Expected Behaviour

API request should be successful (HTTP 200), device should be placed in the rack non-racked.

Observed Behaviour

API request fails (HTTP 400), (formatting applied by me):

{
  "non_field_errors": [
    "The fields rack, position, face must make a unique set."
  ]
}
FestplattenSchnitzel commented 3 weeks ago

This works fine via the UI though.

v0tti commented 3 weeks ago

I believe you have to set the device position within the rack as well as the rack face in order to successfully add a device to a rack. That is also what the error message is trying to tell you.

jeremystretch commented 3 weeks ago

@v0tti is correct: Just as in the UI, you need to specify a unit and face when assigning a device to a rack. I'll leave this open for anyone who would like to work on improving the error message.

FestplattenSchnitzel commented 3 weeks ago

I did not specify rack and position on purpose as the device should be "Not racked". Sorry I didn't state that clearer.

Using GET on a "Not racked" device returns null for both attributes. Explicitly providing both attributes at PATCH results in the same error.

Still: for me it's possible to not provide position and face to place a device in a rack "Not racked" in the UI.

arthanson commented 3 weeks ago

@FestplattenSchnitzel Thank you for opening a bug report. I was unable to reproduce the reported behavior on NetBox v4.0.5. Please re-confirm the reported behavior on the current stable release and adjust your post above as necessary. Remember to provide detailed steps that someone else can follow using a clean installation of NetBox to reproduce the issue. Remember to include the steps taken to create any initial objects or other data.

I tried with two different devices and used your curl command and in both cases it set the rack correctly without an error message (these did not have any rack assigned previously).

FestplattenSchnitzel commented 2 weeks ago

Ah, I can reproduce this works fine with newly created sites and racks. However, if I create a new device in a site that exists for a long time™ already:

curl -H "authorization: Token TOKEN" -H 'Content-Type: application/json' -d '{"role":1,"device_type":1,"site":1,"status":"planned"}' https://netbox.example.org/api/dcim/devices/

then try to place ("not racked") that in a rack in the same site (also exists since long ago):

curl -sX PATCH -H "authorization: Token TOKEN" -H 'Content-Type: application/json' -d '{"rack":2}' https://netbox.example.org/api/dcim/devices/2222/

I get the error from the original comment.

FestplattenSchnitzel commented 2 weeks ago

OK, the problem is you can't place a second device in a rack non-racked when there is a first one already. The old racks all had at least one non-racked device contrary to the newly created ones, which explains the behaviour from my last comment.

With a little more creativity one could've guessed that at the very beginning … :D

Will update initial comment and title soon …

github-actions[bot] commented 1 week ago

This is a reminder that additional information is needed in order to further triage this issue. If the requested details are not provided, the issue will soon be closed automatically.