"""CMDB: Institution + Server management endpoints.""" from typing import List from fastapi import APIRouter, Depends from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from database import get_db from models import Institution, InstitutionOut, Server, ServerOut router = APIRouter(prefix="/api/cmdb", tags=["cmdb"]) @router.get("/institutions", response_model=List[InstitutionOut]) async def list_institutions(db: AsyncSession = Depends(get_db)): result = await db.execute(select(Institution).order_by(Institution.inst_name)) return result.scalars().all() @router.get("/institutions/{inst_code}/servers", response_model=List[ServerOut]) async def list_servers(inst_code: str, db: AsyncSession = Depends(get_db)): r = await db.execute( select(Institution).where(Institution.inst_code == inst_code) ) inst = r.scalars().first() if not inst: from fastapi import HTTPException raise HTTPException(404, detail="기관을 찾을 수 없습니다.") result = await db.execute( select(Server).where(Server.inst_id == inst.id, Server.is_active == True) .order_by(Server.server_name) ) return result.scalars().all()