refactor: update all API blueprints (certs, dhcp, firewall, logs, network, proxy, wireguard)

This commit is contained in:
2026-06-16 03:36:51 +00:00
parent 4fc0fb3f72
commit 708b8b5d15
7 changed files with 199 additions and 149 deletions
+29 -12
View File
@@ -8,6 +8,20 @@ import logging
from flask import Blueprint, request
from daemon.client import BadRequest, NotFound, delete, get, patch, post
from daemon.iface import (
DELETE_DNSMASQ_DNS_RECORD_REMOVE,
DELETE_DNSMASQ_RANGES_REMOVE,
DELETE_DNSMASQ_STATIC_LEASE_REMOVE,
GET_DNSMASQ_CONFIG,
GET_DNSMASQ_LEASES,
GET_DNSMASQ_STATUS,
PATCH_DNSMASQ_CONFIG,
POST_DNSMASQ_APPLY,
POST_DNSMASQ_CONFIG,
POST_DNSMASQ_DNS_RECORD_ADD,
POST_DNSMASQ_RANGES_ADD,
POST_DNSMASQ_STATIC_LEASE_ADD,
)
from webui.api.common import _error, _ok
logger = logging.getLogger(__name__)
@@ -27,7 +41,7 @@ def get_config_bp():
JSON response with the config or an error.
"""
try:
return _ok(get("/dnsmasq/config"))
return _ok(get(GET_DNSMASQ_CONFIG))
except RuntimeError as exc:
logger.error("Failed to read DHCP config: %s", exc)
return _error(str(exc), 500)
@@ -47,7 +61,7 @@ def post_config():
if not isinstance(body, dict):
return _error("Request body must be a JSON object", 400)
try:
post("/dnsmasq/config", body)
post(POST_DNSMASQ_CONFIG, body)
return _ok(None)
except BadRequest as exc:
logger.info("DHCP config save rejected: %s", exc)
@@ -71,7 +85,7 @@ def patch_config():
if not isinstance(body, dict):
return _error("Request body must be a JSON object", 400)
try:
patch("/dnsmasq/config", body)
patch(PATCH_DNSMASQ_CONFIG, body)
return _ok(None)
except BadRequest as exc:
logger.info("DHCP config patch rejected: %s", exc)
@@ -86,7 +100,7 @@ def apply_bp():
"""POST /api/dhcp/apply — Apply the current dnsmasq configuration to the running service."""
try:
post("/dnsmasq/apply")
post(POST_DNSMASQ_APPLY)
logger.info("dnsmasq config applied via API")
return _ok(None)
except RuntimeError as exc:
@@ -104,7 +118,7 @@ def status_bp():
"""GET /api/dhcp/status — Retrieve dnsmasq service status."""
try:
return _ok(get("/dnsmasq/status"))
return _ok(get(GET_DNSMASQ_STATUS))
except RuntimeError as exc:
logger.error("Failed to get DHCP status: %s", exc)
return _error(str(exc), 500)
@@ -134,7 +148,7 @@ def add_range_bp():
return _error("'start' and 'end' are required", 400)
try:
post(
"/dnsmasq/ranges/add",
POST_DNSMASQ_RANGES_ADD,
{
"interface": iface or "",
"start": start,
@@ -170,7 +184,8 @@ def remove_range_bp():
return _error("'start' and 'end' are required", 400)
try:
delete(
"/dnsmasq/ranges/remove", {"interface": iface, "start": start, "end": end}
DELETE_DNSMASQ_RANGES_REMOVE,
{"interface": iface, "start": start, "end": end},
)
logger.info("DHCP range removed via API: %s-%s", start, end)
return _ok(None)
@@ -192,7 +207,7 @@ def leases_bp():
"""GET /api/dhcp/leases — Retrieve the current DHCP lease table."""
try:
return _ok(get("/dnsmasq/leases"))
return _ok(get(GET_DNSMASQ_LEASES))
except RuntimeError as exc:
logger.error("Failed to read lease table: %s", exc)
return _error(str(exc), 500)
@@ -220,7 +235,9 @@ def add_static_lease_bp():
if not mac or not ip:
return _error("'mac' and 'ip' are required", 400)
try:
post("/dnsmasq/static-lease/add", {"mac": mac, "ip": ip, "hostname": hostname})
post(
POST_DNSMASQ_STATIC_LEASE_ADD, {"mac": mac, "ip": ip, "hostname": hostname}
)
logger.info("Static lease added via API: %s -> %s", mac, ip)
return _ok({"mac": mac, "ip": ip, "hostname": hostname})
except BadRequest as exc:
@@ -242,7 +259,7 @@ def remove_static_lease_bp(mac):
JSON response with success status or an error.
"""
try:
delete("/dnsmasq/static-lease/remove", {"mac": mac})
delete(DELETE_DNSMASQ_STATIC_LEASE_REMOVE, {"mac": mac})
logger.info("Static lease removed via API: %s", mac)
return _ok(None)
except NotFound as exc:
@@ -276,7 +293,7 @@ def add_dns_record_bp():
return _error("'name' and 'address' are required", 400)
try:
post(
"/dnsmasq/dns-record/add",
POST_DNSMASQ_DNS_RECORD_ADD,
{"name": name, "address": address, "hostname": hostname},
)
logger.info("DNS record added via API: %s -> %s", name, address)
@@ -300,7 +317,7 @@ def remove_dns_record_bp(name):
JSON response with success status or an error.
"""
try:
delete("/dnsmasq/dns-record/remove", {"name": name})
delete(DELETE_DNSMASQ_DNS_RECORD_REMOVE, {"name": name})
logger.info("DNS record removed via API: %s", name)
return _ok(None)
except NotFound as exc: