diff --git a/backend/app/services/device_discovery.py b/backend/app/services/device_discovery.py index 6a0966e..9529b8b 100644 --- a/backend/app/services/device_discovery.py +++ b/backend/app/services/device_discovery.py @@ -575,7 +575,16 @@ async def _fetch_rack_hosts(dsn: str, table: str) -> list[tuple[str, str, str, s f"FROM \"{table}\" WHERE data->>'host' IS NOT NULL" ) rows = await cursor.fetchall() - return [(str(r[0]), str(r[1]), str(r[2] or r[0]), r[3] or None) for r in rows if r[1]] + result = [] + for r in rows: + if not r[1]: + continue + ext_id = str(r[0]) + short_name = str(r[2]) if r[2] else None + # Combine CAPS id with its short name: "lane-1-1 (01)" + rack_name = f"{ext_id} ({short_name})" if short_name and short_name != ext_id else ext_id + result.append((ext_id, str(r[1]), rack_name, r[3] or None)) + return result # ── Main entry point ────────────────────────────────────────────────────────── @@ -725,21 +734,29 @@ async def discover_via_ssh( # If a rack with that name doesn't exist yet in our racks table, create it now. from app.models.rack import Rack rack_id_mapping: dict[str, int] = {} - for ext_id, _, _, rack_type_db in rack_hosts: + for ext_id, _, rack_name, rack_type_db in rack_hosts: + # rack_name already has the combined form "lane-1-1 (01)" from _fetch_rack_hosts; + # also search for the legacy ext_id-only name to handle already-created racks. existing_rack_row = (await db.execute( - select(Rack).where(Rack.object_id == obj.id, Rack.name == ext_id) + select(Rack).where( + Rack.object_id == obj.id, + Rack.name.in_([ext_id, rack_name]), + ) )).scalar_one_or_none() if existing_rack_row: rack_id_mapping[ext_id] = existing_rack_row.id + if existing_rack_row.name != rack_name: + logger.info(f"[ssh_discovery] Renamed rack '{existing_rack_row.name}' → '{rack_name}'") + existing_rack_row.name = rack_name if rack_type_db and not existing_rack_row.rack_type: existing_rack_row.rack_type = rack_type_db - logger.info(f"[ssh_discovery] Updated rack '{ext_id}' rack_type={rack_type_db}") + logger.info(f"[ssh_discovery] Updated rack '{rack_name}' rack_type={rack_type_db}") else: - new_rack = Rack(object_id=obj.id, name=ext_id, rack_type=rack_type_db) + new_rack = Rack(object_id=obj.id, name=rack_name, rack_type=rack_type_db) db.add(new_rack) await db.flush() # get the new PK rack_id_mapping[ext_id] = new_rack.id - logger.info(f"[ssh_discovery] Created rack entry '{ext_id}' rack_type={rack_type_db} (id={new_rack.id})") + logger.info(f"[ssh_discovery] Created rack entry '{rack_name}' rack_type={rack_type_db} (id={new_rack.id})") # Step 1b: Add main CAPS server and DB server as devices # verified=True means we successfully connected to this host during this discovery run