Skip to content

Commit

Permalink
Merge "Return proper error message when network conflicts" into stabl…
Browse files Browse the repository at this point in the history
…e/grizzly
  • Loading branch information
Jenkins authored and openstack-gerrit committed Jul 23, 2013
2 parents f427f01 + 26ae09c commit 78ebf1a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
5 changes: 5 additions & 0 deletions nova/exception.py
Expand Up @@ -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.")

Expand Down
10 changes: 5 additions & 5 deletions nova/network/manager.py
Expand Up @@ -1113,13 +1113,13 @@ def find_next(subnet):
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:
Expand All @@ -1130,8 +1130,8 @@ def find_next(subnet):
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)
Expand Down
31 changes: 18 additions & 13 deletions nova/tests/network/test_manager.py
Expand Up @@ -1355,11 +1355,12 @@ def test_validate_cidrs_smaller_subnet_in_use(self):
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()
Expand Down Expand Up @@ -1407,10 +1408,11 @@ def test_validate_cidrs_split_all_in_use(self):
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()
Expand All @@ -1426,10 +1428,11 @@ def test_validate_cidrs_already_used(self):
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()
Expand Down Expand Up @@ -1457,9 +1460,10 @@ def test_validate_cidrs_conflict_existing_supernet(self):
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'
Expand All @@ -1479,7 +1483,8 @@ def test_create_networks_cidr_already_used(self):
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'
Expand Down

0 comments on commit 78ebf1a

Please sign in to comment.