|
| 1 | +# Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from collections.abc import AsyncGenerator |
| 5 | +from contextlib import asynccontextmanager |
| 6 | + |
| 7 | +import click |
| 8 | +import pydantic |
| 9 | + |
| 10 | +from parsec._parsec import EmailAddress, OrganizationID, ParsecAddr, SecretKey |
| 11 | +from parsec.cli.options import asyncio_run, db_server_options, logging_config_options |
| 12 | +from parsec.components.memory.datamodel import MemoryDatamodel |
| 13 | +from parsec.components.memory.events import event_bus_factory |
| 14 | +from parsec.components.memory.organization import MemoryOrganizationComponent |
| 15 | +from parsec.components.organization import BaseOrganizationComponent, OrganizationDump |
| 16 | +from parsec.components.postgresql.handler import asyncpg_pool_factory |
| 17 | +from parsec.components.postgresql.organization import PGOrganizationComponent |
| 18 | +from parsec.config import ( |
| 19 | + BackendConfig, |
| 20 | + BaseDatabaseConfig, |
| 21 | + LogLevel, |
| 22 | + MockedBlockStoreConfig, |
| 23 | + MockedEmailConfig, |
| 24 | + PostgreSQLDatabaseConfig, |
| 25 | +) |
| 26 | +from parsec.webhooks import MockedWebhooksComponent |
| 27 | + |
| 28 | + |
| 29 | +@pydantic.dataclasses.dataclass() |
| 30 | +class OrganizationInfo: |
| 31 | + id: OrganizationID |
| 32 | + is_bootstrapped: bool |
| 33 | + # bootstrapped_on: DateTime | None |
| 34 | + is_expired: bool |
| 35 | + # expired_on: DateTime | None |
| 36 | + # active_users_limit: ActiveUsersLimit |
| 37 | + user_profile_outsider_allowed: bool |
| 38 | + realm_minimum_archiving_period_before_deletion: int |
| 39 | + # tos: TermsOfService | None |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def from_dump(cls, dump: OrganizationDump) -> OrganizationInfo: |
| 43 | + return OrganizationInfo( |
| 44 | + id=dump.organization_id, |
| 45 | + is_bootstrapped=dump.is_bootstrapped, |
| 46 | + is_expired=dump.is_expired, |
| 47 | + user_profile_outsider_allowed=dump.user_profile_outsider_allowed, |
| 48 | + realm_minimum_archiving_period_before_deletion=dump.realm_minimum_archiving_period_before_deletion, |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +@click.command(name="list-organizations", short_help="List organization present on the server") |
| 53 | +@db_server_options |
| 54 | +@logging_config_options(default_log_level="INFO") |
| 55 | +@asyncio_run |
| 56 | +async def cmd( |
| 57 | + db: BaseDatabaseConfig, |
| 58 | + db_min_connections: int, |
| 59 | + db_max_connections: int, |
| 60 | + log_level: LogLevel, |
| 61 | + log_format: str, |
| 62 | + log_file: str | None, |
| 63 | +): |
| 64 | + backend_config = BackendConfig( |
| 65 | + debug=False, |
| 66 | + db_config=db, |
| 67 | + blockstore_config=MockedBlockStoreConfig(), |
| 68 | + email_config=MockedEmailConfig(sender=EmailAddress("tasks@parsec.local")), |
| 69 | + server_addr=ParsecAddr("tasks.parsec.local", None, True), |
| 70 | + administration_token="", |
| 71 | + fake_account_password_algorithm_seed=SecretKey.generate(), |
| 72 | + ) |
| 73 | + async with organization_component_factory(backend_config) as component: |
| 74 | + orgs = await list_organizations(component) |
| 75 | + |
| 76 | + adapter = pydantic.TypeAdapter( |
| 77 | + dict[ |
| 78 | + OrganizationID, |
| 79 | + OrganizationInfo, |
| 80 | + ] |
| 81 | + ) |
| 82 | + click.echo_via_pager(adapter.dump_json(orgs, indent=4).decode()) |
| 83 | + |
| 84 | + |
| 85 | +@asynccontextmanager |
| 86 | +async def organization_component_factory( |
| 87 | + config: BackendConfig, |
| 88 | +) -> AsyncGenerator[BaseOrganizationComponent, None]: |
| 89 | + if config.db_config.is_mocked(): |
| 90 | + data = MemoryDatamodel( |
| 91 | + {} if config.backend_mocked_data is None else config.backend_mocked_data |
| 92 | + ) |
| 93 | + async with event_bus_factory() as event_bus: |
| 94 | + yield MemoryOrganizationComponent(data, event_bus, MockedWebhooksComponent(), config) |
| 95 | + |
| 96 | + else: |
| 97 | + assert isinstance(config.db_config, PostgreSQLDatabaseConfig) |
| 98 | + async with asyncpg_pool_factory( |
| 99 | + url=config.db_config.url, |
| 100 | + min_connections=config.db_config.min_connections, |
| 101 | + max_connections=config.db_config.max_connections, |
| 102 | + ) as pool: |
| 103 | + yield PGOrganizationComponent( |
| 104 | + pool=pool, |
| 105 | + webhooks=MockedWebhooksComponent(), |
| 106 | + config=None, # pyright: ignore [reportArgumentType] |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +async def list_organizations( |
| 111 | + component: BaseOrganizationComponent, |
| 112 | +) -> dict[OrganizationID, OrganizationInfo]: |
| 113 | + return { |
| 114 | + id: OrganizationInfo.from_dump(dump) |
| 115 | + for id, dump in (await component.list_organizations()).items() |
| 116 | + } |
0 commit comments