|
5 | 5 | from collections.abc import Callable |
6 | 6 | from contextlib import AbstractAsyncContextManager, nullcontext |
7 | 7 | from datetime import UTC, datetime |
8 | | -from typing import TYPE_CHECKING |
| 8 | +from typing import TYPE_CHECKING, Protocol |
9 | 9 |
|
10 | 10 | from sqlalchemy import select |
11 | 11 | from sqlalchemy.ext.asyncio import AsyncSession |
|
28 | 28 | from renku_data_services.session.config import BuildsConfig |
29 | 29 |
|
30 | 30 |
|
31 | | -class SessionRepository: |
| 31 | +class SessionEnvironmentRepositoryProtocol(Protocol): |
| 32 | + """Protocol for operations on session environments.""" |
| 33 | + |
| 34 | + async def get_environments(self, include_archived: bool = False) -> list[models.Environment]: |
| 35 | + """Get all global session environments from the database.""" |
| 36 | + ... |
| 37 | + |
| 38 | + async def insert_environment( |
| 39 | + self, user: base_models.APIUser, environment: models.UnsavedEnvironment |
| 40 | + ) -> models.Environment: |
| 41 | + """Insert a new session environment.""" |
| 42 | + ... |
| 43 | + |
| 44 | + |
| 45 | +class SessionRepository(SessionEnvironmentRepositoryProtocol): |
32 | 46 | """Repository for sessions.""" |
33 | 47 |
|
34 | 48 | def __init__( |
@@ -1160,3 +1174,22 @@ async def _get_environment_authorization( |
1160 | 1174 | if launcher: |
1161 | 1175 | authorized = await self.project_authz.has_permission(user, ResourceType.project, launcher.project_id, scope) |
1162 | 1176 | return authorized |
| 1177 | + |
| 1178 | + @classmethod |
| 1179 | + def make_session_environment_repo( |
| 1180 | + cls, |
| 1181 | + session_maker: Callable[..., AsyncSession], |
| 1182 | + project_authz: Authz, |
| 1183 | + ) -> SessionEnvironmentRepositoryProtocol: |
| 1184 | + """Create an instance of SessionEnvironmentRepositoryProtocol.""" |
| 1185 | + # NOTE: resource_pools, shipwright_client and builds_config are set to None |
| 1186 | + # because the SessionEnvironmentRepositoryProtocol only exposes database |
| 1187 | + # operations for session environments. |
| 1188 | + instance = cls( |
| 1189 | + session_maker, |
| 1190 | + project_authz=project_authz, |
| 1191 | + resource_pools=None, # type: ignore |
| 1192 | + shipwright_client=None, |
| 1193 | + builds_config=None, # type: ignore |
| 1194 | + ) |
| 1195 | + return instance |
0 commit comments