评估发现项目只有台账能力,缺失真正的多云管理链路。本次按缺口逐项补齐: 适配层 - 新增 aws / linode / namesilo 适配器,registry 登记;阿里云、腾讯云补 list_domains 与 get_account 余额 - NormalizedVPS 补 expiry_date + charge_type:此前云端同步进来的 VPS 拿不到 到期日,永远进不了续费提醒——而续费提醒正是这套台账存在的理由 - base.py 统一云端错误可读化(http_error_message / cloud_error_text):各厂商 把原因放的位置都不同(Linode errors[].reason、Cloudflare errors[].message、 腾讯 Response.Error.Message、AWS XML),此前直接 str(e) 会把 httpx 的多行文本 连同 MDN 链接弹给用户 动作层与 DNS - 新增 /api/actions:实例启停/重启/续费,写操作全部二次确认 + 审计 - 新增 Cloudflare DNS 记录的增删改查 定时同步 - scripts/sync_all_assets.py + /api/sync/assets + vps-sync-assets.timer(08:30) - 08:30 而非随机时刻:必须早于 09:00 的续费检查,否则提醒读到上一天的旧到期日 - sync_all_assets 修正计数:每一项子同步都失败时不再计为「成功」,否则凭证过期 会显示「成功 1」,用户以为数据拉到了、实际一条没有 数据一致性 - AssetRead 输出 external_id(只出不进):前端「云操作」「DNS」按钮靠它渲染, 缺了则所有按钮永远出不来 - import_assets 落库 external_id:导出带了去重键而导入丢弃,会让迁移后首次同步 把所有云端资产当新增再建一份 - _align_preset_providers 取代 _backfill_provider_services:库里声明但注册表未 实现的 sdk_type 清空(声明支持却不支持,比明说不支持更糟),services 只增量 补标签不覆盖,用户自建平台不动 前端如实展示 - cost 为 0 统一显示「未填」而非「0 USD」:0 既可能是没填、也可能是云端不返回 价格,而汇总处又把 0 当「无费用」排除,两处口径不一致 - 总览「本年支出」增加 year_cost_missing,把合计的覆盖缺口说出来 - DNS 弹窗补 MX/SRV 优先级与备注;列表请求失败时 provider 用资产自带 slug 兜底, 否则 Cloudflare 代理勾选框永远不出现 测试:257 -> 309 passed
89 lines
2.2 KiB
Python
89 lines
2.2 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,
|
|
cloudflare,
|
|
digitalocean,
|
|
linode,
|
|
namesilo,
|
|
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 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", []),
|
|
}
|
|
)
|
|
return result
|