diff --git a/powerdnsadmin/models/server.py b/powerdnsadmin/models/server.py index 1c1deba..2034168 100644 --- a/powerdnsadmin/models/server.py +++ b/powerdnsadmin/models/server.py @@ -61,3 +61,26 @@ class Server(object): "Can not get server statistics. DETAIL: {0}".format(e)) current_app.logger.debug(traceback.format_exc()) return [] + + def global_search(self, object_type='all', query=''): + """ + Search zone/record/comment directly from PDNS API + """ + headers = {} + headers['X-API-Key'] = self.PDNS_API_KEY + + try: + jdata = utils.fetch_json(urljoin( + self.PDNS_STATS_URL, self.API_EXTENDED_URL + + '/servers/{}/search-data?object_type={}&q={}'.format( + self.server_id, object_type, query)), + headers=headers, + timeout=int( + Setting().get('pdns_api_timeout')), + method='GET') + return jdata + except Exception as e: + current_app.logger.error( + "Can not make global search. DETAIL: {0}".format(e)) + current_app.logger.debug(traceback.format_exc()) + return [] diff --git a/powerdnsadmin/routes/admin.py b/powerdnsadmin/routes/admin.py index 41ae62c..7e31c28 100644 --- a/powerdnsadmin/routes/admin.py +++ b/powerdnsadmin/routes/admin.py @@ -1015,3 +1015,44 @@ def delete_template(template): current_app.logger.debug(traceback.format_exc()) abort(500) return redirect(url_for('admin.templates')) + + +@admin_bp.route('/global-search', methods=['GET']) +@login_required +@operator_role_required +def global_search(): + if request.method == 'GET': + domains = [] + records = [] + comments = [] + + query = request.args.get('q') + if query: + server = Server(server_id='localhost') + results = server.global_search(object_type='all', query=query) + + # Format the search result + for result in results: + if result['object_type'] == 'zone': + # Remove the dot at the end of string + result['name'] = result['name'][:-1] + domains.append(result) + elif result['object_type'] == 'record': + # Remove the dot at the end of string + result['name'] = result['name'][:-1] + result['zone_id'] = result['zone_id'][:-1] + records.append(result) + elif result['object_type'] == 'comment': + # Get the actual record name, exclude the domain part + result['name'] = result['name'].replace(result['zone_id'], '') + if result['name']: + result['name'] = result['name'][:-1] + else: + result['name'] = '@' + # Remove the dot at the end of string + result['zone_id'] = result['zone_id'][:-1] + comments.append(result) + else: + pass + + return render_template('admin_global_search.html', domains=domains, records=records, comments=comments) diff --git a/powerdnsadmin/templates/admin_global_search.html b/powerdnsadmin/templates/admin_global_search.html new file mode 100644 index 0000000..d0fe231 --- /dev/null +++ b/powerdnsadmin/templates/admin_global_search.html @@ -0,0 +1,189 @@ +{% extends "base.html" %} +{% set active_page = "admin_global_search" %} +{% block title %} +Global Search - {{ SITE_NAME }} +{% endblock %} {% block dashboard_stat %} + +
+

+ Global Search Search for domains, records and comments directly from PDNS API +

+ +
+{% endblock %} {% block content %} +
+
+
+
+ +
+
+ +
+ +
+
+
+
+

Hints: The * character can be used in your keyword as a wildcard character and the ? character can be used as a wildcard for a + single character.

+
+ +
+
+
+
+
+
+
+

Domains ({{ domains|length }})

+
+
+ + + + + + + + {% for domain in domains %} + + + + {% endfor %} + +
Domain
+ {{ domain['name'] }} +
+
+ +
+ +
+ +
+ +
+
+
+
+

Records ({{ records|length }})

+
+
+ + + + + + + + + + + + {% for record in records %} + + + + + + + + {% endfor %} + +
NameTypeStatusTTLData
+ {{ record['name'] }} + {{ record['type'] }}{{ 'Disabled' if record['disabled'] else 'Active' }}{{ record['ttl'] }}{{ record['content'] }}
+
+ +
+ +
+ +
+ +
+
+
+
+

Comments ({{ comments|length }})

+
+
+ + + + + + + + + + {% for comment in comments %} + + + + + + {% endfor %} + +
CommentRecordDomain
{{ comment['content'] }}{{ comment['name'] }} + {{ comment['zone_id'] }} +
+
+ +
+ +
+ +
+
+{% endblock %} +{% block extrascripts %} + + + + + +{% endblock %} diff --git a/powerdnsadmin/templates/base.html b/powerdnsadmin/templates/base.html index 44a3224..7827048 100644 --- a/powerdnsadmin/templates/base.html +++ b/powerdnsadmin/templates/base.html @@ -106,6 +106,9 @@
  • PDNS
  • +
  • + Global Search +
  • History