From 3b64ee66e4b56d81ea167afd7bb4babfe2d39c02 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Fri, 24 Apr 2015 18:32:15 -0400 Subject: [PATCH] Split exceptions into their own file We were getting to the point where we were making some circular depends. Clear that up by splitting the exceptions into their own file. We're importing * to keep the external interface in the ansible modules. Change-Id: I1d804377c3cd4eebcc94624902f9e5ec6ebf9321 --- shade/__init__.py | 20 +--------------- shade/exc.py | 32 +++++++++++++++++++++++++ shade/tests/unit/test_create_server.py | 4 ++-- shade/tests/unit/test_rebuild_server.py | 4 ++-- shade/tests/unit/test_shade.py | 17 ++++++------- 5 files changed, 46 insertions(+), 31 deletions(-) create mode 100644 shade/exc.py diff --git a/shade/__init__.py b/shade/__init__.py index 5cfae53c0..4a9f377a5 100644 --- a/shade/__init__.py +++ b/shade/__init__.py @@ -41,6 +41,7 @@ import troveclient.client as trove_client import warnings warnings.filterwarnings('ignore', 'Certificate has no `subjectAltName`') +from shade.exc import * # noqa from shade import meta from shade import task_manager from shade import _tasks @@ -58,25 +59,6 @@ OBJECT_CONTAINER_ACLS = { } -class OpenStackCloudException(Exception): - def __init__(self, message, extra_data=None): - args = [message] - if extra_data: - args.append(extra_data) - super(OpenStackCloudException, self).__init__(*args) - self.extra_data = extra_data - - def __str__(self): - if self.extra_data is not None: - return "%s (Extra: %s)" % ( - Exception.__str__(self), self.extra_data) - return Exception.__str__(self) - - -class OpenStackCloudTimeout(OpenStackCloudException): - pass - - def openstack_clouds(config=None, debug=False): if not config: config = os_client_config.OpenStackConfig() diff --git a/shade/exc.py b/shade/exc.py new file mode 100644 index 000000000..fed6c4bc3 --- /dev/null +++ b/shade/exc.py @@ -0,0 +1,32 @@ +# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. +# +# 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. + + +class OpenStackCloudException(Exception): + def __init__(self, message, extra_data=None): + args = [message] + if extra_data: + args.append(extra_data) + super(OpenStackCloudException, self).__init__(*args) + self.extra_data = extra_data + + def __str__(self): + if self.extra_data is not None: + return "%s (Extra: %s)" % ( + Exception.__str__(self), self.extra_data) + return Exception.__str__(self) + + +class OpenStackCloudTimeout(OpenStackCloudException): + pass diff --git a/shade/tests/unit/test_create_server.py b/shade/tests/unit/test_create_server.py index cca1570ff..e581d0ad2 100644 --- a/shade/tests/unit/test_create_server.py +++ b/shade/tests/unit/test_create_server.py @@ -20,8 +20,8 @@ Tests for the `create_server` command. """ from mock import patch, Mock -from shade import ( - OpenStackCloud, OpenStackCloudException, OpenStackCloudTimeout) +from shade import OpenStackCloud +from shade.exc import (OpenStackCloudException, OpenStackCloudTimeout) from shade.tests import base diff --git a/shade/tests/unit/test_rebuild_server.py b/shade/tests/unit/test_rebuild_server.py index 960840a85..c455b246a 100644 --- a/shade/tests/unit/test_rebuild_server.py +++ b/shade/tests/unit/test_rebuild_server.py @@ -20,8 +20,8 @@ Tests for the `rebuild_server` command. """ from mock import patch, Mock -from shade import ( - OpenStackCloud, OpenStackCloudException, OpenStackCloudTimeout) +from shade import OpenStackCloud +from shade.exc import (OpenStackCloudException, OpenStackCloudTimeout) from shade.tests.unit import base diff --git a/shade/tests/unit/test_shade.py b/shade/tests/unit/test_shade.py index 5db0d4293..6ddcb7751 100644 --- a/shade/tests/unit/test_shade.py +++ b/shade/tests/unit/test_shade.py @@ -15,6 +15,7 @@ import mock import shade +from shade import exc from shade import meta from shade.tests.unit import base @@ -89,7 +90,7 @@ class TestShade(base.TestCase): @mock.patch.object(shade.OpenStackCloud, 'neutron_client') def test_delete_router_not_found(self, mock_client, mock_search): mock_search.return_value = [] - self.assertRaises(shade.OpenStackCloudException, + self.assertRaises(exc.OpenStackCloudException, self.cloud.delete_router, 'goofy') self.assertFalse(mock_client.delete_router.called) @@ -100,7 +101,7 @@ class TestShade(base.TestCase): router2 = dict(id='456', name='mickey') mock_client.list_routers.return_value = dict(routers=[router1, router2]) - self.assertRaises(shade.OpenStackCloudException, + self.assertRaises(exc.OpenStackCloudException, self.cloud.delete_router, 'mickey') self.assertFalse(mock_client.delete_router.called) @@ -133,7 +134,7 @@ class TestShade(base.TestCase): def test_create_subnet_bad_network(self, mock_client, mock_list): net1 = dict(id='123', name='donald') mock_list.return_value = [net1] - self.assertRaises(shade.OpenStackCloudException, + self.assertRaises(exc.OpenStackCloudException, self.cloud.create_subnet, 'duck', '192.168.199.0/24') self.assertFalse(mock_client.create_subnet.called) @@ -144,7 +145,7 @@ class TestShade(base.TestCase): net1 = dict(id='123', name='donald') net2 = dict(id='456', name='donald') mock_search.return_value = [net1, net2] - self.assertRaises(shade.OpenStackCloudException, + self.assertRaises(exc.OpenStackCloudException, self.cloud.create_subnet, 'donald', '192.168.199.0/24') self.assertFalse(mock_client.create_subnet.called) @@ -161,7 +162,7 @@ class TestShade(base.TestCase): @mock.patch.object(shade.OpenStackCloud, 'neutron_client') def test_delete_subnet_not_found(self, mock_client, mock_search): mock_search.return_value = [] - self.assertRaises(shade.OpenStackCloudException, + self.assertRaises(exc.OpenStackCloudException, self.cloud.delete_subnet, 'goofy') self.assertFalse(mock_client.delete_subnet.called) @@ -172,7 +173,7 @@ class TestShade(base.TestCase): subnet2 = dict(id='456', name='mickey') mock_client.list_subnets.return_value = dict(subnets=[subnet1, subnet2]) - self.assertRaises(shade.OpenStackCloudException, + self.assertRaises(exc.OpenStackCloudException, self.cloud.delete_subnet, 'mickey') self.assertFalse(mock_client.delete_subnet.called) @@ -524,8 +525,8 @@ class TestShadeOperator(base.TestCase): def test_register_machine_port_create_failed(self, mock_client): nics = [{'mac': '00:00:00:00:00:00'}] mock_client.port.create.side_effect = ( - shade.OpenStackCloudException("Error")) - self.assertRaises(shade.OpenStackCloudException, + exc.OpenStackCloudException("Error")) + self.assertRaises(exc.OpenStackCloudException, self.cloud.register_machine, nics) self.assertTrue(mock_client.node.create.called)