From 23972ff09f1abcf1296620955597e3638224686c Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Tue, 7 Jun 2016 15:20:56 +0700 Subject: [PATCH] Adjustment to support add/show domains in pdns v4.x.x --- app/models.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/app/models.py b/app/models.py index 56eb8d0..0830f68 100644 --- a/app/models.py +++ b/app/models.py @@ -7,6 +7,7 @@ import itertools import traceback from datetime import datetime +from distutils.version import StrictVersion from flask.ext.login import AnonymousUserMixin from app import app, db @@ -30,6 +31,11 @@ PDNS_API_KEY = app.config['PDNS_API_KEY'] PDNS_VERSION = app.config['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): def __init__(self): @@ -433,8 +439,7 @@ class Domain(db.Model): headers['X-API-Key'] = PDNS_API_KEY try: 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: # 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)) @@ -468,7 +473,7 @@ class Domain(db.Model): else: # add new domain d = Domain() - d.name = data['name'] + d.name = data['name'].rstrip('.') d.master = str(data['masters']) d.type = data['kind'] d.serial = data['serial'] @@ -492,6 +497,10 @@ class Domain(db.Model): headers = {} 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': post_data = { "name": domain_name, @@ -654,6 +663,14 @@ class Record(object): except: logging.error("Cannot fetch domain's record data from remote powerdns api") 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 def add(self, domain):