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