Sync iTop: import IPs dans server_ips + traceback sur erreur sync
This commit is contained in:
parent
322fa71324
commit
66eeff99e8
@ -501,8 +501,12 @@ def itop_sync_from(request: Request, db=Depends(get_db)):
|
|||||||
if not itop_url or not itop_user:
|
if not itop_url or not itop_user:
|
||||||
return RedirectResponse(url="/referentiel?tab=domains&msg=itop_noconfig", status_code=303)
|
return RedirectResponse(url="/referentiel?tab=domains&msg=itop_noconfig", status_code=303)
|
||||||
|
|
||||||
stats = sync_from_itop(db, itop_url, itop_user, itop_pass)
|
try:
|
||||||
msg = f"itop_from_{stats['servers_created']}_{stats['servers_updated']}_{stats['contacts_created']}_{stats['contacts_updated']}"
|
stats = sync_from_itop(db, itop_url, itop_user, itop_pass)
|
||||||
|
msg = f"itop_from_{stats['servers_created']}_{stats['servers_updated']}_{stats['contacts']}_{stats['domains']}"
|
||||||
|
except Exception as e:
|
||||||
|
import traceback; traceback.print_exc()
|
||||||
|
msg = f"itop_error"
|
||||||
return RedirectResponse(url=f"/referentiel?tab=domains&msg={msg}", status_code=303)
|
return RedirectResponse(url=f"/referentiel?tab=domains&msg={msg}", status_code=303)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -42,6 +42,22 @@ class ITopClient:
|
|||||||
return self._call("core/create", **{"class": cls, "fields": fields, "comment": "PatchCenter sync"})
|
return self._call("core/create", **{"class": cls, "fields": fields, "comment": "PatchCenter sync"})
|
||||||
|
|
||||||
|
|
||||||
|
def _upsert_ip(db, server_id, ip):
|
||||||
|
"""Insert or update IP in server_ips"""
|
||||||
|
if not ip:
|
||||||
|
return
|
||||||
|
existing = db.execute(text(
|
||||||
|
"SELECT id FROM server_ips WHERE server_id=:sid AND ip_address=:ip"),
|
||||||
|
{"sid": server_id, "ip": ip}).fetchone()
|
||||||
|
if not existing:
|
||||||
|
try:
|
||||||
|
db.execute(text(
|
||||||
|
"INSERT INTO server_ips (server_id, ip_address, ip_type, is_ssh, description) VALUES (:sid, :ip, 'primary', true, 'itop')"),
|
||||||
|
{"sid": server_id, "ip": ip})
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def sync_from_itop(db, itop_url, itop_user, itop_pass):
|
def sync_from_itop(db, itop_url, itop_user, itop_pass):
|
||||||
"""Import complet depuis iTop: contacts, domaines, envs, zones, serveurs"""
|
"""Import complet depuis iTop: contacts, domaines, envs, zones, serveurs"""
|
||||||
client = ITopClient(itop_url, itop_user, itop_pass)
|
client = ITopClient(itop_url, itop_user, itop_pass)
|
||||||
@ -239,6 +255,9 @@ def sync_from_itop(db, itop_url, itop_user, itop_pass):
|
|||||||
WHERE id=:sid
|
WHERE id=:sid
|
||||||
"""), {**vals, "sid": existing.id})
|
"""), {**vals, "sid": existing.id})
|
||||||
stats["servers_updated"] += 1
|
stats["servers_updated"] += 1
|
||||||
|
# Update IP
|
||||||
|
if vals["ip"] and existing:
|
||||||
|
_upsert_ip(db, existing.id, vals["ip"])
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
db.execute(text("""
|
db.execute(text("""
|
||||||
@ -253,6 +272,12 @@ def sync_from_itop(db, itop_url, itop_user, itop_pass):
|
|||||||
:patch_freq, :patch_excludes, :domain_ltd,
|
:patch_freq, :patch_excludes, :domain_ltd,
|
||||||
:pref_jour, :pref_heure)
|
:pref_jour, :pref_heure)
|
||||||
"""), vals)
|
"""), vals)
|
||||||
|
db.flush()
|
||||||
|
# Get new server ID and insert IP
|
||||||
|
if vals["ip"]:
|
||||||
|
new_srv = db.execute(text("SELECT id FROM servers WHERE hostname=:h"), {"h": hostname}).fetchone()
|
||||||
|
if new_srv:
|
||||||
|
_upsert_ip(db, new_srv.id, vals["ip"])
|
||||||
stats["servers_created"] += 1
|
stats["servers_created"] += 1
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
@ -280,6 +305,12 @@ def sync_from_itop(db, itop_url, itop_user, itop_pass):
|
|||||||
"osv": s.get("osversion_id_friendlyname", ""),
|
"osv": s.get("osversion_id_friendlyname", ""),
|
||||||
"resp": resp, "desc": s.get("description", ""),
|
"resp": resp, "desc": s.get("description", ""),
|
||||||
})
|
})
|
||||||
|
db.flush()
|
||||||
|
ip = s.get("managementip", "")
|
||||||
|
if ip:
|
||||||
|
new_srv = db.execute(text("SELECT id FROM servers WHERE hostname=:h"), {"h": hostname}).fetchone()
|
||||||
|
if new_srv:
|
||||||
|
_upsert_ip(db, new_srv.id, ip)
|
||||||
stats["servers_created"] += 1
|
stats["servers_created"] += 1
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user