diff --git a/tools/import_etat_itop.py b/tools/import_etat_itop.py index c8f437f..893ab85 100644 --- a/tools/import_etat_itop.py +++ b/tools/import_etat_itop.py @@ -116,24 +116,31 @@ def main(): hostname = (r.get("Nom") or r.get("Hostname") or "").strip() if not hostname or not any(c.isalpha() for c in hostname): continue - # Lit Status (lifecycle iTop) en priorite, fallback Etat - raw = (r.get("Status") or r.get("Etat") or r.get("État") or "").strip() + # Etat d'abord (lifecycle VM). Si valeur "condition" -> NULL. + # Status ignore (toujours 'Production' cote iTop, pas pertinent). + raw = (r.get("Etat") or r.get("État") or "").strip() new_etat = norm_etat(raw) - if raw and new_etat is None and raw not in ("-", "(null)"): + # Cas: valeur condition iTop (Nouveau, Recycle, ...) -> NULL (physique non deploye) + is_condition = raw in ("Nouveau", "Recyclé", "A récupérer", "Cassé", + "Cédé", "En panne", "Perdu", "Occasion", + "A détruire", "Volé") + if raw and new_etat is None and not is_condition and raw not in ("-", "(null)"): unknown.add(raw); continue - if new_etat is None: + # is_condition -> force NULL ; valeur vide -> skip + if new_etat is None and not is_condition: continue + target = new_etat # None si is_condition srv = conn.execute(text("SELECT id, etat FROM servers WHERE hostname=:h"), {"h": hostname}).fetchone() if not srv: not_found += 1; continue - if srv.etat == new_etat: + if srv.etat == target: unchanged += 1; continue if args.dry_run: - print(f" DRY: {hostname} {srv.etat} -> {new_etat}") + print(f" DRY: {hostname} {srv.etat} -> {target or 'NULL'}") else: conn.execute(text("UPDATE servers SET etat=:e WHERE id=:sid"), - {"e": new_etat, "sid": srv.id}) + {"e": target, "sid": srv.id}) updated += 1 print(f"\n[DONE] Maj CSV: {updated} | Inchanges: {unchanged} | Hors base: {not_found}") if unknown: