Files
cli/secnex/app/tenant.py
2026-01-27 16:35:46 +01:00

61 lines
2.1 KiB
Python

import typer
from typing import Optional
from rich.console import Console
from rich.table import Table
from rich.prompt import Prompt
from secnex.kit.config.config import Config
from secnex.kit.clients.tenant import TenantClient
from secnex.kit.clients.user import UserClient
console = Console()
tenant_app = typer.Typer()
@tenant_app.command(name="create", help="Create a new tenant")
def create(owner: Optional[str] = typer.Option(None, "--owner", "-o", help="The owner of the tenant")):
"""Create a new tenant"""
config = Config("default")
tenant_client = TenantClient(config)
name = Prompt.ask("Enter the name of the tenant")
tenant_id, status_code, success = tenant_client.create(name)
user_id = None
if owner is not None and tenant_id is not None:
user_client = UserClient(config)
success, status_code = user_client.add_user_to_tenant(tenant_id, owner)
if not success:
console.print(f"Failed to add user to tenant! Please check the logs for more information.", style="bold red")
return
console.print(f"Tenant created successfully!", style="bold green")
console.print(f"User added to tenant successfully!", style="bold green")
@tenant_app.command(name="ls", help="List all tenants")
def ls():
"""List all tenants"""
config = Config("default")
tenant_client = TenantClient(config)
tenants = tenant_client.get_all()
count_tenants = len(tenants)
console.print(f"Found {count_tenants} tenants")
if count_tenants == 0:
return
table = Table(title="Tenants")
table.add_column("ID", style="bold green")
table.add_column("Name", style="bold yellow")
table.add_column("Enabled", style="bold purple")
table.add_column("Allow Self Registration", style="bold blue")
for t in tenants:
id = t.id
name = t.name
enabled = t.enabled
allow_self_registration = t.allow_self_registration
table.add_row(
id,
name,
str(enabled),
str(allow_self_registration),
)
console.print(table)