Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
- name: Checkout code
uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98

- name: Run the ruff code checker
uses: astral-sh/ruff-action@b79a7ac759a34911be97ecdf3d744c213336da60

- name: Check conformance to Ruff formatting standards
uses: astral-sh/ruff-action@8c228c72ff504d587be6b6cfbcf074768fdd6883
uses: astral-sh/ruff-action@b79a7ac759a34911be97ecdf3d744c213336da60
with:
options: "format --check --diff"

- name: Run the ruff code checker
uses: astral-sh/ruff-action@8c228c72ff504d587be6b6cfbcf074768fdd6883
args: "format --check --diff"
8 changes: 5 additions & 3 deletions examples/custom-scripts/AXFR_Importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ def run(self, data, commit):
)
return

keyring = tsigkeyring.from_text({
tsig_key_name: tsig_key,
})
keyring = tsigkeyring.from_text(
{
tsig_key_name: tsig_key,
}
)

nameserver = data.get("nameserver")
relativize = data.get("relativize_names")
Expand Down
3 changes: 1 addition & 2 deletions netbox_dns/management/commands/cleanup_rrset_ttl.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def _cleanup_rrset_ttl(self, **options):
self.stdout.write("Cleaning up diverging RRset TTL values")

ttl_records = (
Record.objects
.filter(ttl__isnull=False)
Record.objects.filter(ttl__isnull=False)
.exclude(type=RecordTypeChoices.SOA)
.exclude(type=RecordTypeChoices.PTR, managed=True)
)
Expand Down
3 changes: 1 addition & 2 deletions netbox_dns/management/commands/dns_check_zone_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def handle(self, *model_names, **options):
name__endswith=record.fqdn.rstrip(".")
).last()
new_name = (
dns_name
.from_text(record.fqdn)
dns_name.from_text(record.fqdn)
.relativize(dns_name.from_text(new_zone.name))
.to_text()
)
Expand Down
16 changes: 10 additions & 6 deletions netbox_dns/models/nameserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,20 @@ def clean(self, *args, **kwargs):
try:
self.name = normalize_name(self.name)
except NameFormatError as exc:
raise ValidationError({
"name": str(exc),
})
raise ValidationError(
{
"name": str(exc),
}
)

try:
validate_fqdn(self.name)
except ValidationError as exc:
raise ValidationError({
"name": exc,
})
raise ValidationError(
{
"name": exc,
}
)

super().clean(*args, **kwargs)

Expand Down
160 changes: 87 additions & 73 deletions netbox_dns/models/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ def record_data_from_ip_address(ip_address, zone):

data = {
"name": (
dns_name
.from_text(ip_address.dns_name)
dns_name.from_text(ip_address.dns_name)
.relativize(dns_name.from_text(zone.name))
.to_text()
),
Expand Down Expand Up @@ -343,8 +342,7 @@ def rfc2317_ptr_cname_name(self):
def ptr_zone(self):
if self.type == RecordTypeChoices.A:
ptr_zone = (
self.zone.view.zones
.filter(
self.zone.view.zones.filter(
rfc2317_prefix__net_contains=self.value,
)
.order_by("rfc2317_prefix__net_mask_length")
Expand All @@ -355,8 +353,7 @@ def ptr_zone(self):
return ptr_zone

return (
self.zone.view.zones
.filter(arpa_network__net_contains=self.value)
self.zone.view.zones.filter(arpa_network__net_contains=self.value)
.order_by("arpa_network__net_mask_length")
.last()
)
Expand Down Expand Up @@ -439,8 +436,7 @@ def update_ptr_record(self, update_rfc2317_cname=True, save_zone_serial=True):
ptr_name = self.rfc2317_ptr_name
else:
ptr_name = (
dns_name
.from_text(ipaddress.ip_address(self.value).reverse_pointer)
dns_name.from_text(ipaddress.ip_address(self.value).reverse_pointer)
.relativize(dns_name.from_text(ptr_zone.name))
.to_text()
)
Expand Down Expand Up @@ -562,8 +558,9 @@ def remove_from_rfc2317_cname_record(self, save_zone_serial=True):
def update_rfc2317_cname_record(self, save_zone_serial=True):
if self.zone.rfc2317_parent_managed:
cname_name = (
dns_name
.from_text(ipaddress.ip_address(self.ip_address).reverse_pointer)
dns_name.from_text(
ipaddress.ip_address(self.ip_address).reverse_pointer
)
.relativize(dns_name.from_text(self.zone.rfc2317_parent_zone.name))
.to_text()
)
Expand All @@ -573,8 +570,9 @@ def update_rfc2317_cname_record(self, save_zone_serial=True):
self.rfc2317_cname_record.zone = self.zone.rfc2317_parent_zone
self.rfc2317_cname_record.value = self.fqdn
self.rfc2317_cname_record.ttl = min_ttl(
self.rfc2317_cname_record.rfc2317_ptr_records
.exclude(pk=self.pk)
self.rfc2317_cname_record.rfc2317_ptr_records.exclude(
pk=self.pk
)
.aggregate(Min("ttl"))
.get("ttl__min"),
self.ttl,
Expand All @@ -594,8 +592,7 @@ def update_rfc2317_cname_record(self, save_zone_serial=True):

if rfc2317_cname_record is not None:
rfc2317_cname_record.ttl = min_ttl(
rfc2317_cname_record.rfc2317_ptr_records
.exclude(pk=self.pk)
rfc2317_cname_record.rfc2317_ptr_records.exclude(pk=self.pk)
.aggregate(Min("ttl"))
.get("ttl__min"),
self.ttl,
Expand Down Expand Up @@ -676,11 +673,13 @@ def update_fqdn(self, zone=None):
fqdn = dns_name.from_text(self.name, origin=_zone)

if not fqdn.is_subdomain(_zone):
raise ValidationError({
"name": _("{name} is not a name in {zone}").format(
name=self.name, zone=zone.name
),
})
raise ValidationError(
{
"name": _("{name} is not a name in {zone}").format(
name=self.name, zone=zone.name
),
}
)

_zone.to_unicode()
name.to_unicode()
Expand All @@ -698,9 +697,11 @@ def validate_name(self, new_zone=None):
self.update_fqdn(zone=new_zone)

except dns.exception.DNSException as exc:
raise ValidationError({
"name": str(exc),
})
raise ValidationError(
{
"name": str(exc),
}
)

if self.type not in get_plugin_config(
"netbox_dns", "tolerate_non_rfc1035_types", default=[]
Expand All @@ -718,9 +719,11 @@ def validate_name(self, new_zone=None):
),
)
except ValidationError as exc:
raise ValidationError({
"name": exc,
})
raise ValidationError(
{
"name": exc,
}
)

if get_plugin_config("netbox_dns", "enforce_zone_cut_checking"):
try:
Expand Down Expand Up @@ -765,13 +768,15 @@ def check_unique_record(self, new_zone=None):
):
return

raise ValidationError({
"value": _(
"There is already an active {type} record for name {name} in zone {zone} with value {value}."
).format(
type=self.type, name=self.name, zone=self.zone, value=self.value
)
})
raise ValidationError(
{
"value": _(
"There is already an active {type} record for name {name} in zone {zone} with value {value}."
).format(
type=self.type, name=self.name, zone=self.zone, value=self.value
)
}
)

@property
def absolute_value(self):
Expand Down Expand Up @@ -859,8 +864,7 @@ def check_unique_rrset_ttl(self):
return

records = (
self.zone.records
.filter(
self.zone.records.filter(
name=self.name,
type=self.type,
)
Expand All @@ -880,16 +884,18 @@ def check_unique_rrset_ttl(self):
self.ttl = conflicting_ttls.pop()
return

raise ValidationError({
"ttl": _(
"There is at least one active {type} record for name {name} in zone {zone} and TTL is different ({ttls})."
).format(
type=self.type,
name=self.name,
zone=self.zone,
ttls=", ".join(str(ttl) for ttl in conflicting_ttls),
)
})
raise ValidationError(
{
"ttl": _(
"There is at least one active {type} record for name {name} in zone {zone} and TTL is different ({ttls})."
).format(
type=self.type,
name=self.name,
zone=self.zone,
ttls=", ".join(str(ttl) for ttl in conflicting_ttls),
)
}
)

check_unique_rrset_ttl.alters_data = True

Expand All @@ -907,8 +913,7 @@ def update_rrset_ttl(self, ttl=None):
ttl = self.ttl

records = (
self.zone.records
.filter(
self.zone.records.filter(
name=self.name,
type=self.type,
)
Expand Down Expand Up @@ -966,8 +971,7 @@ def clean(self, *args, new_zone=None, **kwargs):
)

if (
ptr_cname_zone.records
.filter(
ptr_cname_zone.records.filter(
name=ptr_cname_name,
active=True,
)
Expand All @@ -978,44 +982,54 @@ def clean(self, *args, new_zone=None, **kwargs):
.exclude(type=RecordTypeChoices.NSEC)
.exists()
):
raise ValidationError({
"value": _(
"There is already an active record for name {name} in zone {zone}, RFC2317 CNAME is not allowed."
).format(name=ptr_cname_name, zone=ptr_cname_zone)
})
raise ValidationError(
{
"value": _(
"There is already an active record for name {name} in zone {zone}, RFC2317 CNAME is not allowed."
).format(name=ptr_cname_name, zone=ptr_cname_zone)
}
)

if self.type == RecordTypeChoices.SOA and self.name != "@":
raise ValidationError({
"name": _(
"SOA records are only allowed with name @ and are created automatically by NetBox DNS"
)
})
raise ValidationError(
{
"name": _(
"SOA records are only allowed with name @ and are created automatically by NetBox DNS"
)
}
)

if self.type == RecordTypeChoices.CNAME:
if records.exclude(type=RecordTypeChoices.NSEC).exists():
raise ValidationError({
"type": _(
"There is already an active record for name {name} in zone {zone}, CNAME is not allowed."
).format(name=self.name, zone=self.zone)
})
raise ValidationError(
{
"type": _(
"There is already an active record for name {name} in zone {zone}, CNAME is not allowed."
).format(name=self.name, zone=self.zone)
}
)

elif (
records.filter(type=RecordTypeChoices.CNAME).exists()
and self.type != RecordTypeChoices.NSEC
):
raise ValidationError({
"type": _(
"There is already an active CNAME record for name {name} in zone {zone}, no other record allowed."
).format(name=self.name, zone=self.zone)
})
raise ValidationError(
{
"type": _(
"There is already an active CNAME record for name {name} in zone {zone}, no other record allowed."
).format(name=self.name, zone=self.zone)
}
)

elif self.type in RecordTypeChoices.SINGLETONS:
if records.filter(type=self.type).exists():
raise ValidationError({
"type": _(
"There is already an active {type} record for name {name} in zone {zone}, more than one are not allowed."
).format(type=self.type, name=self.name, zone=self.zone)
})
raise ValidationError(
{
"type": _(
"There is already an active {type} record for name {name} in zone {zone}, more than one are not allowed."
).format(type=self.type, name=self.name, zone=self.zone)
}
)

super().clean(*args, **kwargs)

Expand Down
20 changes: 12 additions & 8 deletions netbox_dns/models/record_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,11 @@ def validate_name(self):
),
)
except ValidationError as exc:
raise ValidationError({
"record_name": exc,
})
raise ValidationError(
{
"record_name": exc,
}
)

def validate_value(self):
try:
Expand All @@ -150,11 +152,13 @@ def create_record(self, zone):
try:
record = Record.objects.create(**record_data)
except ValidationError as exc:
raise ValidationError({
None: _(
"Error while processing record template {template}: {error}"
).format(template=self, error=exc.messages[0])
})
raise ValidationError(
{
None: _(
"Error while processing record template {template}: {error}"
).format(template=self, error=exc.messages[0])
}
)

if tags := self.tags.all():
record.tags.set(tags)
Expand Down
Loading