Skip to content

Commit

Permalink
Check the type of a record when testing for uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
peteeckel committed Oct 17, 2023
1 parent 7683646 commit 62c3769
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion netbox_dns/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,13 +969,14 @@ def check_unique(self):
records = Record.objects.filter(
zone=self.zone,
name=self.name,
type=self.type,
value=self.value,
status__in=Record.ACTIVE_STATUS_LIST,
)
if len(records):
raise ValidationError(
{
"value": f"There is already an active record for name {self.name} in zone {self.zone} with value {self.value}."
"value": f"There is already an active {self.type} record for name {self.name} in zone {self.zone} with value {self.value}."
}
) from None

Expand Down
23 changes: 23 additions & 0 deletions netbox_dns/tests/record/test_uniqueness.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,29 @@ def test_create_duplicate_records_ok(self):
)
f_record.save()

@override_settings(
PLUGINS_CONFIG={
"netbox_dns": {
"enforce_unique_records": True,
}
}
)
def test_create_different_type_records_ok(self):
f_zone = self.zones[0]

records = [
{"name": "test1", "type": RecordTypeChoices.A, "value": "10.0.1.42"},
{"name": "test1", "type": RecordTypeChoices.TXT, "value": "10.0.1.42"},
]

for record in records:
f_record = Record(
zone=f_zone,
**record,
**self.record_data,
)
f_record.save()

@override_settings(
PLUGINS_CONFIG={
"netbox_dns": {
Expand Down

0 comments on commit 62c3769

Please sign in to comment.