You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

112 lines
3.6 KiB

import json
from fastapi import APIRouter, Depends
from fastapi import Response
from app.api.dependencies import get_backend_service
from app.core.secret_codec import list_secret_fields
from app.models.entities import Backend
from app.schemas.backend import (
BackendCheckResponse,
BackendListResponse,
BackendMutationResponse,
BackendSummary,
CreateBackendRequest,
CreateBackendResponse,
UpdateBackendRequest,
)
from app.services.backend_service import BackendService
router = APIRouter()
def summarize_backend(backend: Backend) -> BackendSummary:
summary = BackendSummary.model_validate(backend, from_attributes=True)
public_config = json.loads(backend.public_config_json)
summary.config = public_config
summary.secret_fields = list_secret_fields(backend.secret_config_json)
return summary
@router.get("", response_model=BackendListResponse)
async def list_backends(
service: BackendService = Depends(get_backend_service),
) -> BackendListResponse:
items = await service.list_backends()
return BackendListResponse(
items=[summarize_backend(item) for item in items]
)
@router.post("", response_model=CreateBackendResponse)
async def create_backend(
payload: CreateBackendRequest,
service: BackendService = Depends(get_backend_service),
) -> CreateBackendResponse:
backend = await service.create_backend(
name=payload.name,
backend_type=payload.type,
stability_class=payload.stability_class,
read_priority=payload.read_priority,
write_priority=payload.write_priority,
config=payload.config,
)
return CreateBackendResponse(backend=summarize_backend(backend))
@router.post("/{backend_id}/check", response_model=BackendCheckResponse)
async def check_backend(
backend_id: str,
service: BackendService = Depends(get_backend_service),
) -> BackendCheckResponse:
backend, status, detail = await service.check_backend(backend_id)
return BackendCheckResponse(
backend_id=backend.id,
status=status,
checked_at=backend.last_health_checked_at,
detail=detail,
)
@router.patch("/{backend_id}", response_model=BackendMutationResponse)
async def update_backend(
backend_id: str,
payload: UpdateBackendRequest,
service: BackendService = Depends(get_backend_service),
) -> BackendMutationResponse:
backend = await service.update_backend(
backend_id,
name=payload.name,
stability_class=payload.stability_class,
read_priority=payload.read_priority,
write_priority=payload.write_priority,
config=payload.config,
)
return BackendMutationResponse(backend=summarize_backend(backend))
@router.post("/{backend_id}/disable", response_model=BackendMutationResponse)
async def disable_backend(
backend_id: str,
service: BackendService = Depends(get_backend_service),
) -> BackendMutationResponse:
backend = await service.disable_backend(backend_id)
return BackendMutationResponse(backend=summarize_backend(backend))
@router.post("/{backend_id}/enable", response_model=BackendMutationResponse)
async def enable_backend(
backend_id: str,
service: BackendService = Depends(get_backend_service),
) -> BackendMutationResponse:
backend = await service.enable_backend(backend_id)
return BackendMutationResponse(backend=summarize_backend(backend))
@router.delete("/{backend_id}", status_code=204)
async def delete_backend(
backend_id: str,
service: BackendService = Depends(get_backend_service),
) -> Response:
await service.delete_backend(backend_id)
return Response(status_code=204)