Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] opensearch support #143

Open
wants to merge 1 commit into
base: 14.0
Choose a base branch
from
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 _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)
xavier-bouquiaux marked this conversation as resolved.
Show resolved Hide resolved
es = elasticsearch.Elasticsearch(

Check warning on line 45 in connector_elasticsearch/components/adapter.py

View check run for this annotation

Codecov / codecov/patch

connector_elasticsearch/components/adapter.py#L44-L45

Added lines #L44 - L45 were not covered by tests
[backend.es_server_host], http_auth=auth
)
else:
es = elasticsearch.Elasticsearch([backend.es_server_host])

Check warning on line 49 in connector_elasticsearch/components/adapter.py

View check run for this annotation

Codecov / codecov/patch

connector_elasticsearch/components/adapter.py#L49

Added line #L49 was not covered by tests

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

return es

Check warning on line 54 in connector_elasticsearch/components/adapter.py

View check run for this annotation

Codecov / codecov/patch

connector_elasticsearch/components/adapter.py#L54

Added line #L54 was not covered by tests
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 @@
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()

Check warning on line 54 in connector_elasticsearch/models/se_backend_elasticsearch.py

View check run for this annotation

Codecov / codecov/patch

connector_elasticsearch/models/se_backend_elasticsearch.py#L53-L54

Added lines #L53 - L54 were not covered by tests
except NotFoundError:
raise UserError(_("Unable to reach host."))

Check warning on line 56 in connector_elasticsearch/models/se_backend_elasticsearch.py

View check run for this annotation

Codecov / codecov/patch

connector_elasticsearch/models/se_backend_elasticsearch.py#L56

Added line #L56 was not covered by tests
except AuthenticationException:
raise UserError(_("Unable to authenticate. Check credentials."))
except Exception as e:
raise UserError(

Check warning on line 60 in connector_elasticsearch/models/se_backend_elasticsearch.py

View check run for this annotation

Codecov / codecov/patch

connector_elasticsearch/models/se_backend_elasticsearch.py#L58-L60

Added lines #L58 - L60 were not covered by tests
_("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
Loading