Commit 6b18362a authored by Łukasz Nowak's avatar Łukasz Nowak Committed by Thomas Gambier

slapos: Drop slapos.slap.util for slapos.util

parent 665d3896
......@@ -35,7 +35,7 @@ from uritemplate import expand
import os
import logging
from .util import _addIpv6Brackets
from ..util import _addIpv6Brackets
from .exception import ResourceNotReady, NotFoundError, \
AuthenticationError, ConnectionError
......
from six.moves.urllib import parse
import netaddr
def _addIpv6Brackets(url):
# if master_url contains an ipv6 without bracket, add it
# Note that this is mostly to limit specific issues with
# backward compatiblity, not to ensure generic detection.
api_scheme, api_netloc, api_path, api_query, api_fragment = parse.urlsplit(url)
try:
ip = netaddr.IPAddress(api_netloc)
port = None
except netaddr.AddrFormatError:
try:
ip = netaddr.IPAddress(':'.join(api_netloc.split(':')[:-1]))
port = api_netloc.split(':')[-1]
except netaddr.AddrFormatError:
ip = port = None
if ip and ip.version == 6:
api_netloc = '[%s]' % ip
if port:
api_netloc = '%s:%s' % (api_netloc, port)
url = parse.urlunsplit((api_scheme, api_netloc, api_path, api_query, api_fragment))
return url
......@@ -36,6 +36,7 @@ import sqlite3
from xml_marshaller.xml_marshaller import dumps, loads
from lxml import etree
import six
from six.moves.urllib import parse
import hashlib
import netaddr
......@@ -189,3 +190,25 @@ def calculate_dict_hash(d):
d.items()
)
))).hexdigest()
def _addIpv6Brackets(url):
# if master_url contains an ipv6 without bracket, add it
# Note that this is mostly to limit specific issues with
# backward compatiblity, not to ensure generic detection.
api_scheme, api_netloc, api_path, api_query, api_fragment = parse.urlsplit(url)
try:
ip = netaddr.IPAddress(api_netloc)
port = None
except netaddr.AddrFormatError:
try:
ip = netaddr.IPAddress(':'.join(api_netloc.split(':')[:-1]))
port = api_netloc.split(':')[-1]
except netaddr.AddrFormatError:
ip = port = None
if ip and ip.version == 6:
api_netloc = '[%s]' % ip
if port:
api_netloc = '%s:%s' % (api_netloc, port)
url = parse.urlunsplit((api_scheme, api_netloc, api_path, api_query, api_fragment))
return url
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment