~ubuntu-branches/ubuntu/vivid/horizon/vivid-proposed

« back to all changes in this revision

Viewing changes to openstack_dashboard/openstack/common/network_utils.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2014-06-17 09:17:40 UTC
  • mfrom: (1.1.41)
  • Revision ID: package-import@ubuntu.com-20140617091740-g73ekvtkxum68uyv
Tags: 1:2014.2~b1-0ubuntu1
* New upstream release.
* debian/control: Open up juno release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2012 OpenStack Foundation.
4
 
# All Rights Reserved.
5
 
#
6
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
7
 
#    not use this file except in compliance with the License. You may obtain
8
 
#    a copy of the License at
9
 
#
10
 
#         http://www.apache.org/licenses/LICENSE-2.0
11
 
#
12
 
#    Unless required by applicable law or agreed to in writing, software
13
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 
#    License for the specific language governing permissions and limitations
16
 
#    under the License.
17
 
 
18
 
"""
19
 
Network-related utilities and helper functions.
20
 
"""
21
 
 
22
 
from openstack_dashboard.openstack.common import log as logging
23
 
 
24
 
 
25
 
LOG = logging.getLogger(__name__)
26
 
 
27
 
 
28
 
def parse_host_port(address, default_port=None):
29
 
    """
30
 
    Interpret a string as a host:port pair.
31
 
    An IPv6 address MUST be escaped if accompanied by a port,
32
 
    because otherwise ambiguity ensues: 2001:db8:85a3::8a2e:370:7334
33
 
    means both [2001:db8:85a3::8a2e:370:7334] and
34
 
    [2001:db8:85a3::8a2e:370]:7334.
35
 
 
36
 
    >>> parse_host_port('server01:80')
37
 
    ('server01', 80)
38
 
    >>> parse_host_port('server01')
39
 
    ('server01', None)
40
 
    >>> parse_host_port('server01', default_port=1234)
41
 
    ('server01', 1234)
42
 
    >>> parse_host_port('[::1]:80')
43
 
    ('::1', 80)
44
 
    >>> parse_host_port('[::1]')
45
 
    ('::1', None)
46
 
    >>> parse_host_port('[::1]', default_port=1234)
47
 
    ('::1', 1234)
48
 
    >>> parse_host_port('2001:db8:85a3::8a2e:370:7334', default_port=1234)
49
 
    ('2001:db8:85a3::8a2e:370:7334', 1234)
50
 
 
51
 
    """
52
 
    if address[0] == '[':
53
 
        # Escaped ipv6
54
 
        _host, _port = address[1:].split(']')
55
 
        host = _host
56
 
        if ':' in _port:
57
 
            port = _port.split(':')[1]
58
 
        else:
59
 
            port = default_port
60
 
    else:
61
 
        if address.count(':') == 1:
62
 
            host, port = address.split(':')
63
 
        else:
64
 
            # 0 means ipv4, >1 means ipv6.
65
 
            # We prohibit unescaped ipv6 addresses with port.
66
 
            host = address
67
 
            port = default_port
68
 
 
69
 
    return (host, None if port is None else int(port))