use plugin utils from neutron-lib
The remainder of the neutron.plugins.common.utils were rehomed into neutron-lib with [1][2]. This patch consumes them by using the functions from neutron-lib, and removing the neutron.plugins.common.utils module all together as it's fully rehomed now. NeutronLibImpact [1] https://review.openstack.org/#/c/560950/ [2] https://review.openstack.org/#/c/554546/ Change-Id: Ic0f7b37861f078ce8c5ee92d97e977b8d2b468ad
This commit is contained in:
parent
be4a1dd6dd
commit
839e575fa6
@ -23,6 +23,7 @@ from neutron_lib.callbacks import resources
|
||||
from neutron_lib import constants
|
||||
from neutron_lib import exceptions
|
||||
from neutron_lib.plugins import directory
|
||||
from neutron_lib.plugins import utils as p_utils
|
||||
from oslo_config import cfg
|
||||
from oslo_db import exception as db_exc
|
||||
from oslo_log import log as logging
|
||||
@ -36,7 +37,6 @@ from neutron.common import utils
|
||||
from neutron.db import api as db_api
|
||||
from neutron.db import provisioning_blocks
|
||||
from neutron.extensions import segment as segment_ext
|
||||
from neutron.plugins.common import utils as p_utils
|
||||
from neutron.quota import resource_registry
|
||||
|
||||
|
||||
|
@ -55,7 +55,6 @@ from neutron.extensions import qos_fip
|
||||
from neutron.objects import base as base_obj
|
||||
from neutron.objects import ports as port_obj
|
||||
from neutron.objects import router as l3_obj
|
||||
from neutron.plugins.common import utils as p_utils
|
||||
from neutron import worker as neutron_worker
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
@ -376,8 +375,8 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
|
||||
'device_owner': DEVICE_OWNER_ROUTER_GW,
|
||||
'admin_state_up': True,
|
||||
'name': ''}
|
||||
gw_port = p_utils.create_port(self._core_plugin,
|
||||
context.elevated(), {'port': port_data})
|
||||
gw_port = plugin_utils.create_port(
|
||||
self._core_plugin, context.elevated(), {'port': port_data})
|
||||
|
||||
if not gw_port['fixed_ips']:
|
||||
LOG.debug('No IPs available for external network %s',
|
||||
@ -820,8 +819,8 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
|
||||
'device_id': router.id,
|
||||
'device_owner': owner,
|
||||
'name': ''}
|
||||
return p_utils.create_port(self._core_plugin, context,
|
||||
{'port': port_data}), [subnet], True
|
||||
return plugin_utils.create_port(
|
||||
self._core_plugin, context, {'port': port_data}), [subnet], True
|
||||
|
||||
@staticmethod
|
||||
def _make_router_interface_info(
|
||||
@ -1307,10 +1306,9 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
|
||||
|
||||
# 'status' in port dict could not be updated by default, use
|
||||
# check_allow_post to stop the verification of system
|
||||
external_port = p_utils.create_port(self._core_plugin,
|
||||
context.elevated(),
|
||||
{'port': port},
|
||||
check_allow_post=False)
|
||||
external_port = plugin_utils.create_port(
|
||||
self._core_plugin, context.elevated(),
|
||||
{'port': port}, check_allow_post=False)
|
||||
|
||||
with plugin_utils.delete_port_on_error(
|
||||
self._core_plugin, context.elevated(),
|
||||
|
@ -49,7 +49,6 @@ from neutron.objects import agent as ag_obj
|
||||
from neutron.objects import base as base_obj
|
||||
from neutron.objects import l3agent as rb_obj
|
||||
from neutron.objects import router as l3_obj
|
||||
from neutron.plugins.common import utils as p_utils
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
@ -213,8 +212,8 @@ class DVRResourceOperationHandler(object):
|
||||
'device_owner': const.DEVICE_OWNER_ROUTER_SNAT,
|
||||
'admin_state_up': True,
|
||||
'name': ''}
|
||||
snat_port = p_utils.create_port(self._core_plugin, context,
|
||||
{'port': port_data})
|
||||
snat_port = plugin_utils.create_port(
|
||||
self._core_plugin, context, {'port': port_data})
|
||||
if not snat_port:
|
||||
msg = _("Unable to create the SNAT Interface Port")
|
||||
raise n_exc.BadRequest(resource='router', msg=msg)
|
||||
@ -896,8 +895,8 @@ class _DVRAgentInterfaceMixin(object):
|
||||
portbindings.HOST_ID: host,
|
||||
'admin_state_up': True,
|
||||
'name': ''}
|
||||
agent_port = p_utils.create_port(self._core_plugin, context,
|
||||
{'port': port_data})
|
||||
agent_port = plugin_utils.create_port(
|
||||
self._core_plugin, context, {'port': port_data})
|
||||
if agent_port:
|
||||
self._populate_mtu_and_subnets_for_ports(context,
|
||||
[agent_port])
|
||||
|
@ -32,6 +32,7 @@ from neutron_lib import exceptions as n_exc
|
||||
from neutron_lib.exceptions import l3 as l3_exc
|
||||
from neutron_lib.exceptions import l3_ext_ha_mode as l3ha_exc
|
||||
from neutron_lib.objects import exceptions as obj_base
|
||||
from neutron_lib.plugins import utils as p_utils
|
||||
from oslo_config import cfg
|
||||
from oslo_db import exception as db_exc
|
||||
from oslo_log import helpers as log_helpers
|
||||
@ -55,7 +56,6 @@ from neutron.db.models import l3ha as l3ha_model
|
||||
from neutron.objects import base
|
||||
from neutron.objects import l3_hamode
|
||||
from neutron.objects import router as l3_obj
|
||||
from neutron.plugins.common import utils as p_utils
|
||||
|
||||
|
||||
VR_ID_RANGE = set(range(1, 255))
|
||||
|
@ -1,76 +0,0 @@
|
||||
# Copyright 2013 Cisco Systems, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
"""
|
||||
Common utilities and helper functions for OpenStack Networking Plugins.
|
||||
These utils are private and for neutron internal use only.
|
||||
"""
|
||||
|
||||
from neutron_lib.api import attributes as lib_attrs
|
||||
from neutron_lib.api.definitions import network as net_def
|
||||
from neutron_lib.api.definitions import port as port_def
|
||||
from neutron_lib.api.definitions import subnet as subnet_def
|
||||
from oslo_config import cfg
|
||||
import webob.exc
|
||||
|
||||
|
||||
# TODO(boden): remove when consuming I2c0e4ef03425ba0bb2651ae3e68d6c8cde7b8f90
|
||||
|
||||
def _fixup_res_dict(context, attr_name, res_dict, check_allow_post=True):
|
||||
attr_info = lib_attrs.RESOURCES[attr_name]
|
||||
attr_ops = lib_attrs.AttributeInfo(attr_info)
|
||||
try:
|
||||
attr_ops.populate_project_id(context, res_dict, True)
|
||||
lib_attrs.populate_project_info(attr_info)
|
||||
attr_ops.verify_attributes(res_dict)
|
||||
except webob.exc.HTTPBadRequest as e:
|
||||
# convert webob exception into ValueError as these functions are
|
||||
# for internal use. webob exception doesn't make sense.
|
||||
raise ValueError(e.detail)
|
||||
attr_ops.fill_post_defaults(res_dict, check_allow_post=check_allow_post)
|
||||
attr_ops.convert_values(res_dict)
|
||||
return res_dict
|
||||
|
||||
|
||||
def create_network(core_plugin, context, net, check_allow_post=True):
|
||||
net_data = _fixup_res_dict(context, net_def.COLLECTION_NAME,
|
||||
net.get('network', {}),
|
||||
check_allow_post=check_allow_post)
|
||||
return core_plugin.create_network(context, {'network': net_data})
|
||||
|
||||
|
||||
def create_subnet(core_plugin, context, subnet, check_allow_post=True):
|
||||
subnet_data = _fixup_res_dict(context, subnet_def.COLLECTION_NAME,
|
||||
subnet.get('subnet', {}),
|
||||
check_allow_post=check_allow_post)
|
||||
return core_plugin.create_subnet(context, {'subnet': subnet_data})
|
||||
|
||||
|
||||
def create_port(core_plugin, context, port, check_allow_post=True):
|
||||
port_data = _fixup_res_dict(context, port_def.COLLECTION_NAME,
|
||||
port.get('port', {}),
|
||||
check_allow_post=check_allow_post)
|
||||
return core_plugin.create_port(context, {'port': port_data})
|
||||
|
||||
|
||||
# TODO(boden): consume with I73f5e8ad7a1a83392094db846d18964d811b8bb2
|
||||
def get_deployment_physnet_mtu():
|
||||
"""Retrieves global physical network MTU setting.
|
||||
|
||||
Plugins should use this function to retrieve the MTU set by the operator
|
||||
that is equal to or less than the MTU of their nodes' physical interfaces.
|
||||
Note that it is the responsibility of the plugin to deduct the value of
|
||||
any encapsulation overhead required before advertising it to VMs.
|
||||
"""
|
||||
return cfg.CONF.global_physnet_mtu
|
@ -17,6 +17,7 @@ import random
|
||||
|
||||
from neutron_lib import context as neutron_ctx
|
||||
from neutron_lib.plugins.ml2 import api
|
||||
from neutron_lib.plugins import utils as p_utils
|
||||
from neutron_lib.utils import helpers
|
||||
from oslo_config import cfg
|
||||
from oslo_db import exception as db_exc
|
||||
@ -25,7 +26,6 @@ from oslo_log import log
|
||||
from neutron.common import exceptions as exc
|
||||
from neutron.db import api as db_api
|
||||
from neutron.objects import base as base_obj
|
||||
from neutron.plugins.common import utils as p_utils
|
||||
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
@ -41,6 +41,7 @@ from neutron_lib.exceptions import port_security as psec_exc
|
||||
from neutron_lib.plugins import constants as plugin_constants
|
||||
from neutron_lib.plugins import directory
|
||||
from neutron_lib.plugins.ml2 import api
|
||||
from neutron_lib.plugins import utils as p_utils
|
||||
from neutron_lib.services.qos import constants as qos_consts
|
||||
from oslo_config import cfg
|
||||
from oslo_db import exception as os_db_exception
|
||||
@ -86,7 +87,6 @@ from neutron.db import subnet_service_type_mixin
|
||||
from neutron.db import vlantransparent_db
|
||||
from neutron.extensions import providernet as provider
|
||||
from neutron.extensions import vlantransparent
|
||||
from neutron.plugins.common import utils as p_utils
|
||||
from neutron.plugins.ml2.common import exceptions as ml2_exc
|
||||
from neutron.plugins.ml2 import db
|
||||
from neutron.plugins.ml2 import driver_context
|
||||
|
@ -25,6 +25,7 @@ from neutron_lib import exceptions as n_exc
|
||||
from neutron_lib.objects import exceptions as obj_exc
|
||||
from neutron_lib.plugins import constants
|
||||
from neutron_lib.plugins import directory
|
||||
from neutron_lib.plugins import utils as p_utils
|
||||
from oslo_log import log as logging
|
||||
|
||||
from neutron._i18n import _
|
||||
@ -35,7 +36,6 @@ from neutron.db import common_db_mixin
|
||||
from neutron.objects import auto_allocate as auto_allocate_obj
|
||||
from neutron.objects import base as base_obj
|
||||
from neutron.objects import network as net_obj
|
||||
from neutron.plugins.common import utils as p_utils
|
||||
from neutron.services.auto_allocate import exceptions
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
@ -41,7 +41,7 @@ class TestDhcpRpcCallback(base.BaseTestCase):
|
||||
set_dirty_p = mock.patch('neutron.quota.resource_registry.'
|
||||
'set_resources_dirty')
|
||||
self.mock_set_dirty = set_dirty_p.start()
|
||||
self.utils_p = mock.patch('neutron.plugins.common.utils.create_port')
|
||||
self.utils_p = mock.patch('neutron_lib.plugins.utils.create_port')
|
||||
self.utils = self.utils_p.start()
|
||||
self.segment_plugin = mock.MagicMock()
|
||||
directory.add_plugin('segments', self.segment_plugin)
|
||||
|
@ -1,117 +0,0 @@
|
||||
# Copyright (c) 2015 IBM Corp.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import hashlib
|
||||
|
||||
import mock
|
||||
from neutron_lib import constants
|
||||
from neutron_lib import exceptions
|
||||
from neutron_lib.plugins import utils
|
||||
import testtools
|
||||
|
||||
from neutron.db import l3_db
|
||||
from neutron.tests import base
|
||||
|
||||
LONG_NAME1 = "A_REALLY_LONG_INTERFACE_NAME1"
|
||||
LONG_NAME2 = "A_REALLY_LONG_INTERFACE_NAME2"
|
||||
SHORT_NAME = "SHORT"
|
||||
MOCKED_HASH = "mockedhash"
|
||||
|
||||
|
||||
class MockSHA(object):
|
||||
def hexdigest(self):
|
||||
return MOCKED_HASH
|
||||
|
||||
|
||||
class TestUtils(base.BaseTestCase):
|
||||
|
||||
@mock.patch.object(hashlib, 'sha1', return_value=MockSHA())
|
||||
def test_get_interface_name(self, mock_sha1):
|
||||
prefix = "pre-"
|
||||
prefix_long = "long_prefix"
|
||||
prefix_exceeds_max_dev_len = "much_too_long_prefix"
|
||||
hash_used = MOCKED_HASH[0:6]
|
||||
|
||||
self.assertEqual("A_REALLY_" + hash_used,
|
||||
utils.get_interface_name(LONG_NAME1))
|
||||
self.assertEqual("SHORT",
|
||||
utils.get_interface_name(SHORT_NAME))
|
||||
self.assertEqual("pre-A_REA" + hash_used,
|
||||
utils.get_interface_name(LONG_NAME1, prefix=prefix))
|
||||
self.assertEqual("pre-SHORT",
|
||||
utils.get_interface_name(SHORT_NAME, prefix=prefix))
|
||||
# len(prefix) > max_device_len - len(hash_used)
|
||||
self.assertRaises(ValueError, utils.get_interface_name, SHORT_NAME,
|
||||
prefix_long)
|
||||
# len(prefix) > max_device_len
|
||||
self.assertRaises(ValueError, utils.get_interface_name, SHORT_NAME,
|
||||
prefix=prefix_exceeds_max_dev_len)
|
||||
|
||||
def test_get_interface_uniqueness(self):
|
||||
prefix = "prefix-"
|
||||
if_prefix1 = utils.get_interface_name(LONG_NAME1, prefix=prefix)
|
||||
if_prefix2 = utils.get_interface_name(LONG_NAME2, prefix=prefix)
|
||||
self.assertNotEqual(if_prefix1, if_prefix2)
|
||||
|
||||
@mock.patch.object(hashlib, 'sha1', return_value=MockSHA())
|
||||
def test_get_interface_max_len(self, mock_sha1):
|
||||
self.assertEqual(constants.DEVICE_NAME_MAX_LEN,
|
||||
len(utils.get_interface_name(LONG_NAME1)))
|
||||
self.assertEqual(10, len(utils.get_interface_name(LONG_NAME1,
|
||||
max_len=10)))
|
||||
self.assertEqual(12, len(utils.get_interface_name(LONG_NAME1,
|
||||
prefix="pre-",
|
||||
max_len=12)))
|
||||
|
||||
def test_delete_port_on_error(self):
|
||||
core_plugin, context = mock.Mock(), mock.Mock()
|
||||
port_id = 'pid'
|
||||
with testtools.ExpectedException(ValueError):
|
||||
with utils.delete_port_on_error(core_plugin, context, port_id):
|
||||
raise ValueError()
|
||||
core_plugin.delete_port.assert_called_once_with(context, port_id,
|
||||
l3_port_check=False)
|
||||
|
||||
def test_delete_port_on_error_fail_port_delete(self):
|
||||
core_plugin, context = mock.Mock(), mock.Mock()
|
||||
core_plugin.delete_port.side_effect = TypeError()
|
||||
port_id = 'pid'
|
||||
with testtools.ExpectedException(ValueError):
|
||||
with utils.delete_port_on_error(core_plugin, context, port_id):
|
||||
raise ValueError()
|
||||
core_plugin.delete_port.assert_called_once_with(context, port_id,
|
||||
l3_port_check=False)
|
||||
|
||||
def test_delete_port_on_error_port_does_not_exist(self):
|
||||
core_plugin, context = mock.Mock(), mock.Mock()
|
||||
port_id = 'pid'
|
||||
core_plugin.delete_port.side_effect = exceptions.PortNotFound(
|
||||
port_id=port_id)
|
||||
with testtools.ExpectedException(exceptions.PortNotFound):
|
||||
with utils.delete_port_on_error(core_plugin, context, port_id):
|
||||
raise exceptions.PortNotFound(port_id=port_id)
|
||||
core_plugin.delete_port.assert_called_once_with(context, port_id,
|
||||
l3_port_check=False)
|
||||
|
||||
@mock.patch.object(l3_db.L3_NAT_dbonly_mixin, '_check_router_port')
|
||||
def test_update_port_on_error(self, mock_check):
|
||||
core_plugin, context = mock.Mock(), mock.Mock()
|
||||
port = mock_check.return_value = {'device_owner': 'xxxxxxxx'}
|
||||
revert_value = {'device_id': '', 'device_owner': port['device_owner']}
|
||||
with testtools.ExpectedException(ValueError):
|
||||
with utils.update_port_on_error(core_plugin,
|
||||
context, 1, revert_value):
|
||||
raise ValueError()
|
||||
core_plugin.update_port.assert_called_once_with(
|
||||
context, 1, {'port': revert_value})
|
Loading…
Reference in New Issue
Block a user