linuxrouter/docker/routlin-dash/app/pages/hostoverrides/action.py

160 lines
5 KiB
Python
Raw Normal View History

2026-05-27 22:04:04 -04:00
from pathlib import Path
2026-05-25 16:07:21 -04:00
import copy
2026-05-18 14:38:23 -04:00
2026-05-17 03:26:01 -04:00
from flask import Blueprint, request, redirect, flash
from auth import require_level
2026-05-30 14:57:33 -04:00
from config_utils import load_config, record_group, diff_fields, verify_config_hash
2026-05-17 03:26:01 -04:00
import sanitize
2026-06-05 01:48:27 -04:00
import mod_validation as validate
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
_PAGE = Path(__file__).parent.name
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
bp = Blueprint(_PAGE, __name__)
2026-05-17 03:26:01 -04:00
def _row_index():
try:
return int(request.form.get('row_index', ''))
except (ValueError, TypeError):
return None
def _hash_ok():
2026-05-25 19:59:42 -04:00
if not verify_config_hash(request.form.get('config_hash', '')):
2026-05-17 03:26:01 -04:00
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
return False
return True
2026-05-27 22:04:04 -04:00
@bp.route('/action/hostoverrides/addoverride_add', methods=['POST'])
2026-05-17 03:26:01 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def addoverride_add():
2026-05-17 03:26:01 -04:00
description = sanitize.text(request.form.get('description', ''))
2026-05-20 17:10:18 -04:00
host = validate.domainname(request.form.get('host', ''))
2026-05-17 03:26:01 -04:00
ip = sanitize.ip(request.form.get('ip', ''))
if not host or not ip:
flash('Hostname and IP address are required.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not _hash_ok():
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 19:59:42 -04:00
cfg = load_config()
2026-05-31 22:29:05 -04:00
err = validate.check_host_override_ip_in_vlans(ip, cfg)
if err:
flash(err, 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-18 14:38:23 -04:00
2026-05-25 16:07:21 -04:00
entry = {'description': description, 'host': host, 'ip': ip, 'enabled': True}
2026-05-25 19:59:42 -04:00
cfg.setdefault('host_overrides', []).append(entry)
errors = validate.validate_config(cfg)
2026-05-20 17:10:18 -04:00
if errors:
for msg in errors:
flash(msg, 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-30 14:57:33 -04:00
changes = diff_fields(None, entry)
flash(record_group(cfg, 'host_overrides', 'host', host, changes, 'core apply'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/hostoverrides/table_toggle', methods=['POST'])
2026-05-17 03:26:01 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def table_toggle():
2026-05-17 03:26:01 -04:00
idx = _row_index()
if idx is None:
flash('Invalid request.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not _hash_ok():
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 19:59:42 -04:00
cfg = load_config()
items = cfg.get('host_overrides', [])
2026-05-17 03:26:01 -04:00
if idx < 0 or idx >= len(items):
flash('Entry not found.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 16:07:21 -04:00
old_enabled = items[idx].get('enabled', True)
2026-05-30 14:57:33 -04:00
before = copy.deepcopy(items[idx])
2026-05-25 16:07:21 -04:00
items[idx]['enabled'] = not old_enabled
2026-05-25 19:59:42 -04:00
errors = validate.validate_config(cfg)
2026-05-20 17:10:18 -04:00
if errors:
for msg in errors:
flash(msg, 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-30 14:57:33 -04:00
host = items[idx]['host']
changes = diff_fields(before, items[idx])
flash(record_group(cfg, 'host_overrides', 'host', host, changes, 'core apply'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/hostoverrides/table_edit', methods=['POST'])
2026-05-17 03:26:01 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def table_edit():
2026-05-17 03:26:01 -04:00
idx = _row_index()
if idx is None:
flash('Invalid request.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
description = sanitize.text(request.form.get('description', ''))
2026-05-20 17:10:18 -04:00
host = validate.domainname(request.form.get('host', ''))
2026-05-17 03:26:01 -04:00
ip = sanitize.ip(request.form.get('ip', ''))
2026-05-17 03:37:26 -04:00
enabled = request.form.get('enabled') == 'on'
2026-05-17 03:26:01 -04:00
if not host or not ip:
flash('Hostname and IP address are required.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not _hash_ok():
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 19:59:42 -04:00
cfg = load_config()
2026-05-31 22:29:05 -04:00
err = validate.check_host_override_ip_in_vlans(ip, cfg)
if err:
flash(err, 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-18 14:38:23 -04:00
2026-05-25 19:59:42 -04:00
items = cfg.get('host_overrides', [])
2026-05-17 03:26:01 -04:00
if idx < 0 or idx >= len(items):
flash('Entry not found.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 16:07:21 -04:00
before = copy.deepcopy(items[idx])
2026-05-17 03:37:26 -04:00
items[idx].update({'description': description, 'host': host, 'ip': ip, 'enabled': enabled})
2026-05-25 19:59:42 -04:00
errors = validate.validate_config(cfg)
2026-05-20 17:10:18 -04:00
if errors:
for msg in errors:
flash(msg, 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-30 14:57:33 -04:00
changes = diff_fields(before, items[idx])
flash(record_group(cfg, 'host_overrides', 'host', host, changes, 'core apply'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/hostoverrides/table_delete', methods=['POST'])
2026-05-17 03:26:01 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def table_delete():
2026-05-17 03:26:01 -04:00
idx = _row_index()
if idx is None:
flash('Invalid request.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not _hash_ok():
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 19:59:42 -04:00
cfg = load_config()
items = cfg.get('host_overrides', [])
2026-05-17 03:26:01 -04:00
if idx < 0 or idx >= len(items):
flash('Entry not found.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
removed = items.pop(idx)
2026-05-25 19:59:42 -04:00
errors = validate.validate_config(cfg)
2026-05-20 17:10:18 -04:00
if errors:
for msg in errors:
flash(msg, 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-30 14:57:33 -04:00
changes = diff_fields(removed, None)
flash(record_group(cfg, 'host_overrides', 'host', removed['host'], changes, 'core apply'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')