use get_port_binding_by_status_and_host from lib
The get_port_binding_by_status_and_host function was rehomed into neutron-lib with https://review.openstack.org/#/c/580786/ and released in neutron-lib 1.18.0. This patch consumes the function by removing it in neutron and replacing all uses with lib's version. NeutronLibImpact Change-Id: Iac3246d0eb59709749e0b7e857091447d11a0133
This commit is contained in:
parent
f7056ef844
commit
d7942945c9
|
@ -23,6 +23,7 @@ from neutron_lib.callbacks import events as callback_events
|
|||
from neutron_lib.callbacks import registry
|
||||
from neutron_lib.callbacks import resources as callback_resources
|
||||
from neutron_lib import constants
|
||||
from neutron_lib.plugins import utils
|
||||
from oslo_log import log as logging
|
||||
import oslo_messaging
|
||||
from oslo_utils import uuidutils
|
||||
|
@ -31,7 +32,6 @@ from neutron.agent import resource_cache
|
|||
from neutron.api.rpc.callbacks import resources
|
||||
from neutron.common import constants as n_const
|
||||
from neutron.common import rpc as n_rpc
|
||||
from neutron.common import utils
|
||||
from neutron import objects
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
|
|
@ -18,6 +18,10 @@ from neutron_lib import exceptions as e
|
|||
from neutron._i18n import _
|
||||
|
||||
|
||||
# TODO(boden): remove rpc shims
|
||||
PortBindingNotFound = e.PortBindingNotFound
|
||||
|
||||
|
||||
class SubnetPoolNotFound(e.NotFound):
|
||||
message = _("Subnet pool %(subnetpool_id)s could not be found.")
|
||||
|
||||
|
@ -336,11 +340,6 @@ class FailedToAddQdiscToDevice(e.NeutronException):
|
|||
"to device %(device)s.")
|
||||
|
||||
|
||||
class PortBindingNotFound(e.NotFound):
|
||||
message = _("Binding for port %(port_id)s for host %(host)s could not be "
|
||||
"found.")
|
||||
|
||||
|
||||
class PortBindingAlreadyActive(e.Conflict):
|
||||
message = _("Binding for port %(port_id)s on host %(host)s is already "
|
||||
"active.")
|
||||
|
|
|
@ -33,7 +33,6 @@ import uuid
|
|||
import eventlet
|
||||
from eventlet.green import subprocess
|
||||
import netaddr
|
||||
from neutron_lib.api.definitions import portbindings_extended as pb_ext
|
||||
from neutron_lib import constants as n_const
|
||||
from neutron_lib.utils import helpers
|
||||
from oslo_config import cfg
|
||||
|
@ -45,7 +44,6 @@ import six
|
|||
import neutron
|
||||
from neutron._i18n import _
|
||||
from neutron.api import api_common
|
||||
from neutron.common import exceptions
|
||||
from neutron.db import api as db_api
|
||||
|
||||
TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
|
||||
|
@ -788,39 +786,6 @@ def bits_to_kilobits(value, base):
|
|||
return int((value + (base - 1)) / base)
|
||||
|
||||
|
||||
def get_port_binding_by_status_and_host(bindings, status, host='',
|
||||
raise_if_not_found=False,
|
||||
port_id=None):
|
||||
"""Returns from an iterable the binding with the specified status and host.
|
||||
|
||||
The input iterable can contain zero or one binding in status ACTIVE
|
||||
and zero or many bindings in status INACTIVE. As a consequence, to
|
||||
unequivocally retrieve an inactive binding, the caller must specify a non
|
||||
empty value for host. If host is the empty string, the first binding
|
||||
satisfying the specified status will be returned. If no binding is found
|
||||
with the specified status and host, None is returned or PortBindingNotFound
|
||||
is raised if raise_if_not_found is True
|
||||
|
||||
:param bindings: An iterable containing port bindings
|
||||
:param status: The status of the port binding to return. Possible values
|
||||
are ACTIVE or INACTIVE as defined in
|
||||
:file:`neutron_lib/constants.py`.
|
||||
:param host: str representing the host of the binding to return.
|
||||
:param raise_if_not_found: If a binding is not found and this parameter is
|
||||
True, a PortBindingNotFound exception is raised
|
||||
:param port_id: The id of the binding's port
|
||||
:returns: The searched for port binding or None if it is not found
|
||||
:raises: PortBindingNotFound if the binding is not found and
|
||||
raise_if_not_found is True
|
||||
"""
|
||||
for binding in bindings:
|
||||
if binding[pb_ext.STATUS] == status:
|
||||
if not host or binding[pb_ext.HOST] == host:
|
||||
return binding
|
||||
if raise_if_not_found:
|
||||
raise exceptions.PortBindingNotFound(port_id=port_id, host=host)
|
||||
|
||||
|
||||
def disable_extension_by_service_plugin(core_plugin, service_plugin):
|
||||
if ('filter-validation' in core_plugin.supported_extension_aliases and
|
||||
not api_common.is_filter_validation_supported(service_plugin)):
|
||||
|
|
|
@ -254,7 +254,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
|
|||
**kwargs):
|
||||
port_id = object_id
|
||||
port = db.get_port(context, port_id)
|
||||
port_binding = utils.get_port_binding_by_status_and_host(
|
||||
port_binding = p_utils.get_port_binding_by_status_and_host(
|
||||
getattr(port, 'port_bindings', []), const.ACTIVE)
|
||||
if not port or not port_binding:
|
||||
LOG.debug("Port %s was deleted so its status cannot be updated.",
|
||||
|
@ -510,7 +510,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
|
|||
# mechanism driver update_port_*commit() calls.
|
||||
try:
|
||||
port_db = self._get_port(plugin_context, port_id)
|
||||
cur_binding = utils.get_port_binding_by_status_and_host(
|
||||
cur_binding = p_utils.get_port_binding_by_status_and_host(
|
||||
port_db.port_bindings, const.ACTIVE)
|
||||
except exc.PortNotFound:
|
||||
port_db, cur_binding = None, None
|
||||
|
@ -558,7 +558,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
|
|||
cur_context_binding = cur_binding
|
||||
if new_binding.status == const.INACTIVE:
|
||||
cur_context_binding = (
|
||||
utils.get_port_binding_by_status_and_host(
|
||||
p_utils.get_port_binding_by_status_and_host(
|
||||
port_db.port_bindings, const.INACTIVE,
|
||||
host=new_binding.host))
|
||||
cur_context = driver_context.PortContext(
|
||||
|
@ -665,7 +665,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
|
|||
@resource_extend.extends([port_def.COLLECTION_NAME])
|
||||
def _ml2_extend_port_dict_binding(port_res, port_db):
|
||||
plugin = directory.get_plugin()
|
||||
port_binding = utils.get_port_binding_by_status_and_host(
|
||||
port_binding = p_utils.get_port_binding_by_status_and_host(
|
||||
port_db.port_bindings, const.ACTIVE)
|
||||
# None when called during unit tests for other plugins.
|
||||
if port_binding:
|
||||
|
@ -1339,7 +1339,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
|
|||
original_port=original_port)
|
||||
with db_api.context_manager.writer.using(context):
|
||||
port_db = self._get_port(context, id)
|
||||
binding = utils.get_port_binding_by_status_and_host(
|
||||
binding = p_utils.get_port_binding_by_status_and_host(
|
||||
port_db.port_bindings, const.ACTIVE)
|
||||
if not binding:
|
||||
raise exc.PortNotFound(port_id=id)
|
||||
|
@ -1558,7 +1558,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
|
|||
with db_api.context_manager.writer.using(context):
|
||||
try:
|
||||
port_db = self._get_port(context, id)
|
||||
binding = utils.get_port_binding_by_status_and_host(
|
||||
binding = p_utils.get_port_binding_by_status_and_host(
|
||||
port_db.port_bindings, const.ACTIVE,
|
||||
raise_if_not_found=True, port_id=id)
|
||||
except exc.PortNotFound:
|
||||
|
@ -1678,7 +1678,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
|
|||
# related attribute port_binding could disappear in
|
||||
# concurrent port deletion.
|
||||
# It's not an error condition.
|
||||
binding = utils.get_port_binding_by_status_and_host(
|
||||
binding = p_utils.get_port_binding_by_status_and_host(
|
||||
port_db.port_bindings, const.ACTIVE)
|
||||
if not binding:
|
||||
LOG.info("Binding info for port %s was not found, "
|
||||
|
@ -1722,7 +1722,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
|
|||
plugin_context, port['id'], host)
|
||||
bindlevelhost_match = host
|
||||
else:
|
||||
binding = utils.get_port_binding_by_status_and_host(
|
||||
binding = p_utils.get_port_binding_by_status_and_host(
|
||||
port_db.port_bindings, const.ACTIVE)
|
||||
bindlevelhost_match = binding.host if binding else None
|
||||
if not binding:
|
||||
|
@ -1808,7 +1808,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
|
|||
# listening for db events can modify the port if necessary
|
||||
context.session.flush()
|
||||
updated_port = self._make_port_dict(port)
|
||||
binding = utils.get_port_binding_by_status_and_host(
|
||||
binding = p_utils.get_port_binding_by_status_and_host(
|
||||
port.port_bindings, const.ACTIVE, raise_if_not_found=True,
|
||||
port_id=port_id)
|
||||
levels = db.get_binding_levels(context, port.id, binding.host)
|
||||
|
@ -2061,7 +2061,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
|
|||
port_id=port_id, host=attrs[pbe_ext.HOST])
|
||||
status = const.ACTIVE
|
||||
is_active_binding = True
|
||||
active_binding = utils.get_port_binding_by_status_and_host(
|
||||
active_binding = p_utils.get_port_binding_by_status_and_host(
|
||||
port_db.port_bindings, const.ACTIVE)
|
||||
if active_binding:
|
||||
status = const.INACTIVE
|
||||
|
@ -2173,12 +2173,12 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
|
|||
port_id = port_id['port_id']
|
||||
port_db = self._get_port(context, port_id)
|
||||
self._validate_compute_port(port_db)
|
||||
active_binding = utils.get_port_binding_by_status_and_host(
|
||||
active_binding = p_utils.get_port_binding_by_status_and_host(
|
||||
port_db.port_bindings, const.ACTIVE)
|
||||
if host == (active_binding and active_binding.host):
|
||||
raise n_exc.PortBindingAlreadyActive(port_id=port_id,
|
||||
host=host)
|
||||
inactive_binding = utils.get_port_binding_by_status_and_host(
|
||||
inactive_binding = p_utils.get_port_binding_by_status_and_host(
|
||||
port_db.port_bindings, const.INACTIVE, host=host)
|
||||
if not inactive_binding or inactive_binding.host != host:
|
||||
raise n_exc.PortBindingNotFound(port_id=port_id, host=host)
|
||||
|
|
|
@ -17,10 +17,10 @@ from neutron_lib.api.definitions import portbindings
|
|||
from neutron_lib import constants
|
||||
from neutron_lib.plugins import constants as plugin_const
|
||||
from neutron_lib.plugins import directory
|
||||
from neutron_lib.plugins import utils
|
||||
from oslo_log import log as logging
|
||||
from sqlalchemy.orm import exc as orm_exc
|
||||
|
||||
from neutron.common import utils
|
||||
from neutron.db import _utils as db_utils
|
||||
from neutron.db.models import securitygroup as sg_db
|
||||
from neutron.objects import ports
|
||||
|
|
|
@ -14,6 +14,7 @@ from neutron_lib.api.definitions import portbindings
|
|||
from neutron_lib.callbacks import events
|
||||
from neutron_lib.callbacks import registry
|
||||
from neutron_lib import constants as lib_constants
|
||||
from neutron_lib.plugins import utils
|
||||
from neutron_lib.services.qos import constants as qos_consts
|
||||
from oslo_log import log as logging
|
||||
|
||||
|
@ -23,7 +24,6 @@ from neutron.api.rpc.callbacks import resources
|
|||
from neutron.api.rpc.handlers import resources_rpc
|
||||
from neutron.common import constants
|
||||
from neutron.common import exceptions
|
||||
from neutron.common import utils
|
||||
from neutron.objects.qos import policy as policy_object
|
||||
|
||||
|
||||
|
|
|
@ -21,18 +21,14 @@ import ddt
|
|||
import eventlet
|
||||
import mock
|
||||
import netaddr
|
||||
from neutron_lib.api.definitions import portbindings_extended as pb_ext
|
||||
from neutron_lib import constants
|
||||
from oslo_log import log as logging
|
||||
from oslo_utils import uuidutils
|
||||
import six
|
||||
import testscenarios
|
||||
import testtools
|
||||
|
||||
from neutron.common import constants as common_constants
|
||||
from neutron.common import exceptions
|
||||
from neutron.common import utils
|
||||
from neutron.objects import ports
|
||||
from neutron.tests import base
|
||||
from neutron.tests.unit import tests
|
||||
|
||||
|
@ -528,31 +524,3 @@ class TestIECUnitConversions(BaseUnitConversionTest, base.BaseTestCase):
|
|||
expected_kilobits,
|
||||
utils.bits_to_kilobits(input_bits, self.base_unit)
|
||||
)
|
||||
|
||||
|
||||
class TestGetPortBindingByStatusAndHost(base.BaseTestCase):
|
||||
|
||||
def test_get_port_binding_by_status_and_host(self):
|
||||
bindings = []
|
||||
self.assertIsNone(utils.get_port_binding_by_status_and_host(
|
||||
bindings, constants.INACTIVE))
|
||||
bindings.extend([ports.PortBinding(
|
||||
port_id=uuidutils.generate_uuid(), host='host-1',
|
||||
status=constants.INACTIVE),
|
||||
ports.PortBinding(
|
||||
port_id=uuidutils.generate_uuid(), host='host-2',
|
||||
status=constants.INACTIVE)])
|
||||
self.assertEqual(
|
||||
'host-1', utils.get_port_binding_by_status_and_host(
|
||||
bindings,
|
||||
constants.INACTIVE)[pb_ext.HOST])
|
||||
self.assertEqual(
|
||||
'host-2', utils.get_port_binding_by_status_and_host(
|
||||
bindings,
|
||||
constants.INACTIVE,
|
||||
host='host-2')[pb_ext.HOST])
|
||||
self.assertIsNone(utils.get_port_binding_by_status_and_host(
|
||||
bindings, constants.ACTIVE))
|
||||
self.assertRaises(exceptions.PortBindingNotFound,
|
||||
utils.get_port_binding_by_status_and_host, bindings,
|
||||
constants.ACTIVE, 'host', True, 'port_id')
|
||||
|
|
|
@ -35,6 +35,7 @@ from neutron_lib import fixture
|
|||
from neutron_lib.plugins import constants as plugin_constants
|
||||
from neutron_lib.plugins import directory
|
||||
from neutron_lib.plugins.ml2 import api as driver_api
|
||||
from neutron_lib.plugins import utils as p_utils
|
||||
from oslo_config import cfg
|
||||
from oslo_db import exception as db_exc
|
||||
from oslo_utils import uuidutils
|
||||
|
@ -1810,7 +1811,7 @@ class TestMl2PortBinding(Ml2PluginV2TestCase,
|
|||
# create a port and delete it so we have an expired mechanism context
|
||||
with self.port() as port:
|
||||
plugin = directory.get_plugin()
|
||||
binding = utils.get_port_binding_by_status_and_host(
|
||||
binding = p_utils.get_port_binding_by_status_and_host(
|
||||
plugin._get_port(self.context,
|
||||
port['port']['id']).port_bindings,
|
||||
constants.ACTIVE)
|
||||
|
@ -1833,7 +1834,7 @@ class TestMl2PortBinding(Ml2PluginV2TestCase,
|
|||
def _create_port_and_bound_context(self, port_vif_type, bound_vif_type):
|
||||
with self.port() as port:
|
||||
plugin = directory.get_plugin()
|
||||
binding = utils.get_port_binding_by_status_and_host(
|
||||
binding = p_utils.get_port_binding_by_status_and_host(
|
||||
plugin._get_port(self.context,
|
||||
port['port']['id']).port_bindings,
|
||||
constants.ACTIVE)
|
||||
|
@ -1950,7 +1951,7 @@ class TestMl2PortBinding(Ml2PluginV2TestCase,
|
|||
def test_update_port_binding_host_id_none(self):
|
||||
with self.port() as port:
|
||||
plugin = directory.get_plugin()
|
||||
binding = utils.get_port_binding_by_status_and_host(
|
||||
binding = p_utils.get_port_binding_by_status_and_host(
|
||||
plugin._get_port(self.context,
|
||||
port['port']['id']).port_bindings,
|
||||
constants.ACTIVE)
|
||||
|
@ -1972,7 +1973,7 @@ class TestMl2PortBinding(Ml2PluginV2TestCase,
|
|||
def test_update_port_binding_host_id_not_changed(self):
|
||||
with self.port() as port:
|
||||
plugin = directory.get_plugin()
|
||||
binding = utils.get_port_binding_by_status_and_host(
|
||||
binding = p_utils.get_port_binding_by_status_and_host(
|
||||
plugin._get_port(self.context,
|
||||
port['port']['id']).port_bindings,
|
||||
constants.ACTIVE)
|
||||
|
@ -2961,7 +2962,7 @@ class TestML2Segments(Ml2PluginV2TestCase):
|
|||
# add writer here to make sure that the following operations are
|
||||
# performed in the same session
|
||||
with db_api.context_manager.writer.using(self.context):
|
||||
binding = utils.get_port_binding_by_status_and_host(
|
||||
binding = p_utils.get_port_binding_by_status_and_host(
|
||||
plugin._get_port(self.context,
|
||||
port['port']['id']).port_bindings,
|
||||
constants.ACTIVE)
|
||||
|
|
|
@ -19,12 +19,12 @@ from neutron_lib.api.definitions import portbindings_extended as pbe_ext
|
|||
from neutron_lib import constants as const
|
||||
from neutron_lib import context
|
||||
from neutron_lib.plugins import directory
|
||||
from neutron_lib.plugins import utils
|
||||
from oslo_config import cfg
|
||||
from oslo_serialization import jsonutils
|
||||
import webob.exc
|
||||
|
||||
from neutron.common import exceptions
|
||||
from neutron.common import utils
|
||||
from neutron.conf.plugins.ml2 import config
|
||||
from neutron.conf.plugins.ml2.drivers import driver_type
|
||||
from neutron.plugins.ml2 import driver_context
|
||||
|
|
Loading…
Reference in New Issue