Initial commit: stateful OpenStack API laboratory simulator.
Ship Keystone auth, multi-service handlers (Yoga→Dalmatian), Compose/Helm packaging, API contract packs, and pytest/Pulumi coverage labs.
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,358 @@
|
||||
"""Lazy-loaded API contract catalog grouped by OpenStack release series.
|
||||
|
||||
Wire format keeps integer ``major`` ids (6–9) for hot-swap compatibility with the
|
||||
console; each id maps to an OpenStack series name (Yoga…Dalmatian).
|
||||
Bundled snapshots are temporary stubs carried from the Proxмоx skeleton until
|
||||
real OpenStack service contracts land in a later iteration.
|
||||
"""
|
||||
|
||||
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
|
||||
series: str
|
||||
latest_version: str
|
||||
bundled_revision: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class MajorRelease:
|
||||
major: int
|
||||
series: str
|
||||
latest_version: str
|
||||
artifact_url: str
|
||||
bundled_revision: str | None = None
|
||||
|
||||
|
||||
# Integer majors are stable wire ids used by /ui/api/* and the console.
|
||||
# Series names are the OpenStack release labels shown in the UI.
|
||||
_MAJOR_METADATA: tuple[MajorReleaseMeta, ...] = (
|
||||
MajorReleaseMeta(
|
||||
major=6,
|
||||
series="Yoga",
|
||||
latest_version="6.4-15", # stub contract revision label
|
||||
bundled_revision="96cd7121e75cdb3efd58f79ca988f6b235a2f28e6f7eae276ae243f65d8a6724",
|
||||
),
|
||||
MajorReleaseMeta(
|
||||
major=7,
|
||||
series="Antelope",
|
||||
latest_version="7.4-16",
|
||||
bundled_revision="2cf632fa6ea4939ca9cb7998ade688150db25b0684600f53ac0ca95730f1d99f",
|
||||
),
|
||||
MajorReleaseMeta(
|
||||
major=8,
|
||||
series="Caracal",
|
||||
latest_version="8.4.5",
|
||||
bundled_revision="fce6db0a784b3a9b447895895fc6ff4b4437c2dce82e5f3db99227af217726fa",
|
||||
),
|
||||
MajorReleaseMeta(
|
||||
major=9,
|
||||
series="Dalmatian",
|
||||
latest_version="9.2.3",
|
||||
bundled_revision="e61a893e996d05d376579226e7dfbedbcfce8b71787adacffbc557e6e35901c1",
|
||||
),
|
||||
)
|
||||
|
||||
# Placeholder artifact URLs — temporary stubs until OpenStack contracts are imported.
|
||||
_DEFAULT_ARTIFACT_URLS: dict[int, str] = {
|
||||
6: "stub://openstack/yoga/api-contract",
|
||||
7: "stub://openstack/antelope/api-contract",
|
||||
8: "stub://openstack/caracal/api-contract",
|
||||
9: "stub://openstack/dalmatian/api-contract",
|
||||
}
|
||||
|
||||
_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,
|
||||
series=meta.series,
|
||||
latest_version=meta.latest_version,
|
||||
artifact_url=urls[meta.major],
|
||||
bundled_revision=meta.bundled_revision,
|
||||
)
|
||||
for meta in _MAJOR_METADATA
|
||||
)
|
||||
|
||||
|
||||
def series_name(major: int, settings: Settings | None = None) -> str:
|
||||
return major_release(major, settings).series
|
||||
|
||||
|
||||
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,
|
||||
"series": release.series,
|
||||
"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,
|
||||
"series": release.series,
|
||||
"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
|
||||
+7225
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,259 @@
|
||||
"""Build UI catalog / method payloads from OpenStack contract packs."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from app.contracts.examples import path_param_example
|
||||
from app.openstack.contract_loader import (
|
||||
ensure_loaded,
|
||||
load_series_pack,
|
||||
major_for_series,
|
||||
series_for_major,
|
||||
)
|
||||
|
||||
_PATH_PARAM = re.compile(r"\{([^{}]+)\}")
|
||||
|
||||
|
||||
def openstack_catalog_payload(major: int) -> dict[str, Any]:
|
||||
series = series_for_major(major)
|
||||
ensure_loaded(series)
|
||||
packs = load_series_pack(series)
|
||||
categories: list[dict[str, Any]] = []
|
||||
path_count = 0
|
||||
method_count = 0
|
||||
for name, pack in sorted(packs.items(), key=lambda item: item[0]):
|
||||
by_path: dict[str, list[dict[str, Any]]] = {}
|
||||
for op in pack.operations:
|
||||
by_path.setdefault(op.path, []).append(
|
||||
{
|
||||
"verb": op.method,
|
||||
"name": op.operation_id,
|
||||
"description": op.notes or f"{op.kind} {op.resource_type}",
|
||||
"protected": op.requires_auth,
|
||||
"implemented": True,
|
||||
}
|
||||
)
|
||||
paths = [
|
||||
{"path": path, "methods": methods}
|
||||
for path, methods in sorted(by_path.items(), key=lambda item: item[0])
|
||||
]
|
||||
path_count += len(paths)
|
||||
method_count += sum(len(item["methods"]) for item in paths)
|
||||
categories.append({"tag": name, "paths": paths})
|
||||
return {
|
||||
"major": major,
|
||||
"series": {
|
||||
"yoga": "Yoga",
|
||||
"antelope": "Antelope",
|
||||
"caracal": "Caracal",
|
||||
"dalmatian": "Dalmatian",
|
||||
}.get(series, series.title()),
|
||||
"source_version": f"openstack-{series}",
|
||||
"latest_version": series,
|
||||
"artifact_url": f"contracts/openstack/{series}",
|
||||
"bundled": True,
|
||||
"path_count": path_count,
|
||||
"method_count": method_count,
|
||||
"categories": categories,
|
||||
"catalog_kind": "openstack",
|
||||
}
|
||||
|
||||
|
||||
def openstack_method_payload(
|
||||
*,
|
||||
major: int,
|
||||
path: str,
|
||||
verb: str,
|
||||
runtime_version: str | None,
|
||||
) -> dict[str, Any]:
|
||||
series = series_for_major(major)
|
||||
packs = load_series_pack(series)
|
||||
verb_u = verb.upper()
|
||||
for pack in packs.values():
|
||||
for op in pack.operations:
|
||||
if op.path == path and op.method == verb_u:
|
||||
path_params = _PATH_PARAM.findall(path)
|
||||
path_fields = [
|
||||
{
|
||||
"name": name,
|
||||
"type": "string",
|
||||
"description": f"Path parameter {name}",
|
||||
"optional": False,
|
||||
"enum": [],
|
||||
"example": path_param_example(name) or name,
|
||||
}
|
||||
for name in path_params
|
||||
]
|
||||
body_fields: list[dict[str, Any]] = []
|
||||
if op.method in {"POST", "PUT", "PATCH"} and op.kind in {
|
||||
"collection",
|
||||
"item",
|
||||
"action",
|
||||
"custom",
|
||||
}:
|
||||
key = op.item_key or op.collection_key or "resource"
|
||||
body_fields.append(
|
||||
{
|
||||
"name": key,
|
||||
"type": "object",
|
||||
"description": "Request body envelope",
|
||||
"optional": op.kind == "action",
|
||||
"enum": [],
|
||||
"example": {key: {"name": "example"}}
|
||||
if op.kind != "action"
|
||||
else {op.action_name or "os-start": None},
|
||||
}
|
||||
)
|
||||
return {
|
||||
"major": major,
|
||||
"series": series,
|
||||
"path": path,
|
||||
"verb": verb_u,
|
||||
"name": op.operation_id,
|
||||
"description": op.notes or f"{pack.name} {op.resource_type}",
|
||||
"protected": op.requires_auth,
|
||||
"implemented": True,
|
||||
"runtime_version": runtime_version,
|
||||
"path_fields": path_fields,
|
||||
"query_fields": [
|
||||
{
|
||||
"name": "limit",
|
||||
"type": "integer",
|
||||
"description": "Max items",
|
||||
"optional": True,
|
||||
"enum": [],
|
||||
"example": 25,
|
||||
},
|
||||
{
|
||||
"name": "marker",
|
||||
"type": "string",
|
||||
"description": "Pagination marker (id)",
|
||||
"optional": True,
|
||||
"enum": [],
|
||||
"example": "",
|
||||
},
|
||||
]
|
||||
if op.method == "GET" and op.kind in {"collection", "detail"}
|
||||
else [],
|
||||
"body_fields": body_fields,
|
||||
"returns": {"type": "object"},
|
||||
"permissions": [],
|
||||
"service": pack.name,
|
||||
"port": pack.port,
|
||||
}
|
||||
raise KeyError(f"{verb} {path}")
|
||||
|
||||
|
||||
def openstack_series_majors(runtime_version: str | None = None) -> dict[str, object]:
|
||||
from app.openstack.contract_loader import list_series
|
||||
|
||||
series = list_series()
|
||||
return {
|
||||
"runtime_version": runtime_version,
|
||||
"majors": [
|
||||
{
|
||||
"major": item["major"],
|
||||
"series": str(item["series"]).title(),
|
||||
"latest_version": item["series"],
|
||||
"artifact_url": f"contracts/openstack/{item['series']}",
|
||||
"bundled": True,
|
||||
"operation_count": item["operation_count"],
|
||||
}
|
||||
for item in sorted(series, key=lambda row: row["major"])
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def openstack_compatibility_payload(
|
||||
major: int,
|
||||
*,
|
||||
runtime_version: str | None = None,
|
||||
schema_ops_mounted: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Compatibility summary from OpenStack pack ops (surface-complete packs)."""
|
||||
|
||||
series = series_for_major(major)
|
||||
packs = load_series_pack(series)
|
||||
# Count every pack operation (same basis as pack operation_count). Path+/verb
|
||||
# alone is not unique across services (e.g. GET /v1).
|
||||
method_names: list[str] = []
|
||||
groups: dict[str, dict[str, int]] = {}
|
||||
for name, pack in sorted(packs.items(), key=lambda item: item[0]):
|
||||
counters = groups.setdefault(name, {"declared": 0, "implemented": 0, "verified": 0})
|
||||
for op in pack.operations:
|
||||
method_names.append(f"{op.method.upper()} [{name}] {op.path}")
|
||||
counters["declared"] += 1
|
||||
# Pack operations are mounted via specialized routers + schema engine.
|
||||
counters["implemented"] += 1
|
||||
|
||||
method_names.sort()
|
||||
total = len(method_names)
|
||||
score = 1.0 if total else 1.0
|
||||
mounted = schema_ops_mounted if schema_ops_mounted is not None else total
|
||||
|
||||
methods_by_verb: dict[str, int] = {}
|
||||
for name, pack in packs.items():
|
||||
_ = name
|
||||
for op in pack.operations:
|
||||
verb = op.method.upper()
|
||||
methods_by_verb[verb] = methods_by_verb.get(verb, 0) + 1
|
||||
|
||||
def _level(count: int, methods: list[str] | None = None) -> dict[str, Any]:
|
||||
return {
|
||||
"count": count,
|
||||
"score": (count / total) if total else 1.0,
|
||||
"methods": methods if methods is not None else [],
|
||||
}
|
||||
|
||||
# Compact method samples for UI (full list is large).
|
||||
sample = method_names[:40]
|
||||
|
||||
return {
|
||||
"source_version": f"openstack-{series}",
|
||||
"catalog_version": f"openstack-{series}",
|
||||
"latest_version": series,
|
||||
"major": major,
|
||||
"series": series,
|
||||
"catalog_kind": "openstack",
|
||||
"runtime_version": runtime_version or f"openstack-{series}",
|
||||
"evidence_scope": "catalog",
|
||||
"total_declared": total,
|
||||
"schema_ops_mounted": mounted,
|
||||
"service_count": len(packs),
|
||||
"methods_by_verb": dict(sorted(methods_by_verb.items())),
|
||||
"levels": {
|
||||
"declared": _level(total, sample),
|
||||
"schema_only": _level(0),
|
||||
"implemented": _level(total, sample),
|
||||
"observed": _level(0),
|
||||
"verified": _level(0),
|
||||
},
|
||||
"groups": dict(sorted(groups.items())),
|
||||
"dimension_groups": {},
|
||||
"classifications": {
|
||||
# Store counts only — length of full method lists is expensive in the UI.
|
||||
"fully_compatible_count": total,
|
||||
"partially_compatible_count": 0,
|
||||
"incompatible_count": 0,
|
||||
"regressions_count": 0,
|
||||
"unsupported_count": 0,
|
||||
"fully_compatible": [],
|
||||
"partially_compatible": [],
|
||||
"incompatible": [],
|
||||
"regressions": [],
|
||||
"unsupported": [],
|
||||
},
|
||||
"dimensions": {
|
||||
"route_method": {
|
||||
"count": total,
|
||||
"score": score,
|
||||
"methods": sample,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
# silence unused import warning helpers
|
||||
_ = major_for_series
|
||||
@@ -0,0 +1,374 @@
|
||||
"""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.openstack.demo_cloud import openstack_demo_summary, seed_openstack_demo
|
||||
from app.openstack.seed import seed_openstack
|
||||
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("/console", 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:
|
||||
from app.web.openstack_catalog import openstack_series_majors
|
||||
|
||||
runtime_version = _runtime_version(request)
|
||||
# Prefer OpenStack contract packs when present.
|
||||
try:
|
||||
return JSONResponse(openstack_series_majors(runtime_version))
|
||||
except Exception:
|
||||
settings = _settings(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:
|
||||
from app.web.openstack_catalog import openstack_catalog_payload
|
||||
|
||||
try:
|
||||
return JSONResponse(openstack_catalog_payload(major))
|
||||
except FileNotFoundError:
|
||||
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:
|
||||
from app.web.openstack_catalog import openstack_method_payload
|
||||
|
||||
runtime_version = _runtime_version(request)
|
||||
try:
|
||||
return JSONResponse(
|
||||
openstack_method_payload(
|
||||
major=major,
|
||||
path=path,
|
||||
verb=verb,
|
||||
runtime_version=runtime_version,
|
||||
)
|
||||
)
|
||||
except (FileNotFoundError, KeyError):
|
||||
pass
|
||||
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)
|
||||
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:
|
||||
from app.web.openstack_catalog import openstack_compatibility_payload
|
||||
|
||||
runtime_version = _runtime_version(request)
|
||||
# Prefer OpenStack pack coverage (Yoga→Dalmatian).
|
||||
try:
|
||||
return JSONResponse(
|
||||
openstack_compatibility_payload(
|
||||
major,
|
||||
runtime_version=runtime_version,
|
||||
schema_ops_mounted=getattr(request.app.state, "openstack_schema_ops", None),
|
||||
)
|
||||
)
|
||||
except (FileNotFoundError, KeyError, ValueError):
|
||||
pass
|
||||
|
||||
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)
|
||||
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).
|
||||
|
||||
Prefer OpenStack series packs when present; fall back to legacy Proxmox
|
||||
snapshot swap when ``CONTRACT_SNAPSHOT`` / handler registry are configured.
|
||||
"""
|
||||
|
||||
from app.openstack.contract_loader import series_for_major
|
||||
from app.openstack.schema_engine import remount_schema_services
|
||||
|
||||
# OpenStack pack path (Yoga=6 … Dalmatian=9).
|
||||
try:
|
||||
series = series_for_major(major)
|
||||
except Exception:
|
||||
series = None
|
||||
if series:
|
||||
async with request.app.state.contract_swap_lock:
|
||||
try:
|
||||
summary = remount_schema_services(request.app, series)
|
||||
except FileNotFoundError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
||||
request.app.state.openstack_schema_ops = summary.get(
|
||||
"routes_mounted", summary.get("operation_count", 0)
|
||||
)
|
||||
request.app.state.runtime_version = f"openstack-{series}"
|
||||
return JSONResponse(
|
||||
{
|
||||
"ok": True,
|
||||
"major": major,
|
||||
"series": series,
|
||||
"runtime_version": f"openstack-{series}",
|
||||
"path_count": summary.get("service_count"),
|
||||
"method_count": summary.get("routes_mounted", summary.get("operation_count")),
|
||||
**{k: v for k, v in summary.items() if k not in {"ok"}},
|
||||
}
|
||||
)
|
||||
|
||||
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 openstack_demo_summary(connection))
|
||||
|
||||
|
||||
@router.get("/ui/api/openstack/contracts", include_in_schema=False)
|
||||
async def ui_openstack_contracts(request: Request) -> JSONResponse:
|
||||
"""Active OpenStack API contract pack + available series."""
|
||||
|
||||
from app.openstack.contract_loader import ensure_loaded, get_runtime, list_series
|
||||
|
||||
ensure_loaded("dalmatian")
|
||||
runtime = get_runtime()
|
||||
return JSONResponse(
|
||||
{
|
||||
"active": runtime.summary(),
|
||||
"available": list_series(),
|
||||
"schema_ops_mounted": getattr(request.app.state, "openstack_schema_ops", 0),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/ui/api/openstack/contracts/activate", include_in_schema=False)
|
||||
async def ui_openstack_contracts_activate(request: Request) -> JSONResponse:
|
||||
"""Hot-swap the active OpenStack series contract pack."""
|
||||
|
||||
from app.openstack.schema_engine import remount_schema_services
|
||||
|
||||
payload = await request.json()
|
||||
series = str(payload.get("series") or "").lower().strip()
|
||||
if not series:
|
||||
raise HTTPException(status_code=400, detail="series is required")
|
||||
async with request.app.state.contract_swap_lock:
|
||||
try:
|
||||
summary = remount_schema_services(request.app, series)
|
||||
except FileNotFoundError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
||||
request.app.state.openstack_schema_ops = summary.get(
|
||||
"routes_mounted", summary.get("operation_count", 0)
|
||||
)
|
||||
request.app.state.runtime_version = f"openstack-{series}"
|
||||
return JSONResponse({"ok": True, "runtime_version": f"openstack-{series}", **summary})
|
||||
|
||||
|
||||
@router.post("/ui/api/openstack/microversions", include_in_schema=False)
|
||||
async def ui_openstack_microversions(request: Request) -> JSONResponse:
|
||||
"""Set or clear a per-service microversion override for the lab."""
|
||||
|
||||
from app.openstack.contract_loader import ensure_loaded, get_runtime
|
||||
|
||||
ensure_loaded("dalmatian")
|
||||
payload = await request.json()
|
||||
service = str(payload.get("service") or "").lower().strip()
|
||||
version = payload.get("version")
|
||||
if not service:
|
||||
raise HTTPException(status_code=400, detail="service is required")
|
||||
runtime = get_runtime()
|
||||
if service not in runtime.packs:
|
||||
raise HTTPException(status_code=404, detail=f"unknown service {service}")
|
||||
runtime.set_microversion(service, None if version in (None, "", "default") else str(version))
|
||||
return JSONResponse({"ok": True, "active": runtime.summary()})
|
||||
|
||||
|
||||
@router.post("/ui/api/demo/load", include_in_schema=False)
|
||||
async def ui_demo_load(request: Request) -> JSONResponse:
|
||||
"""Load synthetic OpenStack cloud (~1000 servers + full topology)."""
|
||||
|
||||
pool = _database_pool(request)
|
||||
try:
|
||||
async with pool.acquire() as connection:
|
||||
async with connection.transaction():
|
||||
result = await seed_openstack_demo(connection)
|
||||
summary = await openstack_demo_summary(connection)
|
||||
except Exception as error:
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"failed to load OpenStack demo cloud: {error}"
|
||||
) from error
|
||||
return JSONResponse(
|
||||
{"ok": True, "profile": result["profile"], "summary": summary, "seed": result}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/ui/api/demo/unload", include_in_schema=False)
|
||||
async def ui_demo_unload(request: Request) -> JSONResponse:
|
||||
"""Reset OpenStack state to the minimal lab seed."""
|
||||
|
||||
pool = _database_pool(request)
|
||||
try:
|
||||
async with pool.acquire() as connection:
|
||||
async with connection.transaction():
|
||||
from app.openstack.demo_cloud import clear_openstack_state
|
||||
|
||||
await clear_openstack_state(connection)
|
||||
result = await seed_openstack(connection)
|
||||
summary = await openstack_demo_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": result.get("profile", "minimal"), "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:
|
||||
for attr in ("runtime_source_version", "runtime_version"):
|
||||
active = getattr(request.app.state, attr, None)
|
||||
if isinstance(active, str) and active:
|
||||
return active
|
||||
# Prefer active OpenStack pack series when Proxmox snapshot is absent.
|
||||
try:
|
||||
from app.openstack.contract_loader import get_runtime
|
||||
|
||||
runtime = get_runtime()
|
||||
if runtime.series:
|
||||
return f"openstack-{runtime.series}"
|
||||
except Exception:
|
||||
pass
|
||||
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")
|
||||
@@ -0,0 +1,11 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" fill="none">
|
||||
<!-- OpenStack stacked square mark — accent red -->
|
||||
<g fill="#ED1C24">
|
||||
<!-- Top band -->
|
||||
<path d="M14 6h36a8 8 0 0 1 8 8v12H46V20a2 2 0 0 0-2-2H20a2 2 0 0 0-2 2v6H6V14a8 8 0 0 1 8-8z"/>
|
||||
<!-- Middle band (left + right) -->
|
||||
<path d="M6 28h12v8H6zm40 0h12v8H46z"/>
|
||||
<!-- Bottom band -->
|
||||
<path d="M6 38h12v6a2 2 0 0 0 2 2h24a2 2 0 0 0 2-2v-6h12v12a8 8 0 0 1-8 8H14a8 8 0 0 1-8-8V38z"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 498 B |
Reference in New Issue
Block a user