Преглед изворни кода

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

jeremystretch пре 3 година
родитељ
комит
7712b81ab9

+ 1 - 0
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
 
 ---
 

+ 3 - 1
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}).'

+ 4 - 3
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])