From 4883837ddac1767284525f62cfb4df8a5f77cb7d Mon Sep 17 00:00:00 2001 From: gouki Date: Wed, 5 Aug 2026 22:55:41 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=98=BF=E9=87=8C=E4=BA=91/=E8=85=BE?= =?UTF-8?q?=E8=AE=AF=E4=BA=91list=5Fvps=E8=A1=A5=E5=88=86=E9=A1=B5?= =?UTF-8?q?=EF=BC=88=E8=B6=85100=E5=8F=B0=E5=AE=9E=E4=BE=8B=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E6=BC=8F=E5=90=8C=E6=AD=A5=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/adapters/aliyun.py | 46 ++++++++++++++++++++++++----------------- app/adapters/tencent.py | 42 ++++++++++++++++++++++--------------- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/app/adapters/aliyun.py b/app/adapters/aliyun.py index 88015d7..fefebf0 100644 --- a/app/adapters/aliyun.py +++ b/app/adapters/aliyun.py @@ -79,25 +79,33 @@ class AliyunAdapter(BaseAdapter): return {"ok": False, "message": str(e)} def list_vps(self) -> list: - data = self._call("DescribeInstances", {"PageSize": "100"}) + # 分页拉取:单次最多 100 条,按 TotalCount 翻页,避免实例超 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, + page = 1 + while True: + data = self._call("DescribeInstances", {"PageSize": "100", "PageNumber": str(page)}) + instances = data.get("Instances", {}).get("Instance", []) + for inst in instances: + 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, + ) ) - ) + total = int(data.get("TotalCount") or 0) + if not instances or page * 100 >= total: + break + page += 1 return result diff --git a/app/adapters/tencent.py b/app/adapters/tencent.py index ea5e052..315e202 100644 --- a/app/adapters/tencent.py +++ b/app/adapters/tencent.py @@ -94,24 +94,32 @@ class TencentAdapter(BaseAdapter): return {"ok": False, "message": str(e)} def list_vps(self) -> list: - data = self._call("DescribeInstances", {"Limit": 100}) + # 分页拉取:单次最多 100 条,按 TotalCount 翻页,避免实例超 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, + offset = 0 + while True: + data = self._call("DescribeInstances", {"Limit": 100, "Offset": offset}) + inst_set = data.get("InstanceSet", []) + for inst in inst_set: + 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, + ) ) - ) + total = int(data.get("TotalCount") or 0) + offset += len(inst_set) + if not inst_set or offset >= total: + break return result