2525
2626@singleton
2727class ElasticSearchConnectionPool :
28-
2928 def __init__ (self ):
3029 if hasattr (settings , "ES" ):
3130 self .ES_CONFIG = settings .ES
@@ -52,12 +51,23 @@ def __init__(self):
5251 raise Exception (msg )
5352
5453 def _connect (self ):
55- self .es_conn = Elasticsearch (
56- self .ES_CONFIG ["hosts" ].split ("," ),
57- basic_auth = (self .ES_CONFIG ["username" ], self .ES_CONFIG [
58- "password" ]) if "username" in self .ES_CONFIG and "password" in self .ES_CONFIG else None ,
59- verify_certs = self .ES_CONFIG .get ("verify_certs" , False ),
60- timeout = 600 )
54+ if "api_key" in self .ES_CONFIG and self .ES_CONFIG ["api_key" ]:
55+ logging .info ('Elasticsearch connection using API_KEY' )
56+ self .es_conn = Elasticsearch (
57+ self .ES_CONFIG ["hosts" ].split ("," ),
58+ api_key = self .ES_CONFIG ["api_key" ],
59+ verify_certs = self .ES_CONFIG .get ("verify_certs" , False ),
60+ timeout = 600
61+ )
62+ else :
63+ logging .info ('Elasticsearch connection using username/password' )
64+ self .es_conn = Elasticsearch (
65+ self .ES_CONFIG ["hosts" ].split ("," ),
66+ basic_auth = (self .ES_CONFIG ["username" ], self .ES_CONFIG ["password" ]) if "username" in self .ES_CONFIG and "password" in self .ES_CONFIG else None ,
67+ verify_certs = self .ES_CONFIG .get ("verify_certs" , False ),
68+ timeout = 600 ,
69+ )
70+
6171 if self .es_conn :
6272 self .info = self .es_conn .info ()
6373 return True
0 commit comments