diff --git a/elasticsearch/client/__init__.py b/elasticsearch/client/__init__.py index 00c0066e..f0930c31 100644 --- a/elasticsearch/client/__init__.py +++ b/elasticsearch/client/__init__.py @@ -1,4 +1,5 @@ import weakref +import logging from ..transport import Transport from ..exceptions import NotFoundError, TransportError @@ -6,6 +7,8 @@ from .indices import IndicesClient from .cluster import ClusterClient from .utils import query_params, _make_path +logger = logging.getLogger('elasticsearch') + def _normalize_hosts(hosts): """ Helper function to transform hosts argument to @@ -15,10 +18,23 @@ def _normalize_hosts(hosts): if hosts is None: return [{}] + # passed in just one string + if isinstance(hosts, (type(''), type(u''))): + hosts = [hosts] + out = [] # normalize hosts to dicts for i, host in enumerate(hosts): if isinstance(host, (type(''), type(u''))): + host = host.strip('/') + # remove schema information + if '://' in host: + logger.warn( + "List of nodes should not include schema information (http://): %r.", + host + ) + host = host[host.index('://') + 3:] + h = {"host": host} if ':' in host: # TODO: detect auth urls diff --git a/test_elasticsearch/test_client/__init__.py b/test_elasticsearch/test_client/__init__.py index 800ecb0b..9ffba6a8 100644 --- a/test_elasticsearch/test_client/__init__.py +++ b/test_elasticsearch/test_client/__init__.py @@ -1,3 +1,5 @@ +from mock import patch + from elasticsearch.client import _normalize_hosts from ..test_cases import TestCase, ElasticsearchTestCase @@ -18,6 +20,22 @@ class TestNormalizeHosts(TestCase): def test_dicts_are_left_unchanged(self): self.assertEquals([{"host": "local", "extra": 123}], _normalize_hosts([{"host": "local", "extra": 123}])) + @patch('elasticsearch.client.logger') + def test_schema_is_stripped_out(self, logger): + self.assertEquals( + [{"host": "elasticsearch.org", "port": 9200}], + _normalize_hosts(["http://elasticsearch.org:9200/"]) + ) + # schema triggers a warning + self.assertEquals(1, logger.warn.call_count) + + def test_single_string_is_wrapped_in_list(self): + self.assertEquals( + [{"host": "elasticsearch.org"}], + _normalize_hosts("elasticsearch.org") + ) + + class TestClient(ElasticsearchTestCase): def test_from_in_search(self): self.client.search(index='i', doc_type='t', from_=10)