Merge "Add a function to trim scope out of ipv6 addr"
This commit is contained in:
commit
d37a161b7c
@ -17,6 +17,7 @@
|
|||||||
Network-related utilities and helper functions.
|
Network-related utilities and helper functions.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import ipaddress
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
@ -148,6 +149,30 @@ def is_valid_ipv6(address):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def get_noscope_ipv6(address):
|
||||||
|
"""Take an IPv6 address and trim scope out if present.
|
||||||
|
|
||||||
|
:param address: Value to change
|
||||||
|
:type address: string
|
||||||
|
:returns: string
|
||||||
|
|
||||||
|
.. versionadded: 7.3.0:
|
||||||
|
"""
|
||||||
|
# TODO(egarciar): Scope was added for IPv6Address on Python 3.9. Same for
|
||||||
|
# the removesuffix str method. Remove try/except when minimum required
|
||||||
|
# version for Python is 3.9
|
||||||
|
try:
|
||||||
|
_ipv6 = ipaddress.IPv6Address(address)
|
||||||
|
if _ipv6.scope_id:
|
||||||
|
address = address.removesuffix('%' + _ipv6.scope_id)
|
||||||
|
return address
|
||||||
|
except (ipaddress.AddressValueError, AttributeError):
|
||||||
|
if is_valid_ipv6(address):
|
||||||
|
parts = address.rsplit("%", 1)
|
||||||
|
return parts[0]
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
def is_valid_cidr(address):
|
def is_valid_cidr(address):
|
||||||
"""Verify that address represents a valid CIDR address.
|
"""Verify that address represents a valid CIDR address.
|
||||||
|
|
||||||
|
@ -212,6 +212,16 @@ class NetworkUtilsTest(test_base.BaseTestCase):
|
|||||||
|
|
||||||
self.assertFalse(netutils.is_valid_ipv6(''))
|
self.assertFalse(netutils.is_valid_ipv6(''))
|
||||||
|
|
||||||
|
def test_get_noscope_ipv6(self):
|
||||||
|
self.assertEqual('2001:db8::ff00:42:8329',
|
||||||
|
netutils.get_noscope_ipv6('2001:db8::ff00:42:8329%1'))
|
||||||
|
self.assertEqual('ff02::5678',
|
||||||
|
netutils.get_noscope_ipv6('ff02::5678%eth0'))
|
||||||
|
self.assertEqual('fe80::1', netutils.get_noscope_ipv6('fe80::1%eth0'))
|
||||||
|
self.assertEqual('::1', netutils.get_noscope_ipv6('::1%eth0'))
|
||||||
|
self.assertEqual('::1', netutils.get_noscope_ipv6('::1'))
|
||||||
|
self.assertRaises(ValueError, netutils.get_noscope_ipv6, '::132:::')
|
||||||
|
|
||||||
def test_escape_ipv6(self):
|
def test_escape_ipv6(self):
|
||||||
self.assertEqual('[1234::1234]', netutils.escape_ipv6('1234::1234'))
|
self.assertEqual('[1234::1234]', netutils.escape_ipv6('1234::1234'))
|
||||||
self.assertEqual('127.0.0.1', netutils.escape_ipv6('127.0.0.1'))
|
self.assertEqual('127.0.0.1', netutils.escape_ipv6('127.0.0.1'))
|
||||||
|
7
releasenotes/notes/bug-2073894-2e11ca85984b7bb7.yaml
Normal file
7
releasenotes/notes/bug-2073894-2e11ca85984b7bb7.yaml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
features:
|
||||||
|
- |
|
||||||
|
`Bug #2073894 <https://bugs.launchpad.net/oslo.utils/+bug/2073894>`_:
|
||||||
|
Added the netutils.get_noscope_ipv6() function in order to fix errors
|
||||||
|
related to IPv6 address formatting that can happen when using an IPv6
|
||||||
|
defined with scope.
|
Loading…
x
Reference in New Issue
Block a user