-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathentity_adapter.py
More file actions
67 lines (56 loc) · 2.15 KB
/
entity_adapter.py
File metadata and controls
67 lines (56 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Adapter for Entity API requests.
Handles conversion of request DTOs to BatchQuerier objects.
"""
from __future__ import annotations
from ai.backend.common.data.permission.types import RBACElementType
from ai.backend.common.dto.manager.rbac.request import (
SearchEntitiesRequest,
)
from ai.backend.common.dto.manager.rbac.response import EntityDTO
from ai.backend.manager.api.rest.adapter import BaseFilterAdapter
from ai.backend.manager.data.permission.entity import EntityData
from ai.backend.manager.repositories.base import (
BatchQuerier,
OffsetPagination,
)
from ai.backend.manager.repositories.permission_controller.options import (
EntityScopeConditions,
)
__all__ = ("EntityAdapter",)
class EntityAdapter(BaseFilterAdapter):
"""Adapter for converting entity requests to BatchQuerier objects."""
def build_querier(
self,
scope_type: RBACElementType,
scope_id: str,
entity_type: RBACElementType,
request: SearchEntitiesRequest,
) -> BatchQuerier:
"""Build a BatchQuerier for entity search.
Args:
scope_type: The scope type to search within
scope_id: The scope ID to search within
entity_type: The type of entity to search
request: The search request containing pagination info
Returns:
BatchQuerier with scope conditions and pagination settings
"""
conditions = [
EntityScopeConditions.by_scope_type(scope_type.to_scope_type()),
EntityScopeConditions.by_scope_id(scope_id),
EntityScopeConditions.by_entity_type(entity_type.to_entity_type()),
]
pagination = OffsetPagination(limit=request.limit, offset=request.offset)
return BatchQuerier(conditions=conditions, orders=[], pagination=pagination)
def convert_to_dto(self, data: EntityData) -> EntityDTO:
"""Convert EntityData to DTO.
Args:
data: Entity data from action result
Returns:
EntityDTO for API response
"""
return EntityDTO(
entity_type=data.entity_type.to_element(),
entity_id=data.entity_id,
)