refactor(sandbox): enhance system default configuration retrieval

- Updated the `get_system_default_config` method to accept a `provider_type` parameter for more precise querying.
- Improved error handling to raise a ValueError if no system default provider is configured for the specified tenant and provider type.
- Added fallback logic to ensure a system default configuration is returned when available.
This commit is contained in:
Harry 2026-01-23 02:06:00 +08:00
parent a409e3d32e
commit 5f0a21d2d4
1 changed files with 19 additions and 3 deletions

View File

@ -188,8 +188,22 @@ class SandboxProviderService:
id=tenant_configed.id, provider_type=tenant_configed.provider_type, config=config
)
else:
return cls.get_system_default_config(session, tenant_id)
system_configed: SandboxProviderSystemConfig | None = (
session.query(SandboxProviderSystemConfig)
.filter_by(provider_type=tenant_configed.provider_type)
.first()
)
if not system_configed:
raise ValueError(
f"No system default provider configured for tenant {tenant_id} and provider type {tenant_configed.provider_type}"
)
return SandboxProviderEntity(
id=tenant_configed.id,
provider_type=system_configed.provider_type,
config=decrypt_system_params(system_configed.encrypted_config),
)
# fallback to system default config
system_configed: SandboxProviderSystemConfig | None = session.query(SandboxProviderSystemConfig).first()
if system_configed:
return SandboxProviderEntity(
@ -201,8 +215,10 @@ class SandboxProviderService:
raise ValueError(f"No sandbox provider configured for tenant {tenant_id}")
@classmethod
def get_system_default_config(cls, session: Session, tenant_id: str) -> SandboxProviderEntity:
system_configed: SandboxProviderSystemConfig | None = session.query(SandboxProviderSystemConfig).first()
def get_system_default_config(cls, session: Session, tenant_id: str, provider_type: str) -> SandboxProviderEntity:
system_configed: SandboxProviderSystemConfig | None = (
session.query(SandboxProviderSystemConfig).filter_by(provider_type=provider_type).first()
)
if system_configed:
return SandboxProviderEntity(
id=system_configed.id,