feat: 云厂商SDK适配层(Vultr/DO/Cloudflare/阿里云/腾讯云)+ 同步服务 + 测试连接/同步接口与前端
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
"""适配器注册表与工厂
|
||||
|
||||
通过 @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
|
||||
aliyun,
|
||||
cloudflare,
|
||||
digitalocean,
|
||||
tencent,
|
||||
vultr,
|
||||
)
|
||||
|
||||
|
||||
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 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", []),
|
||||
}
|
||||
)
|
||||
return result
|
||||
Reference in New Issue
Block a user