From 8f0035ed99effae92d76c046fb29dd76adaa4535 Mon Sep 17 00:00:00 2001 From: Sergey Kraynev Date: Tue, 19 Aug 2014 04:13:08 -0400 Subject: [PATCH] Move neutron_utils functions to neutron client All uses of neutron_utils functions have been migrated to using the neutron client plugin version. Functions in neutron_utils are deprecated and will be deleted in the next patch. Change-Id: I1b00485bddffcb6b9dba2ac770668dde9d6a0d70 --- heat/engine/clients/os/neutron.py | 18 +++++ .../engine/resources/neutron/neutron_utils.py | 7 ++ heat/tests/test_neutron_client.py | 79 +++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 heat/tests/test_neutron_client.py diff --git a/heat/engine/clients/os/neutron.py b/heat/engine/clients/os/neutron.py index e5e7dc979..c69f1ee28 100644 --- a/heat/engine/clients/os/neutron.py +++ b/heat/engine/clients/os/neutron.py @@ -59,6 +59,24 @@ class NeutronClientPlugin(client_plugin.ClientPlugin): return False return ex.status_code == 413 + def find_neutron_resource(self, props, key, key_type): + return neutronV20.find_resourceid_by_name_or_id( + self.client(), key_type, props.get(key)) + + def resolve_network(self, props, net_key, net_id_key): + if props.get(net_key): + props[net_id_key] = self.find_neutron_resource( + props, net_key, 'network') + props.pop(net_key) + return props[net_id_key] + + def resolve_subnet(self, props, subnet_key, subnet_id_key): + if props.get(subnet_key): + props[subnet_id_key] = self.find_neutron_resource( + props, subnet_key, 'subnet') + props.pop(subnet_key) + return props[subnet_id_key] + class NetworkConstraint(constraints.BaseCustomConstraint): diff --git a/heat/engine/resources/neutron/neutron_utils.py b/heat/engine/resources/neutron/neutron_utils.py index 1ea6d0543..c6225acf2 100644 --- a/heat/engine/resources/neutron/neutron_utils.py +++ b/heat/engine/resources/neutron/neutron_utils.py @@ -12,14 +12,19 @@ # under the License. from neutronclient.neutron import v2_0 as neutronV20 +import warnings def find_neutron_resource(neutron_client, props, key, key_type): + warnings.warn('neutron_utils.find_neutron_resource is deprecated. ' + 'Use self.client_plugin("neutron").find_neutron_resource') return neutronV20.find_resourceid_by_name_or_id( neutron_client, key_type, props.get(key)) def resolve_network(neutron_client, props, net_key, net_id_key): + warnings.warn('neutron_utils.resolve_network is deprecated. ' + 'Use self.client_plugin("neutron").resolve_network') if props.get(net_key): props[net_id_key] = find_neutron_resource( neutron_client, props, net_key, 'network') @@ -28,6 +33,8 @@ def resolve_network(neutron_client, props, net_key, net_id_key): def resolve_subnet(neutron_client, props, subnet_key, subnet_id_key): + warnings.warn('neutron_utils.resolve_subnet is deprecated. ' + 'Use self.client_plugin("neutron").resolve_subnet') if props.get(subnet_key): props[subnet_id_key] = find_neutron_resource( neutron_client, props, subnet_key, 'subnet') diff --git a/heat/tests/test_neutron_client.py b/heat/tests/test_neutron_client.py new file mode 100644 index 000000000..8beebcc74 --- /dev/null +++ b/heat/tests/test_neutron_client.py @@ -0,0 +1,79 @@ +# +# 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 mock + + +from heat.engine.clients.os import neutron +from heat.tests.common import HeatTestCase +from heat.tests import utils + + +class NeutronClientPluginTestCase(HeatTestCase): + def setUp(self): + super(NeutronClientPluginTestCase, self).setUp() + self.neutron_client = mock.MagicMock() + + con = utils.dummy_context() + c = con.clients + self.neutron_plugin = c.client_plugin('neutron') + self.neutron_plugin._client = self.neutron_client + + +class NeutronClientPluginTests(NeutronClientPluginTestCase): + def setUp(self): + super(NeutronClientPluginTests, self).setUp() + self.mock_find = self.patchobject(neutron.neutronV20, + 'find_resourceid_by_name_or_id') + self.mock_find.return_value = 42 + + def test_find_neutron_resource(self): + props = {'net': 'test_network'} + + res = self.neutron_plugin.find_neutron_resource(props, 'net', + 'network') + self.assertEqual(42, res) + self.mock_find.assert_called_once_with(self.neutron_client, 'network', + 'test_network') + + def test_resolve_network(self): + props = {'net': 'test_network'} + + res = self.neutron_plugin.resolve_network(props, 'net', 'net_id') + self.assertEqual(42, res) + self.mock_find.assert_called_once_with(self.neutron_client, 'network', + 'test_network') + + # check resolve if was send id instead of name + props = {'net_id': 77} + res = self.neutron_plugin.resolve_network(props, 'net', 'net_id') + self.assertEqual(77, res) + # in this case find_resourceid_by_name_or_id is not called + self.mock_find.assert_called_once_with(self.neutron_client, 'network', + 'test_network') + + def test_resolve_subnet(self): + props = {'snet': 'test_subnet'} + + res = self.neutron_plugin.resolve_subnet(props, 'snet', 'snet_id') + self.assertEqual(42, res) + self.mock_find.assert_called_once_with(self.neutron_client, 'subnet', + 'test_subnet') + + # check resolve if was send id instead of name + props = {'snet_id': 77} + res = self.neutron_plugin.resolve_subnet(props, 'snet', 'snet_id') + self.assertEqual(77, res) + # in this case find_resourceid_by_name_or_id is not called + self.mock_find.assert_called_once_with(self.neutron_client, 'subnet', + 'test_subnet')