diff options
| author | Eugene Kirpichov <ekirpichov@gmail.com> | 2012-09-26 18:23:40 +0000 |
|---|---|---|
| committer | Eugene Kirpichov <ekirpichov@gmail.com> | 2012-09-26 18:25:59 +0000 |
| commit | 8a0c03c9e57926f4bc6c1a0ad9a87b9d59953e6e (patch) | |
| tree | 248af9e99c2da85a2593ba360e090b036fa8feee /openstack | |
| parent | ac5067fb957556fbb2359e0bb8b99b6f733c6a04 (diff) | |
| download | oslo-8a0c03c9e57926f4bc6c1a0ad9a87b9d59953e6e.tar.gz oslo-8a0c03c9e57926f4bc6c1a0ad9a87b9d59953e6e.tar.xz oslo-8a0c03c9e57926f4bc6c1a0ad9a87b9d59953e6e.zip | |
Extracted parse_host_port into network_utils.
Change-Id: I77bcbf03a18659cfa62e99da9ba2136f8348022b
Diffstat (limited to 'openstack')
| -rw-r--r-- | openstack/common/network_utils.py | 68 | ||||
| -rw-r--r-- | openstack/common/rpc/impl_kombu.py | 4 | ||||
| -rw-r--r-- | openstack/common/utils.py | 44 |
3 files changed, 70 insertions, 46 deletions
diff --git a/openstack/common/network_utils.py b/openstack/common/network_utils.py new file mode 100644 index 0000000..69f6732 --- /dev/null +++ b/openstack/common/network_utils.py @@ -0,0 +1,68 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2012 OpenStack LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +Network-related utilities and helper functions. +""" + +import logging + +LOG = logging.getLogger(__name__) + + +def parse_host_port(address, default_port=None): + """ + Interpret a string as a host:port pair. + An IPv6 address MUST be escaped if accompanied by a port, + because otherwise ambiguity ensues: 2001:db8:85a3::8a2e:370:7334 + means both [2001:db8:85a3::8a2e:370:7334] and + [2001:db8:85a3::8a2e:370]:7334. + + >>> parse_host_port('server01:80') + ('server01', 80) + >>> parse_host_port('server01') + ('server01', None) + >>> parse_host_port('server01', default_port=1234) + ('server01', 1234) + >>> parse_host_port('[::1]:80') + ('::1', 80) + >>> parse_host_port('[::1]') + ('::1', None) + >>> parse_host_port('[::1]', default_port=1234) + ('::1', 1234) + >>> parse_host_port('2001:db8:85a3::8a2e:370:7334', default_port=1234) + ('2001:db8:85a3::8a2e:370:7334', 1234) + + """ + if address[0] == '[': + # Escaped ipv6 + _host, _port = address[1:].split(']') + host = _host + if ':' in _port: + port = _port.split(':')[1] + else: + port = default_port + else: + if address.count(':') == 1: + host, port = address.split(':') + else: + # 0 means ipv4, >1 means ipv6. + # We prohibit unescaped ipv6 addresses with port. + host = address + port = default_port + + return (host, None if port is None else int(port)) diff --git a/openstack/common/rpc/impl_kombu.py b/openstack/common/rpc/impl_kombu.py index 34289bc..6c981b2 100644 --- a/openstack/common/rpc/impl_kombu.py +++ b/openstack/common/rpc/impl_kombu.py @@ -33,7 +33,7 @@ from openstack.common import cfg from openstack.common.gettextutils import _ from openstack.common.rpc import amqp as rpc_amqp from openstack.common.rpc import common as rpc_common -from openstack.common import utils +from openstack.common import network_utils kombu_opts = [ cfg.StrOpt('kombu_ssl_version', @@ -405,7 +405,7 @@ class Connection(object): ssl_params = self._fetch_ssl_params() params_list = [] for adr in self.conf.rabbit_hosts: - hostname, port = utils.parse_host_port( + hostname, port = network_utils.parse_host_port( adr, default_port=self.conf.rabbit_port) params = {} diff --git a/openstack/common/utils.py b/openstack/common/utils.py index 7df3dbc..74c571d 100644 --- a/openstack/common/utils.py +++ b/openstack/common/utils.py @@ -64,50 +64,6 @@ def bool_from_string(subject): return False -def parse_host_port(address, default_port=None): - """ - Interpret a string as a host:port pair. - An IPv6 address MUST be escaped if accompanied by a port, - because otherwise ambiguity ensues: 2001:db8:85a3::8a2e:370:7334 - means both [2001:db8:85a3::8a2e:370:7334] and - [2001:db8:85a3::8a2e:370]:7334. - - >>> parse_host_port('server01:80') - ('server01', 80) - >>> parse_host_port('server01') - ('server01', None) - >>> parse_host_port('server01', default_port=1234) - ('server01', 1234) - >>> parse_host_port('[::1]:80') - ('::1', 80) - >>> parse_host_port('[::1]') - ('::1', None) - >>> parse_host_port('[::1]', default_port=1234) - ('::1', 1234) - >>> parse_host_port('2001:db8:85a3::8a2e:370:7334', default_port=1234) - ('2001:db8:85a3::8a2e:370:7334', 1234) - - """ - if address[0] == '[': - # Escaped ipv6 - _host, _port = address[1:].split(']') - host = _host - if ':' in _port: - port = _port.split(':')[1] - else: - port = default_port - else: - if address.count(':') == 1: - host, port = address.split(':') - else: - # 0 means ipv4, >1 means ipv6. - # We prohibit unescaped ipv6 addresses with port. - host = address - port = default_port - - return (host, None if port is None else int(port)) - - def execute(*cmd, **kwargs): """ Helper method to execute command with optional retry. |
