Handle NetworkInUse exception in api layer

In nova-network, if we delete an existing network with port already
associated, a 500 error will be reported and this is not helpful for
end user. This patch catch the network in use exception and raise
an HTTPConflict exception with formatted message.

Change-Id: I7361e461e0eecc0ef6e47fa8ef9a48a24b7d5dfd
Closes-Bug: #1294920
This commit is contained in:
jichenjc 2014-03-23 00:15:25 +08:00
parent e239791fc7
commit 9645f19abf
4 changed files with 47 additions and 0 deletions

View File

@ -100,6 +100,8 @@ class NetworkController(wsgi.Controller):
LOG.info(_("Deleting network with id %s") % id)
try:
self.network_api.delete(context, id)
except exception.NetworkInUse as e:
raise exc.HTTPConflict(explanation=e.format_message())
except exception.NetworkNotFound:
msg = _("Network not found")
raise exc.HTTPNotFound(explanation=msg)

View File

@ -131,6 +131,8 @@ class NetworkController(object):
response = exc.HTTPAccepted()
except exception.PolicyNotAuthorized as e:
raise exc.HTTPForbidden(explanation=str(e))
except exception.NetworkInUse as e:
raise exc.HTTPConflict(explanation=e.format_message())
except exception.NetworkNotFound:
msg = _("Network not found")
raise exc.HTTPNotFound(explanation=msg)

View File

@ -106,6 +106,8 @@ class FakeNetworkAPI(object):
self._vlan_is_disabled = True
def delete(self, context, network_id):
if network_id == -1:
raise exception.NetworkInUse(network_id=network_id)
for i, network in enumerate(self.networks):
if network['id'] == network_id:
del self.networks[0]
@ -304,6 +306,11 @@ class NetworksTest(test.NoDBTestCase):
self.assertRaises(webob.exc.HTTPNotFound,
self.controller.delete, req, 100)
def test_network_delete_in_use(self):
req = fakes.HTTPRequest.blank('/v2/1234/os-networks/-1')
self.assertRaises(webob.exc.HTTPConflict,
self.controller.delete, req, -1)
def test_network_add_vlan_disabled(self):
self.fake_network_api.disable_vlan()
uuid = FAKE_NETWORKS[1]['uuid']

View File

@ -0,0 +1,36 @@
# Copyright 2014 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 mock
import webob
from nova.api.openstack.compute.contrib import os_tenant_networks as networks
from nova import exception
from nova import test
from nova.tests.api.openstack import fakes
class NetworksTest(test.NoDBTestCase):
def setUp(self):
super(NetworksTest, self).setUp()
self.controller = networks.NetworkController()
@mock.patch('nova.network.api.API.delete',
side_effect=exception.NetworkInUse(network_id=1))
def test_network_delete_in_use(self, mock_delete):
req = fakes.HTTPRequest.blank('/v2/1234/os-tenant-networks/1')
self.assertRaises(webob.exc.HTTPConflict,
self.controller.delete, req, 1)