Update get_port_binding_host for multiple bindings

With the adoption of multiple port bindings, get_port_binding_host is
updated to select only the active binding

Change-Id: I57ee16aa970527de2c2d787ebf0df71241f01e74
Partial-Bug: #1580880
This commit is contained in:
Miguel Lavalle 2018-04-04 16:39:02 -05:00
parent 03db94ebaa
commit 390b6a531f
2 changed files with 11 additions and 5 deletions

View File

@ -215,10 +215,11 @@ def get_port_binding_host(context, port_id):
try:
with db_api.context_manager.reader.using(context):
query = (context.session.query(models.PortBinding).
filter(models.PortBinding.port_id.startswith(port_id)).
one())
filter(models.PortBinding.port_id.startswith(port_id)))
query = query.filter(
models.PortBinding.status == n_const.ACTIVE).one()
except exc.NoResultFound:
LOG.debug("No binding found for port %(port_id)s",
LOG.debug("No active binding found for port %(port_id)s",
{'port_id': port_id})
return
except exc.MultipleResultsFound:

View File

@ -62,11 +62,13 @@ class Ml2DBTestCase(testlib_api.SqlTestCase):
port.create()
return port
def _setup_neutron_portbinding(self, port_id, vif_type, host):
def _setup_neutron_portbinding(self, port_id, vif_type, host,
status=constants.ACTIVE):
with db_api.context_manager.writer.using(self.ctx):
self.ctx.session.add(models.PortBinding(port_id=port_id,
vif_type=vif_type,
host=host))
host=host,
status=status))
@staticmethod
def _sort_segments(segments):
@ -217,10 +219,13 @@ class Ml2DBTestCase(testlib_api.SqlTestCase):
network_id = uuidutils.generate_uuid()
port_id = uuidutils.generate_uuid()
host = 'fake_host'
other_host = 'other_fake_host'
vif_type = portbindings.VIF_TYPE_UNBOUND
self._setup_neutron_network(network_id)
self._setup_neutron_port(network_id, port_id)
self._setup_neutron_portbinding(port_id, vif_type, host)
self._setup_neutron_portbinding(port_id, vif_type, other_host,
status=constants.INACTIVE)
port_host = ml2_db.get_port_binding_host(self.ctx, port_id)
self.assertEqual(host, port_host)