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 16
16
"""
17
17
IPv6-related utilities and helper functions.
18
18
"""
19
+ import ipaddress
20
+
19
21
import netaddr
20
22
from neutron_lib import constants as const
21
23
from oslo_log import log
@@ -56,3 +58,19 @@ def valid_ipv6_url(host, port):
56
58
else :
57
59
uri = '%s:%s' % (host , port )
58
60
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 41
41
42
42
from neutron ._i18n import _
43
43
from neutron .common import _constants as n_const
44
+ from neutron .common import ipv6_utils
44
45
from neutron .common .ovn import constants
45
46
from neutron .common .ovn import exceptions as ovn_exc
46
47
from neutron .common import utils as common_utils
@@ -605,6 +606,8 @@ def get_system_dns_resolvers(resolver_file=DNS_RESOLVER_FILE):
605
606
valid_ip = (netutils .is_valid_ipv4 (line , strict = True ) or
606
607
netutils .is_valid_ipv6 (line ))
607
608
if valid_ip :
609
+ if netutils .is_valid_ipv6 (line ):
610
+ line = ipv6_utils .get_noscope_ipv6 (line )
608
611
resolvers .append (line )
609
612
610
613
return resolvers
Original file line number Diff line number Diff line change @@ -98,3 +98,16 @@ def test_valid_hostname_url(self):
98
98
port = 443
99
99
self .assertEqual ("controller:443" ,
100
100
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