Fix error getting segment_id in linux DHCP driver

A recent change [1] added code to the linux DHCP driver
to check the subnet segment_id attribute when adding
extra host routes for a subnet.  I have seen both
KeyError and AttributeError failures in various check
jobs lately since there is no guarantee all subnets
have this attribute.  Be safer by using getattr().

[1] https://review.openstack.org/#/c/438171/

Related-bug: #1668154

Change-Id: I96bea4edab4c96f2ca28ba854f6ca8d4ab4df5f5
This commit is contained in:
Brian Haley 2017-05-03 12:25:57 -04:00 committed by Brian Haley
parent 617fc6087b
commit d605967c97
2 changed files with 7 additions and 7 deletions

View File

@ -869,6 +869,7 @@ class Dnsmasq(DhcpLocalProcess):
isolated_subnets = self.get_isolated_subnets(self.network)
for i, subnet in enumerate(self.network.subnets):
addr_mode = getattr(subnet, 'ipv6_address_mode', None)
segment_id = getattr(subnet, 'segment_id', None)
if (not subnet.enable_dhcp or
(subnet.ip_version == 6 and
addr_mode == constants.IPV6_SLAAC)):
@ -914,11 +915,12 @@ class Dnsmasq(DhcpLocalProcess):
)
if subnet.ip_version == 4:
host_routes.extend(["%s,0.0.0.0" % (s.cidr) for s in
self.network.subnets
if (s.ip_version == 4 and
s.cidr != subnet.cidr and
s.segment_id == subnet.segment_id)])
for s in self.network.subnets:
sub_segment_id = getattr(s, 'segment_id', None)
if (s.ip_version == 4 and
s.cidr != subnet.cidr and
sub_segment_id == segment_id):
host_routes.append("%s,0.0.0.0" % s.cidr)
if host_routes:
if gateway:

View File

@ -385,7 +385,6 @@ class FakeV4Subnet(Dictable):
self.enable_dhcp = True
self.host_routes = [FakeV4HostRoute()]
self.dns_nameservers = ['8.8.8.8']
self.segment_id = None
class FakeV4Subnet2(FakeV4Subnet):
@ -499,7 +498,6 @@ class FakeV4SubnetNoDHCP(object):
self.enable_dhcp = False
self.host_routes = []
self.dns_nameservers = []
self.segment_id = None
class FakeV6SubnetDHCPStateful(Dictable):