mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 06:17:25 +00:00
feat(services): resolve_service_config with provider fallback
Resolve services.<key>.<field> with consumer-override -> provider-native fallback (the provider being the role whose entity name is <key>, e.g. svc-net-tor for tor). Keeps provider defaults like tor.exclusive/tor.primary in one place while consumers stay free to override per app; consumed by utils.cache.domains for onion-domain injection. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
897abcc28c
commit
f6bed25b67
2 files changed
+142
No files matched your search
@@ -0,0 +1,69 @@
|
||||
"""Unit tests for resolve_service_config (consumer-override -> provider-native)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from utils.roles.applications.services.config import resolve_service_config
|
||||
|
||||
# Provider entity name must equal the service key for the registry to register
|
||||
# it as the provider (see discover_role_services): svc-net-tor -> entity "tor".
|
||||
APPS = {
|
||||
"svc-net-tor": {
|
||||
"services": {
|
||||
"tor": {
|
||||
"enabled": True,
|
||||
"shared": True,
|
||||
"exclusive": True,
|
||||
"primary": True,
|
||||
}
|
||||
}
|
||||
},
|
||||
"web-app-consumer": {"services": {"tor": {"enabled": True, "shared": True}}},
|
||||
"web-app-override": {
|
||||
"services": {"tor": {"enabled": True, "shared": True, "exclusive": False}}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class TestResolveServiceConfig(unittest.TestCase):
|
||||
def test_falls_back_to_provider_native(self):
|
||||
self.assertIs(
|
||||
resolve_service_config(APPS, "web-app-consumer", "tor", "exclusive"),
|
||||
True,
|
||||
)
|
||||
self.assertIs(
|
||||
resolve_service_config(APPS, "web-app-consumer", "tor", "primary"),
|
||||
True,
|
||||
)
|
||||
|
||||
def test_consumer_override_wins(self):
|
||||
self.assertIs(
|
||||
resolve_service_config(APPS, "web-app-override", "tor", "exclusive"),
|
||||
False,
|
||||
)
|
||||
|
||||
def test_default_when_neither_declares(self):
|
||||
self.assertEqual(
|
||||
resolve_service_config(
|
||||
APPS, "web-app-consumer", "tor", "nonexistent", default="fb"
|
||||
),
|
||||
"fb",
|
||||
)
|
||||
|
||||
def test_provider_reads_its_own_native_value(self):
|
||||
# The provider role itself resolves to its own declaration, not a
|
||||
# fallback (provider == application_id short-circuits).
|
||||
self.assertIs(
|
||||
resolve_service_config(APPS, "svc-net-tor", "tor", "exclusive"),
|
||||
True,
|
||||
)
|
||||
|
||||
def test_unknown_service_key_returns_default(self):
|
||||
self.assertIsNone(
|
||||
resolve_service_config(APPS, "web-app-consumer", "no-such-svc", "x")
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
unittest.main()
|
||||
@@ -0,0 +1,73 @@
|
||||
"""Resolve a single ``services.<key>.<field>`` with provider fallback.
|
||||
|
||||
A consuming role MAY override a shared service's config field in its own
|
||||
``meta/services.yml`` ``services.<key>`` block. When it does not, the value
|
||||
falls back to the *provider* role's native declaration — the role whose entity
|
||||
name is ``<key>`` (e.g. ``svc-net-tor`` for ``tor``). This keeps provider
|
||||
defaults (e.g. ``tor.exclusive`` / ``tor.primary``) in one place while every
|
||||
consumer stays free to override the field per app.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from utils.roles.applications.config import get
|
||||
from utils.roles.applications.services.registry import (
|
||||
build_service_registry_from_applications,
|
||||
)
|
||||
|
||||
_MISSING = object()
|
||||
|
||||
|
||||
def _service_entry(
|
||||
applications: dict[str, Any],
|
||||
application_id: str,
|
||||
service_key: str,
|
||||
) -> dict[str, Any]:
|
||||
entry = get(
|
||||
applications=applications,
|
||||
application_id=application_id,
|
||||
config_path=f"services.{service_key}",
|
||||
strict=False,
|
||||
default={},
|
||||
skip_missing_app=True,
|
||||
)
|
||||
return entry if isinstance(entry, dict) else {}
|
||||
|
||||
|
||||
def resolve_service_config(
|
||||
applications: dict[str, Any],
|
||||
application_id: str,
|
||||
service_key: str,
|
||||
config_key: str,
|
||||
default: Any = None,
|
||||
*,
|
||||
service_registry: dict[str, Any] | None = None,
|
||||
) -> Any:
|
||||
"""Return ``services.<service_key>.<config_key>`` for ``application_id``.
|
||||
|
||||
Resolution order:
|
||||
1. the consuming role's own override, when it declares ``config_key``;
|
||||
2. the provider role's native value (role whose entity is ``service_key``);
|
||||
3. ``default``.
|
||||
|
||||
Pass ``service_registry`` to reuse a registry already built by the caller
|
||||
(avoids rebuilding it once per lookup inside a loop).
|
||||
"""
|
||||
consumer = _service_entry(applications, application_id, service_key)
|
||||
if config_key in consumer:
|
||||
return consumer[config_key]
|
||||
|
||||
registry = (
|
||||
service_registry
|
||||
if service_registry is not None
|
||||
else build_service_registry_from_applications(applications)
|
||||
)
|
||||
entry = registry.get(service_key) or {}
|
||||
provider = entry.get("role")
|
||||
if isinstance(provider, str) and provider and provider != application_id:
|
||||
provider_cfg = _service_entry(applications, provider, service_key)
|
||||
if config_key in provider_cfg:
|
||||
return provider_cfg[config_key]
|
||||
return default
|
||||
Reference in new issue
Block a user