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 1 commit
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
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ tests-failfast: install-dev

serve: install install-dev
$(VENV)/bin/pserve conf/development.ini --reload

.PHONY: spaceleft
spaceleft:
@if which grin 2>&1 >/dev/null; \
then \
test "$$(grin " $$" daybed/ docs/ -l | wc -l)" -ne "0" && \
grin -l " $$" daybed/ docs/ | xargs sed -i 's/\s*$$//' || exit 0; \
fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really see the point of this here, you could define it in your shell instead, no ?
(.phony is repeated, is that ok ?)

42 changes: 41 additions & 1 deletion daybed/indexer.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 six.moves.urllib.parse import urlparse


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

def __init__(self, hosts, prefix):
self.client = elasticsearch.Elasticsearch(hosts)
self.built_hosts = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you store it as attribute ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was to test it but I suppose I can mock the Elasticsearch constructor?


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
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there any python lib that could handle this for us?
If not, I suggest you could at least move this piece of code to some utility function


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

def search(self, model_id, query, params):
Expand Down
49 changes: 48 additions & 1 deletion daybed/tests/test_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,57 @@
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):

def test_default_config(self):
new_indexer = indexer.ElasticSearchIndexer(['localhost:9200'], 'daybed_')
self.assertEqual(new_indexer.built_hosts, ['localhost:9200'])

def test_default_port(self):
new_indexer = indexer.ElasticSearchIndexer(['localhost'], 'daybed_')
self.assertEqual(new_indexer.built_hosts, ['localhost'])

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

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

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

def test_https_url_with_basic_auth(self):
new_indexer = indexer.ElasticSearchIndexer(['https://admin:password@localhost'], 'daybed_')
self.assertEqual(new_indexer.built_hosts, [{
'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