diff --git a/docs/release-notes/version-3.3.md b/docs/release-notes/version-3.3.md index 378eb993e..6d2bb3b5c 100644 --- a/docs/release-notes/version-3.3.md +++ b/docs/release-notes/version-3.3.md @@ -20,6 +20,7 @@ * [#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 * [#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 --- diff --git a/netbox/virtualization/models.py b/netbox/virtualization/models.py index abad57f88..69376d9c5 100644 --- a/netbox/virtualization/models.py +++ b/netbox/virtualization/models.py @@ -347,10 +347,12 @@ class VirtualMachine(NetBoxModel, ConfigContextModel): }) # 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({ '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: raise ValidationError({ 'device': f'The selected device ({self.device} is not assigned to this site ({self.site}).' diff --git a/netbox/virtualization/tests/test_models.py b/netbox/virtualization/tests/test_models.py index df5816efa..e916486b0 100644 --- a/netbox/virtualization/tests/test_models.py +++ b/netbox/virtualization/tests/test_models.py @@ -68,6 +68,7 @@ class VirtualMachineTestCase(TestCase): with self.assertRaises(ValidationError): VirtualMachine(name='vm1', site=sites[0], cluster=clusters[1]).full_clean() - # VM with cluster site but no direct site should fail - with self.assertRaises(ValidationError): - VirtualMachine(name='vm1', site=None, cluster=clusters[0]).full_clean() + # VM with cluster site but no direct site should have its site set automatically + vm = VirtualMachine(name='vm1', site=None, cluster=clusters[0]) + vm.full_clean() + self.assertEqual(vm.site, sites[0])