feat: host management UI (CRUD)
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
from __future__ import annotations
|
||||
from quart import Blueprint, render_template, request, redirect, url_for, current_app
|
||||
from sqlalchemy import select
|
||||
from fablednetmon.auth.middleware import require_role
|
||||
from fablednetmon.models.hosts import Host, ProbeType
|
||||
from fablednetmon.models.users import UserRole
|
||||
|
||||
hosts_bp = Blueprint("hosts", __name__, url_prefix="/hosts")
|
||||
|
||||
|
||||
@hosts_bp.get("/")
|
||||
@require_role(UserRole.viewer)
|
||||
async def list_hosts():
|
||||
async with current_app.db_sessionmaker() as db:
|
||||
result = await db.execute(select(Host).order_by(Host.name))
|
||||
hosts = result.scalars().all()
|
||||
return await render_template("hosts/list.html", hosts=hosts)
|
||||
|
||||
|
||||
@hosts_bp.get("/new")
|
||||
@require_role(UserRole.operator)
|
||||
async def new_host():
|
||||
return await render_template("hosts/form.html", host=None, probe_types=list(ProbeType))
|
||||
|
||||
|
||||
@hosts_bp.post("/")
|
||||
@require_role(UserRole.operator)
|
||||
async def create_host():
|
||||
form = await request.form
|
||||
host = Host(
|
||||
name=form["name"].strip(),
|
||||
address=form["address"].strip(),
|
||||
probe_type=ProbeType(form.get("probe_type", "tcp")),
|
||||
probe_port=int(form.get("probe_port") or 80),
|
||||
ping_enabled="ping_enabled" in form,
|
||||
dns_enabled="dns_enabled" in form,
|
||||
dns_expected_ip=form.get("dns_expected_ip", "").strip() or None,
|
||||
# poll_interval_seconds not exposed in UI yet; per-host scheduling not implemented
|
||||
)
|
||||
async with current_app.db_sessionmaker() as db:
|
||||
async with db.begin():
|
||||
db.add(host)
|
||||
return redirect(url_for("hosts.list_hosts"))
|
||||
|
||||
|
||||
@hosts_bp.get("/<host_id>/edit")
|
||||
@require_role(UserRole.operator)
|
||||
async def edit_host(host_id: str):
|
||||
async with current_app.db_sessionmaker() as db:
|
||||
result = await db.execute(select(Host).where(Host.id == host_id))
|
||||
host = result.scalar_one_or_none()
|
||||
if host is None:
|
||||
return "Not found", 404
|
||||
return await render_template("hosts/form.html", host=host, probe_types=list(ProbeType))
|
||||
|
||||
|
||||
@hosts_bp.post("/<host_id>")
|
||||
@require_role(UserRole.operator)
|
||||
async def update_host(host_id: str):
|
||||
form = await request.form
|
||||
async with current_app.db_sessionmaker() as db:
|
||||
async with db.begin():
|
||||
result = await db.execute(select(Host).where(Host.id == host_id))
|
||||
host = result.scalar_one_or_none()
|
||||
if host is None:
|
||||
return "Not found", 404
|
||||
host.name = form["name"].strip()
|
||||
host.address = form["address"].strip()
|
||||
host.probe_type = ProbeType(form.get("probe_type", "tcp"))
|
||||
host.probe_port = int(form.get("probe_port") or 80)
|
||||
host.ping_enabled = "ping_enabled" in form
|
||||
host.dns_enabled = "dns_enabled" in form
|
||||
host.dns_expected_ip = form.get("dns_expected_ip", "").strip() or None
|
||||
# poll_interval_seconds not exposed in UI yet
|
||||
return redirect(url_for("hosts.list_hosts"))
|
||||
|
||||
|
||||
@hosts_bp.post("/<host_id>/delete")
|
||||
@require_role(UserRole.admin)
|
||||
async def delete_host(host_id: str):
|
||||
async with current_app.db_sessionmaker() as db:
|
||||
async with db.begin():
|
||||
result = await db.execute(select(Host).where(Host.id == host_id))
|
||||
host = result.scalar_one_or_none()
|
||||
if host:
|
||||
await db.delete(host)
|
||||
return redirect(url_for("hosts.list_hosts"))
|
||||
@@ -0,0 +1,53 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}{% if host %}Edit Host{% else %}New Host{% endif %} — FabledNetMon{% endblock %}
|
||||
{% block content %}
|
||||
<div style="max-width:560px;margin:2rem auto;">
|
||||
<h1 style="color:#c0c0ff;margin-bottom:1.5rem;">{% if host %}Edit Host{% else %}Add Host{% endif %}</h1>
|
||||
<div class="card">
|
||||
<form method="post" action="{% if host %}/hosts/{{ host.id }}{% else %}/hosts/{% endif %}">
|
||||
<div class="form-group">
|
||||
<label>Display Name</label>
|
||||
<input type="text" name="name" value="{{ host.name if host else '' }}" required autofocus>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Address (hostname or IP)</label>
|
||||
<input type="text" name="address" value="{{ host.address if host else '' }}" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Probe Type</label>
|
||||
<select name="probe_type" style="width:100%;padding:0.5rem 0.75rem;background:#0f0f1e;border:1px solid #3a3a5a;border-radius:4px;color:#e0e0e0;">
|
||||
{% for pt in probe_types %}
|
||||
<option value="{{ pt.value }}" {% if host and host.probe_type == pt %}selected{% endif %}>
|
||||
{{ pt.value | upper }}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>TCP Port (used for TCP probe; ignored for ICMP)</label>
|
||||
<input type="number" name="probe_port" value="{{ host.probe_port if host else 80 }}" min="1" max="65535">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label style="display:flex;align-items:center;gap:0.5rem;cursor:pointer;">
|
||||
<input type="checkbox" name="ping_enabled" {% if not host or host.ping_enabled %}checked{% endif %}>
|
||||
Enable ping monitor
|
||||
</label>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label style="display:flex;align-items:center;gap:0.5rem;cursor:pointer;">
|
||||
<input type="checkbox" name="dns_enabled" {% if host and host.dns_enabled %}checked{% endif %}>
|
||||
Enable DNS monitor
|
||||
</label>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Expected IP (optional — leave blank to accept any)</label>
|
||||
<input type="text" name="dns_expected_ip" value="{{ host.dns_expected_ip if host and host.dns_expected_ip else '' }}">
|
||||
</div>
|
||||
<div style="display:flex;gap:1rem;margin-top:1.5rem;">
|
||||
<button type="submit">{% if host %}Save{% else %}Add Host{% endif %}</button>
|
||||
<a href="/hosts/" style="padding:0.5rem 1rem;color:#a0a0c0;text-decoration:none;">Cancel</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,47 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}Hosts — FabledNetMon{% endblock %}
|
||||
{% block content %}
|
||||
<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:1.5rem;">
|
||||
<h1 style="color:#c0c0ff;">Hosts</h1>
|
||||
<a class="btn" href="/hosts/new">Add Host</a>
|
||||
</div>
|
||||
{% if hosts %}
|
||||
<div class="card" style="padding:0;">
|
||||
<table style="width:100%;border-collapse:collapse;">
|
||||
<thead>
|
||||
<tr style="border-bottom:1px solid #2a2a4a;">
|
||||
<th style="text-align:left;padding:0.75rem 1rem;color:#8080a0;font-weight:normal;">Name</th>
|
||||
<th style="text-align:left;padding:0.75rem 1rem;color:#8080a0;font-weight:normal;">Address</th>
|
||||
<th style="text-align:left;padding:0.75rem 1rem;color:#8080a0;font-weight:normal;">Probe</th>
|
||||
<th style="text-align:left;padding:0.75rem 1rem;color:#8080a0;font-weight:normal;">Monitors</th>
|
||||
<th style="padding:0.75rem 1rem;"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for host in hosts %}
|
||||
<tr style="border-bottom:1px solid #1a1a3a;">
|
||||
<td style="padding:0.75rem 1rem;">{{ host.name }}</td>
|
||||
<td style="padding:0.75rem 1rem;color:#a0a0c0;">{{ host.address }}</td>
|
||||
<td style="padding:0.75rem 1rem;color:#a0a0c0;">{{ host.probe_type.value | upper }}:{{ host.probe_port }}</td>
|
||||
<td style="padding:0.75rem 1rem;color:#a0a0c0;">
|
||||
{% if host.ping_enabled %}<span style="color:#60c060;">ping</span>{% endif %}
|
||||
{% if host.dns_enabled %}<span style="color:#6090c0;margin-left:0.5rem;">dns</span>{% endif %}
|
||||
</td>
|
||||
<td style="padding:0.75rem 1rem;text-align:right;">
|
||||
<a href="/hosts/{{ host.id }}/edit" style="color:#a0a0ff;margin-right:1rem;">Edit</a>
|
||||
<form method="post" action="/hosts/{{ host.id }}/delete" style="display:inline;">
|
||||
<button type="submit" style="background:none;border:none;color:#ff6060;cursor:pointer;"
|
||||
onclick="return confirm('Delete {{ host.name }}?')">Delete</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="card">
|
||||
<p style="color:#606080;">No hosts configured yet. <a href="/hosts/new" style="color:#a0a0ff;">Add one.</a></p>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user