Custom script - racks/sites inconsistency #3588

Closed
opened 2025-12-29 18:30:01 +01:00 by adam · 1 comment
Owner

Originally created by @cpmills1975 on GitHub (Apr 20, 2020).

Environment

  • Python version: 3.7.7
  • NetBox version: 2.8.0

Expected Behavior

If creating a device in a rack, the rack I've specified should be on the site specified. Given site A containing rack A1 and site B containing rack B1, it shouldn't be possible to create a device in rack A1 on site B.

Observed Behavior

Given a site A containing rack A1 and a site B containing rack B1, it seems I am able to create a device in rack A1 on site B. This doesn't seem right.

Discussion

I'm writing a custom script to parse an actual Excel workbook to import devices.
My input workbook includes the name of the rack which I resolve using

rack_obj = Rack.objects.get(name=rack_name)

I request the site in the custom script form using

site_obj = ObjectVar(
    label="Site",
    queryset=Site.objects.all()
)

I pass both of these to the Device constructor:

with transaction.atomic():
    device = Device(site=site_obj, rack=rack_obj, <other_stuff>)

and the device is created, in the rack specified and on the site specified - regardless of whether the rack specified actually exists on that site.

I've worked around this by changing the rack query to search for the rack name given in the site specified:

rack_obj = Rack.objects.get(name=rack_name, site=size_obj)

and this helps, but it seems odd that I should be able to create the object in a rack on the wrong site.

Reading the code, I see that the site is mandatory but the rack is optional. Would this be better the other way round or perhaps mandating that one of these is specified? If just the rack, the site should be derived from the site that the rack exists on? If both, should there be some error checking in place to make sure the rack and site are consistent?

Originally created by @cpmills1975 on GitHub (Apr 20, 2020). <!-- NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED. This form is only for reproducible bugs. If you need assistance with NetBox installation, or if you have a general question, DO NOT open an issue. Instead, post to our mailing list: https://groups.google.com/forum/#!forum/netbox-discuss Please describe the environment in which you are running NetBox. Be sure that you are running an unmodified instance of the latest stable release before submitting a bug report, and that any plugins have been disabled. --> ### Environment * Python version: 3.7.7 * NetBox version: 2.8.0 <!-- What did you expect to happen? --> ### Expected Behavior If creating a device in a rack, the rack I've specified should be on the site specified. Given site A containing rack A1 and site B containing rack B1, it shouldn't be possible to create a device in rack A1 on site B. <!-- What happened instead? --> ### Observed Behavior Given a site A containing rack A1 and a site B containing rack B1, it seems I am able to create a device in rack A1 on site B. This doesn't seem right. <!-- Describe in detail the exact steps that someone else can take to reproduce this bug using the current stable release of NetBox. Begin with the creation of any necessary database objects and call out every operation being performed explicitly. If reporting a bug in the REST API, be sure to reconstruct the raw HTTP request(s) being made: Don't rely on a client library such as pynetbox. --> ### Discussion I'm writing a custom script to parse an actual Excel workbook to import devices. My input workbook includes the name of the rack which I resolve using ``` rack_obj = Rack.objects.get(name=rack_name) ``` I request the site in the custom script form using ``` site_obj = ObjectVar( label="Site", queryset=Site.objects.all() ) ``` I pass both of these to the Device constructor: ``` with transaction.atomic(): device = Device(site=site_obj, rack=rack_obj, <other_stuff>) ``` and the device is created, in the rack specified and on the site specified - regardless of whether the rack specified actually exists on that site. I've worked around this by changing the rack query to search for the rack name given in the site specified: ``` rack_obj = Rack.objects.get(name=rack_name, site=size_obj) ``` and this helps, but it seems odd that I should be able to create the object in a rack on the wrong site. Reading the code, I see that the site is mandatory but the rack is optional. Would this be better the other way round or perhaps mandating that one of these is specified? If just the rack, the site should be derived from the site that the rack exists on? If both, should there be some error checking in place to make sure the rack and site are consistent?
adam closed this issue 2025-12-29 18:30:01 +01:00
Author
Owner

@jeremystretch commented on GitHub (Apr 20, 2020):

Thank you for your interest in NetBox. GitHub issues are intended for reporting reproducible bugs and requesting features, and must be submitted using one of the templates provided here. For general discussion, questions, or assistance with installation issues, please post to our mailing list instead.

@jeremystretch commented on GitHub (Apr 20, 2020): Thank you for your interest in NetBox. GitHub issues are intended for reporting reproducible bugs and requesting features, and must be submitted using one of the templates provided [here](https://github.com/netbox-community/netbox/issues/new/choose). For general discussion, questions, or assistance with installation issues, please post to our [mailing list](https://groups.google.com/forum/#!forum/netbox-discuss) instead.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3588