8000 feat: polyglot CM single-owner prototype by dominicusin · Pull Request #11 · dominicusin/portfolio · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Python tests
on:
push: {branches: [main]}
pull_request: {branches: [main]}
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with: {python-version: "3.11"}
- run: pip install pytest
- run: python -m pytest tests -q
Binary file not shown.
66 changes: 66 additions & 0 deletions automation/polyglot_cm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""POLY-001 prototype: one resource, one owning manager, explicit conflicts."""
from dataclasses import dataclass, field
from typing import Any, Callable, Dict, Iterable, List, Optional


class ManagerConflict(RuntimeError):
"""A manager tried to own or change a resource owned by another manager."""


@dataclass
class Manager:
name: str
apply: Callable[[Any], Any]


@dataclass
class Resource:
id: str
kind: str
spec: Dict[str, Any] = field(default_factory=dict)


class Fabric:
"""Minimal control plane where ownership is declared and enforced."""

def __init__(self):
self._managers: Dict[str, Manager] = {}
self._owners: Dict[str, str] = {}
self._applied: List[str] = []

def register(self, manager: Manager) -> None:
self._managers[manager.name] = manager

def declare_owner(self, resource: Resource, manager_name: str) -> None:
if manager_name not in self._managers:
raise KeyError(f"unknown manager: {manager_name}")
current = self._owners.get(resource.id)
if current and current != manager_name:
raise ManagerConflict(
f"{resource.id} is owned by {current}, not {manager_name}")
self._owners[resource.id] = manager_name

def apply(self, resource: Resource, manager_name: str) -> Any:
owner = self._owners.get(resource.id)
if owner is None:
raise ManagerConflict(f"{resource.id} has no declared owner")
if owner != manager_name:
8000 raise ManagerConflict(f"{resource.id} belongs to {owner}")
result = self._managers[manager_name].apply(resource)
self._applied.append(resource.id)
return result

def conflicts(self, claims: Iterable[tuple]) -> List[str]:
"""Detect two managers claiming the same resource id."""
seen: Dict[str, str] = {}
out = []
for resource_id, manager_name in claims:
previous = seen.get(resource_id)
if previous and previous != manager_name:
out.append(f"{resource_id}: {previous} vs {manager_name}")
else:
seen[resource_id] = manager_name
return out

def applied(self) -> List[str]:
return list(self._applied)
Binary file not shown.
39 changes: 39 additions & 0 deletions tests/test_polyglot_cm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""POLY-001 conflict-detection tests."""
import unittest

from automation.polyglot_cm import Fabric, Manager, ManagerConflict, Resource


class TestPolyglotFabric(unittest.TestCase):
def setUp(self):
self.fabric = Fabric()
self.fabric.register(Manager("ansible", lambda r: "ansible:" + r.id))
self.fabric.register(Manager("nix", lambda r: "nix:" + r.id))
self.resource = Resource("host:wdmch", "host")

def test_owner_can_apply(self):
self.fabric.declare_owner(self.resource, "ansible")
self.assertEqual(self.fabric.apply(self.resource, "ansible"), "ansible:host:wdmch")

def test_foreign_manager_cannot_apply(self):
self.fabric.declare_owner(self.resource, "ansible")
with self.assertRaises(ManagerConflict):
self.fabric.apply(self.resource, "nix")
self.assertEqual(self.fabric.applied(), [])

def test_second_owner_is_rejected(self):
self.fabric.declare_owner(self.resource, "ansible")
with self.assertRaises(ManagerConflict):
self.fabric.declare_owner(self.resource, "nix")

def test_conflict_detection(self):
claims = [("host:a", "ansible"), ("host:b", "nix"), ("host:a", "nix")]
self.assertEqual(self.fabric.conflicts(claims), ["host:a: ansible vs nix"])

def test_unknown_manager_is_rejected(self):
with self.assertRaises(KeyError):
self.fabric.declare_owner(self.resource, "puppet")


if __name__ == "__main__":
unittest.main()
Loading
0