Skip to content
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
40 changes: 26 additions & 14 deletions connector_elasticsearch/components/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging

from odoo import _, exceptions
from odoo.exceptions import UserError

from odoo.addons.component.core import Component

Expand Down Expand Up @@ -37,20 +38,31 @@ def _es_connection_class(self):

def _get_es_client(self):
backend = self.backend_record
api_key = (
(backend.api_key_id, backend.api_key)
if backend.api_key_id and backend.api_key
else None
)
if not backend.es_server_host:
raise exceptions.UserError(_("No ElasticSearch host defined"))
# UserError to be consistent with
# se_backend_elasticsearch.py
return elasticsearch.Elasticsearch(
[backend.es_server_host],
connection_class=self._es_connection_class,
api_key=api_key,
)

if backend.is_http_authentication:
if backend.es_user and backend.es_password:
auth = (backend.es_user, backend.es_password)
es = elasticsearch.Elasticsearch(
[backend.es_server_host], http_auth=auth
)
else:
es = elasticsearch.Elasticsearch([backend.es_server_host])

if not es.ping(): # pragma: no cover
raise UserError(_("Connect Exception with elasticsearch"))

return es
else:
api_key = (
(backend.api_key_id, backend.api_key)
if backend.api_key_id and backend.api_key
else None
)
return elasticsearch.Elasticsearch(
[backend.es_server_host],
connection_class=self._es_connection_class,
api_key=api_key,
)

def index(self, records):
es = self._get_es_client()
Expand Down
33 changes: 21 additions & 12 deletions connector_elasticsearch/models/se_backend_elasticsearch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright 2019 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from elasticsearch import AuthenticationException, NotFoundError

from odoo import _, fields, models
Expand Down Expand Up @@ -28,29 +27,39 @@ class SeBackendElasticsearch(models.Model):
tech_name = fields.Char(
related="se_backend_id.tech_name", store=True, readonly=False
)

is_http_authentication = fields.Boolean(
string="use http authentication", default=False
)

api_key_id = fields.Char(help="Elasticsearch Api Key ID", string="Api Key ID")
api_key = fields.Char(help="Elasticsearch Api Key")

es_user = fields.Char(help="Leave blank if not using http authentication.")
es_password = fields.Char(help="Leave blank if not using http authentication.")

@property
def _server_env_fields(self):
env_fields = super()._server_env_fields
env_fields.update({"es_server_host": {}})
env_fields.update({"es_server_host": {}, "es_user": {}, "es_password": {}})
return env_fields

def action_test_connection(self):
with self.specific_backend.work_on(self._name) as work:
adapter = work.component(usage="se.backend.adapter")
es = adapter._get_es_client()
try:
es.security.authenticate()
except NotFoundError:
raise UserError(_("Unable to reach host."))
except AuthenticationException:
raise UserError(_("Unable to authenticate. Check credentials."))
except Exception as e:
raise UserError(
_("Unable to connect to ElasticSearch:") + "\n\n" + repr(e)
)

if not self.is_http_authentication:
try:
es.security.authenticate()
except NotFoundError:
raise UserError(_("Unable to reach host."))
except AuthenticationException:
raise UserError(_("Unable to authenticate. Check credentials."))
except Exception as e:
raise UserError(
_("Unable to connect to ElasticSearch:") + "\n\n" + repr(e)
)
return {
"type": "ir.actions.client",
"tag": "display_notification",
Expand Down
20 changes: 18 additions & 2 deletions connector_elasticsearch/views/se_backend_elasticsearch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,24 @@
string="Host"
placeholder="http://elastic:9200"
/>
<field name="api_key_id" />
<field name="api_key" />
<field name="is_http_authentication" />
<field
name="es_user"
attrs="{'invisible':[('is_http_authentication','=',False)]}"
/>
<field
name="es_password"
password="True"
attrs="{'invisible':[('is_http_authentication','=',False)]}"
/>
<field
name="api_key_id"
attrs="{'invisible':[('is_http_authentication','=',True)]}"
/>
<field
name="api_key"
attrs="{'invisible':[('is_http_authentication','=',True)]}"
/>
</group>
<group name="se-main" position="after">
<group name="se-buttons" colspan="4" col="1">
Expand Down