brynq-sdk-elastic 2.0.0__tar.gz → 2.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {brynq_sdk_elastic-2.0.0 → brynq_sdk_elastic-2.2.0}/PKG-INFO +1 -1
- {brynq_sdk_elastic-2.0.0 → brynq_sdk_elastic-2.2.0}/brynq_sdk/elastic/elastic.py +70 -22
- {brynq_sdk_elastic-2.0.0 → brynq_sdk_elastic-2.2.0}/brynq_sdk_elastic.egg-info/PKG-INFO +1 -1
- {brynq_sdk_elastic-2.0.0 → brynq_sdk_elastic-2.2.0}/setup.py +1 -1
- {brynq_sdk_elastic-2.0.0 → brynq_sdk_elastic-2.2.0}/brynq_sdk/elastic/__init__.py +0 -0
- {brynq_sdk_elastic-2.0.0 → brynq_sdk_elastic-2.2.0}/brynq_sdk_elastic.egg-info/SOURCES.txt +0 -0
- {brynq_sdk_elastic-2.0.0 → brynq_sdk_elastic-2.2.0}/brynq_sdk_elastic.egg-info/dependency_links.txt +0 -0
- {brynq_sdk_elastic-2.0.0 → brynq_sdk_elastic-2.2.0}/brynq_sdk_elastic.egg-info/not-zip-safe +0 -0
- {brynq_sdk_elastic-2.0.0 → brynq_sdk_elastic-2.2.0}/brynq_sdk_elastic.egg-info/requires.txt +0 -0
- {brynq_sdk_elastic-2.0.0 → brynq_sdk_elastic-2.2.0}/brynq_sdk_elastic.egg-info/top_level.txt +0 -0
- {brynq_sdk_elastic-2.0.0 → brynq_sdk_elastic-2.2.0}/setup.cfg +0 -0
|
@@ -9,17 +9,33 @@ import os
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
class Elastic:
|
|
12
|
-
def __init__(self, api_key: str = None, customer_name: str = None, space_name: str = None):
|
|
12
|
+
def __init__(self, api_key: str = None, customer_name: str = None, space_name: str = None, disabled: bool = False):
|
|
13
13
|
"""
|
|
14
14
|
A package to create indexes, users, roles, getting data, etc.
|
|
15
15
|
:param api_key: The api key to connect to elasticsearch if not provided in the .env file
|
|
16
16
|
"""
|
|
17
17
|
try:
|
|
18
18
|
self.verify = False
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
self.disabled = disabled
|
|
20
|
+
elasticsearch_host = os.getenv("ELASTIC_HOST")
|
|
21
|
+
elasticsearch_port = os.getenv("ELASTIC_PORT")
|
|
22
|
+
kibana_port = os.getenv("KIBANA_PORT")
|
|
23
|
+
elastic_token = os.getenv('ELASTIC_API_KEY', api_key)
|
|
24
|
+
|
|
25
|
+
if not self.disabled:
|
|
26
|
+
# Check for missing environment variables and show warnings
|
|
27
|
+
if elasticsearch_host is None:
|
|
28
|
+
raise KeyError("Environment variable ELASTIC_HOST is not set. Please set it and try again")
|
|
29
|
+
if elasticsearch_port is None:
|
|
30
|
+
elasticsearch_port = 9200
|
|
31
|
+
warnings.warn("Environment variable ELASTIC_PORT is not set. Using default port 9200")
|
|
32
|
+
if kibana_port is None:
|
|
33
|
+
kibana_port = 5601
|
|
34
|
+
warnings.warn("Environment variable KIBANA_PORT is not set. Using default port 5601")
|
|
35
|
+
if elastic_token is None:
|
|
36
|
+
raise KeyError("Environment variable ELASTIC_API_KEY is not set and no api_key is provided. Please specify either one and try again")
|
|
37
|
+
if os.getenv("ELASTIC_SPACE") is None:
|
|
38
|
+
warnings.warn("Environment variable ELASTIC_SPACE is not set. Using 'default'")
|
|
23
39
|
|
|
24
40
|
# Build the host URLs
|
|
25
41
|
self.elasticsearch_host = f'https://{elasticsearch_host}:{elasticsearch_port}'
|
|
@@ -28,17 +44,6 @@ class Elastic:
|
|
|
28
44
|
self.space_name = os.getenv('ELASTIC_SPACE', 'default') if space_name is None else space_name
|
|
29
45
|
self.client_user = os.getenv('BRYNQ_SUBDOMAIN', 'default').lower().replace(' ', '_') if customer_name is None else customer_name.lower().replace(' ', '_')
|
|
30
46
|
|
|
31
|
-
# Check for missing environment variables and show warnings
|
|
32
|
-
if elasticsearch_host == "localhost":
|
|
33
|
-
warnings.warn("Environment variable ELASTIC_HOST is not set. Using default: localhost")
|
|
34
|
-
if elasticsearch_port == "9200":
|
|
35
|
-
warnings.warn("Environment variable ELASTIC_PORT is not set. Using default: 9200")
|
|
36
|
-
if kibana_port == "5601":
|
|
37
|
-
warnings.warn("Environment variable KIBANA_PORT is not set. Using default: 5601")
|
|
38
|
-
if elastic_token is None:
|
|
39
|
-
raise KeyError("Environment variable ELASTIC_API_KEY is not set and no api_key is provided. Please specify either one and try again")
|
|
40
|
-
if self.space_name == 'default':
|
|
41
|
-
warnings.warn("Environment variable ELASTIC_SPACE is not set. Using 'default'")
|
|
42
47
|
if self.client_user == 'default':
|
|
43
48
|
warnings.warn("Environment variable BRYNQ_SUBDOMAIN is not set and customer_name is not specified. Using 'default'")
|
|
44
49
|
|
|
@@ -54,8 +59,9 @@ class Elastic:
|
|
|
54
59
|
'Authorization': f'ApiKey {self.elastic_token}',
|
|
55
60
|
'kbn-xsrf': 'true'
|
|
56
61
|
}
|
|
57
|
-
self.
|
|
58
|
-
|
|
62
|
+
if not self.disabled:
|
|
63
|
+
self.get_health()
|
|
64
|
+
self.create_space(space_name=self.space_name)
|
|
59
65
|
except Exception as e:
|
|
60
66
|
raise ConnectionError('Could not establish a connection: {}'.format(str(e)))
|
|
61
67
|
|
|
@@ -65,12 +71,18 @@ class Elastic:
|
|
|
65
71
|
:return: if the connection is established or not
|
|
66
72
|
"""
|
|
67
73
|
# Get the health of the database connection
|
|
68
|
-
|
|
69
|
-
if health != 200:
|
|
70
|
-
raise ConnectionError('Elasticsearch cluster health check failed with status code: {}'.format(health))
|
|
71
|
-
else:
|
|
74
|
+
if self.disabled:
|
|
72
75
|
return 'Healthy connection established with elasticsearch!'
|
|
73
76
|
|
|
77
|
+
try:
|
|
78
|
+
health = requests.get(url=f'{self.elasticsearch_host}/_cat/health?', headers=self.elastic_headers, verify=self.verify).status_code
|
|
79
|
+
if health != 200:
|
|
80
|
+
raise ConnectionError(f"Elasticsearch cluster health check failed with status code: {health}")
|
|
81
|
+
else:
|
|
82
|
+
return 'Healthy connection established with elasticsearch!'
|
|
83
|
+
except Exception as e:
|
|
84
|
+
raise ConnectionError(f'Elasticsearch was not reachable, error is: {e}')
|
|
85
|
+
|
|
74
86
|
def initialize_customer(self):
|
|
75
87
|
# Creates the index for the user if it does not exist yet
|
|
76
88
|
self.create_index(index_name=f'task_execution_log_{self.client_user}')
|
|
@@ -84,6 +96,9 @@ class Elastic:
|
|
|
84
96
|
:param space_name: The name of the space
|
|
85
97
|
:return: The status of the creation of the space
|
|
86
98
|
"""
|
|
99
|
+
if self.disabled:
|
|
100
|
+
return 'Space creation disabled'
|
|
101
|
+
|
|
87
102
|
url = f'{self.kibana_host}/api/spaces/space'
|
|
88
103
|
data = {
|
|
89
104
|
"id": space_name,
|
|
@@ -113,6 +128,9 @@ class Elastic:
|
|
|
113
128
|
:param time_field: The name of the time field
|
|
114
129
|
:return: The status of the creation of the data view
|
|
115
130
|
"""
|
|
131
|
+
if self.disabled:
|
|
132
|
+
return 'Data view creation disabled'
|
|
133
|
+
|
|
116
134
|
url = f'{self.kibana_host}/s/{space_name}/api/data_views/data_view'
|
|
117
135
|
data = {
|
|
118
136
|
"data_view": {
|
|
@@ -140,6 +158,9 @@ class Elastic:
|
|
|
140
158
|
:param index: the name of the index
|
|
141
159
|
:return: The response of the request to elasticsearch
|
|
142
160
|
"""
|
|
161
|
+
if self.disabled:
|
|
162
|
+
return pd.DataFrame()
|
|
163
|
+
|
|
143
164
|
size = 10000
|
|
144
165
|
|
|
145
166
|
# Get all indices with the given index from the function parameter. For each day a new index.
|
|
@@ -182,6 +203,9 @@ class Elastic:
|
|
|
182
203
|
:param index_name: The index you want to delete
|
|
183
204
|
:return: The response of the request to elasticsearch
|
|
184
205
|
"""
|
|
206
|
+
if self.disabled:
|
|
207
|
+
return 'Index deletion disabled'
|
|
208
|
+
|
|
185
209
|
# Check if index exists
|
|
186
210
|
url = f'{self.elasticsearch_host}/{index_name}'
|
|
187
211
|
response = requests.head(url=url, headers=self.elastic_headers, verify=self.verify)
|
|
@@ -202,6 +226,9 @@ class Elastic:
|
|
|
202
226
|
:param index_name: The name of the desired index
|
|
203
227
|
:return: The response of the request to elasticsearch
|
|
204
228
|
"""
|
|
229
|
+
if self.disabled:
|
|
230
|
+
return 'Index creation disabled'
|
|
231
|
+
|
|
205
232
|
url = f'{self.elasticsearch_host}/{index_name}'
|
|
206
233
|
response = requests.head(url=url, headers=self.elastic_headers, verify=self.verify)
|
|
207
234
|
|
|
@@ -221,6 +248,9 @@ class Elastic:
|
|
|
221
248
|
:param index: one or more index names in a list.
|
|
222
249
|
:return: The response of the request to elasticsearch
|
|
223
250
|
"""
|
|
251
|
+
if self.disabled:
|
|
252
|
+
return 'Role creation disabled'
|
|
253
|
+
|
|
224
254
|
url = f'{self.kibana_host}/api/security/role/{role_name}'
|
|
225
255
|
# Set the body
|
|
226
256
|
body = {
|
|
@@ -262,6 +292,9 @@ class Elastic:
|
|
|
262
292
|
Get all the indices in the elasticsearch instance
|
|
263
293
|
:return: A dictionary with all the indices
|
|
264
294
|
"""
|
|
295
|
+
if self.disabled:
|
|
296
|
+
return {}
|
|
297
|
+
|
|
265
298
|
indices = requests.get(url=f'{self.elasticsearch_host}/_cat/indices?format=json', headers=self.elastic_headers, verify=self.verify).json()
|
|
266
299
|
return indices
|
|
267
300
|
|
|
@@ -274,6 +307,9 @@ class Elastic:
|
|
|
274
307
|
:param roles: Give the roles to which the user belongs in a list. Most often the same role_name as the user_name
|
|
275
308
|
:return: The response of the request to elasticsearch
|
|
276
309
|
"""
|
|
310
|
+
if self.disabled:
|
|
311
|
+
return 'User creation disabled'
|
|
312
|
+
|
|
277
313
|
url = f'{self.elasticsearch_host}/_security/user/{user_name}'
|
|
278
314
|
body = {
|
|
279
315
|
'password': f'{password}',
|
|
@@ -300,6 +336,9 @@ class Elastic:
|
|
|
300
336
|
:param document: The document to be posted
|
|
301
337
|
:return: The response of the request to elasticsearch
|
|
302
338
|
"""
|
|
339
|
+
if self.disabled:
|
|
340
|
+
return None
|
|
341
|
+
|
|
303
342
|
url = f'{self.elasticsearch_host}/{index_name}/_doc/'
|
|
304
343
|
body = json.dumps(document)
|
|
305
344
|
response = requests.post(url=url, data=body, headers=self.elastic_headers, verify=self.verify)
|
|
@@ -312,6 +351,9 @@ class Elastic:
|
|
|
312
351
|
:param document_id: The id of the document to be retrieved
|
|
313
352
|
:return: The response of the request to elasticsearch
|
|
314
353
|
"""
|
|
354
|
+
if self.disabled:
|
|
355
|
+
return None
|
|
356
|
+
|
|
315
357
|
url = f'{self.elasticsearch_host}/{index_name}/_doc/{document_id}'
|
|
316
358
|
response = requests.get(url=url, headers=self.elastic_headers, verify=self.verify)
|
|
317
359
|
return response
|
|
@@ -323,6 +365,9 @@ class Elastic:
|
|
|
323
365
|
:param document_id: The id of the document to be deleted
|
|
324
366
|
:return: The response of the request to elasticsearch
|
|
325
367
|
"""
|
|
368
|
+
if self.disabled:
|
|
369
|
+
return None
|
|
370
|
+
|
|
326
371
|
url = f'{self.elasticsearch_host}/{index_name}/_doc/{document_id}'
|
|
327
372
|
response = requests.delete(url=url, headers=self.elastic_headers, verify=self.verify)
|
|
328
373
|
return response
|
|
@@ -333,6 +378,9 @@ class Elastic:
|
|
|
333
378
|
:param information: the information to be inserted into the database.
|
|
334
379
|
:return: the response of the post request
|
|
335
380
|
"""
|
|
381
|
+
if self.disabled:
|
|
382
|
+
return None
|
|
383
|
+
|
|
336
384
|
# Add new document
|
|
337
385
|
url = f'{self.elasticsearch_host}/task_execution_log_{self.client_user}/_doc/'
|
|
338
386
|
body = json.dumps(information)
|
|
File without changes
|
|
File without changes
|
{brynq_sdk_elastic-2.0.0 → brynq_sdk_elastic-2.2.0}/brynq_sdk_elastic.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{brynq_sdk_elastic-2.0.0 → brynq_sdk_elastic-2.2.0}/brynq_sdk_elastic.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|