Files
DECNET/decnet/web/router/topology/api_get_topology.py
anti f182c98ffa feat(api): phase 3 step 2 — topology read endpoints (list/get/status/catalog)
GET /api/v1/topologies — paginated list with status filter. Extends
repo.list_topologies() to accept limit/offset and adds count_topologies()
for the total envelope field.

GET /api/v1/topologies/{id} — hydrated TopologyDetail; 404 if missing.
GET /api/v1/topologies/{id}/status-events — audit trail, limit-capped.

Catalog helpers for the phase-4 canvas UI:
* GET /topologies/services — full service catalog.
* GET /topologies/next-subnet?base=172.20 — wraps SubnetAllocator against
  reserved_subnets across non-torn-down topologies.
* GET /topologies/{id}/lans/{lan_id}/next-ip — IPAllocator pre-seeded
  with existing decky IPs in that LAN.

All read routes are viewer-or-admin. Sub-routers are included in an
order that keeps literal catalog paths (/services, /next-subnet) from
being shadowed by the /{topology_id} trie branch.
2026-04-20 18:25:33 -04:00

67 lines
2.1 KiB
Python

"""GET /topologies/{id} and /topologies/{id}/status-events."""
from __future__ import annotations
from fastapi import APIRouter, Depends, HTTPException, Query
from decnet.telemetry import traced as _traced
from decnet.topology.persistence import hydrate
from decnet.web.db.models import (
DeckyRow,
EdgeRow,
LANRow,
TopologyDetail,
TopologyStatusEventRow,
TopologySummary,
)
from decnet.web.dependencies import repo, require_viewer
router = APIRouter()
@router.get(
"/{topology_id}",
tags=["MazeNET Topologies"],
response_model=TopologyDetail,
responses={
401: {"description": "Missing or invalid credentials"},
403: {"description": "Insufficient permissions"},
404: {"description": "Topology not found"},
},
)
@_traced("api.topology.get")
async def api_get_topology(
topology_id: str,
_viewer: dict = Depends(require_viewer),
) -> TopologyDetail:
hydrated = await hydrate(repo, topology_id)
if hydrated is None:
raise HTTPException(status_code=404, detail="Topology not found")
return TopologyDetail(
topology=TopologySummary(**hydrated["topology"]),
lans=[LANRow(**r) for r in hydrated["lans"]],
deckies=[DeckyRow(**r) for r in hydrated["deckies"]],
edges=[EdgeRow(**r) for r in hydrated["edges"]],
)
@router.get(
"/{topology_id}/status-events",
tags=["MazeNET Topologies"],
response_model=list[TopologyStatusEventRow],
responses={
401: {"description": "Missing or invalid credentials"},
403: {"description": "Insufficient permissions"},
404: {"description": "Topology not found"},
},
)
@_traced("api.topology.status_events")
async def api_get_status_events(
topology_id: str,
limit: int = Query(default=100, ge=1, le=1000),
_viewer: dict = Depends(require_viewer),
) -> list[TopologyStatusEventRow]:
if await repo.get_topology(topology_id) is None:
raise HTTPException(status_code=404, detail="Topology not found")
rows = await repo.list_topology_status_events(topology_id, limit=limit)
return [TopologyStatusEventRow(**r) for r in rows]