In a custom script, it is possible to create a device with an invalid site and rack combination #3587

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

Steps to Reproduce

  1. Write custom script to create devices
  2. Obtain site object using
site_obj = ObjectVar(
    label="Site",
    queryset=Site.objects.all()
)
  1. Obtain rack object using (note rack should NOT be in the site specified in 2 above)
rack_obj = Rack.objects.get(name=rack_name)
  1. in Devices() constructor, specify the rack and site above
with transaction.atomic():
    device = Device(site=site_obj, rack=rack_obj, <other_stuff>)

Expected Behavior

Given site A containing rack A1 and site B containing rack B1, it should not 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 it is possible to create a device in rack A1 on site B. This doesn't seem right.

The background to this is that I am writing a custom script to parse an actual Excel workbook to import devices. The site is specified in an ObjectVar, but the rack is specified in the Excel workbook and searched for by name.

I've worked around this problem 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). ### Environment * Python version: 3.7.7 * NetBox version: 2.8.0 ### Steps to Reproduce 1. Write custom script to create devices 2. Obtain site object using ``` site_obj = ObjectVar( label="Site", queryset=Site.objects.all() ) ``` 3. Obtain rack object using (note rack should NOT be in the site specified in 2 above) ``` rack_obj = Rack.objects.get(name=rack_name) ``` 4. in Devices() constructor, specify the rack and site above ``` with transaction.atomic(): device = Device(site=site_obj, rack=rack_obj, <other_stuff>) ``` <!-- What did you expect to happen? --> ### Expected Behavior Given site A containing rack A1 and site B containing rack B1, it should not 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 it is possible to create a device in rack A1 on site B. This doesn't seem right. The background to this is that I am writing a custom script to parse an actual Excel workbook to import devices. The site is specified in an ObjectVar, but the rack is specified in the Excel workbook and searched for by name. I've worked around this problem 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):

Please see my response to #4511. This is not a bug: merely instantiating a device does not perform any validation. The mailing list is available if you need assistance with your custom script.

@jeremystretch commented on GitHub (Apr 20, 2020): Please see my response to #4511. This is not a bug: merely instantiating a device does not perform any validation. The mailing list is available if you need assistance with your custom script.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3587