|
| 1 | +"""Composite spec for race-free scope provisioning. |
| 2 | +
|
| 3 | +A scope-creator spec coordinates the inserts that together provision a new RBAC |
| 4 | +scope: the scope row itself (domain, project, ...), any parent-scope mapping |
| 5 | +rows, and roles + role-scope associations + permissions instantiated from each |
| 6 | +active role preset matching the scope type. |
| 7 | +
|
| 8 | +The spec itself owns no table; each returned sub-spec owns exactly one table, |
| 9 | +preserving the per-spec single-table rule. Orchestration lives in |
| 10 | +:meth:`ScopeWriteOps.create_scope`. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +from abc import ABC, abstractmethod |
| 16 | +from collections.abc import Sequence |
| 17 | +from dataclasses import dataclass |
| 18 | + |
| 19 | +from ai.backend.common.data.permission.types import RBACElementType |
| 20 | +from ai.backend.manager.models.base import Base |
| 21 | +from ai.backend.manager.models.rbac_models.association_scopes_entities import ( |
| 22 | + AssociationScopesEntitiesRow, |
| 23 | +) |
| 24 | +from ai.backend.manager.models.rbac_models.role import RoleRow |
| 25 | + |
| 26 | +from .creator import CreatorSpec |
| 27 | + |
| 28 | + |
| 29 | +@dataclass(frozen=True) |
| 30 | +class ScopeContext: |
| 31 | + """Locator for a scope row. |
| 32 | +
|
| 33 | + Carries the ``(scope_type, scope_id)`` pair used by downstream RBAC tables |
| 34 | + (``permissions``, ``association_scopes_entities``) to reference the scope. |
| 35 | + """ |
| 36 | + |
| 37 | + scope_type: RBACElementType |
| 38 | + scope_id: str |
| 39 | + |
| 40 | + |
| 41 | +class ScopeCreatorSpec[TScopeRow: Base](ABC): |
| 42 | + """Coordinator for ``scope row + parent-scope association rows``. |
| 43 | +
|
| 44 | + Subclass per scope type (e.g. ``DomainScopeCreatorSpec``, |
| 45 | + ``ProjectScopeCreatorSpec``). Each returned sub-spec owns exactly one table. |
| 46 | + """ |
| 47 | + |
| 48 | + @abstractmethod |
| 49 | + def scope_spec(self) -> CreatorSpec[TScopeRow]: |
| 50 | + """Single-table spec for the scope row.""" |
| 51 | + raise NotImplementedError |
| 52 | + |
| 53 | + @abstractmethod |
| 54 | + def extract_scope_context(self, scope_row: TScopeRow) -> ScopeContext: |
| 55 | + """Derive ``(scope_type, scope_id)`` from the just-inserted scope row. |
| 56 | +
|
| 57 | + The orchestrator uses this to look up matching role presets and to populate |
| 58 | + ``scope_id`` on derived permission and association rows. |
| 59 | + """ |
| 60 | + raise NotImplementedError |
| 61 | + |
| 62 | + @abstractmethod |
| 63 | + def parent_association_specs( |
| 64 | + self, |
| 65 | + scope_row: TScopeRow, |
| 66 | + ) -> Sequence[CreatorSpec[AssociationScopesEntitiesRow]]: |
| 67 | + """Parent-scope mapping rows (e.g. project under domain). |
| 68 | +
|
| 69 | + Return ``[]`` if the scope type has no parent. |
| 70 | + """ |
| 71 | + raise NotImplementedError |
| 72 | + |
| 73 | + |
| 74 | +@dataclass |
| 75 | +class ScopeCreator[TScopeRow: Base]: |
| 76 | + """Bundles a scope-creator spec for ``ScopeWriteOps.create_scope``.""" |
| 77 | + |
| 78 | + spec: ScopeCreatorSpec[TScopeRow] |
| 79 | + |
| 80 | + |
| 81 | +@dataclass |
| 82 | +class ScopeCreatorResult[TScopeRow: Base]: |
| 83 | + """Outcome of a successful scope provisioning. |
| 84 | +
|
| 85 | + Only surfaces the freshly-inserted scope row and the roles that were |
| 86 | + instantiated from active role presets. Auxiliary rows (permissions, |
| 87 | + role-to-scope associations, parent-scope associations) are still |
| 88 | + inserted by the orchestrator but are not returned. |
| 89 | + """ |
| 90 | + |
| 91 | + scope_row: TScopeRow |
| 92 | + role_rows: list[RoleRow] |
0 commit comments