Adding config endpoint

This commit is contained in:
Pascal Scheiben
2025-09-18 11:49:34 +02:00
committed by Magel, Denis
parent 2a165c91b6
commit 579c62319c
6 changed files with 73 additions and 16 deletions

3
src/config/__init__.py Normal file
View File

@@ -0,0 +1,3 @@
from src.config.router import router as config_router
__all__ = ["config_router"]

14
src/config/config.http Normal file
View File

@@ -0,0 +1,14 @@
POST http://127.0.0.1:8000/config
Content-Type: application/json
{
"cluster_list": [
{
"hostname": "cluster1.demo.netapp.com",
"username": "admin",
"password": "Netapp1!"
}
]
}
###

16
src/config/router.py Normal file
View File

@@ -0,0 +1,16 @@
from fastapi import APIRouter
from .schema import ConfigSchema, ConfigReturnSchema
import logging
logger = logging.getLogger("uvicorn")
router = APIRouter(tags=["config"])
@router.post("/config", response_model=ConfigReturnSchema)
async def create_config(config: ConfigSchema) -> ConfigSchema:
"""
Endpoint to receive and return configuration data.
"""
logger.info("Received configuration data")
return config

21
src/config/schema.py Normal file
View File

@@ -0,0 +1,21 @@
# contains the schema definitions for the aggregate service
from pydantic import BaseModel
class ConfigEntrySchema(BaseModel):
hostname: str
username: str
password: str
class ConfigOutSchema(BaseModel):
hostname: str
username: str
class ConfigReturnSchema(BaseModel):
cluster_list: list[ConfigOutSchema]
class ConfigSchema(BaseModel):
cluster_list: list[ConfigEntrySchema]

3
src/config/service.py Normal file
View File

@@ -0,0 +1,3 @@
# contains the business logic for the aggregate service
async def example_service() -> str:
return "This is an aggregate service"

View File

@@ -1,30 +1,30 @@
import os
import json
import logging
import yaml
from src.aggregate import aggregate_router
from src.config import config_router
from pathlib import Path
from dotenv import load_dotenv
from redis import Redis
from contextlib import asynccontextmanager
from pydantic import BaseModel, ValidationError, SecretStr, AnyHttpUrl
from typing import Optional, Literal, List, Union
from fastapi import FastAPI
from database import setup_db_conn, get_inventory_from_redis, get_config_from_db
from database import setup_db_conn, get_config_from_db
from src.initialize import initialize_config
from utils import setup_logging
logger = logging.getLogger("uvicorn")
logger.info("Starting application")
app = FastAPI()
app.include_router(aggregate_router)
app.include_router(config_router)
@asynccontextmanager
async def lifespan(app: FastAPI):
''' make loading it async'''
log = logging.getLogger('uvicorn')
"""make loading it async"""
log = logging.getLogger("uvicorn")
cfg_init_result = initialize_config()
shared_redis_conn = setup_db_conn(os.getenv('redis_host'), os.getenv('redis_port'))
shared_redis_conn = setup_db_conn(os.getenv("redis_host"), os.getenv("redis_port"))
if not shared_redis_conn:
log.error("Cannot connect to Redis DB. Exiting...")
exit(1)
@@ -40,7 +40,7 @@ async def lifespan(app: FastAPI):
setup_logging()
log = logging.getLogger('uvicorn')
log = logging.getLogger("uvicorn")
log.info("Starting FastAPI app...")
app = FastAPI(lifespan=lifespan)