Sync iTop: ajout responsable serveur (contacts_list) + commentaire/description
This commit is contained in:
parent
f73ea25d5d
commit
53e59e7305
@ -33,7 +33,7 @@ class ITopClient:
|
|||||||
return {"code": -1, "message": str(e)}
|
return {"code": -1, "message": str(e)}
|
||||||
|
|
||||||
def get_servers(self):
|
def get_servers(self):
|
||||||
"""Recupere tous les serveurs iTop (Server + VirtualMachine)"""
|
"""Recupere tous les serveurs iTop (Server + VirtualMachine) avec contacts"""
|
||||||
servers = []
|
servers = []
|
||||||
for cls in ["Server", "VirtualMachine"]:
|
for cls in ["Server", "VirtualMachine"]:
|
||||||
r = self._call("core/get", **{
|
r = self._call("core/get", **{
|
||||||
@ -41,11 +41,20 @@ class ITopClient:
|
|||||||
"key": f"SELECT {cls}",
|
"key": f"SELECT {cls}",
|
||||||
"output_fields": "name,description,status,managementip,osfamily_id_friendlyname,"
|
"output_fields": "name,description,status,managementip,osfamily_id_friendlyname,"
|
||||||
"osversion_id_friendlyname,brand_name,model_name,serialnumber,"
|
"osversion_id_friendlyname,brand_name,model_name,serialnumber,"
|
||||||
"org_name,location_name,business_criticity,cpu,ram"
|
"org_name,location_name,business_criticity,cpu,ram,contacts_list"
|
||||||
})
|
})
|
||||||
if r.get("code") == 0 and r.get("objects"):
|
if r.get("code") == 0 and r.get("objects"):
|
||||||
for key, obj in r["objects"].items():
|
for key, obj in r["objects"].items():
|
||||||
f = obj["fields"]
|
f = obj["fields"]
|
||||||
|
# Extract contacts (responsable serveur)
|
||||||
|
contacts = f.get("contacts_list", [])
|
||||||
|
contact_names = []
|
||||||
|
contact_emails = []
|
||||||
|
for c in contacts:
|
||||||
|
cname = c.get("contact_id_friendlyname", "")
|
||||||
|
if cname:
|
||||||
|
contact_names.append(cname)
|
||||||
|
|
||||||
servers.append({
|
servers.append({
|
||||||
"itop_id": obj["key"],
|
"itop_id": obj["key"],
|
||||||
"itop_class": cls,
|
"itop_class": cls,
|
||||||
@ -63,6 +72,7 @@ class ITopClient:
|
|||||||
"criticity": f.get("business_criticity", ""),
|
"criticity": f.get("business_criticity", ""),
|
||||||
"cpu": f.get("cpu", ""),
|
"cpu": f.get("cpu", ""),
|
||||||
"ram": f.get("ram", ""),
|
"ram": f.get("ram", ""),
|
||||||
|
"responsable_nom": contact_names[0] if contact_names else "",
|
||||||
})
|
})
|
||||||
return servers
|
return servers
|
||||||
|
|
||||||
@ -175,6 +185,12 @@ def sync_from_itop(db, itop_url, itop_user, itop_pass):
|
|||||||
if s["location"]:
|
if s["location"]:
|
||||||
sets.append("site = :site")
|
sets.append("site = :site")
|
||||||
updates["site"] = s["location"]
|
updates["site"] = s["location"]
|
||||||
|
if s.get("responsable_nom"):
|
||||||
|
sets.append("responsable_nom = :resp_nom")
|
||||||
|
updates["resp_nom"] = s["responsable_nom"]
|
||||||
|
if s.get("description"):
|
||||||
|
sets.append("commentaire = :comm")
|
||||||
|
updates["comm"] = s["description"]
|
||||||
if sets:
|
if sets:
|
||||||
sets.append("updated_at = NOW()")
|
sets.append("updated_at = NOW()")
|
||||||
db.execute(text(f"UPDATE servers SET {', '.join(sets)} WHERE id = :sid"), updates)
|
db.execute(text(f"UPDATE servers SET {', '.join(sets)} WHERE id = :sid"), updates)
|
||||||
@ -209,7 +225,7 @@ def sync_to_itop(db, itop_url, itop_user, itop_pass):
|
|||||||
itop_servers = {s["name"].lower(): s for s in client.get_servers()}
|
itop_servers = {s["name"].lower(): s for s in client.get_servers()}
|
||||||
|
|
||||||
# Get patchcenter servers
|
# Get patchcenter servers
|
||||||
rows = db.execute(text("SELECT hostname, fqdn, os_version, os_family, etat, site FROM servers")).fetchall()
|
rows = db.execute(text("SELECT hostname, fqdn, os_version, os_family, etat, site, responsable_nom, commentaire FROM servers")).fetchall()
|
||||||
|
|
||||||
status_map = {"en_production": "production", "decommissionne": "obsolete",
|
status_map = {"en_production": "production", "decommissionne": "obsolete",
|
||||||
"stock": "stock", "en_cours": "implementation"}
|
"stock": "stock", "en_cours": "implementation"}
|
||||||
@ -221,7 +237,9 @@ def sync_to_itop(db, itop_url, itop_user, itop_pass):
|
|||||||
|
|
||||||
if itop_srv:
|
if itop_srv:
|
||||||
fields = {}
|
fields = {}
|
||||||
if srv.os_version:
|
if srv.commentaire:
|
||||||
|
fields["description"] = srv.commentaire
|
||||||
|
elif srv.os_version:
|
||||||
fields["description"] = f"OS: {srv.os_version}"
|
fields["description"] = f"OS: {srv.os_version}"
|
||||||
if srv.etat:
|
if srv.etat:
|
||||||
fields["status"] = status_map.get(srv.etat, "production")
|
fields["status"] = status_map.get(srv.etat, "production")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user