feat: 增加查询设备绑定的agent接口

This commit is contained in:
leo 2025-12-06 10:16:37 +08:00
parent da99af4703
commit ded8d1ca93
4 changed files with 50 additions and 4 deletions

View file

@ -105,3 +105,18 @@ async def remove_agent_from_device(
return success_response(data=device.model_dump())
@router.get("/{device_id}/agents", summary="Get device's bound agents")
async def get_device_bound_agents(
device_id: str,
current_user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db)
):
"""Get all agents bound to a specific device"""
result = await device_service.get_device_bound_agents(
db=db,
owner_id=current_user_id,
device_id=device_id
)
return success_response(data=result.model_dump())

View file

@ -18,9 +18,6 @@ class DeviceModel(Base):
# Device unique identifier (external)
device_id: Mapped[str] = mapped_column(String(50), unique=True, nullable=False)
# Device serial number
sn: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
# Owner user ID (null if unbound)
owner_id: Mapped[str | None] = mapped_column(
String(50),

View file

@ -52,3 +52,9 @@ class DeviceListResponse(BaseModel):
"""Device list response"""
devices: List[DeviceWithBindingsResponse]
class DeviceBoundAgentsResponse(BaseModel):
"""Response for device's bound agents"""
device_id: str
agents: List[AgentBindingResponse] = []

View file

@ -8,7 +8,8 @@ from schemas.device import (
DeviceResponse,
DeviceWithBindingsResponse,
DeviceListResponse,
AgentBindingResponse
AgentBindingResponse,
DeviceBoundAgentsResponse
)
@ -140,6 +141,33 @@ class DeviceService:
return await self._build_device_with_bindings(db, device)
async def get_device_bound_agents(
self,
db: AsyncSession,
owner_id: str,
device_id: str
) -> DeviceBoundAgentsResponse:
"""Get all agents bound to a device"""
device = await Device.get_by_id(db, device_id)
if not device:
raise NotFoundException("Device not found")
if device.owner_id != owner_id:
raise BadRequestException("Device does not belong to you")
bindings = await AgentDeviceBinding.get_bindings_by_device(db, device_id)
return DeviceBoundAgentsResponse(
device_id=device_id,
agents=[
AgentBindingResponse(
agent_id=b.agent_id,
is_default=b.is_default,
created_at=b.created_at
)
for b in bindings
]
)
async def _build_device_with_bindings(
self,
db: AsyncSession,