Module demo_api.vm_manager

Gestionnaire de VMs pour demo_api

Functions

def create_vm(name: str | None = None,
email: str = 'jean@dupont21.com',
password: str = 'password123',
os: str = 'Ubuntu 22.04',
cores: int = 2,
ram: int = 4,
disk: int = 50,
status: str = 'stopped',
verbose: bool = False) ‑> None
Expand source code
def create_vm(
    name: Optional[str] = None,
    email: str = "jean@dupont21.com",
    password: str = "password123",
    os: str = "Ubuntu 22.04",
    cores: int = 2,
    ram: int = 4,
    disk: int = 50,
    status: str = "stopped",
    verbose: bool = False,
) -> None:
    """
    🖥️ Créer une VM pour un utilisateur existant

    Authentifie un utilisateur existant et crée une VM pour lui.

    Exemples:

    .. code-block:: shell

        python vm_manager.py create
        python vm_manager.py create --name "Ma VM" --email "alice@example.com" --password "motdepasse"
        python vm_manager.py create -n "VM Test" --ram 8 --disk 100 --verbose
    """

    # Configurer le nom par défaut si non fourni
    vm_name = name or "VM de Jean"

    if verbose:
        typer.echo("🔧 Configuration VM:")
        typer.echo(f"   Nom: {vm_name}")
        typer.echo(f"   OS: {os}")
        typer.echo(f"   CPU: {cores} cores")
        typer.echo(f"   RAM: {ram} GB")
        typer.echo(f"   Disque: {disk} GB")
        typer.echo(f"   Statut: {status}")
        typer.echo(f"   Email: {email}")
        typer.echo()

    logger.info("Début du processus de création de VM", email=email, vm_name=vm_name)

    # Initialisation du client API et du service
    api = Api(config.DEMO_API_BASE_URL)
    vm_service = VMService(api)

    # Authentification de l'utilisateur
    typer.echo("🔐 Authentification de l'utilisateur...")
    user = vm_service.authenticate_user(email=email, password=password)

    if not user:
        typer.echo("❌ Échec de l'authentification")
        raise typer.Exit(1)

    typer.echo(f"✅ Utilisateur authentifié: {user.get('name', email)}")

    # Configuration de la VM
    vm_config = {
        "user_id": user["id"],
        "name": vm_name,
        "operating_system": os,
        "cpu_cores": cores,
        "ram_gb": ram,
        "disk_gb": disk,
        "status": status,
    }

    if verbose:
        typer.echo("🚀 Création de la VM...")

    # Création de la VM
    logger.info("Création de la VM", **vm_config)
    vm_result = vm_service.create_vm_for_user(user, vm_config)

    typer.echo()
    if vm_result:
        typer.echo("🎉 VM créée avec succès!")
        typer.echo(f"   🆔 ID: {vm_result.get('id')}")
        typer.echo(f"   📝 Nom: {vm_result.get('name')}")
        typer.echo(f"   💻 OS: {vm_result.get('operating_system')}")
        typer.echo(f"   🔧 CPU: {vm_result.get('cpu_cores')} cores")
        typer.echo(f"   💾 RAM: {vm_result.get('ram_gb')} GB")
        typer.echo(f"   💿 Disque: {vm_result.get('disk_gb')} GB")
        typer.echo(f"   ⚡ Statut: {vm_result.get('status')}")
        typer.echo()
        typer.echo("✨ Terminé!")
    else:
        typer.echo("❌ Échec de la création de la VM")
        raise typer.Exit(1)

🖥️ Créer une VM pour un utilisateur existant

Authentifie un utilisateur existant et crée une VM pour lui.

Exemples:

.. code-block:: shell

python vm_manager.py create
python vm_manager.py create --name "Ma VM" --email "alice@example.com" --password "motdepasse"
python vm_manager.py create -n "VM Test" --ram 8 --disk 100 --verbose
def create_vm_cli(name: str | None = <typer.models.OptionInfo object>,
email: str = <typer.models.OptionInfo object>,
password: str = <typer.models.OptionInfo object>,
os: str = <typer.models.OptionInfo object>,
cores: int = <typer.models.OptionInfo object>,
ram: int = <typer.models.OptionInfo object>,
disk: int = <typer.models.OptionInfo object>,
status: str = <typer.models.OptionInfo object>,
verbose: bool = <typer.models.OptionInfo object>) ‑> None
Expand source code
def create_vm_cli(
    name: Optional[str] = typer.Option(None, "--name", "-n", help="Nom de la VM"),
    email: str = typer.Option(
        "jean@dupont21.com", "--email", "-e", help="Email de l'utilisateur existant"
    ),
    password: str = typer.Option(
        "password123", "--password", "-p", help="Mot de passe de l'utilisateur"
    ),
    os: str = typer.Option("Ubuntu 22.04", "--os", "-o", help="Système d'exploitation"),
    cores: int = typer.Option(
        2, "--cores", "-c", help="Nombre de cœurs CPU", min=1, max=16
    ),
    ram: int = typer.Option(4, "--ram", "-r", help="RAM en GB", min=1, max=128),
    disk: int = typer.Option(50, "--disk", "-d", help="Disque en GB", min=10, max=2048),
    status: str = typer.Option(
        "stopped", "--status", "-s", help="Statut initial de la VM"
    ),
    verbose: bool = typer.Option(False, "--verbose", "-v", help="Mode verbeux"),
) -> None:
    """
    🖥️ Créer une VM pour un utilisateur existant

    Authentifie un utilisateur existant et crée une VM pour lui.

    Exemples:

    .. code-block:: shell

        python vm_manager.py create
        python vm_manager.py create --name "Ma VM" --email "alice@example.com" --password "motdepasse"
        python vm_manager.py create -n "VM Test" --ram 8 --disk 100 --verbose
    """
    create_vm(name, email, password, os, cores, ram, disk, status, verbose)

🖥️ Créer une VM pour un utilisateur existant

Authentifie un utilisateur existant et crée une VM pour lui.

Exemples:

.. code-block:: shell

python vm_manager.py create
python vm_manager.py create --name "Ma VM" --email "alice@example.com" --password "motdepasse"
python vm_manager.py create -n "VM Test" --ram 8 --disk 100 --verbose