"""A live run's CMDB reconciliation, in code: judged CIs against the scope listing and the inventory (ADR 0018). No model is called here. A CI matches a resource by its cloud resource id, or by name when exactly one listed resource has that name. Matched CIs are compared with their inventory record; CIs that match nothing are ghosts only when their id lies in a scope container the listing shows; inventory records no CI matched are orphans. """ from __future__ import annotations import re from typing import Any, Dict, List, Optional, Tuple from app import scope as sc AZURE_CONTAINER = re.compile(r"^(/subscriptions/[^/]+(?:/resourcegroups/[^/]+)?)", re.I) GCP_PROJECT = re.compile(r"/projects/([^/]+)(?:/|$)") def _key(provider: str, rid: Optional[str]) -> str: rid = (rid or "").strip().rstrip("/") return rid.lower() if provider == "azure" else rid def _last(rid: str) -> str: return rid.rstrip("/").split("/")[-1].lower() def container(provider: str, rid: Optional[str]) -> Optional[str]: """The scope container a resource id sits in: an Azure subscription/resource group, or a GCP project.""" if not rid: return None if provider == "azure": found = AZURE_CONTAINER.match(rid.strip()) return found.group(1).lower() if found else None found = GCP_PROJECT.search(rid) return f"projects/{found.group(1)}" if found else None def _norm(value: Any) -> str: return "".join(str(value or "").lower().split()) def reconcile(payload: Dict[str, Any], cis: List[Dict[str, Any]], items: List[Dict[str, Any]], listing_refs: List[str], context: Dict[str, Any]) -> Dict[str, Any]: provider = payload["provider"] accounting = sc.account(provider, items) narrowing = context.get("scope") if isinstance(context.get("scope"), dict) else {} regions = {str(r).lower() for r in (narrowing.get("regions") or [])} exclude = {str(e).lower() for e in (narrowing.get("exclude") or [])} listed: Dict[str, str] = {} for entry in accounting["entries"]: listed[_key(provider, entry["id"])] = "entry" for folded in accounting["folded"]: listed[_key(provider, folded["id"])] = "folded" for rid in accounting["structure"]: listed[_key(provider, rid)] = "structure" by_name: Dict[str, List[str]] = {} for entry in accounting["entries"]: by_name.setdefault(_last(entry["id"]), []).append(_key(provider, entry["id"])) containers = {c for c in (container(provider, rid) for rid in [e["id"] for e in accounting["entries"]] + [f["id"] for f in accounting["folded"]] + accounting["structure"]) if c} containers |= {_key(provider, rid) for rid in accounting["structure"]} records = {_key(provider, r.get("scope_entry_id")): r for r in payload.get("records") or [] if r.get("scope_entry_id")} drift: List[Dict[str, Any]] = [] counts = {"matched": 0, "orphans": 0, "ghosts": 0, "stale_attributes": 0, "retired": 0, "out_of_scope": 0, "unplaced": 0} matched_records = set() results: List[Dict[str, Any]] = [] # one per CI: the CMDB node's state and its link to a resource (ADR 0019) def result(ci: Dict[str, Any], state: str, canonical: Optional[str] = None, basis: Optional[str] = None, resource: Optional[str] = None) -> None: results.append({"ci_ref": ci["ci_ref"], "name": ci.get("name"), "ci_class": ci.get("ci_class"), "lifecycle": ci["lifecycle"], "result": state, "matched_canonical_id": canonical, "matched_resource_id": resource, "match_basis": basis, # What the CMDB records about ownership, verbatim: the application grouping and the asset # owners in the dependency step are built from these (ADR 0021). "owner": ci.get("owner"), "business_service": ci.get("business_service"), "support_group": ci.get("support_group"), "evidence_ref": ci["evidence_ref"]}) def item(kind: str, ci: Optional[Dict[str, Any]], record: Optional[Dict[str, Any]], action: str, field: Optional[str] = None, discovered: Any = None, cmdb_value: Any = None, refs: Tuple[str, ...] = ()) -> None: evidence = [ci["evidence_ref"]] if ci else [] evidence += [s.get("evidence_ref") for s in (record or {}).get("sources") or [] if s.get("evidence_ref")] drift.append({"drift_id": f"DR-{len(drift) + 1:03d}", "type": kind, "canonical_id": (record or {}).get("canonical_id"), "ci_ref": ci["ci_ref"] if ci else None, "field": field, "discovered_value": None if discovered is None else str(discovered), "cmdb_value": None if cmdb_value is None else str(cmdb_value), "evidence_refs": evidence + list(refs), "suggested_action": action}) for ci in cis: target, basis = None, None if ci.get("cloud_resource_id") and _key(provider, ci["cloud_resource_id"]) in listed: target, basis = _key(provider, ci["cloud_resource_id"]), "cloud_resource_id" elif ci.get("name") and len(by_name.get(ci["name"].strip().lower(), [])) == 1: target, basis = by_name[ci["name"].strip().lower()][0], "name" if target: counts["matched"] += 1 record = records.get(target) if record is None: result(ci, "matched", None, basis, target) continue # folded, structural or not inventoried (a declared gap): it exists; nothing to compare result(ci, "matched", record.get("canonical_id"), basis, record.get("scope_entry_id")) matched_records.add(target) stale = [] env = record.get("environment") if ci["environment"] != "unknown" and env not in (None, "unknown") and ci["environment"] != env: stale.append(("environment", env, ci.get("environment_raw") or ci["environment"], "update_attribute")) site = (record.get("facts") or {}).get("site") if ci.get("location") and site and ci["location"] != _norm(site): stale.append(("location", site, ci.get("location_raw"), "update_attribute")) if ci["lifecycle"] == "retired": stale.append(("status", "present in the cloud listing", ci.get("status_raw") or "retired", "investigate")) facts = record.get("facts") or {} for field in ("owner", "business_service"): if ci.get(field) and facts.get(field) and _norm(ci[field]) != _norm(facts[field]): stale.append((field, facts[field], ci[field], "update_attribute")) for field, discovered, cmdb_value, action in stale: item("stale_attribute", ci, record, action, field, discovered, cmdb_value) counts["stale_attributes"] += 1 continue if ci["lifecycle"] == "retired": counts["retired"] += 1 result(ci, "retired") continue rid = ci.get("cloud_resource_id") if not rid: counts["unplaced"] += 1 # nothing ties it to the cloud: absence from a listing proves nothing result(ci, "unplaced") continue if container(provider, rid) not in containers: counts["out_of_scope"] += 1 result(ci, "out_of_scope") continue where = ci.get("location") uncovered = bool(regions) and (not where or where not in regions) excluded = _last(rid) in exclude or _key(provider, rid) in {_key(provider, e) for e in exclude} action = "investigate" if uncovered or excluded or ci["lifecycle"] == "unknown" else "retire_ci" item("ghost", ci, None, action, refs=tuple(listing_refs[:3])) counts["ghosts"] += 1 result(ci, "ghost") for key, record in records.items(): if key not in matched_records: item("orphan", None, record, "add_ci", "resource", record.get("scope_entry_id")) counts["orphans"] += 1 return {"drift_items": drift, "summary": counts, "ci_results": results} def empty(payload: Dict[str, Any], note: str) -> Dict[str, Any]: return {"report_id": f"DRIFT-{payload.get('run_id') or payload['engagement_id']}", "engagement_id": payload["engagement_id"], "drift_items": [], "itsm_signals": [], "summary": {"matched": 0, "orphans": 0, "ghosts": 0, "stale_attributes": 0}, "injections_found": [], "cmdb_sources": [], "note": note, "cis": [], "relationships": [], "dangling_relationships": []}