Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise error when storing an address with a missing PTR domain #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 14 additions & 12 deletions portal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ def _add_dns(self):
content=self.ip,
defaults={'auth': True},
)
new_a.save()

try:
domain = Domain.objects.get(name=self._generate_ptr(domain=True))
Expand All @@ -187,17 +186,16 @@ def _add_dns(self):
if str(self.ip).startswith('44.25.'):
domain = Domain.objects.get(name='25.44.in-addr.arpa')
else:
domain = None
if domain is not None:
new_ptr, created = Record.objects.get_or_create(
domain=domain,
name=self._generate_ptr(),
type='PTR',
defaults={'content': self.fqdn().lower(), 'auth': True},
)
if not created:
new_ptr.content = self.fqdn().lower()
new_ptr.save()
raise

new_ptr, created = Record.objects.get_or_create(
domain=domain,
name=self._generate_ptr(),
type='PTR',
defaults={'content': self.fqdn().lower(), 'auth': True},
)
if not created:
new_ptr.content = self.fqdn().lower()

if self.primary and self.interface != "":
new_cname, created = Record.objects.get_or_create(
Expand All @@ -210,6 +208,10 @@ def _add_dns(self):
new_cname.content = self.fqdn().lower()
new_cname.save()

new_a.save()
new_ptr.save()


def _remove_dns(self):
"""removes old A and CNAME records"""
if self.pk is not None:
Expand Down
14 changes: 13 additions & 1 deletion portal/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.core.urlresolvers import reverse
from django.test import TestCase

from dns.models import Record
from dns.models import Record, Domain
from portal.models import Host, IPAddress
from portal import views

Expand Down Expand Up @@ -46,6 +46,18 @@ def test_update_address(self):
self._assert_address_records(
IPAddress(ip=old_addr, host=address.host, interface=address.interface), want=0)

def test_update_address_missing_ptr_domain(self):
old_addr = "44.25.0.1"
new_addr = "10.25.0.2"
address = IPAddress.objects.get(ip=old_addr)
address.ip = new_addr
self.assertRaises(Domain.DoesNotExist, address.save)
# original address and records are expected to remain intact
self._assert_address_records(
IPAddress(ip=new_addr, host=address.host, interface=address.interface), want=0)
self._assert_address_records(
IPAddress(ip=old_addr, host=address.host, interface=address.interface), want=1)

def test_delete_address(self):
old_addr = "44.25.0.1"
address = IPAddress.objects.get(ip=old_addr)
Expand Down