summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHaiwei Xu <xu-haiwei@mxw.nes.nec.co.jp>2013-04-01 16:48:56 +0900
committerHaiwei Xu <xu-haiwei@mxw.nes.nec.co.jp>2013-04-01 16:55:15 +0900
commit8a80c4c0968a9deceaa6d99629c6d7f7889769e5 (patch)
tree9869476155c801a0de8998fd6bc2959ebd13e959
parenta831e9d2608aa9fd6200ea873e3cb766322a6c3c (diff)
Return proper error message when network conflicts
Fixes bug 1158075 When creating a network which is already existing, we got an internal server error which can't show the error reason. This patch returns conflict error(HTTP 409) instead of internal server error(HTTP 500), so that users can know the error reason from the error message. Change-Id: I9e39a7152e37756b34b1c8d77de057012743f0f7
-rw-r--r--nova/exception.py5
-rw-r--r--nova/network/manager.py10
-rw-r--r--nova/tests/network/test_manager.py31
3 files changed, 28 insertions, 18 deletions
diff --git a/nova/exception.py b/nova/exception.py
index 43f02b7cb..b3b3d1f1f 100644
--- a/nova/exception.py
+++ b/nova/exception.py
@@ -1083,6 +1083,11 @@ class DuplicateVlan(Duplicate):
message = _("Detected existing vlan with id %(vlan)d")
+class CidrConflict(NovaException):
+ message = _("There was a conflict when trying to complete your request.")
+ code = 409
+
+
class InstanceNotFound(NotFound):
message = _("Instance %(instance_id)s could not be found.")
diff --git a/nova/network/manager.py b/nova/network/manager.py
index b9c8efe44..ac413135a 100644
--- a/nova/network/manager.py
+++ b/nova/network/manager.py
@@ -1115,13 +1115,13 @@ class NetworkManager(manager.Manager):
subnets_v4.append(next_subnet)
subnet = next_subnet
else:
- raise ValueError(_('cidr already in use'))
+ raise exception.CidrConflict(_('cidr already in use'))
for used_subnet in used_subnets:
if subnet in used_subnet:
msg = _('requested cidr (%(cidr)s) conflicts with '
'existing supernet (%(super)s)')
- raise ValueError(msg % {'cidr': subnet,
- 'super': used_subnet})
+ raise exception.CidrConflict(
+ msg % {'cidr': subnet, 'super': used_subnet})
if used_subnet in subnet:
next_subnet = find_next(subnet)
if next_subnet:
@@ -1132,8 +1132,8 @@ class NetworkManager(manager.Manager):
msg = _('requested cidr (%(cidr)s) conflicts '
'with existing smaller cidr '
'(%(smaller)s)')
- raise ValueError(msg % {'cidr': subnet,
- 'smaller': used_subnet})
+ raise exception.CidrConflict(
+ msg % {'cidr': subnet, 'smaller': used_subnet})
networks = []
subnets = itertools.izip_longest(subnets_v4, subnets_v6)
diff --git a/nova/tests/network/test_manager.py b/nova/tests/network/test_manager.py
index c4ce649f0..f0d8e58a9 100644
--- a/nova/tests/network/test_manager.py
+++ b/nova/tests/network/test_manager.py
@@ -1309,11 +1309,12 @@ class CommonNetworkTestCase(test.TestCase):
manager.db.network_get_all(ctxt).AndReturn([{'id': 1,
'cidr': '192.168.2.9/25'}])
self.mox.ReplayAll()
- # ValueError: requested cidr (192.168.2.0/24) conflicts with
- # existing smaller cidr
+ # CidrConflict: requested cidr (192.168.2.0/24) conflicts with
+ # existing smaller cidr
args = (None, 'fake', '192.168.2.0/24', False, 1, 256, None, None,
None, None, None)
- self.assertRaises(ValueError, manager.create_networks, *args)
+ self.assertRaises(exception.CidrConflict,
+ manager.create_networks, *args)
def test_validate_cidrs_split_smaller_cidr_in_use(self):
manager = fake_network.FakeNetworkManager()
@@ -1361,10 +1362,11 @@ class CommonNetworkTestCase(test.TestCase):
self.mox.ReplayAll()
args = (None, 'fake', '192.168.2.0/24', False, 3, 64, None, None,
None, None, None)
- # ValueError: Not enough subnets avail to satisfy requested num_
- # networks - some subnets in requested range already
- # in use
- self.assertRaises(ValueError, manager.create_networks, *args)
+ # CidrConflict: Not enough subnets avail to satisfy requested num_
+ # networks - some subnets in requested range already
+ # in use
+ self.assertRaises(exception.CidrConflict,
+ manager.create_networks, *args)
def test_validate_cidrs_one_in_use(self):
manager = fake_network.FakeNetworkManager()
@@ -1380,10 +1382,11 @@ class CommonNetworkTestCase(test.TestCase):
manager.db.network_get_all(ctxt).AndReturn([{'id': 1,
'cidr': '192.168.0.0/24'}])
self.mox.ReplayAll()
- # ValueError: cidr already in use
+ # CidrConflict: cidr already in use
args = (None, 'fake', '192.168.0.0/24', False, 1, 256, None, None,
None, None, None)
- self.assertRaises(ValueError, manager.create_networks, *args)
+ self.assertRaises(exception.CidrConflict,
+ manager.create_networks, *args)
def test_validate_cidrs_too_many(self):
manager = fake_network.FakeNetworkManager()
@@ -1411,9 +1414,10 @@ class CommonNetworkTestCase(test.TestCase):
self.mox.ReplayAll()
args = (None, 'fake', '192.168.0.0/24', False, 1, 256, None, None,
None, None, None)
- # ValueError: requested cidr (192.168.0.0/24) conflicts
- # with existing supernet
- self.assertRaises(ValueError, manager.create_networks, *args)
+ # CidrConflict: requested cidr (192.168.0.0/24) conflicts
+ # with existing supernet
+ self.assertRaises(exception.CidrConflict,
+ manager.create_networks, *args)
def test_create_networks(self):
cidr = '192.168.0.0/24'
@@ -1433,7 +1437,8 @@ class CommonNetworkTestCase(test.TestCase):
self.mox.ReplayAll()
args = [None, 'foo', '192.168.0.0/24', None, 1, 256,
'fd00::/48', None, None, None, None, None]
- self.assertRaises(ValueError, manager.create_networks, *args)
+ self.assertRaises(exception.CidrConflict,
+ manager.create_networks, *args)
def test_create_networks_many(self):
cidr = '192.168.0.0/16'