Use dict .get() to avoid a KeyError in the segment plugin
On subnet delete, the supplied subnet in the post hook could contain a subnet without certain items, leading to a KeyError in the segment plugin. Fix a number of these occurences so this cannot happen. Also fixed similar code in the segment tests. Change-Id: I645610febde446b78ed6edd868e699673648a4de Closes-bug: #1868724 (cherry picked from commit07b015d789
) (cherry picked from commitb019821abd
)
This commit is contained in:
parent
5656f3cc89
commit
97f6e2569e
@ -260,10 +260,10 @@ class NovaSegmentNotifier(object):
|
|||||||
total += int(netaddr.IPAddress(pool['end']) -
|
total += int(netaddr.IPAddress(pool['end']) -
|
||||||
netaddr.IPAddress(pool['start'])) + 1
|
netaddr.IPAddress(pool['start'])) + 1
|
||||||
if total:
|
if total:
|
||||||
if subnet['gateway_ip']:
|
if subnet.get('gateway_ip'):
|
||||||
total += 1
|
total += 1
|
||||||
reserved += 1
|
reserved += 1
|
||||||
if subnet['enable_dhcp']:
|
if subnet.get('enable_dhcp'):
|
||||||
reserved += 1
|
reserved += 1
|
||||||
return total, reserved
|
return total, reserved
|
||||||
|
|
||||||
@ -561,7 +561,7 @@ class SegmentHostRoutes(object):
|
|||||||
context=context,
|
context=context,
|
||||||
ip_version=netaddr.IPNetwork(subnet['cidr']).version,
|
ip_version=netaddr.IPNetwork(subnet['cidr']).version,
|
||||||
network_id=subnet['network_id'],
|
network_id=subnet['network_id'],
|
||||||
segment_id=subnet['segment_id'],
|
segment_id=segment_id,
|
||||||
host_routes=copy.deepcopy(host_routes),
|
host_routes=copy.deepcopy(host_routes),
|
||||||
gateway_ip=gateway_ip)
|
gateway_ip=gateway_ip)
|
||||||
if (not host_routes or
|
if (not host_routes or
|
||||||
@ -573,11 +573,14 @@ class SegmentHostRoutes(object):
|
|||||||
def host_routes_before_update(self, resource, event, trigger, **kwargs):
|
def host_routes_before_update(self, resource, event, trigger, **kwargs):
|
||||||
context = kwargs['context']
|
context = kwargs['context']
|
||||||
subnet, original_subnet = kwargs['request'], kwargs['original_subnet']
|
subnet, original_subnet = kwargs['request'], kwargs['original_subnet']
|
||||||
segment_id = subnet.get('segment_id', original_subnet['segment_id'])
|
orig_segment_id = original_subnet.get('segment_id')
|
||||||
gateway_ip = subnet.get('gateway_ip', original_subnet['gateway_ip'])
|
segment_id = subnet.get('segment_id', orig_segment_id)
|
||||||
host_routes = subnet.get('host_routes', original_subnet['host_routes'])
|
orig_gateway_ip = original_subnet.get('gateway_ip')
|
||||||
if (segment_id and (host_routes != original_subnet['host_routes'] or
|
gateway_ip = subnet.get('gateway_ip', orig_gateway_ip)
|
||||||
gateway_ip != original_subnet['gateway_ip'])):
|
orig_host_routes = original_subnet.get('host_routes')
|
||||||
|
host_routes = subnet.get('host_routes', orig_host_routes)
|
||||||
|
if (segment_id and (host_routes != orig_host_routes or
|
||||||
|
gateway_ip != orig_gateway_ip)):
|
||||||
calc_host_routes = self._calculate_routed_network_host_routes(
|
calc_host_routes = self._calculate_routed_network_host_routes(
|
||||||
context=context,
|
context=context,
|
||||||
ip_version=netaddr.IPNetwork(original_subnet['cidr']).version,
|
ip_version=netaddr.IPNetwork(original_subnet['cidr']).version,
|
||||||
@ -585,8 +588,8 @@ class SegmentHostRoutes(object):
|
|||||||
segment_id=segment_id,
|
segment_id=segment_id,
|
||||||
host_routes=copy.deepcopy(host_routes),
|
host_routes=copy.deepcopy(host_routes),
|
||||||
gateway_ip=gateway_ip,
|
gateway_ip=gateway_ip,
|
||||||
old_gateway_ip=original_subnet['gateway_ip'] if (
|
old_gateway_ip=orig_gateway_ip if (
|
||||||
gateway_ip != original_subnet['gateway_ip']) else None)
|
gateway_ip != orig_gateway_ip) else None)
|
||||||
if self._host_routes_need_update(host_routes, calc_host_routes):
|
if self._host_routes_need_update(host_routes, calc_host_routes):
|
||||||
subnet['host_routes'] = calc_host_routes
|
subnet['host_routes'] = calc_host_routes
|
||||||
|
|
||||||
|
@ -1700,10 +1700,10 @@ class TestNovaSegmentNotifier(SegmentAwareIpamTestCase):
|
|||||||
total += int(netaddr.IPAddress(pool['end']) -
|
total += int(netaddr.IPAddress(pool['end']) -
|
||||||
netaddr.IPAddress(pool['start'])) + 1
|
netaddr.IPAddress(pool['start'])) + 1
|
||||||
if total:
|
if total:
|
||||||
if subnet['gateway_ip']:
|
if subnet.get('gateway_ip'):
|
||||||
total += 1
|
total += 1
|
||||||
reserved += 1
|
reserved += 1
|
||||||
if subnet['enable_dhcp']:
|
if subnet.get('enable_dhcp'):
|
||||||
reserved += 1
|
reserved += 1
|
||||||
return total, reserved
|
return total, reserved
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user