Use oslo.utils implementation to parse/escape server address

oslo.utils have provided a few useful utility methods to parse or
escape server addresses. Replace the own logic by these methods to
reduce amount of own logic.

Change-Id: I7807314e891e202e47254bf7f98aca4c99005079
This commit is contained in:
Takashi Kajinami 2024-10-05 23:09:02 +09:00
parent 5b707d8552
commit 3ed01f4845
4 changed files with 9 additions and 41 deletions

View File

@ -20,6 +20,7 @@ from confluent_kafka import KafkaException
from oslo_serialization import jsonutils
from oslo_utils import eventletutils
from oslo_utils import importutils
from oslo_utils import netutils
from oslo_messaging._drivers import base
from oslo_messaging._drivers import common as driver_common
@ -125,10 +126,8 @@ class Connection(object):
LOG.warning("Different transport usernames detected")
if host.hostname:
if ':' in host.hostname:
hostaddr = "[%s]:%s" % (host.hostname, host.port)
else:
hostaddr = "%s:%s" % (host.hostname, host.port)
hostaddr = "%s:%s" % (netutils.escape_ipv6(host.hostname),
host.port)
self.hostaddrs.append(hostaddr)

View File

@ -36,6 +36,7 @@ import kombu.messaging
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import eventletutils
from oslo_utils import netutils
import oslo_messaging
from oslo_messaging._drivers import amqp as rpc_amqp
@ -1001,16 +1002,10 @@ class Connection(object):
transport,
parse.quote(host.username or default_username),
parse.quote(host.password or default_password),
self._parse_url_hostname(host.hostname) or default_hostname,
netutils.escape_ipv6(host.hostname) or default_hostname,
str(host.port or 5672),
url.virtual_host or '')
def _parse_url_hostname(self, hostname):
"""Handles hostname returned from urlparse and checks whether it's
ipaddress. If it's ipaddress it ensures that it has brackets for IPv6.
"""
return '[%s]' % hostname if ':' in hostname else hostname
def _fetch_ssl_params(self):
"""Handles fetching what ssl params should be used for the connection
(if any).

View File

@ -305,7 +305,7 @@ class TestTransportUrlCustomisation(test_utils.BaseTestCase):
"fake:/vhost1/localhost:5672/?l=1&l=2&l=3")
self.url4 = transport_url_parse(
"fake:/vhost2/localhost:5672/?d=x:1&d=y:2&d=z:3")
self.url5 = transport_url_parse("fake://noport:/?")
self.url5 = transport_url_parse("fake://noport/?")
def test_hash(self):
urls = {}

View File

@ -21,6 +21,7 @@ import logging
from debtcollector import removals
from oslo_config import cfg
from oslo_utils import netutils
from stevedore import driver
from urllib import parse
@ -397,10 +398,7 @@ class TransportURL(object):
# Build the network location portion of the transport URL
if hostname:
if ':' in hostname:
netloc += '[%s]' % hostname
else:
netloc += hostname
netloc += netutils.escape_ipv6(hostname)
if port is not None:
netloc += ':%d' % port
@ -492,31 +490,7 @@ class TransportURL(object):
password = parse.unquote(password)
username = parse.unquote(username)
if not hostname:
hostname = None
elif hostname.startswith('['):
# Find the closing ']' and extract the hostname
host_end = hostname.find(']')
if host_end < 0:
# NOTE(Vek): Identical to what Python 2.7's
# urlparse.urlparse() raises in this case
raise ValueError('Invalid IPv6 URL')
port_text = hostname[host_end:]
hostname = hostname[1:host_end]
# Now we need the port; this is compliant with how urlparse
# parses the port data
port = None
if ':' in port_text:
port = port_text.split(':', 1)[1]
elif ':' in hostname:
hostname, port = hostname.split(':', 1)
if port == "":
port = None
if port is not None:
port = int(port)
hostname, port = netutils.parse_host_port(hostname)
if username is None or password is None:
hosts_without_credentials.append(hostname)