From 3fbcfa2071a41d7b8930d446960f1923e1b87034 Mon Sep 17 00:00:00 2001 From: Steve Keay Date: Tue, 10 Sep 2024 10:13:55 +0100 Subject: [PATCH] Refactor - demote function to instance method --- .../neutron_understack_mech.py | 86 +++++++++---------- .../tests/test_neutron_understack_mech.py | 5 +- 2 files changed, 46 insertions(+), 45 deletions(-) diff --git a/python/neutron-understack/neutron_understack/neutron_understack_mech.py b/python/neutron-understack/neutron_understack/neutron_understack_mech.py index 7f361c0f4..bc7d4ed85 100644 --- a/python/neutron-understack/neutron_understack/neutron_understack_mech.py +++ b/python/neutron-understack/neutron_understack/neutron_understack_mech.py @@ -138,48 +138,6 @@ def log_call( pprint(context.current) -def _move_to_network( - vif_type: str, - mac_address: str, - device_uuid: UUID, - network_id: str, - argo_client: ArgoClient, -): - """Triggers Argo "trigger-undersync" workflow. - - This has the effect of connecting our server to the given networks: either - "provisioning" (for PXE booting) or "tenant" (normal access to customer's - networks). The choice of network is based on the network ID. - - This only happens when vif_type is VIF_TYPE_OTHER. - - argo_client is injected by the caller to make testing easier - """ - if vif_type != portbindings.VIF_TYPE_OTHER: - return - - if network_id == cfg.CONF.ml2_type_understack.provisioning_network: - network_name = "provisioning" - else: - network_name = "tenant" - - LOG.debug(f"Selected {network_name=} for {device_uuid=} {mac_address=}") - - result = argo_client.submit( - template_name="undersync-device", - entrypoint="trigger-undersync", - parameters={ - "interface_mac": mac_address, - "device_uuid": device_uuid, - "network_name": network_name, - "dry_run": cfg.CONF.ml2_type_understack.argo_dry_run, - "force": cfg.CONF.ml2_type_understack.argo_force, - }, - service_account=cfg.CONF.ml2_type_understack.argo_workflow_sa, - ) - LOG.info(f"Binding workflow submitted: {result}") - - class UnderstackDriver(MechanismDriver): # See MechanismDriver docs for resource_provider_uuid5_namespace resource_provider_uuid5_namespace = UUID("6eae3046-4072-11ef-9bcf-d6be6370a162") @@ -241,7 +199,7 @@ def update_port_postcommit(self, context): namespace=cfg.CONF.ml2_type_understack.argo_namespace, ) - _move_to_network( + self._move_to_network( vif_type=context.current["binding:vif_type"], mac_address=context.current["mac_address"], device_uuid=context.current["binding:host_id"], @@ -297,3 +255,45 @@ def check_segment(self, segment): def check_vlan_transparency(self, context): log_call("check_vlan_transparency", context) + + def _move_to_network( + self, + vif_type: str, + mac_address: str, + device_uuid: UUID, + network_id: str, + argo_client: ArgoClient, + ): + """Triggers Argo "trigger-undersync" workflow. + + This has the effect of connecting our server to the given networks: either + "provisioning" (for PXE booting) or "tenant" (normal access to customer's + networks). The choice of network is based on the network ID. + + This only happens when vif_type is VIF_TYPE_OTHER. + + argo_client is injected by the caller to make testing easier + """ + if vif_type != portbindings.VIF_TYPE_OTHER: + return + + if network_id == cfg.CONF.ml2_type_understack.provisioning_network: + network_name = "provisioning" + else: + network_name = "tenant" + + LOG.debug(f"Selected {network_name=} for {device_uuid=} {mac_address=}") + + result = argo_client.submit( + template_name="undersync-device", + entrypoint="trigger-undersync", + parameters={ + "interface_mac": mac_address, + "device_uuid": device_uuid, + "network_name": network_name, + "dry_run": cfg.CONF.ml2_type_understack.argo_dry_run, + "force": cfg.CONF.ml2_type_understack.argo_force, + }, + service_account=cfg.CONF.ml2_type_understack.argo_workflow_sa, + ) + LOG.info(f"Binding workflow submitted: {result}") diff --git a/python/neutron-understack/neutron_understack/tests/test_neutron_understack_mech.py b/python/neutron-understack/neutron_understack/tests/test_neutron_understack_mech.py index bc6655cca..f8b71fe31 100644 --- a/python/neutron-understack/neutron_understack/tests/test_neutron_understack_mech.py +++ b/python/neutron-understack/neutron_understack/tests/test_neutron_understack_mech.py @@ -2,7 +2,7 @@ import pytest -from neutron_understack import neutron_understack_mech +from neutron_understack.neutron_understack_mech import UnderstackDriver from neutron_understack.argo.workflows import ArgoClient @@ -12,7 +12,8 @@ def argo_client() -> ArgoClient: def test_move_to_network__provisioning(argo_client): - neutron_understack_mech._move_to_network( + driver = UnderstackDriver() + driver._move_to_network( vif_type="other", mac_address="fa:16:3e:35:1c:3d", device_uuid="41d18c6a-5548-4ee9-926f-4e3ebf43153f",