refactor: update all API blueprints (certs, dhcp, firewall, logs, network, proxy, wireguard)
This commit is contained in:
+27
-13
@@ -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_WIREGUARD_PEERS_REMOVE,
|
||||
GET_WIREGUARD_CONFIG,
|
||||
GET_WIREGUARD_PEER_STATUS,
|
||||
GET_WIREGUARD_PEERS,
|
||||
GET_WIREGUARD_STATUS,
|
||||
PATCH_WIREGUARD_CONFIG,
|
||||
POST_WIREGUARD_APPLY,
|
||||
POST_WIREGUARD_CONFIG,
|
||||
POST_WIREGUARD_DOWN,
|
||||
POST_WIREGUARD_GENERATE_CLIENT,
|
||||
POST_WIREGUARD_INITIALIZE,
|
||||
POST_WIREGUARD_PEERS_ADD,
|
||||
)
|
||||
from webui.api.common import _error, _ok
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -25,7 +39,7 @@ def get_config_bp():
|
||||
response on failure.
|
||||
"""
|
||||
try:
|
||||
return _ok(get("/wireguard/config"))
|
||||
return _ok(get(GET_WIREGUARD_CONFIG))
|
||||
except RuntimeError as exc:
|
||||
logger.error("Failed to read WireGuard config: %s", exc)
|
||||
return _error(str(exc), 500)
|
||||
@@ -53,7 +67,7 @@ def post_config():
|
||||
body = dict(body)
|
||||
body["interface"] = dict(body["interface"])
|
||||
body["interface"].pop("private_key", None)
|
||||
post("/wireguard/config", body)
|
||||
post(POST_WIREGUARD_CONFIG, body)
|
||||
return _ok(None)
|
||||
except BadRequest as exc:
|
||||
logger.info("WireGuard config save rejected: %s", exc)
|
||||
@@ -85,7 +99,7 @@ def patch_config():
|
||||
body = dict(body)
|
||||
body["interface"] = dict(body["interface"])
|
||||
body["interface"].pop("private_key", None)
|
||||
patch("/wireguard/config", body)
|
||||
patch(PATCH_WIREGUARD_CONFIG, body)
|
||||
logger.info("WireGuard config patched: %s", sorted(body.keys()))
|
||||
return _ok(None)
|
||||
except BadRequest as exc:
|
||||
@@ -106,7 +120,7 @@ def apply_bp():
|
||||
Success response on acceptance, or 500 on server error.
|
||||
"""
|
||||
try:
|
||||
post("/wireguard/apply")
|
||||
post(POST_WIREGUARD_APPLY)
|
||||
logger.info("WireGuard tunnel applied via API")
|
||||
return _ok(None)
|
||||
except RuntimeError as exc:
|
||||
@@ -124,7 +138,7 @@ def up_bp():
|
||||
Success response on acceptance, or 500 on server error.
|
||||
"""
|
||||
try:
|
||||
post("/wireguard/apply")
|
||||
post(POST_WIREGUARD_APPLY)
|
||||
logger.info("WireGuard tunnel started via API")
|
||||
return _ok(None)
|
||||
except RuntimeError as exc:
|
||||
@@ -142,7 +156,7 @@ def down_bp():
|
||||
Success response on acceptance, or 500 on server error.
|
||||
"""
|
||||
try:
|
||||
post("/wireguard/down")
|
||||
post(POST_WIREGUARD_DOWN)
|
||||
logger.info("WireGuard tunnel brought down via API")
|
||||
return _ok(None)
|
||||
except RuntimeError as exc:
|
||||
@@ -161,7 +175,7 @@ def status_bp():
|
||||
response on failure.
|
||||
"""
|
||||
try:
|
||||
return _ok(get("/wireguard/status"))
|
||||
return _ok(get(GET_WIREGUARD_STATUS))
|
||||
except RuntimeError as exc:
|
||||
logger.error("Failed to get WireGuard status: %s", exc)
|
||||
return _error(str(exc), 500)
|
||||
@@ -177,7 +191,7 @@ def initialize_bp():
|
||||
Success response on acceptance, or 500 on server error.
|
||||
"""
|
||||
try:
|
||||
post("/wireguard/initialize")
|
||||
post(POST_WIREGUARD_INITIALIZE)
|
||||
logger.info("WireGuard initialized via API")
|
||||
return _ok(None)
|
||||
except RuntimeError as exc:
|
||||
@@ -208,7 +222,7 @@ def add_peer_bp():
|
||||
return _error("'name' is required", 400)
|
||||
try:
|
||||
peer = post(
|
||||
"/wireguard/peers/add",
|
||||
POST_WIREGUARD_PEERS_ADD,
|
||||
{
|
||||
"name": name,
|
||||
"endpoint": body.get("endpoint"),
|
||||
@@ -241,7 +255,7 @@ def remove_peer_bp(name):
|
||||
or 500 on server error.
|
||||
"""
|
||||
try:
|
||||
delete("/wireguard/peers/remove", {"name": name})
|
||||
delete(DELETE_WIREGUARD_PEERS_REMOVE, {"name": name})
|
||||
logger.info("WireGuard peer '%s' removed via API", name)
|
||||
return _ok({"name": name})
|
||||
except NotFound as exc:
|
||||
@@ -263,7 +277,7 @@ def peers_bp():
|
||||
on failure.
|
||||
"""
|
||||
try:
|
||||
return _ok(get("/wireguard/peers"))
|
||||
return _ok(get(GET_WIREGUARD_PEERS))
|
||||
except RuntimeError as exc:
|
||||
logger.error("Failed to list WireGuard peers: %s", exc)
|
||||
return _error(str(exc), 500)
|
||||
@@ -280,7 +294,7 @@ def peer_status_bp():
|
||||
on failure.
|
||||
"""
|
||||
try:
|
||||
return _ok(get("/wireguard/peer-status"))
|
||||
return _ok(get(GET_WIREGUARD_PEER_STATUS))
|
||||
except RuntimeError as exc:
|
||||
logger.error("Failed to get WireGuard peer status: %s", exc)
|
||||
return _error(str(exc), 500)
|
||||
@@ -309,7 +323,7 @@ def generate_client_bp():
|
||||
return _error("Field 'server_endpoint' is required", 400)
|
||||
try:
|
||||
result = post(
|
||||
"/wireguard/generate-client",
|
||||
POST_WIREGUARD_GENERATE_CLIENT,
|
||||
{
|
||||
"name": name,
|
||||
"server_endpoint": server_endpoint,
|
||||
|
||||
Reference in New Issue
Block a user