Fix schema server_ips: ip_type au lieu de is_primary, VRF/GW/MASK dans description

This commit is contained in:
Pierre & Lumière 2026-04-14 19:55:42 +02:00
parent c905ab6db3
commit a66e0d853e
2 changed files with 17 additions and 13 deletions

View File

@ -70,27 +70,26 @@ def main():
continue
for idx, ip_val in enumerate(ips_valid):
is_primary = (idx == 0)
ip_type = "primary" if idx == 0 else "secondary"
desc = f"VRF={vrf}" if vrf else None
if args.dry_run:
print(f" DRY: {hostname:20s} {ip_val} {'(primary)' if is_primary else ''}")
print(f" DRY: {hostname:20s} {ip_val} [{ip_type}]")
stats["inserted"] += 1
continue
try:
# Check existant
existing = conn.execute(text(
"SELECT id FROM server_ips WHERE server_id=:sid AND ip_address=CAST(:ip AS inet)"
), {"sid": srv.id, "ip": ip_val}).fetchone()
if existing:
if vrf:
conn.execute(text(
"UPDATE server_ips SET vrf=:v, is_primary=:p WHERE id=:id"
), {"v": vrf, "p": is_primary, "id": existing.id})
"UPDATE server_ips SET ip_type=:t, description=:d WHERE id=:id"
), {"t": ip_type, "d": desc, "id": existing.id})
stats["updated"] += 1
else:
conn.execute(text("""
INSERT INTO server_ips (server_id, ip_address, is_primary, vrf)
VALUES (:sid, CAST(:ip AS inet), :p, :v)
"""), {"sid": srv.id, "ip": ip_val, "p": is_primary, "v": vrf})
INSERT INTO server_ips (server_id, ip_address, ip_type, description)
VALUES (:sid, CAST(:ip AS inet), :t, :d)
"""), {"sid": srv.id, "ip": ip_val, "t": ip_type, "d": desc})
stats["inserted"] += 1
except Exception as e:
print(f" [ERR] {hostname} {ip_val}: {str(e)[:120]}")

View File

@ -67,10 +67,15 @@ def main():
if existing:
skipped += 1
continue
desc_parts = []
if vrf: desc_parts.append(f"VRF={vrf}")
if gw: desc_parts.append(f"GW={gw}")
if mask: desc_parts.append(f"MASK={mask}")
desc = " ".join(desc_parts)[:200] or None
conn.execute(text("""
INSERT INTO server_ips (server_id, ip_address, is_primary, gateway, netmask, vrf)
VALUES (:sid, CAST(:ip AS inet), true, :gw, :mask, :vrf)
"""), {"sid": srv.id, "ip": ip, "gw": gw, "mask": mask, "vrf": vrf})
INSERT INTO server_ips (server_id, ip_address, ip_type, description)
VALUES (:sid, CAST(:ip AS inet), 'primary', :d)
"""), {"sid": srv.id, "ip": ip, "d": desc})
linked += 1
except Exception as e:
print(f" [ERR] {vm_name} {ip}: {str(e)[:150]}")