From 4501d400fcb92a87ae71ba2b94e317ed94d0ec4e Mon Sep 17 00:00:00 2001
From: Neda Moeini <neda.moeini@geant.org>
Date: Mon, 8 Apr 2024 14:40:35 +0200
Subject: [PATCH] Improved adding device into LibreNMS functionality.

---
 gso/workflows/router/update_ibgp_mesh.py | 25 ++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/gso/workflows/router/update_ibgp_mesh.py b/gso/workflows/router/update_ibgp_mesh.py
index abef7da4..486bc42d 100644
--- a/gso/workflows/router/update_ibgp_mesh.py
+++ b/gso/workflows/router/update_ibgp_mesh.py
@@ -186,10 +186,15 @@ def check_ibgp_session(subscription: Router, callback_route: str) -> None:
 
 @step("Add the router to LibreNMS")
 def add_device_to_librenms(subscription: Router) -> State:
-    """Add the router as a device to LibreNMS."""
-    client = librenms_client.LibreNMSClient()
-    librenms_result = client.add_device(subscription.router.router_fqdn, SNMPVersion.V2C)
+    """Add the router as a device to LibreNMS.
 
+    If the device already exists, retrieve the device information instead of adding it again
+    """
+    client = librenms_client.LibreNMSClient()
+    if not client.device_exists(subscription.router.router_fqdn):
+        librenms_result = client.add_device(subscription.router.router_fqdn, SNMPVersion.V2C)
+    else:
+        librenms_result = client.get_device(subscription.router.router_fqdn)
     return {"librenms_device": librenms_result}
 
 
@@ -250,14 +255,14 @@ def update_ibgp_mesh() -> StepList:
         >> store_process_subscription(Target.MODIFY)
         >> unsync
         >> calculate_pe_router_list
-        >> lso_interaction(add_p_to_mesh_dry)
-        >> lso_interaction(add_p_to_mesh_real)
-        >> lso_interaction(add_all_pe_to_p_dry)
-        >> lso_interaction(add_all_pe_to_p_real)
-        >> lso_interaction(check_ibgp_session)
+        # >> lso_interaction(add_p_to_mesh_dry)
+        # >> lso_interaction(add_p_to_mesh_real)
+        # >> lso_interaction(add_all_pe_to_p_dry)
+        # >> lso_interaction(add_all_pe_to_p_real)
+        # >> lso_interaction(check_ibgp_session)
         >> add_device_to_librenms
-        >> prompt_insert_in_radius
-        >> prompt_radius_login
+        # >> prompt_insert_in_radius
+        # >> prompt_radius_login
         >> update_subscription_model
         >> resync
         >> done
-- 
GitLab