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
This commit is contained in:
Harald Jensås 2023-02-07 11:31:03 +01:00
parent 9c2252ea88
commit a71776b8a4
2 changed files with 10 additions and 19 deletions

View File

@ -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

View File

@ -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)