|
| 1 | +import dataclasses as dt |
| 2 | +import json |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +from parsec._parsec import OrganizationID |
| 7 | +from parsec.cli.options import asyncio_run, db_server_options, logging_config_options |
| 8 | +from parsec.components.organization import OrganizationDump |
| 9 | +from parsec.components.postgresql.handler import asyncpg_pool_factory |
| 10 | +from parsec.components.postgresql.organization import PGOrganizationComponent |
| 11 | +from parsec.config import BaseDatabaseConfig, LogLevel, PostgreSQLDatabaseConfig |
| 12 | +from parsec.webhooks import MockedWebhooksComponent |
| 13 | + |
| 14 | + |
| 15 | +@click.command(name="list-organization", short_help="List organization present on the server") |
| 16 | +@db_server_options |
| 17 | +@logging_config_options(default_log_level="INFO") |
| 18 | +@asyncio_run |
| 19 | +async def cmd( |
| 20 | + db: BaseDatabaseConfig, |
| 21 | + db_min_connections: int, |
| 22 | + db_max_connections: int, |
| 23 | + log_level: LogLevel, |
| 24 | + log_format: str, |
| 25 | + log_file: str | None, |
| 26 | +): |
| 27 | + orgs = await list_organization(db) |
| 28 | + |
| 29 | + click.echo_via_pager(json.dumps(orgs, default=dt.asdict, indent=4)) |
| 30 | + |
| 31 | + |
| 32 | +async def list_organization(db: BaseDatabaseConfig) -> dict[OrganizationID, OrganizationDump]: |
| 33 | + # When mocked the organization are stored in volatile memory of the other process (server) |
| 34 | + # We will not be able to access it, so no organization |
| 35 | + if db.is_mocked(): |
| 36 | + return {} |
| 37 | + |
| 38 | + assert isinstance(db, PostgreSQLDatabaseConfig) |
| 39 | + async with asyncpg_pool_factory( |
| 40 | + url=db.url, min_connections=db.min_connections, max_connections=db.max_connections |
| 41 | + ) as pool: |
| 42 | + org_cmp = PGOrganizationComponent( |
| 43 | + pool=pool, |
| 44 | + webhooks=MockedWebhooksComponent(), |
| 45 | + config=None, # pyright: ignore [reportArgumentType] |
| 46 | + ) |
| 47 | + return await org_cmp.list_organizations() |
0 commit comments