From a71776b8a4a9e95665bcdb1d05668213467b73e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harald=20Jens=C3=A5s?= Date: Tue, 7 Feb 2023 11:31:03 +0100 Subject: [PATCH] Get ports by 'binding:host_id' query filter By retriving ports from neutron by 'binding:host_id', i.e ironic node uuid we can remove the list_node_vifs() call for each node. Story: 2010571 Task: 47302 Change-Id: I1d85f3341e04ddf969027a6f6839f4a807666258 --- metalsmith/_instance.py | 21 ++++++++------------- metalsmith/test/test_instance.py | 8 ++------ 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/metalsmith/_instance.py b/metalsmith/_instance.py index c269a69..e49cd91 100644 --- a/metalsmith/_instance.py +++ b/metalsmith/_instance.py @@ -16,8 +16,6 @@ import enum import logging -from openstack import exceptions as os_exc - from metalsmith import _utils @@ -122,17 +120,14 @@ class Instance(object): with full representations of their networks. """ result = [] - vifs = self._connection.baremetal.list_node_vifs(self.node) - for vif in vifs: - try: - port = self._connection.network.get_port(vif) - if port.network_id not in Instance.network_cache: - Instance.network_cache[port.network_id] = ( - self._connection.network.get_network(port.network_id)) - port.network = Instance.network_cache[port.network_id] - result.append(port) - except os_exc.ResourceNotFound: - LOG.warning('vif has missing port: %s', vif) + ports_query = {'binding:host_id': self.node.id} + ports = self._connection.network.ports(**ports_query) + for port in ports: + if port.network_id not in Instance.network_cache: + Instance.network_cache[port.network_id] = ( + self._connection.network.get_network(port.network_id)) + port.network = Instance.network_cache[port.network_id] + result.append(port) return result @property diff --git a/metalsmith/test/test_instance.py b/metalsmith/test/test_instance.py index 726eb05..f249a4b 100644 --- a/metalsmith/test/test_instance.py +++ b/metalsmith/test/test_instance.py @@ -15,8 +15,6 @@ from unittest import mock -from openstack import exceptions as os_exc - from metalsmith import _instance from metalsmith.test import test_provisioner @@ -25,13 +23,12 @@ class TestInstanceIPAddresses(test_provisioner.Base): def setUp(self): super(TestInstanceIPAddresses, self).setUp() self.instance = _instance.Instance(self.api, self.node) - self.api.baremetal.list_node_vifs.return_value = ['111', '222'] self.ports = [ mock.Mock(spec=['network_id', 'fixed_ips', 'network'], network_id=n, fixed_ips=[{'ip_address': ip}]) for n, ip in [('0', '192.168.0.1'), ('1', '10.0.0.2')] ] - self.api.network.get_port.side_effect = self.ports + self.api.network.ports.return_value = self.ports self.nets = [ mock.Mock(spec=['id', 'name'], id=str(i)) for i in range(2) ] @@ -56,9 +53,8 @@ class TestInstanceIPAddresses(test_provisioner.Base): mock.Mock(spec=['network_id', 'fixed_ips', 'network'], network_id='0', fixed_ips=[{'ip_address': '192.168.0.1'}]), - os_exc.ResourceNotFound(), ] - self.api.network.get_port.side_effect = self.ports + self.api.network.ports.return_value = self.ports ips = self.instance.ip_addresses() self.assertEqual({'name-0': ['192.168.0.1']}, ips)