diff --git a/src/lvmapi/app.py b/src/lvmapi/app.py index a547ac4..39b5960 100644 --- a/src/lvmapi/app.py +++ b/src/lvmapi/app.py @@ -8,6 +8,8 @@ from __future__ import annotations +import os + import taskiq_fastapi from fastapi import FastAPI, Request @@ -66,6 +68,15 @@ async def get_id_route(request: Request): # Add kubernetes API instance to state. app.state.kubernetes = Kubernetes() +# Fake states for testing. +app.state.use_fake_states = os.environ.get("LVM_USE_FAKE_STATES", "0") != "0" +app.state.fake_states = { + "wind_alert": False, + "humidity_alert": False, + "door_alert": False, + "is_day": False, +} + @app.get("/") def root(request: Request): diff --git a/src/lvmapi/routers/actors.py b/src/lvmapi/routers/actors.py index fd0a59b..8d8ec6c 100644 --- a/src/lvmapi/routers/actors.py +++ b/src/lvmapi/routers/actors.py @@ -66,14 +66,14 @@ async def get_actor_health(request: Request) -> list[HealthResponse]: @router.get("/ping", summary="Actor ping") -async def ping_route(actors: list[str] | None = None) -> dict[str, bool]: +async def get_ping_route(actors: list[str] | None = None) -> dict[str, bool]: """Pings a list of actors.""" return await ping_actors(actors=actors) -@router.get("/restart/{actor}", summary="Restart actor") -async def restart_actor_route(actor: str) -> str: +@router.get("/restart/{actor}", summary="Restart an actor") +async def get_restart_actor_route(actor: str) -> str: """Restarts an actor. Scheduled as a task and returns the task ID""" deployment = config["actors.actor_to_deployment"][actor] @@ -84,6 +84,21 @@ async def restart_actor_route(actor: str) -> str: return task.task_id +@router.get("/stop/{actor}", summary="Stop an actor") +async def get_stop_actor_route(actor: str) -> bool: + """Stops an actor.""" + + from lvmapi.app import app + + deployment = config["actors.actor_to_deployment"][actor] + if deployment is None: + raise ValueError(f"Actor {actor} does not have a deployment.") + + app.state.kubernetes.delete_deployment(deployment) + + return True + + @router.get("/versions", summary="Get actor versions") async def get_actor_versions_route( actor: Annotated[