Adjustment to support add/show domains in pdns v4.x.x

This commit is contained in:
Khanh Ngo 2016-06-07 15:20:56 +07:00
parent 382807fdd7
commit 23972ff09f

View File

@ -7,6 +7,7 @@ import itertools
import traceback import traceback
from datetime import datetime from datetime import datetime
from distutils.version import StrictVersion
from flask.ext.login import AnonymousUserMixin from flask.ext.login import AnonymousUserMixin
from app import app, db from app import app, db
@ -30,6 +31,11 @@ PDNS_API_KEY = app.config['PDNS_API_KEY']
PDNS_VERSION = app.config['PDNS_VERSION'] PDNS_VERSION = app.config['PDNS_VERSION']
API_EXTENDED_URL = utils.pdns_api_extended_uri(PDNS_VERSION) API_EXTENDED_URL = utils.pdns_api_extended_uri(PDNS_VERSION)
# Flag for pdns v4.x.x
# TODO: Find another way to do this
if StrictVersion(PDNS_VERSION) >= StrictVersion('4.0.0'):
NEW_SCHEMA = True
class Anonymous(AnonymousUserMixin): class Anonymous(AnonymousUserMixin):
def __init__(self): def __init__(self):
@ -433,8 +439,7 @@ class Domain(db.Model):
headers['X-API-Key'] = PDNS_API_KEY headers['X-API-Key'] = PDNS_API_KEY
try: try:
jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones'), headers=headers) jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones'), headers=headers)
list_jdomain = [d['name'] for d in jdata] list_jdomain = [d['name'].rstrip('.') for d in jdata]
try: try:
# domains should remove from db since it doesn't exist in powerdns anymore # domains should remove from db since it doesn't exist in powerdns anymore
should_removed_db_domain = list(set(list_db_domain).difference(list_jdomain)) should_removed_db_domain = list(set(list_db_domain).difference(list_jdomain))
@ -468,7 +473,7 @@ class Domain(db.Model):
else: else:
# add new domain # add new domain
d = Domain() d = Domain()
d.name = data['name'] d.name = data['name'].rstrip('.')
d.master = str(data['masters']) d.master = str(data['masters'])
d.type = data['kind'] d.type = data['kind']
d.serial = data['serial'] d.serial = data['serial']
@ -492,6 +497,10 @@ class Domain(db.Model):
headers = {} headers = {}
headers['X-API-Key'] = PDNS_API_KEY headers['X-API-Key'] = PDNS_API_KEY
if NEW_SCHEMA:
domain_name = domain_name + '.'
domain_ns = [ns + '.' for ns in domain_ns]
if soa_edit_api == 'OFF': if soa_edit_api == 'OFF':
post_data = { post_data = {
"name": domain_name, "name": domain_name,
@ -654,6 +663,14 @@ class Record(object):
except: except:
logging.error("Cannot fetch domain's record data from remote powerdns api") logging.error("Cannot fetch domain's record data from remote powerdns api")
return False return False
if NEW_SCHEMA:
rrsets = jdata['rrsets']
for rrset in rrsets:
rrset['content'] = rrset['records'][0]['content']
rrset['disabled'] = rrset['records'][0]['disabled']
return {'records': rrsets}
return jdata return jdata
def add(self, domain): def add(self, domain):