"""The live path end to end: which uploads to read, normalise them, list the scope, reconcile (ADR 0018).""" from __future__ import annotations from typing import Any, Awaitable, Callable, Dict, List, Tuple from app import cmdb, reconcile from app import run_context as rc from app import scope as sc Listing = Callable[[str], Awaitable[Tuple[List[Dict[str, Any]], List[str], List[str]]]] class ScopeIncomplete(cmdb.CmdbFailed): """The scope couldn't be listed in full, so ghosts can't be told from resources that weren't listed.""" def list_scope(provider: str, call: sc.Call): return sc.list_all(provider, call) async def build(payload: Dict[str, Any], ask: cmdb.Ask, listing: Listing, batch_size: int = 25, max_tries: int = 3, log: cmdb.Log = lambda *a, **k: None, sleep=None, ask_relationships: cmdb.Ask = None) -> Dict[str, Any]: gaps = {g.get("source") for g in payload.get("source_gaps") or []} text = payload.get("source_documents") if isinstance(payload.get("source_documents"), str) else None uploads = rc.uploads_from(text, "cmdb") if "cmdb" in gaps or not uploads: why = ("the plan declares no CMDB source" if "cmdb" in gaps else "no CMDB export is listed in the run context's uploads") return reconcile.empty(payload, f"No CMDB was read: {why}. No asset is reported as missing from a CMDB.") kwargs = {"batch_size": batch_size, "max_tries": max_tries, "log": log} if sleep is not None: kwargs["sleep"] = sleep cis, sources = await cmdb.normalise(uploads, ask, **kwargs) rel_uploads = rc.uploads_from(text, "cmdb_relationships") rows: List[Dict[str, Any]] = [] if rel_uploads: rows, rel_sources = await cmdb.normalise_relationships(rel_uploads, ask_relationships or ask, **kwargs) sources += rel_sources relationships, dangling = cmdb.resolve(cis, rows) items, refs, issues = await listing(payload["provider"]) if issues: raise ScopeIncomplete("the scope could not be listed in full, so the CMDB was not reconciled: " + "; ".join(issues)) result = reconcile.reconcile(payload, cis, items, refs, rc.context_section(text)) injections = [x["injection_note"] for x in cis + rows if x.get("injection_detected")] s = result["summary"] note = (f"Read {len(cis)} CIs from {', '.join(u['name'] for u in uploads)} and reconciled them against " f"{len(items)} listed resources and {len(payload.get('records') or [])} inventory records: " f"{s['matched']} matched, {s['stale_attributes']} stale attributes, {s['orphans']} orphans, " f"{s['ghosts']} ghosts. Not drift: {s['retired']} retired, {s['out_of_scope']} outside the scope, " f"{s['unplaced']} with no cloud id or unique name match." + (f" {len(relationships)} relationships between CIs, {len(dangling)} dangling." if relationships or dangling or rel_uploads else "") + (f" {len(injections)} rows carried text that reads as an instruction; it was not acted on." if injections else "") + " No ITSM source was read.") log("cmdb_reconciled", **s) return {"report_id": f"DRIFT-{payload.get('run_id') or payload['engagement_id']}", "engagement_id": payload["engagement_id"], "drift_items": result["drift_items"], "itsm_signals": [], "summary": s, "injections_found": injections, "cmdb_sources": sources, "note": note, "cis": result["ci_results"], "relationships": relationships, "dangling_relationships": dangling}