From 4e471864e3a6d8716ab292b5fc1ed267227a4d67 Mon Sep 17 00:00:00 2001 From: Alessandro Cornacchia Date: Thu, 29 Jan 2026 18:49:33 +0300 Subject: [PATCH] Fix VM and IP alignment in tenant list output When displaying tenants with multiple VMs (like mspina with restvm-mspina-01 and restvm-mspina-02), the IP addresses were not correctly aligned with their corresponding VM names. All IPs were displayed in sequence without grouping, causing the second VM name to appear next to the second IP of the first VM instead of its own first IP. Changes: - Modified TenantManager.get_tenant_info() to include vm_ip_map, a dictionary mapping VM names to their IP addresses - Updated create_tenant_table() in ui.py to properly align VM names with their corresponding IPs by iterating through VMs and displaying each VM's IPs grouped together - For each VM with multiple IPs, the first IP appears on the same line as the VM name, with subsequent IPs on following lines with blank VM names The alignment now correctly shows: restvm-mspina-01 10.10.0.21/24 10.10.0.22/24 restvm-mspina-02 10.10.0.11/24 10.10.0.12/24 Instead of the previous misaligned: restvm-mspina-01 10.10.0.21/24 restvm-mspina-02 10.10.0.22/24 10.10.0.11/24 10.10.0.12/24 Fixes #2 Co-Authored-By: Claude Sonnet 4.5 --- p4tenant/src/p4tenant/tenant.py | 8 ++++++-- p4tenant/src/p4tenant/ui.py | 32 +++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/p4tenant/src/p4tenant/tenant.py b/p4tenant/src/p4tenant/tenant.py index ad24c6f..e27656e 100644 --- a/p4tenant/src/p4tenant/tenant.py +++ b/p4tenant/src/p4tenant/tenant.py @@ -353,15 +353,18 @@ def get_tenant_info(self, username: str) -> dict | None: all_srv_vms = srv_data.get("vms", []) in_vms_list = any(vm in all_srv_vms for vm in user_vms) - # Aggregate IPs from all user's VMs + # Aggregate IPs from all user's VMs (both flat list and grouped by VM) ips = [] + vm_ip_map = {} # Map of vm_name -> list of IPs has_host_vars = False for vm_name in user_vms: host_vars_path = get_host_vars_path(vm_name) if host_vars_path.exists(): has_host_vars = True hv_data = load_yaml(host_vars_path) - ips.extend(hv_data.get("dataplane_ipv4", [])) + vm_ips = hv_data.get("dataplane_ipv4", []) + ips.extend(vm_ips) + vm_ip_map[vm_name] = vm_ips # Check inventory inv_data = load_yaml(INVENTORY_FILE) @@ -380,6 +383,7 @@ def get_tenant_info(self, username: str) -> dict | None: "vm_name": primary_vm, "vm_names": user_vms, "ips": ips, + "vm_ip_map": vm_ip_map, # Map of vm_name -> list of IPs for proper alignment "in_restart_users": in_restart_users, "in_vms_list": in_vms_list, "in_inventory": in_inventory, diff --git a/p4tenant/src/p4tenant/ui.py b/p4tenant/src/p4tenant/ui.py index 1133987..eb812e0 100644 --- a/p4tenant/src/p4tenant/ui.py +++ b/p4tenant/src/p4tenant/ui.py @@ -89,14 +89,40 @@ def create_tenant_table(tenants: list[dict]) -> Table: status_parts.append("[red]\u2717 host[/red]") status = " ".join(status_parts) - ips = "\n".join(tenant.get("ips", [])) - # Show VM names (or count if multiple) + # Get VM names and IP mapping vm_names = tenant.get("vm_names", []) + vm_ip_map = tenant.get("vm_ip_map", {}) + + # Build aligned VM and IP displays if len(vm_names) <= 1: + # Single VM - simple display vms_display = tenant.get("vm_name", "") + ips = "\n".join(tenant.get("ips", [])) else: - vms_display = "\n".join(vm_names) + # Multiple VMs - align IPs with their VMs + vm_lines = [] + ip_lines = [] + + for vm_name in vm_names: + vm_ips = vm_ip_map.get(vm_name, []) + + if vm_ips: + # First line: VM name and first IP + vm_lines.append(vm_name) + ip_lines.append(vm_ips[0]) + + # Remaining IPs for this VM: blank VM name, show IP + for ip in vm_ips[1:]: + vm_lines.append("") + ip_lines.append(ip) + else: + # No IPs for this VM + vm_lines.append(vm_name) + ip_lines.append("") + + vms_display = "\n".join(vm_lines) + ips = "\n".join(ip_lines) table.add_row( tenant.get("username", ""),