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.

Conflicts:
    neutron/services/segments/plugin.py

Change-Id: I645610febde446b78ed6edd868e699673648a4de
Closes-bug: #1868724
(cherry picked from commit 07b015d789)
(cherry picked from commit b019821abd)
This commit is contained in:
Brian Haley 2020-03-24 14:10:38 -04:00 committed by Slawek Kaplonski
parent 84562fc892
commit 45f5635606
2 changed files with 4 additions and 4 deletions

View File

@ -236,10 +236,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

View File

@ -1545,10 +1545,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