Просмотр исходного кода

Closes #1943: Relax uniqueness constraint on cluster names

jeremystretch 4 лет назад
Родитель
Сommit
8375995680

+ 1 - 1
docs/models/virtualization/cluster.md

@@ -1,5 +1,5 @@
 # Clusters
 
-A cluster is a logical grouping of physical resources within which virtual machines run. A cluster must be assigned a type (technological classification), and may optionally be assigned to a cluster group, site, and/or tenant.
+A cluster is a logical grouping of physical resources within which virtual machines run. A cluster must be assigned a type (technological classification), and may optionally be assigned to a cluster group, site, and/or tenant. Each cluster must have a unique name within its assigned group and/or site, if any.
 
 Physical devices may be associated with clusters as hosts. This allows users to track on which host(s) a particular virtual machine may reside. However, NetBox does not support pinning a specific VM within a cluster to a particular host device.

+ 1 - 0
docs/release-notes/version-3.1.md

@@ -10,6 +10,7 @@
 ### Enhancements
 
 * [#1337](https://github.com/netbox-community/netbox/issues/1337) - Add WWN field to interfaces
+* [#1943](https://github.com/netbox-community/netbox/issues/1943) - Relax uniqueness constraint on cluster names
 * [#3839](https://github.com/netbox-community/netbox/issues/3839) - Add `airflow` field for devices types and devices
 * [#6711](https://github.com/netbox-community/netbox/issues/6711) - Add `longtext` custom field type with Markdown support
 * [#6715](https://github.com/netbox-community/netbox/issues/6715) - Add tenant assignment for cables

+ 2 - 2
netbox/virtualization/api/serializers.py

@@ -44,9 +44,9 @@ class ClusterGroupSerializer(OrganizationalModelSerializer):
 class ClusterSerializer(PrimaryModelSerializer):
     url = serializers.HyperlinkedIdentityField(view_name='virtualization-api:cluster-detail')
     type = NestedClusterTypeSerializer()
-    group = NestedClusterGroupSerializer(required=False, allow_null=True)
+    group = NestedClusterGroupSerializer(required=False, allow_null=True, default=None)
     tenant = NestedTenantSerializer(required=False, allow_null=True)
-    site = NestedSiteSerializer(required=False, allow_null=True)
+    site = NestedSiteSerializer(required=False, allow_null=True, default=None)
     device_count = serializers.IntegerField(read_only=True)
     virtualmachine_count = serializers.IntegerField(read_only=True)
 

+ 21 - 0
netbox/virtualization/migrations/0024_cluster_relax_uniqueness.py

@@ -0,0 +1,21 @@
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('dcim', '0136_device_airflow'),
+        ('virtualization', '0023_virtualmachine_natural_ordering'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='cluster',
+            name='name',
+            field=models.CharField(max_length=100),
+        ),
+        migrations.AlterUniqueTogether(
+            name='cluster',
+            unique_together={('site', 'name'), ('group', 'name')},
+        ),
+    ]

+ 5 - 2
netbox/virtualization/models.py

@@ -115,8 +115,7 @@ class Cluster(PrimaryModel):
     A cluster of VirtualMachines. Each Cluster may optionally be associated with one or more Devices.
     """
     name = models.CharField(
-        max_length=100,
-        unique=True
+        max_length=100
     )
     type = models.ForeignKey(
         to=ClusterType,
@@ -167,6 +166,10 @@ class Cluster(PrimaryModel):
 
     class Meta:
         ordering = ['name']
+        unique_together = (
+            ('group', 'name'),
+            ('site', 'name'),
+        )
 
     def __str__(self):
         return self.name