committed by
Honza Král
parent
03a817690d
commit
923ee018cf
@@ -1,9 +1,10 @@
|
|||||||
|
from __future__ import unicode_literals
|
||||||
import weakref
|
import weakref
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from ..transport import Transport
|
from ..transport import Transport
|
||||||
from ..exceptions import NotFoundError, TransportError
|
from ..exceptions import NotFoundError, TransportError
|
||||||
from ..compat import string_types
|
from ..compat import string_types, urlparse
|
||||||
from .indices import IndicesClient
|
from .indices import IndicesClient
|
||||||
from .cluster import ClusterClient
|
from .cluster import ClusterClient
|
||||||
from .cat import CatClient
|
from .cat import CatClient
|
||||||
@@ -30,22 +31,27 @@ def _normalize_hosts(hosts):
|
|||||||
# normalize hosts to dicts
|
# normalize hosts to dicts
|
||||||
for i, host in enumerate(hosts):
|
for i, host in enumerate(hosts):
|
||||||
if isinstance(host, string_types):
|
if isinstance(host, string_types):
|
||||||
host = host.strip('/')
|
if '://' not in host:
|
||||||
# remove schema information
|
host = "//%s" % host
|
||||||
if '://' in host:
|
|
||||||
|
parsed_url = urlparse(host)
|
||||||
|
h = {"host": parsed_url.hostname}
|
||||||
|
|
||||||
|
if parsed_url.port:
|
||||||
|
h["port"] = parsed_url.port
|
||||||
|
|
||||||
|
if parsed_url.scheme == "https":
|
||||||
|
h['port'] = parsed_url.port or 443
|
||||||
|
h['use_ssl'] = True
|
||||||
|
elif parsed_url.scheme == "http":
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"List of nodes should not include schema information (http://): %r.",
|
"List of nodes should not include schema information (http://): %r.",
|
||||||
host
|
host
|
||||||
)
|
)
|
||||||
host = host[host.index('://') + 3:]
|
|
||||||
|
|
||||||
h = {"host": host}
|
if parsed_url.username or parsed_url.password:
|
||||||
if ':' in host:
|
h['http_auth'] = '%s:%s' % (parsed_url.username, parsed_url.password)
|
||||||
# TODO: detect auth urls
|
|
||||||
host, port = host.rsplit(':', 1)
|
|
||||||
if port.isdigit():
|
|
||||||
port = int(port)
|
|
||||||
h = {"host": host, "port": port}
|
|
||||||
out.append(h)
|
out.append(h)
|
||||||
else:
|
else:
|
||||||
out.append(host)
|
out.append(host)
|
||||||
|
|||||||
@@ -5,8 +5,9 @@ PY2 = sys.version_info[0] == 2
|
|||||||
if PY2:
|
if PY2:
|
||||||
string_types = basestring,
|
string_types = basestring,
|
||||||
from urllib import quote_plus, urlencode
|
from urllib import quote_plus, urlencode
|
||||||
|
from urlparse import urlparse
|
||||||
from itertools import imap as map
|
from itertools import imap as map
|
||||||
else:
|
else:
|
||||||
string_types = str, bytes
|
string_types = str, bytes
|
||||||
from urllib.parse import quote_plus, urlencode
|
from urllib.parse import quote_plus, urlencode, urlparse
|
||||||
map = map
|
map = map
|
||||||
|
|||||||
@@ -13,12 +13,19 @@ class TestNormalizeHosts(TestCase):
|
|||||||
def test_strings_are_used_as_hostnames(self):
|
def test_strings_are_used_as_hostnames(self):
|
||||||
self.assertEquals([{"host": "elasticsearch.org"}], _normalize_hosts(["elasticsearch.org"]))
|
self.assertEquals([{"host": "elasticsearch.org"}], _normalize_hosts(["elasticsearch.org"]))
|
||||||
|
|
||||||
def test_strings_are_parsed_for_port(self):
|
def test_strings_are_parsed_for_port_and_user(self):
|
||||||
self.assertEquals(
|
self.assertEquals(
|
||||||
[{"host": "elasticsearch.org", "port": 42}, {"host": "user:secret@elasticsearch.com"}],
|
[{"host": "elasticsearch.org", "port": 42}, {"host": "elasticsearch.com", "http_auth": "user:secret"}],
|
||||||
_normalize_hosts(["elasticsearch.org:42", "user:[email protected]"])
|
_normalize_hosts(["elasticsearch.org:42", "user:[email protected]"])
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_strings_are_parsed_for_scheme(self):
|
||||||
|
self.assertEquals(
|
||||||
|
[{"host": "elasticsearch.org", "port": 42, "use_ssl": True},
|
||||||
|
{"host": "elasticsearch.com", "http_auth": "user:secret", "use_ssl": True, "port": 443}],
|
||||||
|
_normalize_hosts(["https://elasticsearch.org:42", "https://user:[email protected]"])
|
||||||
|
)
|
||||||
|
|
||||||
def test_dicts_are_left_unchanged(self):
|
def test_dicts_are_left_unchanged(self):
|
||||||
self.assertEquals([{"host": "local", "extra": 123}], _normalize_hosts([{"host": "local", "extra": 123}]))
|
self.assertEquals([{"host": "local", "extra": 123}], _normalize_hosts([{"host": "local", "extra": 123}]))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user