Skip to content

Commit 6f40df7

Browse files
[ADD] opensearch support
1 parent 8e74398 commit 6f40df7

File tree

3 files changed

+66
-25
lines changed

3 files changed

+66
-25
lines changed

connector_elasticsearch/components/adapter.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import logging
55

6-
from odoo import exceptions
6+
from odoo import _, exceptions
7+
from odoo.exceptions import UserError
78

89
from odoo.addons.component.core import Component
910

@@ -37,16 +38,31 @@ def _es_connection_class(self):
3738

3839
def _get_es_client(self):
3940
backend = self.backend_record
40-
api_key = (
41-
(backend.api_key_id, backend.api_key)
42-
if backend.api_key_id and backend.api_key
43-
else None
44-
)
45-
return elasticsearch.Elasticsearch(
46-
[backend.es_server_host],
47-
connection_class=self._es_connection_class,
48-
api_key=api_key,
49-
)
41+
42+
if backend.is_http_authentification:
43+
if backend.es_user and backend.es_password:
44+
auth = (backend.es_user, backend.es_password)
45+
es = elasticsearch.Elasticsearch(
46+
[backend.es_server_host], http_auth=auth
47+
)
48+
else:
49+
es = elasticsearch.Elasticsearch([backend.es_server_host])
50+
51+
if not es.ping(): # pragma: no cover
52+
raise UserError(_("Connect Exception with elasticsearch"))
53+
54+
return es
55+
else:
56+
api_key = (
57+
(backend.api_key_id, backend.api_key)
58+
if backend.api_key_id and backend.api_key
59+
else None
60+
)
61+
return elasticsearch.Elasticsearch(
62+
[backend.es_server_host],
63+
connection_class=self._es_connection_class,
64+
api_key=api_key,
65+
)
5066

5167
def index(self, records):
5268
es = self._get_es_client()

connector_elasticsearch/models/se_backend_elasticsearch.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Copyright 2019 ACSONE SA/NV
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3-
43
from elasticsearch import AuthenticationException, NotFoundError
54

65
from odoo import _, fields, models
@@ -28,29 +27,39 @@ class SeBackendElasticsearch(models.Model):
2827
tech_name = fields.Char(
2928
related="se_backend_id.tech_name", store=True, readonly=False
3029
)
30+
31+
is_http_authentification = fields.Boolean(
32+
string="use http authentification", default=False
33+
)
34+
3135
api_key_id = fields.Char(help="Elasticsearch Api Key ID", string="Api Key ID")
3236
api_key = fields.Char(help="Elasticsearch Api Key")
3337

38+
es_user = fields.Char(help="Leave blank if not using http authentication.")
39+
es_password = fields.Char(help="Leave blank if not using http authentication.")
40+
3441
@property
3542
def _server_env_fields(self):
3643
env_fields = super()._server_env_fields
37-
env_fields.update({"es_server_host": {}})
44+
env_fields.update({"es_server_host": {}, "es_user": {}, "es_password": {}})
3845
return env_fields
3946

4047
def action_test_connection(self):
4148
with self.specific_backend.work_on(self._name) as work:
4249
adapter = work.component(usage="se.backend.adapter")
4350
es = adapter._get_es_client()
44-
try:
45-
es.security.authenticate()
46-
except NotFoundError:
47-
raise UserError(_("Unable to reach host."))
48-
except AuthenticationException:
49-
raise UserError(_("Unable to authenticate. Check credentials."))
50-
except Exception as e:
51-
raise UserError(
52-
_("Unable to connect to ElasticSearch:") + "\n\n" + repr(e)
53-
)
51+
52+
if not self.is_http_authentification:
53+
try:
54+
es.security.authenticate()
55+
except NotFoundError:
56+
raise UserError(_("Unable to reach host."))
57+
except AuthenticationException:
58+
raise UserError(_("Unable to authenticate. Check credentials."))
59+
except Exception as e:
60+
raise UserError(
61+
_("Unable to connect to ElasticSearch:") + "\n\n" + repr(e)
62+
)
5463
return {
5564
"type": "ir.actions.client",
5665
"tag": "display_notification",

connector_elasticsearch/views/se_backend_elasticsearch.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,24 @@
1818
string="Host"
1919
placeholder="http://elastic:9200"
2020
/>
21-
<field name="api_key_id" />
22-
<field name="api_key" />
21+
<field name="is_http_authentification" />
22+
<field
23+
name="es_user"
24+
attrs="{'invisible':[('is_http_authentification','=',False)]}"
25+
/>
26+
<field
27+
name="es_password"
28+
password="True"
29+
attrs="{'invisible':[('is_http_authentification','=',False)]}"
30+
/>
31+
<field
32+
name="api_key_id"
33+
attrs="{'invisible':[('is_http_authentification','=',True)]}"
34+
/>
35+
<field
36+
name="api_key"
37+
attrs="{'invisible':[('is_http_authentification','=',True)]}"
38+
/>
2339
</group>
2440
<group name="se-main" position="after">
2541
<group name="se-buttons" colspan="4" col="1">

0 commit comments

Comments
 (0)