Add a stateful Proxmox API console and broad handler coverage beyond the
initial QEMU slice, backed by imported contracts for majors 6–9. - Implement durable handlers for access/auth, cluster, LXC, storage, HA, firewall, Ceph, SDN, ACME, notifications, pools, mapping, and node ops - Serve an interactive Web UI with catalog browsing, demo seed controls, and OpenAPI/help surfaces - Bundle PVE 6.4-15, 7.4-16, and 8.4.5 contract revisions alongside 9.2.3 - Support in-memory runtime contract Apply (POST /ui/api/contract/apply) so /version and /api2 routes follow the selected major until restart - Expand seed profiles (including demo-cluster), migrations 007–008, TLS gateway config, Compose/Makefile tooling, and compatibility evidence - Tighten .gitignore for macOS, hidden directories (.*/), and local secrets
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
"""Static web console assets."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
_WEB_ROOT = Path(__file__).parent
|
||||
_CONSOLE_HTML = _WEB_ROOT / "index.html"
|
||||
|
||||
|
||||
def console_html() -> str:
|
||||
"""Return the latest console markup from disk."""
|
||||
|
||||
return _CONSOLE_HTML.read_text(encoding="utf-8")
|
||||
@@ -0,0 +1,77 @@
|
||||
"""Catalog-scoped compatibility summaries for the web console."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app.compatibility import CompatibilityDimension, CompatibilityReport, build_report
|
||||
from app.config import Settings
|
||||
from app.contracts.model import Snapshot
|
||||
from app.web.contract_catalog import major_release
|
||||
|
||||
MethodKey = tuple[str, str]
|
||||
|
||||
|
||||
def compatibility_payload(
|
||||
snapshot: Snapshot,
|
||||
major: int,
|
||||
*,
|
||||
implemented_methods: frozenset[MethodKey] | None,
|
||||
runtime_report: CompatibilityReport | None,
|
||||
runtime_version: str | None,
|
||||
settings: Settings | None,
|
||||
) -> dict[str, object]:
|
||||
"""Build a compatibility summary for the selected catalog major."""
|
||||
|
||||
declared = frozenset(
|
||||
(contract_path.path, method.verb.upper())
|
||||
for contract_path in snapshot.paths
|
||||
for method in contract_path.methods
|
||||
)
|
||||
implemented = (implemented_methods or frozenset()) & declared
|
||||
|
||||
if runtime_report is not None and runtime_report.source_version == snapshot.source_version:
|
||||
payload = runtime_report.as_json()
|
||||
evidence_scope = "full"
|
||||
else:
|
||||
dimensions: dict[CompatibilityDimension, frozenset[MethodKey]] = {
|
||||
CompatibilityDimension.ROUTE_METHOD: declared,
|
||||
}
|
||||
if runtime_report is not None:
|
||||
for dimension, methods in runtime_report.dimensions.items():
|
||||
if dimension == CompatibilityDimension.ROUTE_METHOD:
|
||||
continue
|
||||
dimensions[dimension] = methods & declared
|
||||
else:
|
||||
for dimension in CompatibilityDimension:
|
||||
if dimension != CompatibilityDimension.ROUTE_METHOD:
|
||||
dimensions[dimension] = frozenset()
|
||||
|
||||
empty: frozenset[MethodKey] = frozenset()
|
||||
if runtime_report is None:
|
||||
observed = empty
|
||||
verified = empty
|
||||
incompatible = empty
|
||||
regressions = empty
|
||||
else:
|
||||
observed = runtime_report.observed & declared
|
||||
verified = runtime_report.verified & declared
|
||||
incompatible = runtime_report.incompatible & declared
|
||||
regressions = runtime_report.regressions & declared
|
||||
catalog_report = build_report(
|
||||
snapshot,
|
||||
implemented=implemented,
|
||||
observed=observed,
|
||||
verified=verified,
|
||||
dimensions=dimensions,
|
||||
incompatible=incompatible,
|
||||
regressions=regressions,
|
||||
)
|
||||
payload = catalog_report.as_json()
|
||||
evidence_scope = "catalog"
|
||||
|
||||
release = major_release(major, settings)
|
||||
payload["major"] = major
|
||||
payload["catalog_version"] = snapshot.source_version
|
||||
payload["latest_version"] = release.latest_version
|
||||
payload["runtime_version"] = runtime_version
|
||||
payload["evidence_scope"] = evidence_scope
|
||||
return payload
|
||||
@@ -0,0 +1,293 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Proxmox API Emulator</title>
|
||||
<style>
|
||||
:root {
|
||||
color-scheme: dark;
|
||||
--bg: #0b1020;
|
||||
--panel: #121a2f;
|
||||
--panel-2: #18233d;
|
||||
--border: #2a3555;
|
||||
--text: #e8eefc;
|
||||
--muted: #93a0c0;
|
||||
--accent: #5b8cff;
|
||||
--accent-2: #3dd6c6;
|
||||
--danger: #ff6b7a;
|
||||
--ok: #4ade80;
|
||||
--shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
|
||||
--radius: 16px;
|
||||
--mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
||||
--sans: Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, sans-serif;
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
margin: 0;
|
||||
min-height: 100vh;
|
||||
font-family: var(--sans);
|
||||
background:
|
||||
radial-gradient(circle at top left, rgba(91, 140, 255, 0.18), transparent 28%),
|
||||
radial-gradient(circle at top right, rgba(61, 214, 198, 0.12), transparent 24%),
|
||||
var(--bg);
|
||||
color: var(--text);
|
||||
}
|
||||
.wrap { max-width: 1180px; margin: 0 auto; padding: 32px 20px 48px; }
|
||||
header {
|
||||
display: flex; justify-content: space-between; align-items: flex-start;
|
||||
gap: 16px; margin-bottom: 28px;
|
||||
}
|
||||
h1 { margin: 0 0 8px; font-size: clamp(1.6rem, 2vw, 2.2rem); }
|
||||
.subtitle { color: var(--muted); max-width: 56ch; line-height: 1.5; }
|
||||
.links { display: flex; gap: 10px; flex-wrap: wrap; }
|
||||
.links a {
|
||||
color: var(--text); text-decoration: none; border: 1px solid var(--border);
|
||||
background: rgba(255,255,255,0.03); padding: 8px 12px; border-radius: 999px;
|
||||
font-size: 0.92rem;
|
||||
}
|
||||
.grid {
|
||||
display: grid; grid-template-columns: repeat(12, 1fr); gap: 18px;
|
||||
}
|
||||
.card {
|
||||
background: linear-gradient(180deg, rgba(255,255,255,0.03), transparent), var(--panel);
|
||||
border: 1px solid var(--border); border-radius: var(--radius); padding: 18px;
|
||||
box-shadow: var(--shadow);
|
||||
}
|
||||
.span-4 { grid-column: span 4; }
|
||||
.span-5 { grid-column: span 5; }
|
||||
.span-7 { grid-column: span 7; }
|
||||
.span-8 { grid-column: span 8; }
|
||||
.span-12 { grid-column: span 12; }
|
||||
@media (max-width: 900px) {
|
||||
.span-4, .span-5, .span-7, .span-8 { grid-column: span 12; }
|
||||
}
|
||||
h2 { margin: 0 0 14px; font-size: 1rem; letter-spacing: 0.02em; }
|
||||
label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 6px; }
|
||||
input, select, textarea, button {
|
||||
width: 100%; font: inherit; border-radius: 12px; border: 1px solid var(--border);
|
||||
background: var(--panel-2); color: var(--text); padding: 11px 12px;
|
||||
}
|
||||
textarea { min-height: 120px; font-family: var(--mono); font-size: 0.88rem; resize: vertical; }
|
||||
button {
|
||||
cursor: pointer; background: linear-gradient(135deg, var(--accent), #4068d8);
|
||||
border: none; font-weight: 600; margin-top: 10px;
|
||||
}
|
||||
button.secondary {
|
||||
background: transparent; border: 1px solid var(--border); font-weight: 500;
|
||||
}
|
||||
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
|
||||
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
|
||||
.stat {
|
||||
background: var(--panel-2); border: 1px solid var(--border); border-radius: 14px;
|
||||
padding: 14px;
|
||||
}
|
||||
.stat .label { color: var(--muted); font-size: 0.82rem; margin-bottom: 6px; }
|
||||
.stat .value { font-size: 1.25rem; font-weight: 700; }
|
||||
.pill {
|
||||
display: inline-flex; align-items: center; gap: 8px; padding: 6px 10px;
|
||||
border-radius: 999px; background: rgba(74, 222, 128, 0.12); color: var(--ok);
|
||||
border: 1px solid rgba(74, 222, 128, 0.25); font-size: 0.85rem;
|
||||
}
|
||||
.pill.warn { color: #fbbf24; background: rgba(251, 191, 36, 0.12); border-color: rgba(251,191,36,0.25); }
|
||||
pre {
|
||||
margin: 0; padding: 14px; border-radius: 14px; background: #070b14;
|
||||
border: 1px solid var(--border); overflow: auto; font-family: var(--mono);
|
||||
font-size: 0.84rem; line-height: 1.45; min-height: 180px;
|
||||
}
|
||||
.actions { display: flex; gap: 10px; flex-wrap: wrap; }
|
||||
.actions button { width: auto; flex: 1 1 140px; margin-top: 0; }
|
||||
.hint { color: var(--muted); font-size: 0.85rem; margin-top: 8px; line-height: 1.4; }
|
||||
.error { color: var(--danger); }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrap">
|
||||
<header>
|
||||
<div>
|
||||
<h1>Proxmox API Emulator</h1>
|
||||
<p class="subtitle">
|
||||
Stateful API console for the emulator. Authenticate, inspect cluster state,
|
||||
send requests, and follow UPID tasks against the same `/api2/json` surface
|
||||
used by proxmoxer and other clients.
|
||||
</p>
|
||||
</div>
|
||||
<div class="links">
|
||||
<a href="/docs" target="_blank" rel="noreferrer">OpenAPI</a>
|
||||
<a href="/admin/compatibility.html" target="_blank" rel="noreferrer">Compatibility</a>
|
||||
<a href="/health/ready" target="_blank" rel="noreferrer">Readiness</a>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="grid">
|
||||
<section class="card span-4">
|
||||
<h2>Authentication</h2>
|
||||
<label for="username">Username</label>
|
||||
<input id="username" value="root@pam" autocomplete="username">
|
||||
<label for="password">Password</label>
|
||||
<input id="password" type="password" value="secret" autocomplete="current-password">
|
||||
<button id="login-btn">Get ticket</button>
|
||||
<p class="hint">Development credentials from the deterministic seed profile.</p>
|
||||
<label for="csrf">CSRFPreventionToken</label>
|
||||
<input id="csrf" readonly>
|
||||
<div id="auth-status" class="pill warn" style="margin-top:12px;">Not authenticated</div>
|
||||
</section>
|
||||
|
||||
<section class="card span-8">
|
||||
<h2>Cluster snapshot</h2>
|
||||
<div class="stats">
|
||||
<div class="stat"><div class="label">PVE version</div><div class="value" id="stat-version">—</div></div>
|
||||
<div class="stat"><div class="label">Nodes</div><div class="value" id="stat-nodes">—</div></div>
|
||||
<div class="stat"><div class="label">Resources</div><div class="value" id="stat-resources">—</div></div>
|
||||
</div>
|
||||
<div class="actions" style="margin-top:14px;">
|
||||
<button class="secondary" data-action="refresh">Refresh overview</button>
|
||||
<button class="secondary" data-action="nodes">GET /nodes</button>
|
||||
<button class="secondary" data-action="resources">GET /cluster/resources</button>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="card span-5">
|
||||
<h2>Quick request</h2>
|
||||
<div class="row">
|
||||
<div>
|
||||
<label for="method">Method</label>
|
||||
<select id="method">
|
||||
<option>GET</option>
|
||||
<option>POST</option>
|
||||
<option>PUT</option>
|
||||
<option>DELETE</option>
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label for="path">Path</label>
|
||||
<input id="path" value="/api2/json/nodes/pve01/qemu">
|
||||
</div>
|
||||
</div>
|
||||
<label for="body">Body (JSON or form key=value)</label>
|
||||
<textarea id="body"></textarea>
|
||||
<button id="send-btn">Send request</button>
|
||||
<p class="hint">Mutating requests automatically attach the CSRF header when a ticket is present.</p>
|
||||
</section>
|
||||
|
||||
<section class="card span-7">
|
||||
<h2>Response</h2>
|
||||
<pre id="output">Waiting for a request…</pre>
|
||||
</section>
|
||||
|
||||
<section class="card span-12">
|
||||
<h2>Task monitor</h2>
|
||||
<div class="row">
|
||||
<div>
|
||||
<label for="node">Node</label>
|
||||
<input id="node" value="pve01">
|
||||
</div>
|
||||
<div>
|
||||
<label for="upid">UPID</label>
|
||||
<input id="upid" placeholder="UPID returned by a mutation">
|
||||
</div>
|
||||
</div>
|
||||
<div class="actions" style="margin-top:10px;">
|
||||
<button class="secondary" id="task-status-btn">Task status</button>
|
||||
<button class="secondary" id="task-log-btn">Task log</button>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const output = document.getElementById("output");
|
||||
const csrfInput = document.getElementById("csrf");
|
||||
const authStatus = document.getElementById("auth-status");
|
||||
|
||||
function show(data, status) {
|
||||
output.textContent = JSON.stringify({ status, data }, null, 2);
|
||||
}
|
||||
|
||||
async function apiFetch(path, options = {}) {
|
||||
const headers = new Headers(options.headers || {});
|
||||
if (csrfInput.value && options.method && options.method !== "GET") {
|
||||
headers.set("CSRFPreventionToken", csrfInput.value);
|
||||
}
|
||||
const response = await fetch(path, { ...options, headers, credentials: "include" });
|
||||
let data;
|
||||
try { data = await response.json(); } catch { data = { raw: await response.text() }; }
|
||||
show(data, response.status);
|
||||
return { response, data };
|
||||
}
|
||||
|
||||
async function refreshOverview() {
|
||||
const version = await apiFetch("/api2/json/version");
|
||||
document.getElementById("stat-version").textContent =
|
||||
version.data?.data?.version || "unknown";
|
||||
const nodes = await apiFetch("/api2/json/nodes");
|
||||
const resources = await apiFetch("/api2/json/cluster/resources");
|
||||
document.getElementById("stat-nodes").textContent =
|
||||
Array.isArray(nodes.data?.data) ? nodes.data.data.length : "0";
|
||||
document.getElementById("stat-resources").textContent =
|
||||
Array.isArray(resources.data?.data) ? resources.data.data.length : "0";
|
||||
}
|
||||
|
||||
document.getElementById("login-btn").addEventListener("click", async () => {
|
||||
const username = document.getElementById("username").value;
|
||||
const password = document.getElementById("password").value;
|
||||
const body = new URLSearchParams({ username, password });
|
||||
const result = await apiFetch("/api2/json/access/ticket", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body,
|
||||
});
|
||||
const ticket = result.data?.data;
|
||||
if (ticket?.CSRFPreventionToken) {
|
||||
csrfInput.value = ticket.CSRFPreventionToken;
|
||||
authStatus.textContent = `Authenticated as ${ticket.username}`;
|
||||
authStatus.classList.remove("warn");
|
||||
} else {
|
||||
authStatus.textContent = "Authentication failed";
|
||||
authStatus.classList.add("warn");
|
||||
}
|
||||
await refreshOverview();
|
||||
});
|
||||
|
||||
document.getElementById("send-btn").addEventListener("click", async () => {
|
||||
const method = document.getElementById("method").value;
|
||||
const path = document.getElementById("path").value;
|
||||
const rawBody = document.getElementById("body").value.trim();
|
||||
const options = { method };
|
||||
if (rawBody && method !== "GET" && method !== "DELETE") {
|
||||
if (rawBody.startsWith("{")) {
|
||||
options.headers = { "Content-Type": "application/json" };
|
||||
options.body = rawBody;
|
||||
} else {
|
||||
options.headers = { "Content-Type": "application/x-www-form-urlencoded" };
|
||||
options.body = rawBody;
|
||||
}
|
||||
}
|
||||
await apiFetch(path, options);
|
||||
});
|
||||
|
||||
document.querySelector('[data-action="refresh"]').addEventListener("click", refreshOverview);
|
||||
document.querySelector('[data-action="nodes"]').addEventListener("click", () =>
|
||||
apiFetch("/api2/json/nodes"));
|
||||
document.querySelector('[data-action="resources"]').addEventListener("click", () =>
|
||||
apiFetch("/api2/json/cluster/resources"));
|
||||
|
||||
document.getElementById("task-status-btn").addEventListener("click", async () => {
|
||||
const node = document.getElementById("node").value;
|
||||
const upid = encodeURIComponent(document.getElementById("upid").value);
|
||||
await apiFetch(`/api2/json/nodes/${node}/tasks/${upid}/status`);
|
||||
});
|
||||
|
||||
document.getElementById("task-log-btn").addEventListener("click", async () => {
|
||||
const node = document.getElementById("node").value;
|
||||
const upid = encodeURIComponent(document.getElementById("upid").value);
|
||||
await apiFetch(`/api2/json/nodes/${node}/tasks/${upid}/log`);
|
||||
});
|
||||
|
||||
refreshOverview().catch((error) => {
|
||||
output.textContent = String(error);
|
||||
output.classList.add("error");
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,336 @@
|
||||
"""Lazy-loaded Proxmox API contract catalog grouped by major PVE release."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
from app.api.openapi import contract_openapi_tag
|
||||
from app.config import Settings
|
||||
from app.contracts.examples import path_param_example, schema_example
|
||||
from app.contracts.importer import RemoteSourceImporter
|
||||
from app.contracts.model import Method, Parameter, Snapshot
|
||||
from app.contracts.normalize import normalize_snapshot
|
||||
from app.contracts.source import ApiViewerParser
|
||||
from app.contracts.store import RevisionStore
|
||||
|
||||
_PATH_PARAM = re.compile(r"\{([^{}]+)\}")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class MajorReleaseMeta:
|
||||
major: int
|
||||
latest_version: str
|
||||
bundled_revision: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class MajorRelease:
|
||||
major: int
|
||||
latest_version: str
|
||||
artifact_url: str
|
||||
bundled_revision: str | None = None
|
||||
|
||||
|
||||
_MAJOR_METADATA: tuple[MajorReleaseMeta, ...] = (
|
||||
MajorReleaseMeta(
|
||||
major=6,
|
||||
latest_version="6.4-15",
|
||||
bundled_revision="96cd7121e75cdb3efd58f79ca988f6b235a2f28e6f7eae276ae243f65d8a6724",
|
||||
),
|
||||
MajorReleaseMeta(
|
||||
major=7,
|
||||
latest_version="7.4-16",
|
||||
bundled_revision="2cf632fa6ea4939ca9cb7998ade688150db25b0684600f53ac0ca95730f1d99f",
|
||||
),
|
||||
MajorReleaseMeta(
|
||||
major=8,
|
||||
latest_version="8.4.5",
|
||||
bundled_revision="fce6db0a784b3a9b447895895fc6ff4b4437c2dce82e5f3db99227af217726fa",
|
||||
),
|
||||
MajorReleaseMeta(
|
||||
major=9,
|
||||
latest_version="9.2.3",
|
||||
bundled_revision="e61a893e996d05d376579226e7dfbedbcfce8b71787adacffbc557e6e35901c1",
|
||||
),
|
||||
)
|
||||
|
||||
_DEFAULT_ARTIFACT_URLS: dict[int, str] = {
|
||||
6: "https://pve.proxmox.com/pve-docs-6/api-viewer/apidoc.js",
|
||||
7: "https://pve.proxmox.com/pve-docs-7/api-viewer/apidoc.js",
|
||||
8: "https://pve.proxmox.com/pve-docs-8/api-viewer/apidoc.js",
|
||||
9: "https://pve.proxmox.com/pve-docs/api-viewer/apidoc.js",
|
||||
}
|
||||
|
||||
_SNAPSHOT_CACHE: dict[int, Snapshot] = {}
|
||||
_SNAPSHOT_LOCK = asyncio.Lock()
|
||||
_DEFAULT_STORE = Path("contracts")
|
||||
|
||||
|
||||
def _artifact_urls(settings: Settings | None) -> dict[int, str]:
|
||||
if settings is None:
|
||||
return dict(_DEFAULT_ARTIFACT_URLS)
|
||||
return settings.catalog_artifact_urls()
|
||||
|
||||
|
||||
def get_major_releases(settings: Settings | None = None) -> tuple[MajorRelease, ...]:
|
||||
urls = _artifact_urls(settings)
|
||||
return tuple(
|
||||
MajorRelease(
|
||||
major=meta.major,
|
||||
latest_version=meta.latest_version,
|
||||
artifact_url=urls[meta.major],
|
||||
bundled_revision=meta.bundled_revision,
|
||||
)
|
||||
for meta in _MAJOR_METADATA
|
||||
)
|
||||
|
||||
|
||||
def major_release(major: int, settings: Settings | None = None) -> MajorRelease:
|
||||
releases = {release.major: release for release in get_major_releases(settings)}
|
||||
try:
|
||||
return releases[major]
|
||||
except KeyError as error:
|
||||
raise ValueError(f"unsupported major version: {major}") from error
|
||||
|
||||
|
||||
def list_majors(
|
||||
*,
|
||||
runtime_version: str | None,
|
||||
settings: Settings | None = None,
|
||||
) -> dict[str, object]:
|
||||
return {
|
||||
"runtime_version": runtime_version,
|
||||
"majors": [
|
||||
{
|
||||
"major": release.major,
|
||||
"latest_version": release.latest_version,
|
||||
"artifact_url": release.artifact_url,
|
||||
"bundled": release.bundled_revision is not None,
|
||||
}
|
||||
for release in get_major_releases(settings)
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
async def load_snapshot(
|
||||
major: int,
|
||||
store_root: Path | None = None,
|
||||
*,
|
||||
settings: Settings | None = None,
|
||||
) -> Snapshot:
|
||||
if major in _SNAPSHOT_CACHE:
|
||||
return _SNAPSHOT_CACHE[major]
|
||||
async with _SNAPSHOT_LOCK:
|
||||
if major in _SNAPSHOT_CACHE:
|
||||
return _SNAPSHOT_CACHE[major]
|
||||
release = major_release(major, settings)
|
||||
store = RevisionStore(store_root or _DEFAULT_STORE)
|
||||
if release.bundled_revision is not None:
|
||||
snapshot_path = store.root / release.bundled_revision / "snapshot.json"
|
||||
if snapshot_path.is_file():
|
||||
snapshot = Snapshot.model_validate_json(snapshot_path.read_bytes())
|
||||
_SNAPSHOT_CACHE[major] = snapshot
|
||||
return snapshot
|
||||
existing = _find_cached_revision(store, release.latest_version)
|
||||
if existing is not None:
|
||||
snapshot = Snapshot.model_validate_json(existing.read_bytes())
|
||||
_SNAPSHOT_CACHE[major] = snapshot
|
||||
return snapshot
|
||||
raw = await RemoteSourceImporter(release.artifact_url).load()
|
||||
parsed = ApiViewerParser().parse(raw)
|
||||
snapshot, manifest = normalize_snapshot(
|
||||
parsed,
|
||||
raw=raw,
|
||||
source_version=release.latest_version,
|
||||
retrieved_at=datetime.now(UTC),
|
||||
)
|
||||
try:
|
||||
store.save(raw, snapshot, manifest)
|
||||
except OSError:
|
||||
pass
|
||||
_SNAPSHOT_CACHE[major] = snapshot
|
||||
return snapshot
|
||||
|
||||
|
||||
def _find_cached_revision(store: RevisionStore, source_version: str) -> Path | None:
|
||||
if not store.root.is_dir():
|
||||
return None
|
||||
for revision in store.list():
|
||||
manifest = store.manifest(revision)
|
||||
if manifest.source_version == source_version:
|
||||
return store.root / revision / "snapshot.json"
|
||||
return None
|
||||
|
||||
|
||||
def _catalog_entry_path(entry: dict[str, object]) -> str:
|
||||
return str(entry["path"])
|
||||
|
||||
|
||||
def catalog_payload(
|
||||
snapshot: Snapshot,
|
||||
major: int,
|
||||
*,
|
||||
implemented_methods: frozenset[tuple[str, str]] | None = None,
|
||||
settings: Settings | None = None,
|
||||
) -> dict[str, object]:
|
||||
grouped: dict[str, list[dict[str, object]]] = {}
|
||||
for contract_path in snapshot.paths:
|
||||
tag = contract_openapi_tag(contract_path.path)
|
||||
methods = [
|
||||
{
|
||||
"verb": method.verb,
|
||||
"name": method.name,
|
||||
"description": method.description,
|
||||
"protected": method.protected,
|
||||
"implemented": (
|
||||
(contract_path.path, method.verb.upper()) in implemented_methods
|
||||
if implemented_methods is not None
|
||||
else None
|
||||
),
|
||||
}
|
||||
for method in contract_path.methods
|
||||
]
|
||||
entry: dict[str, object] = {
|
||||
"path": contract_path.path,
|
||||
"methods": methods,
|
||||
}
|
||||
grouped.setdefault(tag, []).append(entry)
|
||||
categories: list[dict[str, object]] = []
|
||||
for tag in sorted(grouped):
|
||||
entries = grouped[tag]
|
||||
categories.append(
|
||||
{
|
||||
"tag": tag,
|
||||
"paths": sorted(entries, key=_catalog_entry_path),
|
||||
}
|
||||
)
|
||||
release = major_release(major, settings)
|
||||
return {
|
||||
"major": major,
|
||||
"source_version": snapshot.source_version,
|
||||
"latest_version": release.latest_version,
|
||||
"artifact_url": release.artifact_url,
|
||||
"bundled": release.bundled_revision is not None,
|
||||
"path_count": snapshot.path_count,
|
||||
"method_count": snapshot.method_count,
|
||||
"categories": categories,
|
||||
}
|
||||
|
||||
|
||||
def _path_param_names(path: str) -> tuple[str, ...]:
|
||||
return tuple(match.group(1) for match in _PATH_PARAM.finditer(path))
|
||||
|
||||
|
||||
def _parameter_payload(parameter: Parameter) -> dict[str, object]:
|
||||
schema = parameter.definition
|
||||
return {
|
||||
"name": parameter.name,
|
||||
"type": schema.type,
|
||||
"description": schema.description,
|
||||
"optional": bool(schema.optional),
|
||||
"enum": list(schema.enum),
|
||||
"example": schema_example(schema, name=parameter.name),
|
||||
}
|
||||
|
||||
|
||||
def method_payload(
|
||||
snapshot: Snapshot,
|
||||
*,
|
||||
major: int,
|
||||
path: str,
|
||||
verb: str,
|
||||
runtime_version: str | None,
|
||||
implemented_methods: frozenset[tuple[str, str]] | None,
|
||||
) -> dict[str, object]:
|
||||
contract_path = next((item for item in snapshot.paths if item.path == path), None)
|
||||
if contract_path is None:
|
||||
raise KeyError(path)
|
||||
method = next(
|
||||
(item for item in contract_path.methods if item.verb.upper() == verb.upper()),
|
||||
None,
|
||||
)
|
||||
if method is None:
|
||||
raise KeyError(verb)
|
||||
path_params = _path_param_names(path)
|
||||
path_fields = [
|
||||
_parameter_payload(parameter)
|
||||
for parameter in method.parameters
|
||||
if parameter.name in path_params
|
||||
]
|
||||
for name in path_params:
|
||||
if name not in {field["name"] for field in path_fields}:
|
||||
path_fields.append(
|
||||
{
|
||||
"name": name,
|
||||
"type": "string",
|
||||
"description": None,
|
||||
"optional": False,
|
||||
"enum": [],
|
||||
"example": path_param_example(name) or name,
|
||||
}
|
||||
)
|
||||
body_fields = [
|
||||
_parameter_payload(parameter)
|
||||
for parameter in method.parameters
|
||||
if parameter.name not in path_params and "[n]" not in parameter.name
|
||||
]
|
||||
indexed_fields = [
|
||||
_parameter_payload(parameter) for parameter in method.parameters if "[n]" in parameter.name
|
||||
]
|
||||
body_example = _body_example(method, path_params)
|
||||
resolved_path = _resolve_path(path, path_fields)
|
||||
implemented = (
|
||||
(path, method.verb.upper()) in implemented_methods
|
||||
if implemented_methods is not None
|
||||
else None
|
||||
)
|
||||
return {
|
||||
"major": major,
|
||||
"source_version": snapshot.source_version,
|
||||
"runtime_version": runtime_version,
|
||||
"path": path,
|
||||
"verb": method.verb.upper(),
|
||||
"name": method.name,
|
||||
"description": method.description,
|
||||
"resolved_path": resolved_path,
|
||||
"path_fields": path_fields,
|
||||
"body_fields": body_fields,
|
||||
"indexed_fields": indexed_fields,
|
||||
"body_example": body_example,
|
||||
"implemented": implemented,
|
||||
}
|
||||
|
||||
|
||||
def _resolve_path(path: str, path_fields: list[dict[str, object]]) -> str:
|
||||
resolved = path
|
||||
for field in path_fields:
|
||||
name = str(field["name"])
|
||||
example = field.get("example", name)
|
||||
resolved = resolved.replace(f"{{{name}}}", str(example))
|
||||
return resolved
|
||||
|
||||
|
||||
def _body_example(method: Method, path_params: tuple[str, ...]) -> dict[str, object]:
|
||||
body: dict[str, object] = {}
|
||||
for parameter in method.parameters:
|
||||
if parameter.name in path_params:
|
||||
continue
|
||||
if "[n]" in parameter.name:
|
||||
concrete = parameter.name.replace("[n]", "0")
|
||||
if not parameter.definition.optional:
|
||||
body[concrete] = schema_example(parameter.definition, name=concrete)
|
||||
continue
|
||||
if parameter.definition.optional:
|
||||
continue
|
||||
body[parameter.name] = schema_example(parameter.definition, name=parameter.name)
|
||||
return body
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def default_store_root() -> Path:
|
||||
return _DEFAULT_STORE
|
||||
+6799
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,218 @@
|
||||
"""Browser console for exercising the simulator API."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Annotated
|
||||
|
||||
from asyncpg import Pool # type: ignore[import-untyped]
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from fastapi.responses import HTMLResponse, JSONResponse
|
||||
|
||||
from app.api.registry import HandlerRegistry
|
||||
from app.config import Settings
|
||||
from app.contracts.runtime import apply_runtime_contract_locked, contract_store_root
|
||||
from app.contracts.source import SourceError
|
||||
from app.db.pool import AsyncpgDatabase
|
||||
from app.dependencies import get_database
|
||||
from app.simulation.seed import apply_seed, build_profile, simulation_state_summary
|
||||
from app.web.assets import console_html
|
||||
from app.web.compatibility_catalog import compatibility_payload
|
||||
from app.web.contract_catalog import catalog_payload, list_majors, load_snapshot, method_payload
|
||||
|
||||
router = APIRouter(tags=["Simulator"])
|
||||
|
||||
|
||||
@router.get("/", response_class=HTMLResponse, include_in_schema=True)
|
||||
async def console() -> HTMLResponse:
|
||||
"""Interactive API console and cluster overview."""
|
||||
|
||||
return HTMLResponse(
|
||||
console_html(),
|
||||
headers={"Cache-Control": "no-store"},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/ui/api/versions", include_in_schema=False)
|
||||
async def ui_versions(request: Request) -> JSONResponse:
|
||||
settings = _settings(request)
|
||||
runtime_version = _runtime_version(request)
|
||||
return JSONResponse(list_majors(runtime_version=runtime_version, settings=settings))
|
||||
|
||||
|
||||
@router.get("/ui/api/catalog", include_in_schema=False)
|
||||
async def ui_catalog(
|
||||
request: Request,
|
||||
major: Annotated[int, Query(ge=6, le=9)],
|
||||
) -> JSONResponse:
|
||||
settings = _settings(request)
|
||||
try:
|
||||
snapshot = await load_snapshot(major, _store_root(request), settings=settings)
|
||||
except SourceError as error:
|
||||
raise HTTPException(status_code=503, detail=str(error)) from error
|
||||
implemented = getattr(request.app.state, "implemented_methods", None)
|
||||
return JSONResponse(
|
||||
catalog_payload(snapshot, major, implemented_methods=implemented, settings=settings)
|
||||
)
|
||||
|
||||
|
||||
@router.get("/ui/api/method", include_in_schema=False)
|
||||
async def ui_method(
|
||||
request: Request,
|
||||
major: Annotated[int, Query(ge=6, le=9)],
|
||||
path: Annotated[str, Query(min_length=1)],
|
||||
verb: Annotated[str, Query(min_length=1)],
|
||||
) -> JSONResponse:
|
||||
settings = _settings(request)
|
||||
try:
|
||||
snapshot = await load_snapshot(major, _store_root(request), settings=settings)
|
||||
except SourceError as error:
|
||||
raise HTTPException(status_code=503, detail=str(error)) from error
|
||||
runtime_version = _runtime_version(request)
|
||||
implemented = getattr(request.app.state, "implemented_methods", None)
|
||||
try:
|
||||
payload = method_payload(
|
||||
snapshot,
|
||||
major=major,
|
||||
path=path,
|
||||
verb=verb,
|
||||
runtime_version=runtime_version,
|
||||
implemented_methods=implemented,
|
||||
)
|
||||
except KeyError as error:
|
||||
raise HTTPException(status_code=404, detail=f"unknown contract method: {error}") from error
|
||||
return JSONResponse(payload)
|
||||
|
||||
|
||||
@router.get("/ui/api/compatibility", include_in_schema=False)
|
||||
async def ui_compatibility(
|
||||
request: Request,
|
||||
major: Annotated[int, Query(ge=6, le=9)],
|
||||
) -> JSONResponse:
|
||||
settings = _settings(request)
|
||||
try:
|
||||
snapshot = await load_snapshot(major, _store_root(request), settings=settings)
|
||||
except SourceError as error:
|
||||
raise HTTPException(status_code=503, detail=str(error)) from error
|
||||
implemented = getattr(request.app.state, "implemented_methods", None)
|
||||
runtime_report = getattr(request.app.state, "compatibility_report", None)
|
||||
runtime_version = _runtime_version(request)
|
||||
return JSONResponse(
|
||||
compatibility_payload(
|
||||
snapshot,
|
||||
major,
|
||||
implemented_methods=implemented,
|
||||
runtime_report=runtime_report,
|
||||
runtime_version=runtime_version,
|
||||
settings=settings,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@router.post("/ui/api/contract/apply", include_in_schema=False)
|
||||
async def ui_contract_apply(
|
||||
request: Request,
|
||||
major: Annotated[int, Query(ge=6, le=9)],
|
||||
) -> JSONResponse:
|
||||
"""Hot-swap the in-memory runtime contract to a catalog major (memory-only)."""
|
||||
|
||||
settings = _settings(request)
|
||||
handlers = getattr(request.app.state, "handlers", None)
|
||||
if (
|
||||
settings is None
|
||||
or settings.contract_snapshot is None
|
||||
or not isinstance(handlers, HandlerRegistry)
|
||||
):
|
||||
raise HTTPException(status_code=503, detail="runtime contract is not available")
|
||||
store_root = _store_root(request)
|
||||
try:
|
||||
snapshot = await load_snapshot(major, store_root, settings=settings)
|
||||
except SourceError as error:
|
||||
raise HTTPException(status_code=503, detail=str(error)) from error
|
||||
await apply_runtime_contract_locked(
|
||||
request.app,
|
||||
snapshot,
|
||||
handlers=handlers,
|
||||
store_root=store_root,
|
||||
fallback=settings.contract_fallback,
|
||||
settings=settings,
|
||||
require_evidence_match=False,
|
||||
register_admin=True,
|
||||
)
|
||||
method_count = sum(len(path.methods) for path in snapshot.paths)
|
||||
return JSONResponse(
|
||||
{
|
||||
"ok": True,
|
||||
"major": major,
|
||||
"runtime_version": snapshot.source_version,
|
||||
"path_count": len(snapshot.paths),
|
||||
"method_count": method_count,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/ui/api/demo/state", include_in_schema=False)
|
||||
async def ui_demo_state(request: Request) -> JSONResponse:
|
||||
pool = _database_pool(request)
|
||||
async with pool.acquire() as connection:
|
||||
return JSONResponse(await simulation_state_summary(connection))
|
||||
|
||||
|
||||
@router.post("/ui/api/demo/load", include_in_schema=False)
|
||||
async def ui_demo_load(request: Request) -> JSONResponse:
|
||||
pool = _database_pool(request)
|
||||
profile = build_profile("demo-cluster")
|
||||
async with pool.acquire() as connection:
|
||||
await apply_seed(connection, profile)
|
||||
summary = await simulation_state_summary(connection)
|
||||
return JSONResponse({"ok": True, "profile": profile.name, "summary": summary})
|
||||
|
||||
|
||||
@router.post("/ui/api/demo/unload", include_in_schema=False)
|
||||
async def ui_demo_unload(request: Request) -> JSONResponse:
|
||||
"""Reset to minimal seed, wiping API-created state first."""
|
||||
pool = _database_pool(request)
|
||||
profile = build_profile("minimal")
|
||||
try:
|
||||
async with pool.acquire() as connection:
|
||||
await apply_seed(connection, profile)
|
||||
summary = await simulation_state_summary(connection)
|
||||
except Exception as error:
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"failed to remove demo data: {error}"
|
||||
) from error
|
||||
return JSONResponse({"ok": True, "profile": profile.name, "summary": summary})
|
||||
|
||||
|
||||
def _database_pool(request: Request) -> Pool:
|
||||
database = get_database(request)
|
||||
if not isinstance(database, AsyncpgDatabase):
|
||||
raise HTTPException(status_code=503, detail="database is not available")
|
||||
return database.pool
|
||||
|
||||
|
||||
def _settings(request: Request) -> Settings | None:
|
||||
return getattr(request.app.state, "settings", None)
|
||||
|
||||
|
||||
def _runtime_version(request: Request) -> str | None:
|
||||
active = getattr(request.app.state, "runtime_source_version", None)
|
||||
if isinstance(active, str) and active:
|
||||
return active
|
||||
settings = _settings(request)
|
||||
if settings is None or settings.contract_snapshot is None:
|
||||
return None
|
||||
from app.contracts.model import Snapshot
|
||||
|
||||
snapshot = Snapshot.model_validate_json(settings.contract_snapshot.read_bytes())
|
||||
return snapshot.source_version
|
||||
|
||||
|
||||
def _store_root(request: Request) -> Path:
|
||||
stored = getattr(request.app.state, "contract_store_root", None)
|
||||
if isinstance(stored, Path):
|
||||
return stored
|
||||
settings = _settings(request)
|
||||
if settings is not None:
|
||||
return contract_store_root(settings)
|
||||
return Path("contracts")
|
||||
Reference in New Issue
Block a user