Counts the number of agent networks the specified proxy is connected to. Inspects the proxy container and counts networks matching the `tsk-agent-*` pattern (excluding the external network).
(&self, container_name: &str)
| 393 | /// Inspects the proxy container and counts networks matching the |
| 394 | /// `tsk-agent-*` pattern (excluding the external network). |
| 395 | pub async fn count_connected_agents(&self, container_name: &str) -> Result<usize> { |
| 396 | // Inspect the proxy container to get its network connections |
| 397 | match self.docker_client.inspect_container(container_name).await { |
| 398 | Ok(json_data) => { |
| 399 | let data: serde_json::Value = serde_json::from_str(&json_data) |
| 400 | .map_err(|e| anyhow::anyhow!("Failed to parse container info: {e}"))?; |
| 401 | |
| 402 | // Count networks, excluding the external network |
| 403 | let count = data |
| 404 | .get("NetworkSettings") |
| 405 | .and_then(|ns| ns.get("Networks")) |
| 406 | .and_then(|n| n.as_object()) |
| 407 | .map(|networks| { |
| 408 | networks |
| 409 | .keys() |
| 410 | .filter(|name| name.starts_with(TSK_AGENT_NETWORK_PREFIX)) |
| 411 | .count() |
| 412 | }) |
| 413 | .unwrap_or(0); |
| 414 | |
| 415 | Ok(count) |
| 416 | } |
| 417 | Err(e) if e.to_lowercase().contains("no such container") => Ok(0), |
| 418 | Err(e) => Err(anyhow::anyhow!(e)), |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /// Atomically acquires a proxy session for a task. |
| 423 | /// |