feat: 云厂商SDK适配层(Vultr/DO/Cloudflare/阿里云/腾讯云)+ 同步服务 + 测试连接/同步接口与前端

This commit is contained in:
gouki
2026-08-02 15:56:48 +00:00
parent cc7b2c67fe
commit 1aacf51bea
13 changed files with 930 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
"""云厂商 SDK 适配层
通过 registry.get_adapter(sdk_type, config) 获取对应平台适配器,
调用 test_connection / list_vps / list_domains / get_account 等统一接口。
"""
+103
View File
@@ -0,0 +1,103 @@
"""阿里云适配器(含国际版,OpenAPI RPC 风格 + HMAC-SHA1 签名)
API 文档:https://help.aliyun.com/document_detail/25484.html
所需配置:{"access_key_id": "...", "access_key_secret": "...", "region": "cn-hangzhou"}
国际版(alibabacloud)只需将 region 设为海外区域(如 ap-southeast-1),端点自动按区域构造。
说明:此处实现 RPC V1 签名(HMAC-SHA1),可直接调用 ECS OpenAPI
生产环境也可替换为官方 alibabacloud-ecs SDK,接口契约保持不变。
"""
import base64
import hashlib
import hmac
import time
import uuid
from urllib.parse import quote
import httpx
from app.adapters.base import BaseAdapter, NormalizedVPS
from app.adapters.registry import register
@register("aliyun-sdk", "alibabacloud-sdk")
class AliyunAdapter(BaseAdapter):
required_config = ["access_key_id", "access_key_secret"]
API_VERSION = "2014-05-26"
def _region(self) -> str:
return self.config.get("region") or "cn-hangzhou"
def _endpoint(self) -> str:
return f"https://ecs.{self._region()}.aliyuncs.com"
@staticmethod
def _percent_encode(value) -> str:
return quote(str(value), safe="~")
def _sign(self, params: dict, method: str = "GET") -> str:
canonical = "&".join(
f"{self._percent_encode(k)}={self._percent_encode(v)}"
for k, v in sorted(params.items())
)
string_to_sign = f"{method}&{self._percent_encode('/')}&{self._percent_encode(canonical)}"
key = (self.config.get("access_key_secret", "") + "&").encode()
digest = hmac.new(key, string_to_sign.encode(), hashlib.sha1).digest()
return base64.b64encode(digest).decode()
def _call(self, action: str, biz_params: dict = None) -> dict:
params = {
"Format": "JSON",
"Version": self.API_VERSION,
"AccessKeyId": self.config.get("access_key_id", ""),
"SignatureMethod": "HMAC-SHA1",
"Timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"SignatureVersion": "1.0",
"SignatureNonce": str(uuid.uuid4()),
"Action": action,
"RegionId": self._region(),
}
if biz_params:
params.update(biz_params)
params["Signature"] = self._sign(params)
with httpx.Client(timeout=30) as client:
resp = client.get(self._endpoint(), params=params)
resp.raise_for_status()
data = resp.json()
if data.get("Code"):
raise RuntimeError(f"{data.get('Code')}: {data.get('Message')}")
return data
def test_connection(self) -> dict:
try:
data = self._call("DescribeRegions")
count = len(data.get("Regions", {}).get("Region", []))
return {"ok": True, "message": f"连接成功,可用区域 {count}"}
except httpx.HTTPStatusError as e:
return {"ok": False, "message": f"HTTP {e.response.status_code}:凭证无效"}
except Exception as e: # noqa: BLE001
return {"ok": False, "message": str(e)}
def list_vps(self) -> list:
data = self._call("DescribeInstances", {"PageSize": "100"})
result = []
for inst in data.get("Instances", {}).get("Instance", []):
public_ips = inst.get("PublicIpAddress", {}).get("IpAddress", [])
eip = inst.get("EipAddress", {}).get("IpAddress")
mem_mb = inst.get("Memory") or 0
status_map = {"Running": "active", "Stopped": "stopped"}
result.append(
NormalizedVPS(
external_id=inst.get("InstanceId"),
name=inst.get("InstanceName") or inst.get("InstanceId"),
ip_address=eip or (public_ips[0] if public_ips else None),
region=inst.get("RegionId"),
os=inst.get("OSName"),
cpu_cores=inst.get("Cpu"),
memory_gb=round(mem_mb / 1024, 1) if mem_mb else None,
status=status_map.get(inst.get("Status"), inst.get("Status") or "unknown"),
currency="CNY",
raw=inst,
)
)
return result
+101
View File
@@ -0,0 +1,101 @@
"""云厂商 SDK 适配层 — 抽象基类与标准化数据结构
各云厂商适配器继承 BaseAdapter,实现统一接口,将平台特定的 API 返回
转换为标准化的资产结构(NormalizedVPS / NormalizedDomain / AccountInfo),
供同步服务写入数据库。这样上层逻辑无需关心各平台 API 差异。
"""
from abc import ABC, abstractmethod
from dataclasses import asdict, dataclass, field
from typing import Optional
@dataclass
class NormalizedVPS:
"""标准化的 VPS 实例"""
external_id: str
name: str
ip_address: Optional[str] = None
region: Optional[str] = None
os: Optional[str] = None
cpu_cores: Optional[int] = None
memory_gb: Optional[float] = None
disk_gb: Optional[int] = None
status: str = "active"
monthly_cost: Optional[float] = None
currency: str = "USD"
raw: dict = field(default_factory=dict)
def to_dict(self) -> dict:
return asdict(self)
@dataclass
class NormalizedDomain:
"""标准化的域名"""
external_id: str
domain_name: str
registrar: Optional[str] = None
expiry_date: Optional[str] = None # ISO 日期字符串 YYYY-MM-DD
status: str = "active"
raw: dict = field(default_factory=dict)
def to_dict(self) -> dict:
return asdict(self)
@dataclass
class AccountInfo:
"""标准化的账户信息"""
balance: Optional[float] = None
currency: str = "USD"
pending_charges: Optional[float] = None
raw: dict = field(default_factory=dict)
def to_dict(self) -> dict:
return asdict(self)
def _overridden(instance: "BaseAdapter", method_name: str) -> bool:
"""判断子类是否重写了某方法(用于能力探测)"""
return type(instance).__dict__.get(method_name) is not None
class BaseAdapter(ABC):
"""云厂商适配器抽象基类
子类需:
- 通过 @register("xxx-sdk") 注册到注册表
- 设置 required_config(所需凭证字段,供前端表单提示)
- 实现 test_connection;按需重写 list_vps / list_domains / get_account
"""
#: 适配器所需的配置字段名(如 ["api_key"] 或 ["access_key_id", "access_key_secret"]
required_config: list = []
def __init__(self, config: Optional[dict] = None):
self.config = config or {}
@abstractmethod
def test_connection(self) -> dict:
"""测试连接 / 凭证有效性,返回 {"ok": bool, "message": str, ...}"""
def list_vps(self) -> list:
raise NotImplementedError(f"{type(self).__name__} 未实现 list_vps")
def list_domains(self) -> list:
raise NotImplementedError(f"{type(self).__name__} 未实现 list_domains")
def get_account(self) -> AccountInfo:
raise NotImplementedError(f"{type(self).__name__} 未实现 get_account")
def capabilities(self) -> dict:
"""返回适配器实际支持的能力(是否重写了相应方法)"""
return {
"list_vps": _overridden(self, "list_vps"),
"list_domains": _overridden(self, "list_domains"),
"get_account": _overridden(self, "get_account"),
}
+57
View File
@@ -0,0 +1,57 @@
"""Cloudflare 适配器(REST API v4Bearer Token
API 文档:https://developers.cloudflare.com/api/
所需配置:{"api_token": "..."}(建议用 API Token,权限含 Zone:Read / Account:Read
Cloudflare 无传统 VPS,主要同步托管域名(zones);Workers/R2/Tunnel 子资产留待后续阶段。
"""
import httpx
from app.adapters.base import BaseAdapter, NormalizedDomain
from app.adapters.registry import register
@register("cloudflare-api")
class CloudflareAdapter(BaseAdapter):
required_config = ["api_token"]
BASE = "https://api.cloudflare.com/client/v4"
def _headers(self) -> dict:
return {"Authorization": f"Bearer {self.config.get('api_token', '')}"}
def _get(self, path: str) -> dict:
with httpx.Client(timeout=30) as client:
resp = client.get(self.BASE + path, headers=self._headers())
resp.raise_for_status()
data = resp.json()
if not data.get("success", True):
errors = data.get("errors", [])
raise RuntimeError(errors[0].get("message") if errors else "Cloudflare API 返回失败")
return data
def test_connection(self) -> dict:
try:
data = self._get("/user/tokens/verify")
status = data.get("result", {}).get("status")
if status == "active":
return {"ok": True, "message": "API Token 有效"}
return {"ok": False, "message": f"Token 状态异常:{status}"}
except httpx.HTTPStatusError as e:
return {"ok": False, "message": f"HTTP {e.response.status_code}Token 无效或权限不足"}
except Exception as e: # noqa: BLE001
return {"ok": False, "message": str(e)}
def list_domains(self) -> list:
data = self._get("/zones?per_page=50")
result = []
for z in data.get("result", []):
result.append(
NormalizedDomain(
external_id=z.get("id"),
domain_name=z.get("name"),
registrar="cloudflare",
status="active" if z.get("status") == "active" else (z.get("status") or "unknown"),
raw=z,
)
)
return result
+89
View File
@@ -0,0 +1,89 @@
"""DigitalOcean 适配器(REST API v2Bearer Token
API 文档:https://docs.digitalocean.com/reference/api/
所需配置:{"api_key": "..."}
"""
import httpx
from app.adapters.base import (
AccountInfo,
BaseAdapter,
NormalizedDomain,
NormalizedVPS,
)
from app.adapters.registry import register
@register("do-api")
class DigitalOceanAdapter(BaseAdapter):
required_config = ["api_key"]
BASE = "https://api.digitalocean.com/v2"
def _headers(self) -> dict:
return {"Authorization": f"Bearer {self.config.get('api_key', '')}"}
def _get(self, path: str) -> dict:
with httpx.Client(timeout=30) as client:
resp = client.get(self.BASE + path, headers=self._headers())
resp.raise_for_status()
return resp.json()
def test_connection(self) -> dict:
try:
data = self._get("/account")
balance = data.get("account", {}).get("balance")
return {"ok": True, "message": f"连接成功,账户余额 {balance} USD"}
except httpx.HTTPStatusError as e:
return {"ok": False, "message": f"HTTP {e.response.status_code}Token 无效或权限不足"}
except Exception as e: # noqa: BLE001
return {"ok": False, "message": str(e)}
def get_account(self) -> AccountInfo:
acc = self._get("/account").get("account", {})
return AccountInfo(balance=acc.get("balance"), currency="USD", raw=acc)
@staticmethod
def _main_ip(droplet: dict) -> str:
try:
v4 = droplet.get("networks", {}).get("v4", [])
return v4[0].get("ip_address") if v4 else None
except Exception: # noqa: BLE001
return None
def list_vps(self) -> list:
data = self._get("/droplets")
result = []
for d in data.get("droplets", []):
image = d.get("image", {})
os_name = f"{image.get('distribution', '')} {image.get('name', '')}".strip()
mem_mb = d.get("memory") or 0
region = d.get("region", {})
result.append(
NormalizedVPS(
external_id=str(d.get("id")),
name=d.get("name"),
ip_address=self._main_ip(d),
region=region.get("slug") or region.get("name"),
os=os_name or None,
cpu_cores=d.get("vcpus"),
memory_gb=round(mem_mb / 1024, 1) if mem_mb else None,
disk_gb=d.get("disk"),
status="active" if d.get("status") == "active" else (d.get("status") or "unknown"),
currency="USD",
raw=d,
)
)
return result
def list_domains(self) -> list:
data = self._get("/domains")
return [
NormalizedDomain(
external_id=d.get("name"),
domain_name=d.get("name"),
registrar="digitalocean",
raw=d,
)
for d in data.get("domains", [])
]
+72
View File
@@ -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
+117
View File
@@ -0,0 +1,117 @@
"""腾讯云适配器(含国际版,CVM OpenAPI + TC3-HMAC-SHA256 签名)
API 文档:https://cloud.tencent.com/document/api/213/15753
所需配置:{"secret_id": "...", "secret_key": "...", "region": "ap-guangzhou"}
国际版(intl)将 region 设为海外区域即可,端点统一为 cvm.tencentcloudapi.com。
说明:此处实现 TC3-HMAC-SHA256 签名,可直接调用 CVM OpenAPI
生产环境也可替换为官方 tencentcloud-sdk-python,接口契约保持不变。
"""
import hashlib
import hmac
import json
import time
import httpx
from app.adapters.base import BaseAdapter, NormalizedVPS
from app.adapters.registry import register
@register("tencent-sdk", "tencent-intl-sdk")
class TencentAdapter(BaseAdapter):
required_config = ["secret_id", "secret_key"]
HOST = "cvm.tencentcloudapi.com"
SERVICE = "cvm"
VERSION = "2017-03-12"
ALGORITHM = "TC3-HMAC-SHA256"
def _region(self) -> str:
return self.config.get("region") or "ap-guangzhou"
@staticmethod
def _hmac_sha256(key: bytes, msg: str) -> bytes:
return hmac.new(key, msg.encode(), hashlib.sha256).digest()
def _build_authorization(self, action: str, payload_json: str, timestamp: int, date: str):
secret_id = self.config.get("secret_id", "")
secret_key = self.config.get("secret_key", "")
content_type = "application/json; charset=utf-8"
canonical_headers = (
f"content-type:{content_type}\nhost:{self.HOST}\nx-tc-action:{action.lower()}\n"
)
signed_headers = "content-type;host;x-tc-action"
hashed_payload = hashlib.sha256(payload_json.encode()).hexdigest()
canonical_request = (
f"POST\n/\n\n{canonical_headers}\n{signed_headers}\n{hashed_payload}"
)
credential_scope = f"{date}/{self.SERVICE}/tc3_request"
hashed_canonical = hashlib.sha256(canonical_request.encode()).hexdigest()
string_to_sign = (
f"{self.ALGORITHM}\n{timestamp}\n{credential_scope}\n{hashed_canonical}"
)
secret_date = self._hmac_sha256(("TC3" + secret_key).encode(), date)
secret_service = self._hmac_sha256(secret_date, self.SERVICE)
secret_signing = self._hmac_sha256(secret_service, "tc3_request")
signature = hmac.new(secret_signing, string_to_sign.encode(), hashlib.sha256).hexdigest()
return (
f"{self.ALGORITHM} Credential={secret_id}/{credential_scope}, "
f"SignedHeaders={signed_headers}, Signature={signature}"
)
def _call(self, action: str, payload: dict = None) -> dict:
payload_json = json.dumps(payload or {})
timestamp = int(time.time())
date = time.strftime("%Y-%m-%d", time.gmtime(timestamp))
authorization = self._build_authorization(action, payload_json, timestamp, date)
headers = {
"Authorization": authorization,
"Content-Type": "application/json; charset=utf-8",
"Host": self.HOST,
"X-TC-Action": action,
"X-TC-Timestamp": str(timestamp),
"X-TC-Version": self.VERSION,
"X-TC-Region": self._region(),
}
with httpx.Client(timeout=30) as client:
resp = client.post(f"https://{self.HOST}", content=payload_json, headers=headers)
resp.raise_for_status()
data = resp.json()
response = data.get("Response", {})
if response.get("Error"):
err = response["Error"]
raise RuntimeError(f"{err.get('Code')}: {err.get('Message')}")
return response
def test_connection(self) -> dict:
try:
data = self._call("DescribeRegions")
count = len(data.get("RegionSet", []))
return {"ok": True, "message": f"连接成功,可用区域 {count}"}
except httpx.HTTPStatusError as e:
return {"ok": False, "message": f"HTTP {e.response.status_code}:凭证无效"}
except Exception as e: # noqa: BLE001
return {"ok": False, "message": str(e)}
def list_vps(self) -> list:
data = self._call("DescribeInstances", {"Limit": 100})
result = []
status_map = {"RUNNING": "active", "STOPPED": "stopped"}
for inst in data.get("InstanceSet", []):
public_ips = inst.get("PublicIpAddresses", [])
result.append(
NormalizedVPS(
external_id=inst.get("InstanceId"),
name=inst.get("InstanceName") or inst.get("InstanceId"),
ip_address=public_ips[0] if public_ips else None,
region=inst.get("Placement", {}).get("Zone"),
os=inst.get("OsName"),
cpu_cores=inst.get("CPU"),
memory_gb=inst.get("Memory"),
disk_gb=inst.get("SystemDisk", {}).get("DiskSize"),
status=status_map.get(inst.get("InstanceState"), inst.get("InstanceState") or "unknown"),
currency="CNY",
raw=inst,
)
)
return result
+84
View File
@@ -0,0 +1,84 @@
"""Vultr 适配器(REST API v2Bearer Token
API 文档:https://www.vultr.com/api/
所需配置:{"api_key": "..."}
"""
import httpx
from app.adapters.base import (
AccountInfo,
BaseAdapter,
NormalizedDomain,
NormalizedVPS,
)
from app.adapters.registry import register
@register("vultr-api")
class VultrAdapter(BaseAdapter):
required_config = ["api_key"]
BASE = "https://api.vultr.com/v2"
def _headers(self) -> dict:
return {"Authorization": f"Bearer {self.config.get('api_key', '')}"}
def _get(self, path: str) -> dict:
with httpx.Client(timeout=30) as client:
resp = client.get(self.BASE + path, headers=self._headers())
resp.raise_for_status()
return resp.json()
def test_connection(self) -> dict:
try:
data = self._get("/account")
balance = data.get("account", {}).get("balance")
return {"ok": True, "message": f"连接成功,账户余额 {balance} USD"}
except httpx.HTTPStatusError as e:
return {"ok": False, "message": f"HTTP {e.response.status_code}API Key 无效或权限不足"}
except Exception as e: # noqa: BLE001
return {"ok": False, "message": str(e)}
def get_account(self) -> AccountInfo:
acc = self._get("/account").get("account", {})
return AccountInfo(
balance=acc.get("balance"),
currency="USD",
pending_charges=acc.get("pending_charges"),
raw=acc,
)
def list_vps(self) -> list:
data = self._get("/instances")
result = []
for inst in data.get("instances", []):
ram_mb = inst.get("ram") or 0
result.append(
NormalizedVPS(
external_id=inst.get("id"),
name=inst.get("label") or inst.get("id"),
ip_address=inst.get("main_ip"),
region=inst.get("region"),
os=inst.get("os"),
cpu_cores=inst.get("vcpu_count"),
memory_gb=round(ram_mb / 1024, 1) if ram_mb else None,
disk_gb=inst.get("disk"),
status="active" if inst.get("status") == "active" else (inst.get("status") or "unknown"),
monthly_cost=inst.get("monthly_cost"),
currency="USD",
raw=inst,
)
)
return result
def list_domains(self) -> list:
data = self._get("/domains")
return [
NormalizedDomain(
external_id=d.get("domain"),
domain_name=d.get("domain"),
registrar="vultr",
raw=d,
)
for d in data.get("domains", [])
]