Skip to content

Commit

Permalink
Merge pull request #2320 from szok/dns-improvements
Browse files Browse the repository at this point in the history
DNS form improvements
  • Loading branch information
szok committed Mar 24, 2016
2 parents 57bb729 + d313613 commit 74b9397
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
3 changes: 0 additions & 3 deletions docs/user/dns.md
Expand Up @@ -15,6 +15,3 @@ Ralph integrated with DNSAAS [PowerDNS](https://github.com/allegro/django-powerd

On the edit page of DataCenterAsset will appear a new tab DNS Edit.
DNS records are matched using DataCenterAssets IP


> "DNS edit is not transaction-safe, since it's communicating with external system through the api."
11 changes: 5 additions & 6 deletions src/ralph/dns/dnsaas.py
Expand Up @@ -55,8 +55,7 @@ def get_dns_records(self, ipaddresses):
)
)
api_results = self.get_api_result(url)
ptr_list = set(
[i['content'] for i in api_results if i['type'] == 'PTR'])
ptrs = set([i['content'] for i in api_results if i['type'] == 'PTR'])

for item in api_results:
if item['type'] in {'A', 'CNAME', 'TXT'}:
Expand All @@ -65,7 +64,7 @@ def get_dns_records(self, ipaddresses):
'name': item['name'],
'type': RecordType.from_name(item['type'].lower()).id,
'content': item['content'],
'ptr': item['name'] in ptr_list and item['type'] == 'A',
'ptr': item['name'] in ptrs and item['type'] == 'A',
'owner': settings.DNSAAS_OWNER
})
return sorted(dns_records, key=lambda x: x['type'])
Expand All @@ -78,7 +77,7 @@ def update_dns_records(self, record):
record: record cleaned data
Returns:
True or False if an error from api
Validation error from API or None if update correct
"""
url = urljoin(
settings.DNSAAS_URL, 'api/records/{}/'.format(record['pk'])
Expand Down Expand Up @@ -126,7 +125,7 @@ def create_dns_records(self, record):
records: Record cleaned data
Returns:
True or False if an error from api
Validation error from API or None if create correct
"""

url = urljoin(settings.DNSAAS_URL, 'api/records/')
Expand Down Expand Up @@ -162,7 +161,7 @@ def delete_dns_records(self, record_id):
record_ids: ID's to delete
Returns:
True or False if an error from api
Validation error from API or None if delete correct
"""
url = urljoin(
settings.DNSAAS_URL, 'api/records/{}/'.format(record_id)
Expand Down
8 changes: 8 additions & 0 deletions src/ralph/dns/forms.py
Expand Up @@ -45,3 +45,11 @@ class DNSRecordForm(forms.Form):
initial=False,
required=False
)

def clean(self):
cleaned_data = super().clean()
if (
cleaned_data.get('ptr', False) and
cleaned_data.get('type', None) != str(RecordType.a.id)
):
raise forms.ValidationError(_('Only A type record can be PTR'))
2 changes: 1 addition & 1 deletion src/ralph/dns/templates/dns/dns_edit.html
Expand Up @@ -4,7 +4,7 @@
{% block view_content %}
<div class="row">
<div class="large-12 columns">
<h1>DNS edit</h1>
<h1>{% trans 'DNS edit' %}</h1>
<table>
<thead>
{% for name, field in forms.0.fields.items %}
Expand Down
6 changes: 4 additions & 2 deletions src/ralph/dns/views.py
Expand Up @@ -35,12 +35,14 @@ def get_forms(self):
return forms

def get(self, request, *args, **kwargs):
kwargs['forms'] = self.get_forms()
if 'forms' not in kwargs:
kwargs['forms'] = self.get_forms()
return super().get(request, *kwargs, **kwargs)

def post(self, request, *args, **kwargs):
forms = self.get_forms()
posted_form = DNSRecordForm(request.POST)
# Find form which request's data belongs to
for i, form in enumerate(forms):
if (
str(form.data.get('pk', '')) ==
Expand Down Expand Up @@ -69,4 +71,4 @@ def post(self, request, *args, **kwargs):
return HttpResponseRedirect('.')

kwargs['forms'] = forms
return super().get(request, *args, **kwargs)
return self.get(request, *args, **kwargs)

0 comments on commit 74b9397

Please sign in to comment.