feat: Vultr/DO/Cloudflare适配器分页处理(避免多VPS/域名同步遗漏)+ 分页测试

This commit is contained in:
gouki
2026-08-02 17:59:51 +00:00
parent 8ba58caaa8
commit 3ef84180b4
4 changed files with 135 additions and 68 deletions
+16 -10
View File
@@ -42,16 +42,22 @@ class CloudflareAdapter(BaseAdapter):
return {"ok": False, "message": str(e)} return {"ok": False, "message": str(e)}
def list_domains(self) -> list: def list_domains(self) -> list:
data = self._get("/zones?per_page=50")
result = [] result = []
for z in data.get("result", []): page = 1
result.append( while True:
NormalizedDomain( data = self._get(f"/zones?per_page=50&page={page}")
external_id=z.get("id"), for z in data.get("result", []):
domain_name=z.get("name"), result.append(
registrar="cloudflare", NormalizedDomain(
status="active" if z.get("status") == "active" else (z.get("status") or "unknown"), external_id=z.get("id"),
raw=z, domain_name=z.get("name"),
registrar="cloudflare",
status="active" if z.get("status") == "active" else (z.get("status") or "unknown"),
raw=z,
)
) )
) info = data.get("result_info", {})
if page >= info.get("total_pages", 1):
break
page += 1
return result return result
+42 -30
View File
@@ -52,38 +52,50 @@ class DigitalOceanAdapter(BaseAdapter):
return None return None
def list_vps(self) -> list: def list_vps(self) -> list:
data = self._get("/droplets")
result = [] result = []
for d in data.get("droplets", []): page = 1
image = d.get("image", {}) while True:
os_name = f"{image.get('distribution', '')} {image.get('name', '')}".strip() data = self._get(f"/droplets?per_page=100&page={page}")
mem_mb = d.get("memory") or 0 for d in data.get("droplets", []):
region = d.get("region", {}) image = d.get("image", {})
result.append( os_name = f"{image.get('distribution', '')} {image.get('name', '')}".strip()
NormalizedVPS( mem_mb = d.get("memory") or 0
external_id=str(d.get("id")), region = d.get("region", {})
name=d.get("name"), result.append(
ip_address=self._main_ip(d), NormalizedVPS(
region=region.get("slug") or region.get("name"), external_id=str(d.get("id")),
os=os_name or None, name=d.get("name"),
cpu_cores=d.get("vcpus"), ip_address=self._main_ip(d),
memory_gb=round(mem_mb / 1024, 1) if mem_mb else None, region=region.get("slug") or region.get("name"),
disk_gb=d.get("disk"), os=os_name or None,
status="active" if d.get("status") == "active" else (d.get("status") or "unknown"), cpu_cores=d.get("vcpus"),
currency="USD", memory_gb=round(mem_mb / 1024, 1) if mem_mb else None,
raw=d, disk_gb=d.get("disk"),
status="active" if d.get("status") == "active" else (d.get("status") or "unknown"),
currency="USD",
raw=d,
)
) )
) if not data.get("links", {}).get("pages", {}).get("next"):
break
page += 1
return result return result
def list_domains(self) -> list: def list_domains(self) -> list:
data = self._get("/domains") result = []
return [ page = 1
NormalizedDomain( while True:
external_id=d.get("name"), data = self._get(f"/domains?per_page=100&page={page}")
domain_name=d.get("name"), for d in data.get("domains", []):
registrar="digitalocean", result.append(
raw=d, NormalizedDomain(
) external_id=d.get("name"),
for d in data.get("domains", []) domain_name=d.get("name"),
] registrar="digitalocean",
raw=d,
)
)
if not data.get("links", {}).get("pages", {}).get("next"):
break
page += 1
return result
+46 -28
View File
@@ -49,36 +49,54 @@ class VultrAdapter(BaseAdapter):
) )
def list_vps(self) -> list: def list_vps(self) -> list:
data = self._get("/instances")
result = [] result = []
for inst in data.get("instances", []): cursor = ""
ram_mb = inst.get("ram") or 0 while True:
result.append( path = "/instances?per_page=100"
NormalizedVPS( if cursor:
external_id=inst.get("id"), path += f"&cursor={cursor}"
name=inst.get("label") or inst.get("id"), data = self._get(path)
ip_address=inst.get("main_ip"), for inst in data.get("instances", []):
region=inst.get("region"), ram_mb = inst.get("ram") or 0
os=inst.get("os"), result.append(
cpu_cores=inst.get("vcpu_count"), NormalizedVPS(
memory_gb=round(ram_mb / 1024, 1) if ram_mb else None, external_id=inst.get("id"),
disk_gb=inst.get("disk"), name=inst.get("label") or inst.get("id"),
status="active" if inst.get("status") == "active" else (inst.get("status") or "unknown"), ip_address=inst.get("main_ip"),
monthly_cost=inst.get("monthly_cost"), region=inst.get("region"),
currency="USD", os=inst.get("os"),
raw=inst, 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,
)
) )
) cursor = data.get("meta", {}).get("links", {}).get("next") or ""
if not cursor:
break
return result return result
def list_domains(self) -> list: def list_domains(self) -> list:
data = self._get("/domains") result = []
return [ cursor = ""
NormalizedDomain( while True:
external_id=d.get("domain"), path = "/domains?per_page=100"
domain_name=d.get("domain"), if cursor:
registrar="vultr", path += f"&cursor={cursor}"
raw=d, data = self._get(path)
) for d in data.get("domains", []):
for d in data.get("domains", []) result.append(
] NormalizedDomain(
external_id=d.get("domain"),
domain_name=d.get("domain"),
registrar="vultr",
raw=d,
)
)
cursor = data.get("meta", {}).get("links", {}).get("next") or ""
if not cursor:
break
return result
+31
View File
@@ -220,3 +220,34 @@ def test_ai_capabilities_account_only():
def test_registry_supports_ai_types(): def test_registry_supports_ai_types():
for t in ["deepseek-api", "moonshot-api", "openai-api", "minimax-api"]: for t in ["deepseek-api", "moonshot-api", "openai-api", "minimax-api"]:
assert registry.is_supported(t), f"{t} 应被支持" assert registry.is_supported(t), f"{t} 应被支持"
# --------------------------- 分页处理 --------------------------- #
def test_vultr_pagination_cursor():
adapter = VultrAdapter({"api_key": "x"})
page1 = {"instances": [{"id": "a", "label": "a", "ram": 1024}], "meta": {"links": {"next": "cursor2"}}}
page2 = {"instances": [{"id": "b", "label": "b", "ram": 2048}], "meta": {"links": {"next": ""}}}
with patch.object(VultrAdapter, "_get", side_effect=[page1, page2]):
result = adapter.list_vps()
assert len(result) == 2
assert {v.external_id for v in result} == {"a", "b"}
def test_do_pagination_page():
adapter = DigitalOceanAdapter({"api_key": "x"})
page1 = {"droplets": [{"id": 1, "name": "d1", "memory": 1024, "networks": {"v4": []}}], "links": {"pages": {"next": "x"}}}
page2 = {"droplets": [{"id": 2, "name": "d2", "memory": 1024, "networks": {"v4": []}}], "links": {}}
with patch.object(DigitalOceanAdapter, "_get", side_effect=[page1, page2]):
result = adapter.list_vps()
assert len(result) == 2
assert {v.external_id for v in result} == {"1", "2"}
def test_cloudflare_pagination_result_info():
adapter = CloudflareAdapter({"api_token": "x"})
page1 = {"result": [{"id": "z1", "name": "a.com", "status": "active"}], "result_info": {"total_pages": 2, "page": 1}}
page2 = {"result": [{"id": "z2", "name": "b.com", "status": "active"}], "result_info": {"total_pages": 2, "page": 2}}
with patch.object(CloudflareAdapter, "_get", side_effect=[page1, page2]):
result = adapter.list_domains()
assert len(result) == 2
assert {d.domain_name for d in result} == {"a.com", "b.com"}