From 6fc2ac7969bba4940f65341ed6f76a673c4cdf86 Mon Sep 17 00:00:00 2001 From: Linus Vogel Date: Wed, 11 Feb 2026 23:20:10 +0100 Subject: [PATCH] added some doc comments --- .gitignore | 1 + pillar_tool/ptcli/__init__.py | 1 + pillar_tool/{ptcli.py => ptcli/main.py} | 0 pillar_tool/routers/host.py | 55 ++++++++++++++++++++++++- pillartool.egg-info/PKG-INFO | 1 + pillartool.egg-info/SOURCES.txt | 18 +++++++- pillartool.egg-info/requires.txt | 1 + 7 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 pillar_tool/ptcli/__init__.py rename pillar_tool/{ptcli.py => ptcli/main.py} (100%) diff --git a/.gitignore b/.gitignore index bd780f2..ec42f31 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ __build__/ __pycache__/ /dist/ /.idea/ +/pillartool.egg-info/ pillar_tool.toml diff --git a/pillar_tool/ptcli/__init__.py b/pillar_tool/ptcli/__init__.py new file mode 100644 index 0000000..b668da9 --- /dev/null +++ b/pillar_tool/ptcli/__init__.py @@ -0,0 +1 @@ +from .main import main \ No newline at end of file diff --git a/pillar_tool/ptcli.py b/pillar_tool/ptcli/main.py similarity index 100% rename from pillar_tool/ptcli.py rename to pillar_tool/ptcli/main.py diff --git a/pillar_tool/routers/host.py b/pillar_tool/routers/host.py index 6dd31e9..8a5b588 100644 --- a/pillar_tool/routers/host.py +++ b/pillar_tool/routers/host.py @@ -18,12 +18,24 @@ router = APIRouter( ) - +# TODO: check comments in this file (they are written by AI) @router.get("") def hosts_get(req: Request): + """ + Retrieve a list of all hosts (excluding hostgroups) in the system. + + Queries the database for all host entries where is_hostgroup is False, + returning only the names of the hosts in a flat list format. + + Args: + req: FastAPI request object containing database session + + Returns: + JSONResponse containing a list of host names as strings + """ db: Session = req.state.db - result = db.execute(select(Host)).fetchall() + result = db.execute(select(Host).where(Host.is_hostgroup == False)).fetchall() hosts: list[Host] = list(map(lambda x: x[0], result)) return JSONResponse(status_code=200, content=list(map(lambda x: x.name, hosts))) @@ -32,6 +44,23 @@ def hosts_get(req: Request): @router.get("/{fqdn}") def host_get(req: Request, fqdn: str): + """ + Retrieve detailed information about a specific host including its hierarchical path. + + Fetches host details from the database and constructs the full path by traversing + parent relationships up to the root. Returns both the host name and its complete + hierarchical path as a slash-separated string. + + Args: + req: FastAPI request object containing database session + fqdn: Fully qualified domain name of the host to retrieve + + Returns: + JSONResponse containing host name and hierarchical path + + Raises: + HTTPException: If FQDN format is invalid or host is not found + """ db: Session = req.state.db if not validate_fqdn(fqdn): @@ -67,11 +96,31 @@ def host_get(req: Request, fqdn: str): @router.post("/{fqdn}") async def host_add(request: Request, fqdn: str, params: HostCreateParams): + """ + Create a new host with optional parent hierarchy. + + Validates FQDN format and parent path structure before creating the host. + If a parent is specified, ensures all parent components exist in the database. + Creates the host with a unique UUID and proper hierarchical relationships. + + Args: + request: FastAPI request object containing database session + fqdn: Fully qualified domain name for the new host + params: Host creation parameters including optional parent path + + Returns: + JSONResponse with creation details and host information + + Raises: + HTTPException: If FQDN format is invalid or parent doesn't exist + """ db: Session = request.state.db + # Validate that the provided FQDN is properly formatted if not validate_fqdn(fqdn): raise HTTPException(status_code=400, detail="Provided host is not an FQDN") + # Process parent path if provided if params.parent is not None: parent_labels = split_and_validate_path(params.parent) if parent_labels is None: @@ -79,6 +128,7 @@ async def host_add(request: Request, fqdn: str, params: HostCreateParams): else: parent_labels = [] + # Traverse the parent hierarchy to ensure all components exist parent_id = None stmt_select_respecting_parent = select(Host).where(Host.name == bindparam("label") and Host.parent_id == bindparam("parent_id")) for label in parent_labels: @@ -95,6 +145,7 @@ async def host_add(request: Request, fqdn: str, params: HostCreateParams): parent_id = result[0][0].parent_id + # Create new host with unique ID and hierarchical structure new_host = Host( id=uuid.uuid4(), name=fqdn, diff --git a/pillartool.egg-info/PKG-INFO b/pillartool.egg-info/PKG-INFO index b7c9578..125a0bd 100644 --- a/pillartool.egg-info/PKG-INFO +++ b/pillartool.egg-info/PKG-INFO @@ -11,4 +11,5 @@ Requires-Dist: jinja2>=3.1.6 Requires-Dist: psycopg2>=2.9.11 Requires-Dist: pycryptodome>=3.23.0 Requires-Dist: pydantic>=2.12.5 +Requires-Dist: requests>=2.32.5 Requires-Dist: sqlalchemy>=2.0.45 diff --git a/pillartool.egg-info/SOURCES.txt b/pillartool.egg-info/SOURCES.txt index 6004b2f..74b0c1d 100644 --- a/pillartool.egg-info/SOURCES.txt +++ b/pillartool.egg-info/SOURCES.txt @@ -1,7 +1,7 @@ pyproject.toml pillar_tool/__init__.py pillar_tool/main.py -pillar_tool/ptcli.py +pillar_tool/schemas.py pillar_tool/db/__init__.py pillar_tool/db/base_model.py pillar_tool/db/database.py @@ -14,19 +14,35 @@ pillar_tool/db/migrations/versions/2025_12_27_1159-4cc7f4e295f1_added_unique_to_ pillar_tool/db/migrations/versions/2025_12_27_1958-678356102624_added_pillar_structure.py pillar_tool/db/migrations/versions/2025_12_30_1009-c6fe061ad732_better_uniqueness_contraints.py pillar_tool/db/migrations/versions/2026_01_01_1503-54537e95fc4d_coupled_host_and_hostgroup_into_single_.py +pillar_tool/db/migrations/versions/2026_02_08_2034-7eb66922e256_pillars_are_directly_assigned_to_the_.py +pillar_tool/db/migrations/versions/2026_02_08_2051-0a912926be8b_added_environments_and_states_to_the_db_.py +pillar_tool/db/migrations/versions/2026_02_08_2220-e33744090598_mark_hosts_as_hostgroups.py +pillar_tool/db/migrations/versions/2026_02_10_2147-dd573f631ee4_added_environment_assignments_to_.py pillar_tool/db/models/__init__.py pillar_tool/db/models/pillar_data.py +pillar_tool/db/models/top_data.py pillar_tool/db/models/user.py pillar_tool/db/queries/__init__.py pillar_tool/db/queries/auth_queries.py +pillar_tool/db/queries/host_queries.py pillar_tool/db/queries/pillar_queries.py pillar_tool/frontend/__init__.py pillar_tool/frontend/pillar_view.py pillar_tool/middleware/__init__.py pillar_tool/middleware/basicauth_backend.py pillar_tool/middleware/db_connection.py +pillar_tool/middleware/logging.py +pillar_tool/ptcli/__init__.py +pillar_tool/ptcli/main.py +pillar_tool/routers/__init__.py +pillar_tool/routers/environment.py +pillar_tool/routers/host.py +pillar_tool/routers/hostgroup.py +pillar_tool/routers/pillar.py +pillar_tool/routers/state.py pillar_tool/util/__init__.py pillar_tool/util/config.py +pillar_tool/util/validation.py pillartool.egg-info/PKG-INFO pillartool.egg-info/SOURCES.txt pillartool.egg-info/dependency_links.txt diff --git a/pillartool.egg-info/requires.txt b/pillartool.egg-info/requires.txt index 7f1efed..6d088d8 100644 --- a/pillartool.egg-info/requires.txt +++ b/pillartool.egg-info/requires.txt @@ -6,4 +6,5 @@ jinja2>=3.1.6 psycopg2>=2.9.11 pycryptodome>=3.23.0 pydantic>=2.12.5 +requests>=2.32.5 sqlalchemy>=2.0.45