File tree Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 1616"""
1717IPv6-related utilities and helper functions.
1818"""
19+ import ipaddress
20+
1921import netaddr
2022from neutron_lib import constants as const
2123from oslo_log import log
@@ -56,3 +58,19 @@ def valid_ipv6_url(host, port):
5658 else :
5759 uri = '%s:%s' % (host , port )
5860 return uri
61+
62+
63+ # TODO(egarciar): Remove and use oslo.utils version of this function whenever
64+ # it is available for Neutron.
65+ # https://review.opendev.org/c/openstack/oslo.utils/+/925469
66+ def get_noscope_ipv6 (address ):
67+ try :
68+ _ipv6 = ipaddress .IPv6Address (address )
69+ if _ipv6 .scope_id :
70+ address = address .removesuffix ('%' + _ipv6 .scope_id )
71+ return address
72+ except (ipaddress .AddressValueError , AttributeError ):
73+ if netutils .is_valid_ipv6 (address ):
74+ parts = address .rsplit ("%" , 1 )
75+ return parts [0 ]
76+ raise
Original file line number Diff line number Diff line change 4141
4242from neutron ._i18n import _
4343from neutron .common import _constants as n_const
44+ from neutron .common import ipv6_utils
4445from neutron .common .ovn import constants
4546from neutron .common .ovn import exceptions as ovn_exc
4647from neutron .common import utils as common_utils
@@ -605,6 +606,8 @@ def get_system_dns_resolvers(resolver_file=DNS_RESOLVER_FILE):
605606 valid_ip = (netutils .is_valid_ipv4 (line , strict = True ) or
606607 netutils .is_valid_ipv6 (line ))
607608 if valid_ip :
609+ if netutils .is_valid_ipv6 (line ):
610+ line = ipv6_utils .get_noscope_ipv6 (line )
608611 resolvers .append (line )
609612
610613 return resolvers
Original file line number Diff line number Diff line change @@ -98,3 +98,16 @@ def test_valid_hostname_url(self):
9898 port = 443
9999 self .assertEqual ("controller:443" ,
100100 ipv6_utils .valid_ipv6_url (host , port ))
101+
102+
103+ class TestNoscopeIpv6 (base .BaseTestCase ):
104+ def test_get_noscope_ipv6 (self ):
105+ self .assertEqual ('2001:db8::f0:42:8329' ,
106+ ipv6_utils .get_noscope_ipv6 ('2001:db8::f0:42:8329%1' ))
107+ self .assertEqual ('ff02::5678' ,
108+ ipv6_utils .get_noscope_ipv6 ('ff02::5678%eth0' ))
109+ self .assertEqual ('fe80::1' ,
110+ ipv6_utils .get_noscope_ipv6 ('fe80::1%eth0' ))
111+ self .assertEqual ('::1' , ipv6_utils .get_noscope_ipv6 ('::1%eth0' ))
112+ self .assertEqual ('::1' , ipv6_utils .get_noscope_ipv6 ('::1' ))
113+ self .assertRaises (ValueError , ipv6_utils .get_noscope_ipv6 , '::132:::' )
You can’t perform that action at this time.
0 commit comments