GapFill Flowee
This commit is contained in:
@@ -15,6 +15,7 @@ Solo admin e super_admin possono accedere.
|
||||
Il super_admin puo' operare su qualsiasi tenant tramite query param ?tenant_id=<uuid>.
|
||||
"""
|
||||
|
||||
import time
|
||||
import uuid
|
||||
from typing import Annotated, Optional
|
||||
|
||||
@@ -23,6 +24,7 @@ from fastapi import APIRouter, HTTPException, Query, status
|
||||
from app.config import get_settings as get_app_settings
|
||||
from app.dependencies import AdminUser, DB
|
||||
from app.schemas.tenant_settings import (
|
||||
ConservatoreTestResult,
|
||||
IndexingJobStatus,
|
||||
IndexingStats,
|
||||
StartReindexRequest,
|
||||
@@ -59,15 +61,21 @@ def _resolve_tenant_id(
|
||||
summary="Legge le impostazioni del tenant",
|
||||
description=(
|
||||
"Restituisce la configurazione operativa del tenant: "
|
||||
"modalita' archiviazione (mock/produzione), endpoint e stato credenziali conservatore."
|
||||
"modalita' archiviazione (mock/produzione), endpoint e stato credenziali conservatore. "
|
||||
"Il super_admin puo' specificare ?tenant_id=<uuid> per leggere le impostazioni di un tenant arbitrario."
|
||||
),
|
||||
)
|
||||
async def get_settings(
|
||||
current_user: AdminUser,
|
||||
db: DB,
|
||||
tenant_id: Optional[uuid.UUID] = Query(
|
||||
default=None,
|
||||
description="(solo super_admin) UUID del tenant su cui operare",
|
||||
),
|
||||
) -> TenantSettingsResponse:
|
||||
target_tenant_id = _resolve_tenant_id(current_user, tenant_id)
|
||||
service = TenantSettingsService(db)
|
||||
settings = await service.get_or_create(current_user.tenant_id)
|
||||
settings = await service.get_or_create(target_tenant_id)
|
||||
return TenantSettingsService.to_response(settings)
|
||||
|
||||
|
||||
@@ -78,21 +86,176 @@ async def get_settings(
|
||||
description=(
|
||||
"Aggiorna la configurazione operativa del tenant. "
|
||||
"Tutti i campi sono opzionali (semantica PATCH). "
|
||||
"Il passaggio a modalita' 'production' richiede un endpoint conservatore configurato."
|
||||
"Il passaggio a modalita' 'production' richiede un endpoint conservatore configurato. "
|
||||
"Il super_admin puo' specificare ?tenant_id=<uuid> per aggiornare le impostazioni di un tenant arbitrario."
|
||||
),
|
||||
)
|
||||
async def update_settings(
|
||||
body: TenantSettingsUpdate,
|
||||
current_user: AdminUser,
|
||||
db: DB,
|
||||
tenant_id: Optional[uuid.UUID] = Query(
|
||||
default=None,
|
||||
description="(solo super_admin) UUID del tenant su cui operare",
|
||||
),
|
||||
) -> TenantSettingsResponse:
|
||||
target_tenant_id = _resolve_tenant_id(current_user, tenant_id)
|
||||
service = TenantSettingsService(db)
|
||||
settings = await service.update(current_user.tenant_id, body)
|
||||
settings = await service.update(target_tenant_id, body)
|
||||
await db.commit()
|
||||
await db.refresh(settings)
|
||||
return TenantSettingsService.to_response(settings)
|
||||
|
||||
|
||||
# ─── Test connessione conservatore ───────────────────────────────────────────
|
||||
|
||||
@router.post(
|
||||
"/test-conservatore",
|
||||
response_model=ConservatoreTestResult,
|
||||
summary="Testa la connessione al conservatore configurato",
|
||||
description=(
|
||||
"Verifica che le credenziali salvate per il conservatore siano valide "
|
||||
"effettuando una chiamata di autenticazione reale. "
|
||||
"Supporta Aeterna (JWT) e conservatori generici (HTTP Basic). "
|
||||
"Non modifica alcun dato, non invia pacchetti."
|
||||
),
|
||||
)
|
||||
async def test_conservatore_connection(
|
||||
current_user: AdminUser,
|
||||
db: DB,
|
||||
tenant_id: Optional[uuid.UUID] = Query(
|
||||
default=None,
|
||||
description="(solo super_admin) UUID del tenant su cui operare",
|
||||
),
|
||||
) -> ConservatoreTestResult:
|
||||
target_tenant_id = _resolve_tenant_id(current_user, tenant_id)
|
||||
service = TenantSettingsService(db)
|
||||
creds = await service.get_conservatore_credentials(target_tenant_id)
|
||||
|
||||
if creds.get("mode") != "production":
|
||||
return ConservatoreTestResult(
|
||||
success=False,
|
||||
message="La modalita' di archiviazione e' impostata su 'mock'. "
|
||||
"Configura l'endpoint e le credenziali, poi imposta la modalita' su 'produzione'.",
|
||||
)
|
||||
|
||||
endpoint = creds.get("endpoint")
|
||||
username = creds.get("username")
|
||||
password = creds.get("password")
|
||||
tenant_slug = creds.get("tenant_slug")
|
||||
conservatore_id = creds.get("conservatore_id", "")
|
||||
|
||||
if not endpoint or not username or not password:
|
||||
return ConservatoreTestResult(
|
||||
success=False,
|
||||
message="Credenziali o endpoint non configurati. "
|
||||
"Compila tutti i campi obbligatori prima di testare la connessione.",
|
||||
)
|
||||
|
||||
# Rileva Aeterna da conservatore_id o URL
|
||||
is_aeterna = (
|
||||
(conservatore_id or "").lower() == "aeterna"
|
||||
or "aeterna" in (endpoint or "").lower()
|
||||
or "idrainformatica" in (endpoint or "").lower()
|
||||
)
|
||||
|
||||
t_start = time.monotonic()
|
||||
|
||||
if is_aeterna:
|
||||
# Test Aeterna: POST /api/v1/auth/login + GET /api/v1/auth/me
|
||||
if not tenant_slug:
|
||||
return ConservatoreTestResult(
|
||||
success=False,
|
||||
message="Provider Aeterna richiede il campo 'Tenant Slug'. Configuralo nelle impostazioni.",
|
||||
)
|
||||
try:
|
||||
import httpx
|
||||
async with httpx.AsyncClient(timeout=15) as client:
|
||||
resp_login = await client.post(
|
||||
f"{endpoint.rstrip('/')}/api/v1/auth/login",
|
||||
json={
|
||||
"email": username,
|
||||
"password": password,
|
||||
"tenant_slug": tenant_slug,
|
||||
},
|
||||
)
|
||||
|
||||
latency_ms = int((time.monotonic() - t_start) * 1000)
|
||||
|
||||
if resp_login.status_code != 200:
|
||||
return ConservatoreTestResult(
|
||||
success=False,
|
||||
message=f"Login Aeterna fallito (HTTP {resp_login.status_code}): {resp_login.text[:200]}",
|
||||
latency_ms=latency_ms,
|
||||
)
|
||||
|
||||
login_data = resp_login.json()
|
||||
token = login_data.get("access_token")
|
||||
|
||||
async with httpx.AsyncClient(timeout=10) as client:
|
||||
resp_me = await client.get(
|
||||
f"{endpoint.rstrip('/')}/api/v1/auth/me",
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
|
||||
latency_ms = int((time.monotonic() - t_start) * 1000)
|
||||
me = resp_me.json() if resp_me.status_code == 200 else {}
|
||||
|
||||
return ConservatoreTestResult(
|
||||
success=resp_me.status_code == 200,
|
||||
message=(
|
||||
f"Connessione ad Aeterna riuscita (utente: {me.get('email', '?')})"
|
||||
if resp_me.status_code == 200
|
||||
else f"Login riuscito ma /me ha risposto HTTP {resp_me.status_code}"
|
||||
),
|
||||
latency_ms=latency_ms,
|
||||
provider_info={
|
||||
"platform": "Aeterna",
|
||||
"tenant_slug": tenant_slug,
|
||||
"user_email": me.get("email"),
|
||||
"permissions_count": len(me.get("permissions", [])),
|
||||
} if me else None,
|
||||
)
|
||||
except Exception as e:
|
||||
return ConservatoreTestResult(
|
||||
success=False,
|
||||
message=f"Errore connessione ad Aeterna: {e}",
|
||||
latency_ms=int((time.monotonic() - t_start) * 1000),
|
||||
)
|
||||
|
||||
else:
|
||||
# Test generico: HTTP Basic HEAD o GET sull'endpoint
|
||||
import base64
|
||||
import httpx
|
||||
auth_str = base64.b64encode(f"{username}:{password}".encode()).decode()
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=10) as client:
|
||||
response = await client.get(
|
||||
endpoint,
|
||||
headers={"Authorization": f"Basic {auth_str}"},
|
||||
)
|
||||
latency_ms = int((time.monotonic() - t_start) * 1000)
|
||||
|
||||
if response.status_code < 500:
|
||||
return ConservatoreTestResult(
|
||||
success=True,
|
||||
message=f"Endpoint raggiungibile (HTTP {response.status_code})",
|
||||
latency_ms=latency_ms,
|
||||
)
|
||||
else:
|
||||
return ConservatoreTestResult(
|
||||
success=False,
|
||||
message=f"Endpoint ha risposto con errore HTTP {response.status_code}",
|
||||
latency_ms=latency_ms,
|
||||
)
|
||||
except Exception as e:
|
||||
return ConservatoreTestResult(
|
||||
success=False,
|
||||
message=f"Errore connessione: {e}",
|
||||
latency_ms=int((time.monotonic() - t_start) * 1000),
|
||||
)
|
||||
|
||||
|
||||
# ─── Indicizzazione full-text ─────────────────────────────────────────────────
|
||||
|
||||
@router.get(
|
||||
|
||||
Reference in New Issue
Block a user