diff options
author | melanie witt <melwitt@yahoo-inc.com> | 2015-04-09 02:39:34 +0000 |
---|---|---|
committer | melanie witt <melwitt@yahoo-inc.com> | 2015-04-09 18:04:51 +0000 |
commit | 3d3083562371fb1ffb1a89d791cfd469035873a6 (patch) | |
tree | 0ebbfa90d170bd1512f14392dc64d555f945d906 | |
parent | c99d7ab0384d9b7bbcc78c9cf6086aac9baaee6b (diff) | |
download | nova-3d3083562371fb1ffb1a89d791cfd469035873a6.tar.gz |
Refactor nova-net cidr validation in prep for bug fix
The network manager create_networks function is on the verge of the
"too complex" pep8 check error, so this change refactors the cidr
validation code out into a private function in preparation for
fixing a bug.
Change-Id: Id0603ddd642acccfa12ae53a52ecfb66dca53702
-rw-r--r-- | nova/network/manager.py | 67 |
1 files changed, 35 insertions, 32 deletions
diff --git a/nova/network/manager.py b/nova/network/manager.py index ea3098b142..1e8d2459e7 100644 --- a/nova/network/manager.py +++ b/nova/network/manager.py @@ -1227,6 +1227,40 @@ class NetworkManager(manager.Manager): raise exception.AddressOutOfRange(address=ip, cidr=str(subnet)) return index + def _validate_cidr(self, context, nets, subnets_v4, fixed_net_v4): + used_subnets = [net.cidr for net in nets] + + def find_next(subnet): + next_subnet = subnet.next() + while next_subnet in subnets_v4: + next_subnet = next_subnet.next() + if next_subnet in fixed_net_v4: + return next_subnet + + for subnet in list(subnets_v4): + if subnet in used_subnets: + next_subnet = find_next(subnet) + if next_subnet: + subnets_v4.remove(subnet) + subnets_v4.append(next_subnet) + subnet = next_subnet + else: + raise exception.CidrConflict(cidr=subnet, + other=subnet) + for used_subnet in used_subnets: + if subnet in used_subnet: + raise exception.CidrConflict(cidr=subnet, + other=used_subnet) + if used_subnet in subnet: + next_subnet = find_next(subnet) + if next_subnet: + subnets_v4.remove(subnet) + subnets_v4.append(next_subnet) + subnet = next_subnet + else: + raise exception.CidrConflict(cidr=subnet, + other=used_subnet) + def _do_create_networks(self, context, label, cidr, multi_host, num_networks, network_size, cidr_v6, gateway, gateway_v6, bridge, @@ -1271,38 +1305,7 @@ class NetworkManager(manager.Manager): except exception.NoNetworksFound: nets = [] num_used_nets = len(nets) - used_subnets = [net.cidr for net in nets] - - def find_next(subnet): - next_subnet = subnet.next() - while next_subnet in subnets_v4: - next_subnet = next_subnet.next() - if next_subnet in fixed_net_v4: - return next_subnet - - for subnet in list(subnets_v4): - if subnet in used_subnets: - next_subnet = find_next(subnet) - if next_subnet: - subnets_v4.remove(subnet) - subnets_v4.append(next_subnet) - subnet = next_subnet - else: - raise exception.CidrConflict(cidr=subnet, - other=subnet) - for used_subnet in used_subnets: - if subnet in used_subnet: - raise exception.CidrConflict(cidr=subnet, - other=used_subnet) - if used_subnet in subnet: - next_subnet = find_next(subnet) - if next_subnet: - subnets_v4.remove(subnet) - subnets_v4.append(next_subnet) - subnet = next_subnet - else: - raise exception.CidrConflict(cidr=subnet, - other=used_subnet) + self._validate_cidr(context, nets, subnets_v4, fixed_net_v4) networks = objects.NetworkList(context=context, objects=[]) subnets = itertools.izip_longest(subnets_v4, subnets_v6) |