From fd2f04615435cc149060a33c16f7bd8c23383c5a Mon Sep 17 00:00:00 2001
From: Guilhem Saurel <guilhem.saurel@laas.fr>
Date: Mon, 11 Mar 2024 18:42:16 +0100
Subject: [PATCH] machines

---
 scripts/machines.py | 57 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
 create mode 100755 scripts/machines.py

diff --git a/scripts/machines.py b/scripts/machines.py
new file mode 100755
index 0000000..ddbb460
--- /dev/null
+++ b/scripts/machines.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python3
+"""Utils to manage Gepetto computers"""
+
+from datetime import date
+
+import pandas as pd
+from ldap3 import Connection
+
+ATTRIBUTES = [
+    "cn",
+    "laas-date-install",
+    "laas-mach-datePeremption",
+    "laas-mach-inventaire",
+    "laas-mach-modele",
+    "laas-mach-os",
+    "laas-mach-responsable",
+    # "laas-mach-type",
+    "laas-mach-utilisateur",
+    "roomNumber",
+]
+
+
+def parse(k, v):
+    """Parse ldap value."""
+    if not v:
+        return ""
+    v = v[0]
+    if "date" in k:
+        d, m, y = (int(i) for i in v.split("/"))
+        v = date(y, m, d)
+    return v
+
+
+def machines_ldap():
+    """Get a dict of Gepettists machines from LDAP."""
+    conn = Connection("ldap.laas.fr", auto_bind=True)
+    conn.search(
+        "ou=machines,dc=laas,dc=fr",
+        "(&(laas-mach-group=gepetto)(laas-mach-type=PC))",
+        attributes=ATTRIBUTES,
+    )
+    df = pd.DataFrame(
+        {
+            str(entry.cn): {
+                k: parse(k, v)
+                for k, v in entry.entry_attributes_as_dict.items()
+                if k != "cn"
+            }
+            for entry in conn.entries
+        }
+    ).T.sort_values(by="laas-mach-datePeremption")
+    print(df.to_markdown())
+    return df
+
+
+if __name__ == "__main__":
+    df = machines_ldap()
-- 
GitLab