|
| 1 | +import uuid |
| 2 | +from collections.abc import Iterable, Mapping |
| 3 | +from dataclasses import dataclass, field |
| 4 | +from datetime import datetime |
| 5 | +from typing import Any, Optional, override |
| 6 | + |
| 7 | +from ai.backend.common.types import ResourceSlot, VFolderHostPermissionMap |
| 8 | +from ai.backend.manager.data.permission.id import ScopeId |
| 9 | +from ai.backend.manager.data.permission.types import ( |
| 10 | + EntityType, |
| 11 | + OperationType, |
| 12 | + ScopeType, |
| 13 | +) |
| 14 | +from ai.backend.manager.data.user.types import UserRole |
| 15 | +from ai.backend.manager.types import Creator, OptionalState, PartialModifier, TriState |
| 16 | + |
| 17 | + |
| 18 | +@dataclass |
| 19 | +class UserInfo: |
| 20 | + id: uuid.UUID |
| 21 | + role: UserRole |
| 22 | + domain_name: str |
| 23 | + |
| 24 | + |
| 25 | +@dataclass |
| 26 | +class DomainData: |
| 27 | + name: str |
| 28 | + description: Optional[str] |
| 29 | + is_active: bool |
| 30 | + created_at: datetime = field(compare=False) |
| 31 | + modified_at: datetime = field(compare=False) |
| 32 | + total_resource_slots: ResourceSlot |
| 33 | + allowed_vfolder_hosts: VFolderHostPermissionMap |
| 34 | + allowed_docker_registries: list[str] |
| 35 | + dotfiles: bytes |
| 36 | + integration_id: Optional[str] |
| 37 | + |
| 38 | + def scope_id(self) -> ScopeId: |
| 39 | + return ScopeId( |
| 40 | + scope_type=ScopeType.DOMAIN, |
| 41 | + scope_id=self.name, |
| 42 | + ) |
| 43 | + |
| 44 | + def role_name(self) -> str: |
| 45 | + return f"domain-{self.name}-admin" |
| 46 | + |
| 47 | + def entity_operations(self) -> Mapping[EntityType, Iterable[OperationType]]: |
| 48 | + return { |
| 49 | + entity: OperationType.admin_operations() |
| 50 | + for entity in EntityType.admin_accessible_entity_types_in_domain() |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | +@dataclass |
| 55 | +class DomainCreator(Creator): |
| 56 | + name: str |
| 57 | + description: Optional[str] = None |
| 58 | + is_active: Optional[bool] = None |
| 59 | + total_resource_slots: Optional[ResourceSlot] = None |
| 60 | + allowed_vfolder_hosts: Optional[dict[str, list[str]]] = None |
| 61 | + allowed_docker_registries: Optional[list[str]] = None |
| 62 | + integration_id: Optional[str] = None |
| 63 | + dotfiles: Optional[bytes] = None |
| 64 | + |
| 65 | + @override |
| 66 | + def fields_to_store(self) -> dict[str, Any]: |
| 67 | + to_store: dict[str, Any] = {"name": self.name} |
| 68 | + if self.description is not None: |
| 69 | + to_store["description"] = self.description |
| 70 | + if self.is_active is not None: |
| 71 | + to_store["is_active"] = self.is_active |
| 72 | + if self.total_resource_slots is not None: |
| 73 | + to_store["total_resource_slots"] = self.total_resource_slots |
| 74 | + if self.allowed_vfolder_hosts is not None: |
| 75 | + to_store["allowed_vfolder_hosts"] = self.allowed_vfolder_hosts |
| 76 | + if self.allowed_docker_registries is not None: |
| 77 | + to_store["allowed_docker_registries"] = self.allowed_docker_registries |
| 78 | + if self.integration_id is not None: |
| 79 | + to_store["integration_id"] = self.integration_id |
| 80 | + if self.dotfiles is not None: |
| 81 | + to_store["dotfiles"] = self.dotfiles |
| 82 | + return to_store |
| 83 | + |
| 84 | + |
| 85 | +@dataclass |
| 86 | +class DomainModifier(PartialModifier): |
| 87 | + name: OptionalState[str] = field(default_factory=OptionalState.nop) |
| 88 | + description: TriState[str] = field(default_factory=TriState.nop) |
| 89 | + is_active: OptionalState[bool] = field(default_factory=OptionalState.nop) |
| 90 | + total_resource_slots: TriState[ResourceSlot] = field(default_factory=TriState.nop) |
| 91 | + allowed_vfolder_hosts: OptionalState[dict[str, list[str]]] = field( |
| 92 | + default_factory=OptionalState.nop |
| 93 | + ) |
| 94 | + allowed_docker_registries: OptionalState[list[str]] = field(default_factory=OptionalState.nop) |
| 95 | + integration_id: TriState[str] = field(default_factory=TriState.nop) |
| 96 | + |
| 97 | + @override |
| 98 | + def fields_to_update(self) -> dict[str, Any]: |
| 99 | + to_update: dict[str, Any] = {} |
| 100 | + self.name.update_dict(to_update, "name") |
| 101 | + self.description.update_dict(to_update, "description") |
| 102 | + self.is_active.update_dict(to_update, "is_active") |
| 103 | + self.total_resource_slots.update_dict(to_update, "total_resource_slots") |
| 104 | + self.allowed_vfolder_hosts.update_dict(to_update, "allowed_vfolder_hosts") |
| 105 | + self.allowed_docker_registries.update_dict(to_update, "allowed_docker_registries") |
| 106 | + self.integration_id.update_dict(to_update, "integration_id") |
| 107 | + return to_update |
| 108 | + |
| 109 | + |
| 110 | +@dataclass |
| 111 | +class DomainNodeModifier(PartialModifier): |
| 112 | + description: TriState[str] = field(default_factory=TriState[str].nop) |
| 113 | + is_active: OptionalState[bool] = field(default_factory=OptionalState[bool].nop) |
| 114 | + total_resource_slots: TriState[ResourceSlot] = field(default_factory=TriState[ResourceSlot].nop) |
| 115 | + allowed_vfolder_hosts: OptionalState[dict[str, list[str]]] = field( |
| 116 | + default_factory=OptionalState[dict[str, list[str]]].nop |
| 117 | + ) |
| 118 | + allowed_docker_registries: OptionalState[list[str]] = field( |
| 119 | + default_factory=OptionalState[list[str]].nop |
| 120 | + ) |
| 121 | + integration_id: TriState[str] = field(default_factory=TriState[str].nop) |
| 122 | + dotfiles: OptionalState[bytes] = field(default_factory=OptionalState[bytes].nop) |
| 123 | + |
| 124 | + @override |
| 125 | + def fields_to_update(self) -> dict[str, Any]: |
| 126 | + to_update: dict[str, Any] = {} |
| 127 | + self.description.update_dict(to_update, "description") |
| 128 | + self.is_active.update_dict(to_update, "is_active") |
| 129 | + self.total_resource_slots.update_dict(to_update, "total_resource_slots") |
| 130 | + self.allowed_vfolder_hosts.update_dict(to_update, "allowed_vfolder_hosts") |
| 131 | + self.allowed_docker_registries.update_dict(to_update, "allowed_docker_registries") |
| 132 | + self.integration_id.update_dict(to_update, "integration_id") |
| 133 | + self.dotfiles.update_dict(to_update, "dotfiles") |
| 134 | + return to_update |
0 commit comments