API Bulk-Create of Devices does not check Rack-Space in its own context #5498

Closed
opened 2025-12-29 19:28:43 +01:00 by adam · 4 comments
Owner

Originally created by @moonrail on GitHub (Oct 8, 2021).

Originally assigned to: @arthanson on GitHub.

NetBox version

v3.0.6

Python version

3.9

Steps to Reproduce

  1. Create Rack with 42U
  2. Create Device-Type with Height of 2U
  3. POST /dcim/devices/:
[
  {
    "device_role": 1,
    "device_type": <your_id>,
    "site": 1,
    "rack": <your_id>,
    "face": "front",
    "position": 1
  },
  {
    "device_role": 1,
    "device_type": <your_id>,
    "site": 1,
    "rack": <your_id>,
    "face": "front",
    "position": 2
  }
]

Expected Behavior

A ValidationError with >400 HTTP Status, as both Devices overlap each other in the Rack.

Observed Behavior

API returns 201 HTTP Status and both Devices are there.
Further Handling other than deleting them or moving them to another position is not possible.
This also breaks SVG-rendering of Racks:

  File "/opt/netbox/netbox/dcim/api/views.py", line 218, in elevation
    drawing = rack.get_elevation_svg(
  File "/opt/netbox/netbox/dcim/models/racks.py", line 392, in get_elevation_svg
    return elevation.render(face, unit_width, unit_height, legend_width)
  File "/opt/netbox/netbox/dcim/svg.py", line 192, in render
    for unit in self.merge_elevations(face):
  File "/opt/netbox/netbox/dcim/svg.py", line 156, in merge_elevations
    elevation = self.rack.get_rack_units(face=face, expand_devices=False)
  File "/opt/netbox/netbox/dcim/models/racks.py", line 313, in get_rack_units
    elevation[device.position]['device'] = device
KeyError: 2

This Issue does not occur on API POST /dcim/devices/ with only one Device.
This Issue does not occur in Frontend-Import-View for Devices.
In both these circumstances the transaction is rolled back due to insufficient Space.

Originally created by @moonrail on GitHub (Oct 8, 2021). Originally assigned to: @arthanson on GitHub. ### NetBox version v3.0.6 ### Python version 3.9 ### Steps to Reproduce 1. Create Rack with 42U 2. Create Device-Type with Height of 2U 3. POST /dcim/devices/: ```json [ { "device_role": 1, "device_type": <your_id>, "site": 1, "rack": <your_id>, "face": "front", "position": 1 }, { "device_role": 1, "device_type": <your_id>, "site": 1, "rack": <your_id>, "face": "front", "position": 2 } ] ``` ### Expected Behavior A ValidationError with >400 HTTP Status, as both Devices overlap each other in the Rack. ### Observed Behavior API returns 201 HTTP Status and both Devices are there. Further Handling other than deleting them or moving them to another position is not possible. This also breaks SVG-rendering of Racks: ```python File "/opt/netbox/netbox/dcim/api/views.py", line 218, in elevation drawing = rack.get_elevation_svg( File "/opt/netbox/netbox/dcim/models/racks.py", line 392, in get_elevation_svg return elevation.render(face, unit_width, unit_height, legend_width) File "/opt/netbox/netbox/dcim/svg.py", line 192, in render for unit in self.merge_elevations(face): File "/opt/netbox/netbox/dcim/svg.py", line 156, in merge_elevations elevation = self.rack.get_rack_units(face=face, expand_devices=False) File "/opt/netbox/netbox/dcim/models/racks.py", line 313, in get_rack_units elevation[device.position]['device'] = device KeyError: 2 ``` This Issue does not occur on API POST /dcim/devices/ with only one Device. This Issue does not occur in Frontend-Import-View for Devices. In both these circumstances the transaction is rolled back due to insufficient Space.
adam added the type: bugstatus: accepted labels 2025-12-29 19:28:43 +01:00
adam closed this issue 2025-12-29 19:28:44 +01:00
Author
Owner

@github-actions[bot] commented on GitHub (Dec 8, 2021):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Please see our contributing guide.

@github-actions[bot] commented on GitHub (Dec 8, 2021): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@jeremystretch commented on GitHub (Dec 29, 2021):

We'll probably have to sit on this until we figure out what the implementation of #51 looks like.

@jeremystretch commented on GitHub (Dec 29, 2021): We'll probably have to sit on this until we figure out what the implementation of #51 looks like.
Author
Owner

@github-actions[bot] commented on GitHub (Oct 26, 2022):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions[bot] commented on GitHub (Oct 26, 2022): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@jeremystretch commented on GitHub (Apr 10, 2023):

Caught up with @arthanson on his approach for this in #10223. While valid, I worry that the approach in that PR is going to be difficult to maintain long-term. We identified an alternative approach which involves changing the order in which objects are validated and created within the REST API endpoint.

Rather than validating the new set of devices individually and then attempting to create them all (which can result in the above exception), we could create each device immediately upon its validation. This would mean device1 has already been created when device2 is being validated, which should result in the clean detection and reporting of any validation failures.

@jeremystretch commented on GitHub (Apr 10, 2023): Caught up with @arthanson on his approach for this in #10223. While valid, I worry that the approach in that PR is going to be difficult to maintain long-term. We identified an alternative approach which involves changing the order in which objects are validated and created within the REST API endpoint. Rather than validating the new set of devices individually and then attempting to create them all (which can result in the above exception), we could create each device immediately upon its validation. This would mean device1 has already been created when device2 is being validated, which should result in the clean detection and reporting of any validation failures.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#5498