From 6560462ea5212b07e67189dc6b4c1ef6344d8a6b 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 | 6 ++++++
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/gso/services/kentik_client.py b/gso/services/kentik_client.py
index f1a1f158..be289e5c 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 688e3ecc..cbb3048d 100644
--- a/gso/workflows/router/terminate_router.py
+++ b/gso/workflows/router/terminate_router.py
@@ -21,6 +21,7 @@ from orchestrator.workflows.utils import wrap_modify_initial_input_form
 from gso.products.product_blocks.router import RouterRole
 from gso.products.product_types.router import Router
 from gso.services import infoblox, lso_client
+from gso.services.kentik_client import KentikClient
 from gso.services.librenms_client import LibreNMSClient
 from gso.services.lso_client import execute_playbook, lso_interaction
 from gso.services.netbox_client import NetboxClient
@@ -237,6 +238,12 @@ def remove_device_from_librenms(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),
@@ -271,6 +278,7 @@ def terminate_router() -> StepList:
         >> run_config_steps(lso_interaction(remove_config_from_router_real))
         >> router_is_nokia(remove_device_from_netbox)
         >> remove_device_from_librenms
+        >> 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 a41e8b32..fe6ebb9c 100644
--- a/test/workflows/router/test_terminate_router.py
+++ b/test/workflows/router/test_terminate_router.py
@@ -14,7 +14,9 @@ from test.workflows import assert_complete, assert_lso_interaction_success, extr
 @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.LibreNMSClient.remove_device")
+@patch("gso.workflows.router.terminate_router.KentikClient")
 def test_terminate_pe_router_full_success(
+    mock_kentik_client,
     mock_librenms_remove_device,
     mock_delete_host_by_ip,
     mock_delete_device,
@@ -54,6 +56,7 @@ def test_terminate_pe_router_full_success(
     assert mock_delete_device.call_count == 1
     assert mock_delete_host_by_ip.call_count == 1
     assert mock_librenms_remove_device.call_count == 1
+    assert mock_kentik_client.call_count == 1
     assert mock_execute_playbook.call_count == lso_interaction_count
 
 
@@ -61,6 +64,7 @@ def test_terminate_pe_router_full_success(
 @pytest.mark.parametrize("remove_configuration", [True, False])
 @pytest.mark.parametrize("update_ibgp_mesh", [True, False])
 @patch("gso.services.lso_client._send_request")
+@patch("gso.workflows.router.terminate_router.KentikClient")
 @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.LibreNMSClient.remove_device")
@@ -68,6 +72,7 @@ def test_terminate_p_router_full_success(
     mock_librenms_remove_device,
     mock_delete_host_by_ip,
     mock_delete_device,
+    mock_kentik_client,
     mock_execute_playbook,
     remove_configuration,
     update_ibgp_mesh,
@@ -104,4 +109,5 @@ def test_terminate_p_router_full_success(
     assert mock_delete_device.call_count == 1
     assert mock_delete_host_by_ip.call_count == 1
     assert mock_librenms_remove_device.call_count == 1
+    assert mock_kentik_client.call_count == 1
     assert mock_execute_playbook.call_count == lso_interaction_count
-- 
GitLab