From 7d8f2630ff004ecc874874cc4463077fe541f078 Mon Sep 17 00:00:00 2001
From: Karel van Klink <karel.vanklink@geant.org>
Date: Tue, 2 Jan 2024 14:56:50 +0100
Subject: [PATCH] use conditionals in IP trunk migration workflow

---
 gso/workflows/iptrunk/migrate_iptrunk.py | 67 +++++++++++++-----------
 1 file changed, 37 insertions(+), 30 deletions(-)

diff --git a/gso/workflows/iptrunk/migrate_iptrunk.py b/gso/workflows/iptrunk/migrate_iptrunk.py
index 555a41b8..923b4cf9 100644
--- a/gso/workflows/iptrunk/migrate_iptrunk.py
+++ b/gso/workflows/iptrunk/migrate_iptrunk.py
@@ -164,7 +164,6 @@ def initial_input_form_generator(subscription_id: UUIDstr) -> FormGenerator:
         | new_side_iptrunk_router_input.dict()
         | new_side_input.dict()
         | {"replace_index": replace_index}
-        | {"new_side_is_nokia": new_side_is_nokia}
     )
 
 
@@ -526,8 +525,8 @@ def update_subscription_model(
     return {"subscription": subscription, "old_side_data": old_side_data}
 
 
-@step("Reserve interfaces in Netbox")
-def reserve_interfaces_in_netbox(
+@step("Netbox: Reserve new interfaces")
+def netbox_reserve_interfaces(
     subscription: Iptrunk,
     new_node: UUIDstr,
     new_lag_interface: str,
@@ -560,34 +559,37 @@ def reserve_interfaces_in_netbox(
     return {"subscription": subscription}
 
 
-@step("Update Netbox. Allocate new interfaces and deallocate old ones.")
-def update_netbox(
-    subscription: Iptrunk,
-    replace_index: int,
-    old_side_data: dict,
-) -> State:
-    """Update Netbox, reallocating the old and new interfaces."""
-    new_side = subscription.iptrunk.iptrunk_sides[replace_index]
+@step("Netbox: Remove old LAG interface")
+def netbox_remove_old_interfaces(old_side_data: dict) -> State:
+    """Remove the old :term:`LAG` interface from Netbox, only relevant if the old side is a Nokia router."""
     nbclient = NetboxClient()
-    if get_router_vendor(new_side.iptrunk_side_node.owner_subscription_id) == RouterVendor.NOKIA:
-        for interface in new_side.iptrunk_side_ae_members:
-            nbclient.allocate_interface(
-                device_name=new_side.iptrunk_side_node.router_fqdn,
-                iface_name=interface.interface_name,
-            )
-    if get_router_vendor(old_side_data["iptrunk_side_node"]["owner_subscription_id"]) == RouterVendor.NOKIA:
-        # Set interfaces to free
-        for iface in old_side_data["iptrunk_side_ae_members"]:
-            nbclient.free_interface(
-                old_side_data["iptrunk_side_node"]["router_fqdn"],
-                iface["interface_name"],
-            )
 
-        # Delete :term:`LAG` interfaces
-        nbclient.delete_interface(
+    for iface in old_side_data["iptrunk_side_ae_members"]:
+        nbclient.free_interface(
             old_side_data["iptrunk_side_node"]["router_fqdn"],
-            old_side_data["iptrunk_side_ae_iface"],
+            iface["interface_name"],
+        )
+
+    nbclient.delete_interface(
+        old_side_data["iptrunk_side_node"]["router_fqdn"],
+        old_side_data["iptrunk_side_ae_iface"],
+    )
+
+    return {}
+
+
+@step("Netbox: Allocate new LAG member interfaces")
+def netbox_allocate_new_interfaces(subscription: Iptrunk, replace_index: int) -> State:
+    """Allocate the new :term:`LAG` interface in Netbox. Only relevant if the new router is a Nokia."""
+    nbclient = NetboxClient()
+    new_side = subscription.iptrunk.iptrunk_sides[replace_index]
+
+    for interface in new_side.iptrunk_side_ae_members:
+        nbclient.allocate_interface(
+            device_name=new_side.iptrunk_side_node.router_fqdn,
+            iface_name=interface.interface_name,
         )
+
     return {"subscription": subscription}
 
 
@@ -614,14 +616,18 @@ def migrate_iptrunk() -> StepList:
 
     TODO: add interface checks
     """
-    new_side_is_nokia = conditional(lambda state: state["new_side_is_nokia"])
+    new_side_is_nokia = conditional(lambda state: get_router_vendor(state["new_node"]) == RouterVendor.NOKIA)
+    old_side_is_nokia = conditional(
+        lambda state: get_router_vendor(state["old_side_data"]["iptrunk_side_node"]["owner_subscription_id"])
+        == RouterVendor.NOKIA
+    )
     should_restore_isis_metric = conditional(lambda state: state["restore_isis_metric"])
 
     return (
         init
         >> store_process_subscription(Target.MODIFY)
         >> unsync
-        >> new_side_is_nokia(reserve_interfaces_in_netbox)
+        >> new_side_is_nokia(netbox_reserve_interfaces)
         >> pp_interaction(set_isis_to_90000)
         >> pp_interaction(disable_old_config_dry)
         >> pp_interaction(disable_old_config_real)
@@ -635,7 +641,8 @@ def migrate_iptrunk() -> StepList:
         >> pp_interaction(delete_old_config_real)
         >> update_ipam
         >> update_subscription_model
-        >> update_netbox
+        >> old_side_is_nokia(netbox_remove_old_interfaces)
+        >> new_side_is_nokia(netbox_allocate_new_interfaces)
         >> resync
         >> done
     )
-- 
GitLab