mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 02:54:26 +00:00
compose_networks, container_networks and compose_external_networks each rebuilt the same Ansible-side context before calling their renderer: resolve the variables, settle the deployment mode, load applications, build the service registry, and close over the config and database lookups. Around forty lines, three times, each with its own copy of _resolve_var. A change to how the registry or the closures are obtained could reach one lookup and miss another. build_context() returns that context once. honour_mode_force carries the only real difference between the callers: compose_external_networks never consulted compose_mode_force, and still does not. The lookups keep only what is theirs - term validation, their own arguments, their renderer call - and drop from 94/90/87 lines to 59/54/53. No assertion changed in the tests; only the mock targets moved with the seam, from the plugin modules to utils.networks.lookup_context. Twelve legacy comments in those test files lost their grandfathering when the files entered the lint's diff scope: two became docstrings naming the call contract, the rest were narration and are gone. Behaviour-preserving: the same 13 lookup tests pass before and after. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
"""Lookup ``compose_external_networks``: the provider role ids whose overlay
|
|
the compose file of ``application_id`` references as ``external: true``.
|
|
|
|
Usage:
|
|
|
|
{{ lookup('compose_external_networks') }}
|
|
|
|
Returns a list of role ids (e.g. ``svc-db-redis``), matching the
|
|
``external: true`` entries ``compose_networks`` emits. The swarm deploy
|
|
handler pre-creates those overlays so a shared provider's network is present
|
|
even when the provider role did not run in the same play.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from ansible.errors import AnsibleError
|
|
from ansible.plugins.lookup import LookupBase
|
|
|
|
from utils.networks.lookup_context import build_context, resolve_var
|
|
from utils.networks.render import compute_external_network_roles
|
|
|
|
|
|
class LookupModule(LookupBase):
|
|
def run(
|
|
self,
|
|
terms: list[Any] | None,
|
|
variables: dict[str, Any] | None = None,
|
|
**kwargs: Any,
|
|
) -> list[Any]:
|
|
if terms:
|
|
raise AnsibleError(
|
|
"compose_external_networks lookup takes no positional terms"
|
|
)
|
|
|
|
ctx = build_context(self, variables, honour_mode_force=False)
|
|
|
|
application_id = resolve_var(ctx.templar, ctx.vars.get("application_id"))
|
|
if not application_id:
|
|
raise AnsibleError(
|
|
"compose_external_networks lookup: application_id is required in variables"
|
|
)
|
|
|
|
return [
|
|
compute_external_network_roles(
|
|
application_id=str(application_id),
|
|
deployment_mode=ctx.deployment_mode,
|
|
registry=ctx.registry,
|
|
lookup_config=ctx.lookup_config,
|
|
lookup_database=ctx.lookup_database,
|
|
)
|
|
]
|