Development
This commit is contained in:
parent
eed1d295dc
commit
d9f3bd8289
45 changed files with 635 additions and 666 deletions
|
|
@ -1,3 +1,4 @@
|
|||
from pathlib import Path
|
||||
import copy
|
||||
import ipaddress
|
||||
|
||||
|
|
@ -7,10 +8,9 @@ from config_utils import load_config, save_config_with_snapshot, verify_config_h
|
|||
import sanitize
|
||||
import validation as validate
|
||||
|
||||
bp = Blueprint('hostoverrides', __name__)
|
||||
|
||||
VIEW = '/view/view_hostoverrides'
|
||||
_PAGE = Path(__file__).parent.name
|
||||
|
||||
bp = Blueprint(_PAGE, __name__)
|
||||
|
||||
def _vlan_networks(cfg):
|
||||
nets = []
|
||||
|
|
@ -48,23 +48,23 @@ def _hash_ok():
|
|||
return True
|
||||
|
||||
|
||||
@bp.route('/action/add_host_override', methods=['POST'])
|
||||
@bp.route('/action/hostoverrides/addoverride_add', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def add_host_override():
|
||||
def addoverride_add():
|
||||
description = sanitize.text(request.form.get('description', ''))
|
||||
host = validate.domainname(request.form.get('host', ''))
|
||||
ip = sanitize.ip(request.form.get('ip', ''))
|
||||
|
||||
if not host or not ip:
|
||||
flash('Hostname and IP address are required.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
cfg = load_config()
|
||||
if not _ip_in_vlan(ip, cfg):
|
||||
flash('IP address does not fall within any configured VLAN subnet.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
entry = {'description': description, 'host': host, 'ip': ip, 'enabled': True}
|
||||
cfg.setdefault('host_overrides', []).append(entry)
|
||||
|
|
@ -72,7 +72,7 @@ def add_host_override():
|
|||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
flash(save_config_with_snapshot(
|
||||
cfg,
|
||||
|
|
@ -80,24 +80,24 @@ def add_host_override():
|
|||
before=None, after=entry,
|
||||
description=f'Added host override: {host} → {ip}',
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/toggle_host_override', methods=['POST'])
|
||||
@bp.route('/action/hostoverrides/table_toggle', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def toggle_host_override():
|
||||
def table_toggle():
|
||||
idx = _row_index()
|
||||
if idx is None:
|
||||
flash('Invalid request.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
cfg = load_config()
|
||||
items = cfg.get('host_overrides', [])
|
||||
if idx < 0 or idx >= len(items):
|
||||
flash('Entry not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
old_enabled = items[idx].get('enabled', True)
|
||||
items[idx]['enabled'] = not old_enabled
|
||||
|
|
@ -105,7 +105,7 @@ def toggle_host_override():
|
|||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
action = 'Enabled' if not old_enabled else 'Disabled'
|
||||
flash(save_config_with_snapshot(
|
||||
|
|
@ -114,16 +114,16 @@ def toggle_host_override():
|
|||
before={'enabled': old_enabled}, after={'enabled': not old_enabled},
|
||||
description=f'{action} host override: {items[idx]["host"]}',
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/edit_host_override', methods=['POST'])
|
||||
@bp.route('/action/hostoverrides/table_edit', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def edit_host_override():
|
||||
def table_edit():
|
||||
idx = _row_index()
|
||||
if idx is None:
|
||||
flash('Invalid request.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
description = sanitize.text(request.form.get('description', ''))
|
||||
host = validate.domainname(request.form.get('host', ''))
|
||||
|
|
@ -132,19 +132,19 @@ def edit_host_override():
|
|||
|
||||
if not host or not ip:
|
||||
flash('Hostname and IP address are required.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
cfg = load_config()
|
||||
if not _ip_in_vlan(ip, cfg):
|
||||
flash('IP address does not fall within any configured VLAN subnet.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
items = cfg.get('host_overrides', [])
|
||||
if idx < 0 or idx >= len(items):
|
||||
flash('Entry not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
before = copy.deepcopy(items[idx])
|
||||
items[idx].update({'description': description, 'host': host, 'ip': ip, 'enabled': enabled})
|
||||
|
|
@ -152,7 +152,7 @@ def edit_host_override():
|
|||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
flash(save_config_with_snapshot(
|
||||
cfg,
|
||||
|
|
@ -160,31 +160,31 @@ def edit_host_override():
|
|||
before=before, after=copy.deepcopy(items[idx]),
|
||||
description=f'Edited host override: {host} → {ip}',
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/delete_host_override', methods=['POST'])
|
||||
@bp.route('/action/hostoverrides/table_delete', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def delete_host_override():
|
||||
def table_delete():
|
||||
idx = _row_index()
|
||||
if idx is None:
|
||||
flash('Invalid request.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
cfg = load_config()
|
||||
items = cfg.get('host_overrides', [])
|
||||
if idx < 0 or idx >= len(items):
|
||||
flash('Entry not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
removed = items.pop(idx)
|
||||
errors = validate.validate_config(cfg)
|
||||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
flash(save_config_with_snapshot(
|
||||
cfg,
|
||||
|
|
@ -192,4 +192,4 @@ def delete_host_override():
|
|||
before=removed, after=None,
|
||||
description=f'Deleted host override: {removed["host"]}',
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
{
|
||||
"id": "view_hostoverrides",
|
||||
"client_requirement": "client_is_viewer+",
|
||||
"items": [
|
||||
{
|
||||
|
|
@ -43,7 +42,7 @@
|
|||
"row_actions": [
|
||||
{
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"action": "/action/edit_host_override",
|
||||
"action": "/action/hostoverrides/table_edit",
|
||||
"method": "inline_edit",
|
||||
"text": "Edit",
|
||||
"class": "btn-ghost btn-sm",
|
||||
|
|
@ -71,7 +70,7 @@
|
|||
},
|
||||
{
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"action": "/action/delete_host_override",
|
||||
"action": "/action/hostoverrides/table_delete",
|
||||
"method": "post",
|
||||
"text": "Delete",
|
||||
"class": "btn-danger btn-sm"
|
||||
|
|
@ -86,7 +85,7 @@
|
|||
"items": [
|
||||
{
|
||||
"type": "form",
|
||||
"action": "/action/add_host_override",
|
||||
"action": "/action/hostoverrides/addoverride_add",
|
||||
"method": "post",
|
||||
"items": [
|
||||
{
|
||||
|
|
@ -117,7 +116,7 @@
|
|||
"items": [
|
||||
{
|
||||
"type": "button_primary",
|
||||
"action": "/action/add_host_override",
|
||||
"action": "/action/hostoverrides/addoverride_add",
|
||||
"method": "post",
|
||||
"text": "Add Host Override"
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue