From c66483df6ca243fa4afb9a8bc5acc70d1e27ecbe Mon Sep 17 00:00:00 2001
From: Karel van Klink <karel.vanklink@geant.org>
Date: Fri, 26 Jul 2024 13:07:40 +0200
Subject: [PATCH] Add Kentik device removal to router termination workflow

---
 gso/services/kentik_client.py                  | 2 +-
 gso/workflows/router/terminate_router.py       | 8 ++++++++
 test/workflows/router/test_terminate_router.py | 3 +++
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/gso/services/kentik_client.py b/gso/services/kentik_client.py
index f1a1f1586..be289e5c6 100644
--- a/gso/services/kentik_client.py
+++ b/gso/services/kentik_client.py
@@ -144,7 +144,7 @@ class KentikClient:
         """Update an existing device in Kentik."""
         return self._send_request("PUT", f"v5/device/{device_id}", updated_device).json()
 
-    def remove_device(self, device_id: str, *, archive: bool = True) -> None:
+    def remove_device(self, device_id: str, *, archive: bool) -> None:
         """Remove a device from Kentik.
 
         :param str device_id: The Kentik internal ID of the device that is to be removed.
diff --git a/gso/workflows/router/terminate_router.py b/gso/workflows/router/terminate_router.py
index b9e521da8..fb6a425a9 100644
--- a/gso/workflows/router/terminate_router.py
+++ b/gso/workflows/router/terminate_router.py
@@ -20,6 +20,7 @@ from orchestrator.workflows.utils import wrap_modify_initial_input_form
 
 from gso.products.product_types.router import Router
 from gso.services import infoblox
+from gso.services.kentik_client import KentikClient
 from gso.services.lso_client import execute_playbook, lso_interaction
 from gso.services.netbox_client import NetboxClient
 from gso.utils.shared_enums import Vendor
@@ -104,6 +105,12 @@ def remove_device_from_netbox(subscription: Router) -> dict[str, Router]:
     return {"subscription": subscription}
 
 
+@step("Archive device in Kentik")
+def remove_device_from_kentik(subscription: Router) -> None:
+    """Archive the device in Kentik."""
+    KentikClient().remove_device(subscription.router.router_fqdn, archive=True)
+
+
 @workflow(
     "Terminate router",
     initial_input_form=wrap_modify_initial_input_form(initial_input_form_generator),
@@ -128,6 +135,7 @@ def terminate_router() -> StepList:
         >> run_config_steps(lso_interaction(remove_config_from_router_dry))
         >> run_config_steps(lso_interaction(remove_config_from_router_real))
         >> router_is_nokia(remove_device_from_netbox)
+        >> remove_device_from_kentik
         >> set_status(SubscriptionLifecycle.TERMINATED)
         >> resync
         >> done
diff --git a/test/workflows/router/test_terminate_router.py b/test/workflows/router/test_terminate_router.py
index e52826820..ccdf99224 100644
--- a/test/workflows/router/test_terminate_router.py
+++ b/test/workflows/router/test_terminate_router.py
@@ -11,7 +11,9 @@ from test.workflows import assert_complete, assert_lso_interaction_success, extr
 @patch("gso.services.lso_client._send_request")
 @patch("gso.workflows.router.terminate_router.NetboxClient.delete_device")
 @patch("gso.workflows.router.terminate_router.infoblox.delete_host_by_ip")
+@patch("gso.workflows.router.terminate_router.KentikClient")
 def test_terminate_router_full_success(
+    mock_kentik_client,
     mock_delete_host_by_ip,
     mock_delete_device,
     mock_execute_playbook,
@@ -44,4 +46,5 @@ def test_terminate_router_full_success(
     assert subscription.status == "terminated"
     assert mock_delete_device.call_count == 1
     assert mock_delete_host_by_ip.call_count == 1
+    assert mock_kentik_client.call_count == 1
     assert mock_execute_playbook.call_count == lso_interaction_count
-- 
GitLab