Commit bcca8478 authored by janis.streib's avatar janis.streib 🦉 Committed by Janis Streib
Browse files

UPD: api for dhcp-leases

parent b0483903
Pipeline #111865 passed with stages
in 8 minutes and 17 seconds
......@@ -264,6 +264,9 @@ class DBMgr(Mgr, DBObject):
def has_range(self, range_key_nr):
return str(range_key_nr) in self.__acd_cache['dns']['m2r']
def has_bcd(self, bcd_key_nr):
return str(bcd_key_nr) in self.__acd_cache['dns']['m2bcd']
def has_permission(self, permission):
return permission in self.__acd_cache['cntl']['xup']
......@@ -316,6 +319,7 @@ class FqdnNameType(object):
return [FqdnNameType(**r) for r in res]
class NetArea(object):
def __init__(self, name, suffix, **args): = name if not name.endswith('/') else name[:-1]
......@@ -352,6 +356,18 @@ class NetArea(object):
raise NotImplementedError()
class BCD(DBObject):
def __init__(self, key_nr):
self.key_nr = key_nr
def get_by_name(cls, db, connection, name):
res = db.execute(connection, "SELECT key_nr from nd_bcd WHERE \"name\" = {name}", {'name': name})
if len(res) == 0:
return None
return cls(**res[0])
class DBNetArea(DBObject, NetArea):
TABLE = 'dns_range'
......@@ -888,7 +904,6 @@ class APIToken(MetaDBObject):
""".format(dict=json.dumps({'name': 'pk', 'old_value':}).replace('{', '{{').replace('}', '}}')).replace(
'{}', '{{}}')
query, {'login_name': self.login_name})
self.token = None
......@@ -100,10 +100,10 @@ class DhcpLeaseDB(object):
data = self.cursor.fetchall()
return data
def get_active_macs(self, dbrange: str) -> list:
def get_active_macs(self, bcd: str) -> list:
"""Function returns all clients that have an IP address currently assigned to it."""
self.cursor.execute("SELECT mac, name, ip FROM dhcp_lease WHERE ip IS NOT NULL AND range = %s ORDER BY ip",
self.cursor.execute("SELECT mac, name, ip FROM dhcp_lease WHERE ip IS NOT NULL AND bcd = %s ORDER BY ip",
data = self.cursor.fetchall()
if len(data) is 0:
return None
from flask import render_template, g, current_app, request, url_for, abort
from flask import jsonify, g, current_app, request, abort
from net_suite.views import login_required, get_db_conn
from . import dhcp_leases
from . import dhcp_leases_model
from net_suite import db
from flask_breadcrumbs import register_breadcrumb
from net_suite.model import DBNetArea
from net_suite.model import BCD
def get_dhcpleaseDB():
if not hasattr(g, 'dhcpleaseDB'):
......@@ -22,59 +21,18 @@ def close_db(error):
@register_breadcrumb(dhcp_leases, '.', 'DHCP Leases')
def overview():
# get the user context from the app
user = request.environ['beaker.session']['login']
ranges = user.get_areas(db=db, connection=get_db_conn())
dhcpDB = get_dhcpleaseDB()
res_ranges = list()
counts = dhcpDB.get_active_mac_counts()
for dhcp_range in ranges:
if != 4:
name = dhcp_range.print_name
count = 0
for c in counts:
if c['range'] == name:
count = c['count']
res_ranges.append({'range': dhcp_range,
'leases': count})
# return the finished template
return render_template('dhcp_leases/overview.html', ranges=res_ranges,
title='Aktive DHCP leases')
def view_net(*args, **kwargs):
return [{'text': request.view_args['range'] + '/' + str(request.view_args['suffix']),
'url': url_for('dhcp_leases.active_leases', range=request.view_args['range'],
@register_breadcrumb(dhcp_leases, '.net', '', dynamic_list_constructor=view_net)
def active_leases(range, suffix):
def active_leases(bcd_name):
dhcpDB = get_dhcpleaseDB()
user = request.environ['beaker.session']['login']
area = None
bcd = BCD.get_by_name(db, get_db_conn(), bcd_name)
if not user.has_permission('dns.admin'):
areas =user.get_areas(db, get_db_conn())
for a in areas:
if == 4 and == range and a.suffix == int(suffix):
area = a
area = DBNetArea.get_by_name(db=db, connection=get_db_conn(), name=range, suffix=suffix)
if not == 4:
area = None
# 1. Create the list of active dhcp leases in the different ranges
if area is None:
if not user.has_bcd(bcd.key_nr):
bcd = None
if bcd is None:
return abort(404)
leases = dhcpDB.get_active_macs(range + '/' + str(suffix))
return render_template('dhcp_leases/active_leases.html', title='Aktive DHCP leases: {}/{}'.format(range, suffix),
leases=leases, area=area,
subtitle='<code>{}</code>, {} aktive Leases'.format(, len(leases) if leases else '0'))
leases = dhcpDB.get_active_macs(bcd_name)
return jsonify(leases)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment