线上平台列表里 cloudcone、zeabur、xinwang、dynadot、agnes 的 sdk_type 仍为空, 平台卡片只能显示「未对接」,用户无法同步。按各自真实 API 能力补齐: - CloudCone:实例清单 + 开机/关机/重启;无公开账单接口,不声明余额与续费 - Zeabur:服务器清单 + 重启;毫核/MB 归一化为核/GB,未采集时不伪造规格 - Dynadot:域名清单(含到期日)+ 账户余额;set_dns2 位置参数无法安全 CRUD - 新网:代理 API 按域名逐个查到期日;无列表接口,需在账号里填 domains 清单 - Agnes:/models 校验 Key;无余额接口,如实标记不支持 配套改动: - _align_preset_providers 为库中为空的 sdk_type 回填种子值(启动即生效), 避免新适配器上线后线上仍停留在「未对接」 - 适配器可声明 plain_config,代理号/域名等非密钥字段前端按明文输入 - PRESET_PROVIDERS 五条记录填上对应 sdk_type 测试:445 passed(新增 tests/test_platform_adapters.py),vitest 62 passed。
95 lines
2.3 KiB
Python
95 lines
2.3 KiB
Python
"""适配器注册表与工厂
|
|
|
|
通过 @register("sdk-type") 装饰器将适配器类登记到注册表,
|
|
上层按 Provider.sdk_type 取得对应适配器实例。
|
|
"""
|
|
|
|
from typing import Optional
|
|
|
|
from app.adapters.base import BaseAdapter
|
|
|
|
_REGISTRY: dict = {}
|
|
|
|
|
|
def register(*sdk_types: str):
|
|
"""装饰器:将适配器类注册到一个或多个 sdk_type"""
|
|
|
|
def decorator(cls):
|
|
for t in sdk_types:
|
|
_REGISTRY[t] = cls
|
|
cls.sdk_types = list(sdk_types)
|
|
return cls
|
|
|
|
return decorator
|
|
|
|
|
|
def load_all() -> None:
|
|
"""导入所有适配器模块以触发注册(幂等)"""
|
|
from app.adapters import ( # noqa: F401
|
|
ai,
|
|
aliyun,
|
|
aws,
|
|
cloudcone,
|
|
cloudflare,
|
|
digitalocean,
|
|
dynadot,
|
|
linode,
|
|
namesilo,
|
|
tencent,
|
|
vultr,
|
|
whmcs,
|
|
xinnet,
|
|
zeabur,
|
|
)
|
|
|
|
|
|
def get_adapter(sdk_type: str, config: Optional[dict] = None) -> BaseAdapter:
|
|
"""按 sdk_type 创建适配器实例"""
|
|
load_all()
|
|
cls = _REGISTRY.get(sdk_type)
|
|
if not cls:
|
|
raise ValueError(f"未支持的 SDK 类型:{sdk_type}")
|
|
return cls(config)
|
|
|
|
|
|
def is_supported(sdk_type: Optional[str]) -> bool:
|
|
load_all()
|
|
return sdk_type in _REGISTRY
|
|
|
|
|
|
def supported_types() -> list:
|
|
load_all()
|
|
return sorted(_REGISTRY.keys())
|
|
|
|
|
|
def sdk_capabilities() -> dict:
|
|
"""返回 sdk_type -> capabilities 映射(供前端判断测试/同步按钮)"""
|
|
load_all()
|
|
result = {}
|
|
for sdk_type, cls in _REGISTRY.items():
|
|
try:
|
|
result[sdk_type] = cls({}).capabilities()
|
|
except Exception: # noqa: BLE001
|
|
result[sdk_type] = {}
|
|
return result
|
|
|
|
|
|
def adapter_meta() -> list:
|
|
"""返回所有已注册适配器的元信息(供前端展示所需凭证字段与能力)"""
|
|
load_all()
|
|
seen = set()
|
|
result = []
|
|
for sdk_type, cls in sorted(_REGISTRY.items()):
|
|
if cls.__name__ in seen:
|
|
continue
|
|
seen.add(cls.__name__)
|
|
result.append(
|
|
{
|
|
"class": cls.__name__,
|
|
"sdk_types": getattr(cls, "sdk_types", [sdk_type]),
|
|
"required_config": getattr(cls, "required_config", []),
|
|
"plain_config": getattr(cls, "plain_config", []),
|
|
}
|
|
)
|
|
return result
|