diff --git a/neutron_lib/api/attributes.py b/neutron_lib/api/attributes.py new file mode 100644 index 000000000..1e00e94e4 --- /dev/null +++ b/neutron_lib/api/attributes.py @@ -0,0 +1,43 @@ +# 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. + +from webob import exc + +from neutron_lib._i18n import _ + + +def populate_project_info(attributes): + """Ensure that both project_id and tenant_id attributes are present. + + If either project_id or tenant_id is present in attributes then ensure + that both are present. + + If neither are present then attributes is not updated. + + :param attributes: a dictionary of resource/API attributes + :type attributes: dict + + :return: the updated attributes dictionary + :rtype: dict + + """ + if 'tenant_id' in attributes and 'project_id' not in attributes: + attributes['project_id'] = attributes['tenant_id'] + elif 'project_id' in attributes and 'tenant_id' not in attributes: + # Backward compatibility for code still using tenant_id + attributes['tenant_id'] = attributes['project_id'] + + if attributes.get('project_id') != attributes.get('tenant_id'): + msg = _("'project_id' and 'tenant_id' do not match") + raise exc.HTTPBadRequest(msg) + + return attributes diff --git a/neutron_lib/api/utils.py b/neutron_lib/api/utils.py index bbcaeff32..103b0a16f 100644 --- a/neutron_lib/api/utils.py +++ b/neutron_lib/api/utils.py @@ -10,35 +10,14 @@ # License for the specific language governing permissions and limitations # under the License. -from webob import exc +""" +NOTE: Do not use this module to add random utils. +We want to avoid having a generic utils.py dumping ground. +""" -from neutron_lib._i18n import _ +from debtcollector import moves +from neutron_lib.api import attributes -def populate_project_info(attributes): - """Ensure that both project_id and tenant_id attributes are present. - - If either project_id or tenant_id is present in attributes then ensure - that both are present. - - If neither are present then attributes is not updated. - - :param attributes: a dictionary of resource/API attributes - :type attributes: dict - - :return: the updated attributes dictionary - :rtype: dict - - """ - if 'tenant_id' in attributes and 'project_id' not in attributes: - # TODO(HenryG): emit a deprecation warning here - attributes['project_id'] = attributes['tenant_id'] - elif 'project_id' in attributes and 'tenant_id' not in attributes: - # Backward compatibility for code still using tenant_id - attributes['tenant_id'] = attributes['project_id'] - - if attributes.get('project_id') != attributes.get('tenant_id'): - msg = _("'project_id' and 'tenant_id' do not match") - raise exc.HTTPBadRequest(msg) - - return attributes +populate_project_info = moves.moved_function( + attributes.populate_project_info, 'populate_project_info', __name__) diff --git a/neutron_lib/tests/unit/api/test_utils.py b/neutron_lib/tests/unit/api/test_attributes.py similarity index 85% rename from neutron_lib/tests/unit/api/test_utils.py rename to neutron_lib/tests/unit/api/test_attributes.py index d481276bc..efa39467e 100644 --- a/neutron_lib/tests/unit/api/test_utils.py +++ b/neutron_lib/tests/unit/api/test_attributes.py @@ -12,7 +12,7 @@ from webob import exc -from neutron_lib.api import utils +from neutron_lib.api import attributes from oslo_utils import uuidutils from neutron_lib.tests import _base as base @@ -22,14 +22,14 @@ class TestApiUtils(base.BaseTestCase): def test_populate_project_info_add_project(self): attrs_in = {'tenant_id': uuidutils.generate_uuid()} - attrs_out = utils.populate_project_info(attrs_in) + attrs_out = attributes.populate_project_info(attrs_in) self.assertIn('project_id', attrs_out) self.assertEqual(attrs_in['tenant_id'], attrs_out['project_id']) self.assertEqual(2, len(attrs_out)) def test_populate_project_info_add_tenant(self): attrs_in = {'project_id': uuidutils.generate_uuid()} - attrs_out = utils.populate_project_info(attrs_in) + attrs_out = attributes.populate_project_info(attrs_in) self.assertIn('tenant_id', attrs_out) self.assertEqual(attrs_in['project_id'], attrs_out['tenant_id']) self.assertEqual(2, len(attrs_out)) @@ -37,7 +37,7 @@ class TestApiUtils(base.BaseTestCase): def test_populate_project_info_ids_match(self): project_id = uuidutils.generate_uuid() attrs_in = {'project_id': project_id, 'tenant_id': project_id} - attrs_out = utils.populate_project_info(attrs_in) + attrs_out = attributes.populate_project_info(attrs_in) self.assertEqual(attrs_in, attrs_out) def test_populate_project_info_id_mismatch(self): @@ -46,4 +46,4 @@ class TestApiUtils(base.BaseTestCase): 'tenant_id': uuidutils.generate_uuid() } self.assertRaises(exc.HTTPBadRequest, - utils.populate_project_info, attrs) + attributes.populate_project_info, attrs) diff --git a/releasenotes/notes/deprecate-api-utils-4f86288591c95679.yaml b/releasenotes/notes/deprecate-api-utils-4f86288591c95679.yaml new file mode 100644 index 000000000..e67cbc94e --- /dev/null +++ b/releasenotes/notes/deprecate-api-utils-4f86288591c95679.yaml @@ -0,0 +1,5 @@ +--- +deprecations: + - The function ``neutron_lib.api.utils.populate_project_info`` has moved + to ``neutron_lib.api.attributes.populate_project_info``. It will be removed + from the old location in the future.