Skip to content

Commit

Permalink
Closes #10374: Require unique tenant names & slugs per group (not glo…
Browse files Browse the repository at this point in the history
…bally)
  • Loading branch information
jeremystretch committed Mar 1, 2023
1 parent 5517963 commit 8a08d36
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/models/tenancy/tenant.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ A tenant represents a discrete grouping of resources used for administrative pur

### Name

A unique human-friendly name.
A human-friendly name, unique to the assigned group.

### Slug

A unique URL-friendly identifier. (This value can be used for filtering.)
A URL-friendly identifier, unique to the assigned group. (This value can be used for filtering.)

### Group

Expand Down
1 change: 1 addition & 0 deletions docs/release-notes/version-3.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ A new ASN range model has been introduced to facilitate the provisioning of new

* [#9073](https://github.com/netbox-community/netbox/issues/9073) - Enable syncing config context data from remote sources
* [#9653](https://github.com/netbox-community/netbox/issues/9653) - Enable setting a default platform for device types
* [#10374](https://github.com/netbox-community/netbox/issues/10374) - Require unique tenant names & slugs per group (not globally)
* [#10729](https://github.com/netbox-community/netbox/issues/10729) - Add date & time custom field type
* [#11254](https://github.com/netbox-community/netbox/issues/11254) - Introduce the `X-Request-ID` HTTP header to annotate the unique ID of each request for change logging
* [#11440](https://github.com/netbox-community/netbox/issues/11440) - Add an `enabled` field for device type interfaces
Expand Down
39 changes: 39 additions & 0 deletions netbox/tenancy/migrations/0010_tenant_relax_uniqueness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by Django 4.1.7 on 2023-03-01 01:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('tenancy', '0009_standardize_description_comments'),
]

operations = [
migrations.AlterField(
model_name='tenant',
name='name',
field=models.CharField(max_length=100),
),
migrations.AlterField(
model_name='tenant',
name='slug',
field=models.SlugField(max_length=100),
),
migrations.AddConstraint(
model_name='tenant',
constraint=models.UniqueConstraint(fields=('group', 'name'), name='tenancy_tenant_unique_group_name', violation_error_message='Tenant name must be unique per group.'),
),
migrations.AddConstraint(
model_name='tenant',
constraint=models.UniqueConstraint(condition=models.Q(('group__isnull', True)), fields=('name',), name='tenancy_tenant_unique_name'),
),
migrations.AddConstraint(
model_name='tenant',
constraint=models.UniqueConstraint(fields=('group', 'slug'), name='tenancy_tenant_unique_group_slug', violation_error_message='Tenant slug must be unique per group.'),
),
migrations.AddConstraint(
model_name='tenant',
constraint=models.UniqueConstraint(condition=models.Q(('group__isnull', True)), fields=('slug',), name='tenancy_tenant_unique_slug'),
),
]
29 changes: 25 additions & 4 deletions netbox/tenancy/models/tenants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.db.models import Q
from django.urls import reverse

from netbox.models import NestedGroupModel, PrimaryModel
Expand Down Expand Up @@ -36,12 +37,10 @@ class Tenant(PrimaryModel):
department.
"""
name = models.CharField(
max_length=100,
unique=True
max_length=100
)
slug = models.SlugField(
max_length=100,
unique=True
max_length=100
)
group = models.ForeignKey(
to='tenancy.TenantGroup',
Expand All @@ -62,6 +61,28 @@ class Tenant(PrimaryModel):

class Meta:
ordering = ['name']
constraints = (
models.UniqueConstraint(
fields=('group', 'name'),
name='%(app_label)s_%(class)s_unique_group_name',
violation_error_message="Tenant name must be unique per group."
),
models.UniqueConstraint(
fields=('name',),
name='%(app_label)s_%(class)s_unique_name',
condition=Q(group__isnull=True)
),
models.UniqueConstraint(
fields=('group', 'slug'),
name='%(app_label)s_%(class)s_unique_group_slug',
violation_error_message="Tenant slug must be unique per group."
),
models.UniqueConstraint(
fields=('slug',),
name='%(app_label)s_%(class)s_unique_slug',
condition=Q(group__isnull=True)
),
)

def __str__(self):
return self.name
Expand Down

0 comments on commit 8a08d36

Please sign in to comment.