Files
computer-playbook/plugins/lookup/config.py
T
kevinveenbirkenbachandClaude Opus 4.7 9c35909752 refactor(utils/cache): split data.py into base/applications/users/domains
The 934-line `utils/cache/data.py` monolith mixed cross-cutting helpers
(constants, yaml-loaders, deep-merge, signatures, render machinery)
with three independent domain caches (applications, users, domains).
Per the KISS/SPOT/DRY guidelines, split it along domain boundaries so
each file has a single, well-scoped responsibility:

  utils/cache/__init__.py     — package; owns _reset_cache_for_tests()
                                orchestrator
  utils/cache/base.py         — cross-cutting helpers: constants,
                                yaml-loaders, _deep_merge, cache-key +
                                content-fingerprint signatures, the
                                cross-domain re-entry guard, and the
                                templar-render machinery (the only
                                ansible-coupled symbol, lazy-imports
                                its dependency)
  utils/cache/applications.py — variants + defaults +
                                get_merged_applications. Strictly
                                ansible-free at import time so the
                                GitHub Actions runner-host CLI path
                                (cli.deploy.development.init →
                                plan_dev_inventory_matrix →
                                get_variants) keeps working without
                                ansible installed
  utils/cache/users.py        — user defs, token store hydration,
                                alias materialization,
                                get_user_defaults, get_merged_users
  utils/cache/domains.py      — get_merged_domains, derived from the
                                merged applications view
  utils/cache/data.py         — DELETED. Façade is gone; the package
                                exports nothing the domain modules
                                don't already

Backward-compat: the facade was deleted (per operator request) and all
36+ consumers — 18 plugins, 2 CLI entry points, 9 test files, 4 doc
files, plus a role-internal lookup_plugin — were rewired to import
directly from the owning domain module. `_reset_cache_for_tests` is
now exposed at `from utils.cache import _reset_cache_for_tests`.

Tests added (per-module, focused):
  tests/unit/utils/cache/test_base.py            (31 cases)
  tests/unit/utils/cache/test_applications.py    ( 9 cases)
  tests/unit/utils/cache/test_users_module.py    (21 cases; suffix
                                                  disambiguates from
                                                  the existing
                                                  tests/unit/plugins/
                                                  lookup/test_users.py)
  tests/unit/utils/cache/test_domains.py         ( 6 cases)

The ansible-block invariants from CI runs 24934007615 and 24935979190
are now pinned via subprocess isolation: each ansible-less import test
spawns a fresh `python3 -c '…'` with a meta_path filter denying every
`ansible*` import and asserts the import + invocation succeed in that
clean process. The earlier in-process sys.modules-manipulation shims
fragmented module identity and broke under Python 3.13 inside the
infinito container; subprocess isolation has neither problem.

Doc-style audit (per docs/contributing/documentation.md):
  - Removed em-dash from utils/cache/README.md (split into two
    sentences)
  - Updated stale `utils.runtime_data` reference in
    docs/contributing/design/matrix-deploy.md
  - Removed two SPOT self-declarations
    (docs/agents/files/group_vars/applications.md and
    docs/contributing/artefact/files/plugins/lookup/users.md) per
    the documentation rule that SPOT status is conferred by
    cross-links, not by the page asserting it about itself

Empirical proof:
  - All four domain modules import without ansible (subprocess
    isolation tests)
  - get_variants(roles_dir=ROLES_DIR) returns 99 apps in the
    ansible-blocked subprocess
  - 142 cache tests + 1085 wider-suite tests pass; the only
    remaining failures (test_host_vars free-function fixture bug,
    test_runtime_performance timing flake) are pre-existing and
    unrelated to this refactor (verified via `git stash`).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-26 00:27:57 +02:00

121 lines
4.1 KiB
Python

# lookup_plugins/config.py
from __future__ import annotations
from typing import Any, Dict, Optional
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from utils.applications.config import get
from utils.cache.applications import get_merged_applications
from utils.cache.base import _render_with_templar
from utils.cache.users import get_merged_users
class LookupModule(LookupBase):
"""
lookup('config', application_id, config_path[, default])
- applications are resolved via lookup('applications')
- default behavior is strict=True (missing keys raise)
- if a 3rd argument (default) is provided, strict=False and that default is returned
- parameters:
1) application_id
2) config_path
3) optional default value
"""
def run(self, terms, variables: Optional[Dict[str, Any]] = None, **kwargs):
if not terms or len(terms) not in (2, 3):
raise AnsibleError(
"lookup('config', application_id, config_path[, default]) expects 2 or 3 terms."
)
application_id = terms[0]
config_path = terms[1]
default_provided = len(terms) == 3
default_value = terms[2] if default_provided else None
strict = not default_provided
templar = getattr(self, "_templar", None)
variables = variables or getattr(self._templar, "available_variables", {}) or {}
applications = get_merged_applications(
variables=variables,
roles_dir=kwargs.get("roles_dir"),
templar=templar,
)
if config_path.startswith("users."):
user_path = config_path.split(".")
if len(user_path) < 2:
raise AnsibleError(
"lookup('config', ...): users path must include a canonical user key."
)
app_users = get(
applications=applications,
application_id=application_id,
config_path="users",
strict=False,
default={},
skip_missing_app=False,
)
canonical_user_key = user_path[1]
if not isinstance(app_users, dict) or canonical_user_key not in app_users:
if default_provided:
return [default_value]
raise AnsibleError(
f"lookup('config', ...): application '{application_id}' does not define user "
f"'{canonical_user_key}'."
)
users = get_merged_users(
variables=variables,
roles_dir=kwargs.get("roles_dir"),
templar=templar,
)
user_value = users.get(canonical_user_key)
if user_value is None:
if default_provided:
return [default_value]
raise AnsibleError(
f"lookup('config', ...): canonical user '{canonical_user_key}' not found."
)
value: Any = user_value
for part in user_path[2:]:
if not isinstance(value, dict) or part not in value:
if default_provided:
return [default_value]
raise AnsibleError(
f"lookup('config', ...): missing user path '{config_path}'."
)
value = value[part]
return [
_render_with_templar(
value,
templar=templar,
variables=variables,
raw_applications=applications,
raw_users=users,
)
]
value = get(
applications=applications,
application_id=application_id,
config_path=config_path,
strict=strict,
default=default_value,
skip_missing_app=not strict,
)
value = _render_with_templar(
value,
templar=templar,
variables=variables,
raw_applications=applications,
)
# lookup plugins must return a list
return [value]