Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #16786: Clone device location from rack #17508

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion netbox/dcim/models/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ def clean(self):
"Location {location} does not belong to site {site}."
).format(location=self.location, site=self.site)
})
if self.rack and self.location and self.rack.location != self.location:
if self.rack and self.location and not self.location.get_descendants(True).contains(self.rack.location):
raise ValidationError({
'rack': _(
"Rack {rack} does not belong to location {location}."
Expand Down
57 changes: 57 additions & 0 deletions netbox/dcim/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,63 @@ def test_device_duplicate_names(self):
device2.full_clean()
device2.save()

def test_device_mismatched_site_location(self):
sites = (
Site(name='Site 1', slug='site-1'),
Site(name='Site 2', slug='site-2'),
)
Site.objects.bulk_create(sites)

locations = (
Location(name='Location 1', slug='location-1', site=sites[0]),
Location(name='Location 2', slug='location-2', site=sites[1]),
)
for location in locations:
location.save()

device_type = DeviceType.objects.first()
device_role = DeviceRole.objects.first()

# Same site, should pass
Device(name='device1', site=sites[0], location=locations[0], device_type=device_type, role=device_role).full_clean()

# Mismatched site, should fail
with self.assertRaises(ValidationError):
Device(name='device2', site=sites[0], location=locations[1], device_type=device_type, role=device_role).full_clean()

def test_device_mismatched_rack_location(self):
site = Site.objects.first()

locations = (
Location(name='Location 1', slug='location-1', site=site),
Location(name='Location 2', slug='location-2', site=site),
)
for location in locations:
location.save()

rack = Rack.objects.create(name='Rack 1', site=site, location=locations[0])

device_type = DeviceType.objects.first()
device_role = DeviceRole.objects.first()

# Device should use location from rack
with self.assertRaises(ValidationError):
Device(name='device1', site=site, location=locations[1], rack=rack, device_type=device_type, role=device_role).full_clean()

def test_device_rack_clone_fields(self):
site = Site.objects.first()
location = Location(name='Location 1', slug='location-1', site=site)
location.save()

rack = Rack.objects.create(name='Rack 1', site=site, location=location)

device_type = DeviceType.objects.first()
device_role = DeviceRole.objects.first()

# Device should use location from rack
device = Device.objects.create(name='device1', site=site, rack=rack, device_type=device_type, role=device_role)
self.assertEqual(device.location, location)

def test_device_mismatched_site_cluster(self):
cluster_type = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1')
Cluster.objects.create(name='Cluster 1', type=cluster_type)
Expand Down