|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +__all__ = ("app",) |
| 4 | + |
| 5 | +import asyncio |
| 6 | +import json |
| 7 | +from typing import Annotated, Optional |
| 8 | + |
| 9 | +import typer |
| 10 | + |
| 11 | +from diracx.client.aio import AsyncDiracClient |
| 12 | + |
| 13 | +from .utils import AsyncTyper |
| 14 | + |
| 15 | +app = AsyncTyper() |
| 16 | + |
| 17 | + |
| 18 | +async def installation_metadata(): |
| 19 | + async with AsyncDiracClient() as api: |
| 20 | + return await api.well_known.get_installation_metadata() |
| 21 | + |
| 22 | + |
| 23 | +def vo_callback(vo: str | None) -> str: |
| 24 | + metadata = asyncio.run(installation_metadata()) |
| 25 | + vos = list(metadata.virtual_organizations) |
| 26 | + if not vo: |
| 27 | + raise typer.BadParameter( |
| 28 | + f"VO must be specified, available options are: {' '.join(vos)}" |
| 29 | + ) |
| 30 | + if vo not in vos: |
| 31 | + raise typer.BadParameter( |
| 32 | + f"Unknown VO {vo}, available options are: {' '.join(vos)}" |
| 33 | + ) |
| 34 | + return vo |
| 35 | + |
| 36 | + |
| 37 | +@app.async_command() |
| 38 | +async def generate_pilot_secrets( |
| 39 | + vo: Annotated[ |
| 40 | + str, |
| 41 | + typer.Argument(callback=vo_callback, help="Virtual Organization name"), |
| 42 | + ], |
| 43 | + n: Annotated[ |
| 44 | + int, |
| 45 | + typer.Argument(help="Number of secrets to generate."), |
| 46 | + ], |
| 47 | + expiration_minutes: Optional[int] = typer.Option( |
| 48 | + 60, |
| 49 | + help="Expiration in minutes of the secrets.", |
| 50 | + ), |
| 51 | + max_use: Optional[int] = typer.Option( |
| 52 | + 60, |
| 53 | + help="Number of uses max for a secret.", |
| 54 | + ), |
| 55 | +): |
| 56 | + async with AsyncDiracClient() as api: |
| 57 | + secrets = await api.pilots.create_pilot_secrets( |
| 58 | + n=n, |
| 59 | + expiration_minutes=expiration_minutes, |
| 60 | + pilot_secret_use_count_max=max_use, |
| 61 | + vo=vo, |
| 62 | + ) |
| 63 | + # Convert each model to dict |
| 64 | + secrets_dict = [secret.as_dict() for secret in secrets] |
| 65 | + |
| 66 | + print(json.dumps(secrets_dict, indent=2)) |
0 commit comments