From 382807fdd71bd6c7b0ab1dcb9d0d8170e2f9d886 Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Tue, 7 Jun 2016 13:50:31 +0700 Subject: [PATCH] Adjustment to support new api url format in pdns 4.x.x --- app/lib/utils.py | 10 ++++++++++ app/models.py | 30 ++++++++++++++++-------------- config_template.py | 1 + 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/app/lib/utils.py b/app/lib/utils.py index f43ae17..bc57c94 100644 --- a/app/lib/utils.py +++ b/app/lib/utils.py @@ -4,6 +4,7 @@ import json import requests import urlparse from app import app +from distutils.version import StrictVersion if 'TIMEOUT' in app.config.keys(): TIMEOUT = app.config['TIMEOUT'] @@ -132,3 +133,12 @@ def display_time(amount, units='s', remove_seconds=True): return final_string[:final_string.rfind(' ')] return final_string + +def pdns_api_extended_uri(version): + """ + Check the pdns version + """ + if StrictVersion(version) >= StrictVersion('4.0.0'): + return "/api/v1" + else: + return "" diff --git a/app/models.py b/app/models.py index 9d0fbc8..56eb8d0 100644 --- a/app/models.py +++ b/app/models.py @@ -27,6 +27,8 @@ else: PDNS_STATS_URL = app.config['PDNS_STATS_URL'] PDNS_API_KEY = app.config['PDNS_API_KEY'] +PDNS_VERSION = app.config['PDNS_VERSION'] +API_EXTENDED_URL = utils.pdns_api_extended_uri(PDNS_VERSION) class Anonymous(AnonymousUserMixin): @@ -410,7 +412,7 @@ class Domain(db.Model): """ headers = {} headers['X-API-Key'] = PDNS_API_KEY - jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, '/servers/localhost/zones'), headers=headers) + jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones'), headers=headers) return jdata def get_id_by_name(self, name): @@ -430,7 +432,7 @@ class Domain(db.Model): headers = {} headers['X-API-Key'] = PDNS_API_KEY try: - jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_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] try: @@ -507,7 +509,7 @@ class Domain(db.Model): } try: - jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, '/servers/localhost/zones'), headers=headers, method='POST', data=post_data) + jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones'), headers=headers, method='POST', data=post_data) if 'error' in jdata.keys(): logging.error(jdata['error']) return {'status': 'error', 'msg': jdata['error']} @@ -528,7 +530,7 @@ class Domain(db.Model): headers = {} headers['X-API-Key'] = PDNS_API_KEY try: - jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, '/servers/localhost/zones/%s' % domain_name), headers=headers, method='DELETE') + jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones/%s' % domain_name), headers=headers, method='DELETE') logging.info('Delete domain %s successfully' % domain_name) return {'status': 'ok', 'msg': 'Delete domain successfully'} except Exception, e: @@ -587,7 +589,7 @@ class Domain(db.Model): headers = {} headers['X-API-Key'] = PDNS_API_KEY try: - jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, '/servers/localhost/zones/%s/axfr-retrieve' % domain), headers=headers, method='PUT') + jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones/%s/axfr-retrieve' % domain), headers=headers, method='PUT') return {'status': 'ok', 'msg': 'Update from Master successfully'} except: return {'status': 'error', 'msg': 'There was something wrong, please contact administrator'} @@ -603,7 +605,7 @@ class Domain(db.Model): headers = {} headers['X-API-Key'] = PDNS_API_KEY try: - jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, '/servers/localhost/zones/%s/cryptokeys' % domain.name), headers=headers, method='GET') + jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones/%s/cryptokeys' % domain.name), headers=headers, method='GET') if 'error' in jdata: return {'status': 'error', 'msg': 'DNSSEC is not enabled for this domain'} else: @@ -648,7 +650,7 @@ class Record(object): headers = {} headers['X-API-Key'] = PDNS_API_KEY try: - jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, '/servers/localhost/zones/%s' % domain), headers=headers) + jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones/%s' % domain), headers=headers) except: logging.error("Cannot fetch domain's record data from remote powerdns api") return False @@ -688,7 +690,7 @@ class Record(object): ] } try: - jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, '/servers/localhost/zones/%s' % domain), headers=headers, method='PATCH', data=data) + jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones/%s' % domain), headers=headers, method='PATCH', data=data) logging.debug(jdata) return {'status': 'ok', 'msg': 'Record was added successfully'} except Exception, e: @@ -782,10 +784,10 @@ class Record(object): try: headers = {} headers['X-API-Key'] = PDNS_API_KEY - jdata1 = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, '/servers/localhost/zones/%s' % domain), headers=headers, method='PATCH', data=postdata_for_delete) + jdata1 = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones/%s' % domain), headers=headers, method='PATCH', data=postdata_for_delete) #logging.debug(jdata1) - jdata2 = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, '/servers/localhost/zones/%s' % domain), headers=headers, method='PATCH', data=postdata_for_new) + jdata2 = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones/%s' % domain), headers=headers, method='PATCH', data=postdata_for_new) #logging.debug(jdata2) if 'error' in jdata2.keys(): @@ -821,7 +823,7 @@ class Record(object): ] } try: - jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, '/servers/localhost/zones/%s' % domain), headers=headers, method='PATCH', data=data) + jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones/%s' % domain), headers=headers, method='PATCH', data=data) logging.debug(jdata) return {'status': 'ok', 'msg': 'Record was removed successfully'} except: @@ -853,7 +855,7 @@ class Server(object): headers['X-API-Key'] = PDNS_API_KEY try: - jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, '/servers/%s/config' % self.server_id), headers=headers, method='GET') + jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/%s/config' % self.server_id), headers=headers, method='GET') return jdata except: logging.error("Can not get server configuration.") @@ -866,9 +868,9 @@ class Server(object): """ headers = {} headers['X-API-Key'] = PDNS_API_KEY - + try: - jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, '/servers/%s/statistics' % self.server_id), headers=headers, method='GET') + jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/%s/statistics' % self.server_id), headers=headers, method='GET') return jdata except: logging.error("Can not get server statistics.") diff --git a/config_template.py b/config_template.py index c8c205d..ad50d03 100644 --- a/config_template.py +++ b/config_template.py @@ -40,6 +40,7 @@ SIGNUP_ENABLED = True # POWERDNS CONFIG PDNS_STATS_URL = 'http://172.16.214.131:8081/' PDNS_API_KEY = 'you never know' +PDNS_VERSION = '3.4.7' # RECORDS ALLOWED TO EDIT RECORDS_ALLOW_EDIT = ['A', 'AAAA', 'CNAME', 'SPF', 'PTR', 'MX', 'TXT']