rehome create functions from plugin utils

While we rehomed a bulk of the plugin utils with commit
Iabb155b5d2d0ec6104ebee5dd42cf292bdf3ec61, the create_* functions were
not included in that commit. Today we have a handful of consumers that
use these create functions [1], so this patch rehomes them into lib.

[1] http://codesearch.openstack.org/?q=util.*%5C.create_
(port%7Cnetwork%7Csubnet)%5C(&i=nope&files=&repos=

Change-Id: I2c0e4ef03425ba0bb2651ae3e68d6c8cde7b8f90
This commit is contained in:
Boden R 2018-03-20 07:48:36 -06:00
parent aafab19cfd
commit ebf776ac85
3 changed files with 91 additions and 0 deletions

View File

@ -19,8 +19,13 @@ import hashlib
from oslo_log import log as logging
from oslo_utils import encodeutils
from oslo_utils import excutils
from webob import exc as web_exc
from neutron_lib._i18n import _
from neutron_lib.api import attributes
from neutron_lib.api.definitions import network as net_apidef
from neutron_lib.api.definitions import port as port_apidef
from neutron_lib.api.definitions import subnet as subnet_apidef
from neutron_lib import constants
from neutron_lib import exceptions
@ -286,3 +291,43 @@ def get_interface_name(name, prefix='', max_len=constants.DEVICE_NAME_MAX_LEN):
{'requested_name': requested_name,
'limit': max_len, 'new_name': new_name})
return new_name
def _fixup_res_dict(context, attr_name, res_dict, check_allow_post=True):
attr_info = attributes.RESOURCES[attr_name]
attr_ops = attributes.AttributeInfo(attr_info)
try:
attr_ops.populate_project_id(context, res_dict, True)
attributes.populate_project_info(attr_info)
attr_ops.verify_attributes(res_dict)
except web_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_apidef.COLLECTION_NAME,
net.get(net_apidef.RESOURCE_NAME, {}),
check_allow_post=check_allow_post)
return core_plugin.create_network(
context, {net_apidef.RESOURCE_NAME: net_data})
def create_subnet(core_plugin, context, subnet, check_allow_post=True):
subnet_data = _fixup_res_dict(context, subnet_apidef.COLLECTION_NAME,
subnet.get(subnet_apidef.RESOURCE_NAME, {}),
check_allow_post=check_allow_post)
return core_plugin.create_subnet(
context, {subnet_apidef.RESOURCE_NAME: subnet_data})
def create_port(core_plugin, context, port, check_allow_post=True):
port_data = _fixup_res_dict(context, port_apidef.COLLECTION_NAME,
port.get(port_apidef.RESOURCE_NAME, {}),
check_allow_post=check_allow_post)
return core_plugin.create_port(
context, {port_apidef.RESOURCE_NAME: port_data})

View File

@ -16,6 +16,7 @@ import hashlib
import mock
from oslo_utils import excutils
from oslo_utils import uuidutils
from neutron_lib import constants
from neutron_lib import exceptions
@ -207,3 +208,43 @@ class TestUtils(base.BaseTestCase):
self.assertEqual(12, len(utils.get_interface_name(LONG_NAME1,
prefix="pre-",
max_len=12)))
def test_create_network(self):
mock_plugin = mock.Mock()
mock_plugin.create_network = lambda c, n: n
mock_ctx = mock.Mock()
mock_ctx.project_id = 'p1'
net = utils.create_network(
mock_plugin, mock_ctx, {'network': {'name': 'n1'}})
self.assertDictEqual(
{'project_id': 'p1', 'admin_state_up': True, 'shared': False,
'tenant_id': 'p1', 'name': 'n1'},
net['network'])
def test_create_subnet(self):
mock_plugin = mock.Mock()
mock_plugin.create_subnet = lambda c, s: s
mock_ctx = mock.Mock()
mock_ctx.project_id = 'p1'
net_id = uuidutils.generate_uuid()
snet = utils.create_subnet(
mock_plugin, mock_ctx,
{'subnet': {'network_id': net_id, 'ip_version': 4}})
self.assertEqual('p1', snet['subnet']['tenant_id'])
self.assertEqual('p1', snet['subnet']['project_id'])
self.assertEqual(4, snet['subnet']['ip_version'])
self.assertEqual(net_id, snet['subnet']['network_id'])
def test_create_port(self):
mock_plugin = mock.Mock()
mock_plugin.create_port = lambda c, p: p
mock_ctx = mock.Mock()
mock_ctx.project_id = 'p1'
net_id = uuidutils.generate_uuid()
port = utils.create_port(
mock_plugin, mock_ctx,
{'port': {'network_id': net_id, 'name': 'aport'}})
self.assertEqual('p1', port['port']['tenant_id'])
self.assertEqual('p1', port['port']['project_id'])
self.assertEqual('aport', port['port']['name'])
self.assertEqual(net_id, port['port']['network_id'])

View File

@ -0,0 +1,5 @@
---
features:
- The ``create_network``, ``create_subnet`` and ``create_port`` functions
from ``neutron.plugins.common.utils`` are now available in
``neutron_lib.plugins.utils``.