Fixes #10517: Automatically inherit site assignment from cluster when creating a virtual machine

This commit is contained in:
jeremystretch 2022-10-03 15:35:45 -04:00
parent 7feb86fe55
commit 7712b81ab9
3 changed files with 8 additions and 4 deletions

View File

@ -20,6 +20,7 @@
* [#10480](https://github.com/netbox-community/netbox/issues/10480) - Cable trace SVG links should not force a new window * [#10480](https://github.com/netbox-community/netbox/issues/10480) - Cable trace SVG links should not force a new window
* [#10491](https://github.com/netbox-community/netbox/issues/10491) - Clarify representation of blocking contact assignments during contact deletion * [#10491](https://github.com/netbox-community/netbox/issues/10491) - Clarify representation of blocking contact assignments during contact deletion
* [#10513](https://github.com/netbox-community/netbox/issues/10513) - Disable the reassignment of a module to a new device * [#10513](https://github.com/netbox-community/netbox/issues/10513) - Disable the reassignment of a module to a new device
* [#10517](https://github.com/netbox-community/netbox/issues/10517) - Automatically inherit site assignment from cluster when creating a virtual machine
--- ---

View File

@ -347,10 +347,12 @@ class VirtualMachine(NetBoxModel, ConfigContextModel):
}) })
# Validate site for cluster & device # Validate site for cluster & device
if self.cluster and self.cluster.site != self.site: if self.cluster and self.site and self.cluster.site != self.site:
raise ValidationError({ raise ValidationError({
'cluster': f'The selected cluster ({self.cluster} is not assigned to this site ({self.site}).' 'cluster': f'The selected cluster ({self.cluster} is not assigned to this site ({self.site}).'
}) })
elif self.cluster:
self.site = self.cluster.site
if self.device and self.device.site != self.site: if self.device and self.device.site != self.site:
raise ValidationError({ raise ValidationError({
'device': f'The selected device ({self.device} is not assigned to this site ({self.site}).' 'device': f'The selected device ({self.device} is not assigned to this site ({self.site}).'

View File

@ -68,6 +68,7 @@ class VirtualMachineTestCase(TestCase):
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
VirtualMachine(name='vm1', site=sites[0], cluster=clusters[1]).full_clean() VirtualMachine(name='vm1', site=sites[0], cluster=clusters[1]).full_clean()
# VM with cluster site but no direct site should fail # VM with cluster site but no direct site should have its site set automatically
with self.assertRaises(ValidationError): vm = VirtualMachine(name='vm1', site=None, cluster=clusters[0])
VirtualMachine(name='vm1', site=None, cluster=clusters[0]).full_clean() vm.full_clean()
self.assertEqual(vm.site, sites[0])