Skip to content
This repository has been archived by the owner on Aug 2, 2019. It is now read-only.

Commit

Permalink
@leplatrem review.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémy HUBSCHER committed Nov 12, 2014
1 parent ca4de89 commit 8edda0c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 53 deletions.
44 changes: 3 additions & 41 deletions daybed/indexer.py → daybed/indexer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from elasticsearch.exceptions import RequestError, ElasticsearchException

from daybed import logger
from six.moves.urllib.parse import urlparse
from .utils import build_elasticsearch_hosts


class SearchError(Exception):
Expand All @@ -17,46 +17,8 @@ def __init__(self, *args):
class ElasticSearchIndexer(object):

def __init__(self, hosts, prefix):
self.built_hosts = []

for host in hosts:
arguments = urlparse(host)

# If the argument is not an URL, let it go.
if not arguments.netloc:
self.built_hosts.append(host)
continue

http_auth = None
use_ssl = False
port = 80

netloc = arguments.netloc.split('@')

if len(netloc) == 2:
http_auth = netloc[0]
netloc = netloc[1]
else:
netloc = arguments.netloc

if ':' in netloc:
hostname, port = netloc.split(':')
if arguments.scheme == 'https':
use_ssl = True
else:
hostname = netloc
if arguments.scheme == 'https':
use_ssl = True
port = 443

self.built_hosts.append({
'host': hostname,
'port': int(port),
'use_ssl': use_ssl,
'http_auth': http_auth
})

self.client = elasticsearch.Elasticsearch(self.built_hosts)
built_hosts = build_elasticsearch_hosts(hosts)
self.client = elasticsearch.Elasticsearch(built_hosts)
self.prefix = lambda x: u'%s_%s' % (prefix, x)

def search(self, model_id, query, params):
Expand Down
51 changes: 51 additions & 0 deletions daybed/indexer/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
from six.moves.urllib.parse import urlparse


def build_elasticsearch_hosts(hosts):
"""Take a list of hosts and build an Elasticsearch parameter list.
>>> build_elasticsearch_hosts(['https://admin:password@localhost'])
[{'use_ssl': True, 'host': 'localhost', 'http_auth': 'admin:password', 'port': 443}]
"""
built_hosts = []

for host in hosts:
arguments = urlparse(host)

# If the argument is not an URL, let it go.
if not arguments.netloc:
built_hosts.append(host)
continue

http_auth = None
use_ssl = False
port = 80

netloc = arguments.netloc.split('@')

if len(netloc) == 2:
http_auth = netloc[0]
netloc = netloc[1]
else:
netloc = arguments.netloc

if ':' in netloc:
hostname, port = netloc.split(':')
if arguments.scheme == 'https':
use_ssl = True
else:
hostname = netloc
if arguments.scheme == 'https':
use_ssl = True
port = 443

built_hosts.append({
'host': hostname,
'port': int(port),
'use_ssl': use_ssl,
'http_auth': http_auth
})

return built_hosts
31 changes: 19 additions & 12 deletions daybed/tests/test_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,58 @@

from daybed.schemas import registry
from daybed import indexer
from daybed.indexer.utils import build_elasticsearch_hosts

from .support import BaseWebTest, unittest
from .test_views import MODEL_DEFINITION, MODEL_RECORD


class ConfigurationTest(unittest.TestCase):

def test_default_config(self):
@mock.patch('elasticsearch.Elasticsearch')
def test_default_config(self, elasticsearch_mock):
new_indexer = indexer.ElasticSearchIndexer(['localhost:9200'], 'daybed_')
self.assertEqual(new_indexer.built_hosts, ['localhost:9200'])
elasticsearch_mock.assert_called_with(['localhost:9200'])

def test_default_port(self):
@mock.patch('elasticsearch.Elasticsearch')
def test_default_port(self, elasticsearch_mock):
new_indexer = indexer.ElasticSearchIndexer(['localhost'], 'daybed_')
self.assertEqual(new_indexer.built_hosts, ['localhost'])
elasticsearch_mock.assert_called_with(['localhost'])

def test_http_url(self):
@mock.patch('elasticsearch.Elasticsearch')
def test_http_url(self, elasticsearch_mock):
new_indexer = indexer.ElasticSearchIndexer(['http://localhost:9200'], 'daybed_')
self.assertEqual(new_indexer.built_hosts, [{
elasticsearch_mock.assert_called_with([{
'host': 'localhost',
'port': 9200,
'use_ssl': False,
'http_auth': None
}])

def test_https_url(self):
@mock.patch('elasticsearch.Elasticsearch')
def test_https_url(self, elasticsearch_mock):
new_indexer = indexer.ElasticSearchIndexer(['https://localhost'], 'daybed_')
self.assertEqual(new_indexer.built_hosts, [{
elasticsearch_mock.assert_called_with([{
'host': 'localhost',
'port': 443,
'use_ssl': True,
'http_auth': None
}])

def test_http_url_with_basic_auth(self):
@mock.patch('elasticsearch.Elasticsearch')
def test_http_url_with_basic_auth(self, elasticsearch_mock):
new_indexer = indexer.ElasticSearchIndexer(['http://admin:password@localhost'], 'daybed_')
self.assertEqual(new_indexer.built_hosts, [{
elasticsearch_mock.assert_called_with([{
'host': 'localhost',
'port': 80,
'use_ssl': False,
'http_auth': 'admin:password'
}])

def test_https_url_with_basic_auth(self):
@mock.patch('elasticsearch.Elasticsearch')
def test_https_url_with_basic_auth(self, elasticsearch_mock):
new_indexer = indexer.ElasticSearchIndexer(['https://admin:password@localhost'], 'daybed_')
self.assertEqual(new_indexer.built_hosts, [{
elasticsearch_mock.assert_called_with([{
'host': 'localhost',
'port': 443,
'use_ssl': True,
Expand Down

0 comments on commit 8edda0c

Please sign in to comment.