Skip to content
This repository was archived by the owner on Aug 2, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion daybed/indexer.py → daybed/indexer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from elasticsearch.exceptions import RequestError, ElasticsearchException

from daybed import logger
from .utils import build_elasticsearch_hosts


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

def __init__(self, hosts, prefix):
self.client = elasticsearch.Elasticsearch(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
52 changes: 52 additions & 0 deletions daybed/indexer/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- 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
57 changes: 56 additions & 1 deletion daybed/tests/test_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,65 @@
from daybed.schemas import registry
from daybed import indexer

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


class ConfigurationTest(unittest.TestCase):

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

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

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

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

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

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


class ModelsIndicesTest(BaseWebTest):

@mock.patch('elasticsearch.client.indices.IndicesClient.create')
Expand Down
6 changes: 3 additions & 3 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ access the endpoint using Basic Auth Authorization scheme::

{
"credentials": {
"algorithm": "sha256",
"id": "371ef18f8a054e5c9fb0961cc5b81006080e9ad22078d30b3727c7c32843579f",
"algorithm": "sha256",
"id": "371ef18f8a054e5c9fb0961cc5b81006080e9ad22078d30b3727c7c32843579f",
"key": "87e72a8e5dcaf8b4be00b8729462814da3d438ce2fd8b6efee335db722d8369d"
},
},
"token": "9f19de0237c9bd59f803de1785f7aea4e3499b6929df3428e1b415fed81f797a"
}

Expand Down