From 4b015e72c8b73ed6e3d5ebe338c5930db6c7ddbb Mon Sep 17 00:00:00 2001 From: Karel van Klink <karel.vanklink@geant.org> Date: Mon, 3 Jun 2024 10:06:06 +0200 Subject: [PATCH] Externalise the generation of an active PE-router dict --- gso/services/subscriptions.py | 21 ++++++++++++ gso/workflows/router/update_ibgp_mesh.py | 41 +++--------------------- 2 files changed, 25 insertions(+), 37 deletions(-) diff --git a/gso/services/subscriptions.py b/gso/services/subscriptions.py index 8d9900a3..03e4f108 100644 --- a/gso/services/subscriptions.py +++ b/gso/services/subscriptions.py @@ -20,6 +20,8 @@ from orchestrator.types import SubscriptionLifecycle from pydantic_forms.types import UUIDstr from gso.products import ProductName, ProductType +from gso.products.product_blocks.router import RouterRole +from gso.products.product_types.router import Router from gso.products.product_types.site import Site SubscriptionType = dict[str, Any] @@ -220,3 +222,22 @@ def get_site_by_name(site_name: str) -> Site: raise ValueError(msg) return Site.from_subscription(subscription[0].subscription_id) + + +def get_active_pe_router_dict() -> dict[str, Any]: + """Generate an Ansible-compatible inventory for executing playbooks. Contains all active PE routers.""" + all_routers = [Router.from_subscription(r["subscription_id"]) for r in get_active_router_subscriptions()] + + return { + "all": { + "hosts": { + router.router.router_fqdn: { + "lo4": str(router.router.router_lo_ipv4_address), + "lo6": str(router.router.router_lo_ipv6_address), + "vendor": str(router.router.vendor), + } + for router in all_routers + if router.router.router_role == RouterRole.PE + } + }, + } diff --git a/gso/workflows/router/update_ibgp_mesh.py b/gso/workflows/router/update_ibgp_mesh.py index 118eb2ee..2d6f2d48 100644 --- a/gso/workflows/router/update_ibgp_mesh.py +++ b/gso/workflows/router/update_ibgp_mesh.py @@ -12,7 +12,6 @@ from orchestrator.workflows.steps import resync, store_process_subscription, uns from orchestrator.workflows.utils import wrap_modify_initial_input_form from pydantic import ConfigDict, model_validator -from gso.products.product_blocks.router import RouterRole from gso.products.product_types.router import Router from gso.services import librenms_client, lso_client, subscriptions from gso.services.lso_client import lso_interaction @@ -49,37 +48,8 @@ def initial_input_form_generator(subscription_id: UUIDstr) -> FormGenerator: return user_input.model_dump() -@step("Calculate list of all active PE routers") -def calculate_pe_router_list() -> State: - """Calculate a list of all active PE routers in the network.""" - all_routers = [ - Router.from_subscription(r["subscription_id"]) for r in subscriptions.get_active_router_subscriptions() - ] - all_pe_routers = [router for router in all_routers if router.router.router_role == RouterRole.PE] - - return {"pe_router_list": all_pe_routers} - - -def _generate_pe_inventory(pe_router_list: list[Router]) -> dict[str, Any]: - """Generate an Ansible-compatible inventory for executing playbooks. Contains all active PE routers.""" - return { - "all": { - "hosts": { - router.router.router_fqdn: { - "lo4": str(router.router.router_lo_ipv4_address), - "lo6": str(router.router.router_lo_ipv6_address), - "vendor": str(router.router.vendor), - } - for router in pe_router_list - } - }, - } - - @step("[DRY RUN] Add P router to iBGP mesh") -def add_p_to_mesh_dry( - subscription: dict[str, Any], callback_route: str, pe_router_list: list[Router], tt_number: str, process_id: UUIDstr -) -> None: +def add_p_to_mesh_dry(subscription: dict[str, Any], callback_route: str, tt_number: str, process_id: UUIDstr) -> None: """Perform a dry run of adding the new P router to the PE router mesh.""" extra_vars = { "dry_run": True, @@ -91,15 +61,13 @@ def add_p_to_mesh_dry( lso_client.execute_playbook( playbook_name="update_ibgp_mesh.yaml", callback_route=callback_route, - inventory=_generate_pe_inventory(pe_router_list), + inventory=subscriptions.get_active_pe_router_dict(), extra_vars=extra_vars, ) @step("[FOR REAL] Add P router to iBGP mesh") -def add_p_to_mesh_real( - subscription: dict[str, Any], callback_route: str, pe_router_list: list[Router], tt_number: str, process_id: UUIDstr -) -> None: +def add_p_to_mesh_real(subscription: dict[str, Any], callback_route: str, tt_number: str, process_id: UUIDstr) -> None: """Add the P router to the mesh of PE routers.""" extra_vars = { "dry_run": False, @@ -111,7 +79,7 @@ def add_p_to_mesh_real( lso_client.execute_playbook( playbook_name="update_ibgp_mesh.yaml", callback_route=callback_route, - inventory=_generate_pe_inventory(pe_router_list), + inventory=subscriptions.get_active_pe_router_dict(), extra_vars=extra_vars, ) @@ -251,7 +219,6 @@ def update_ibgp_mesh() -> StepList: begin >> 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) -- GitLab