import os import logging from typing import Dict, Any, Optional import httpx # Configurazione OCI API Gateway OCI_GATEWAY_ID = "ocid1.apigatewayapi.oc1.eu-milan-1.amaaaaaavx6czpaabfdwezue3fo7jpfsrxqwlo2nt7pwhs7o2ezvqztuaffa" ORACLE_NODE_E_IP = os.getenv("ORACLE_NODE_E_IP", "80.225.89.217") ORACLE_NODE_E_PORT = int(os.getenv("ORACLE_NODE_E_PORT", "8080")) INTERNAL_TOKEN = os.getenv("INTERNAL_TOKEN", "") logger = logging.getLogger("agente_ai.oci_gateway") class OCIGatewayManager: """ Gestore per Oracle Cloud API Gateway. Funge da ponte tra il cluster AI e il Nodo E (Milano) per bypassare il Network Firewall (Palo Alto) tramite endpoint HTTPS gestiti. """ def __init__(self): self.gateway_id = OCI_GATEWAY_ID self.base_url = f"https://{self.gateway_id}.apigateway.eu-milan-1.oci.customer-oci.com" self.token = INTERNAL_TOKEN async def get_gateway_status(self) -> Dict[str, Any]: """Verifica lo stato del gateway e la connettività verso il Nodo E.""" try: async with httpx.AsyncClient() as client: # Nota: L'URL reale dipenderà dal deployment specifico su OCI # Questo è un placeholder per la logica di health check via gateway response = await client.get( f"{self.base_url}/health", headers={"Authorization": f"Bearer {self.token}"}, timeout=10.0 ) return { "ok": response.status_code == 200, "gateway_id": self.gateway_id, "status_code": response.status_code, "provider": "Oracle Cloud API Gateway" } except Exception as e: logger.error(f"Errore health check gateway: {e}") return {"ok": False, "error": str(e)} async def proxy_to_node_e(self, path: str, method: str = "GET", body: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: """Invia una richiesta al Nodo E passando attraverso l'API Gateway.""" url = f"{self.base_url}{path}" try: async with httpx.AsyncClient() as client: if method == "POST": response = await client.post(url, json=body, headers={"Authorization": f"Bearer {self.token}"}) else: response = await client.get(url, headers={"Authorization": f"Bearer {self.token}"}) return response.json() if response.status_code == 200 else {"ok": False, "status": response.status_code} except Exception as e: logger.error(f"Errore proxy gateway: {e}") return {"ok": False, "error": str(e)} # Istanza globale oci_gateway = OCIGatewayManager()