brynq-sdk-elastic 2.2.0__tar.gz → 2.2.2__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.2.0 → brynq_sdk_elastic-2.2.2}/PKG-INFO +1 -1
- {brynq_sdk_elastic-2.2.0 → brynq_sdk_elastic-2.2.2}/brynq_sdk/elastic/elastic.py +92 -76
- {brynq_sdk_elastic-2.2.0 → brynq_sdk_elastic-2.2.2}/brynq_sdk_elastic.egg-info/PKG-INFO +1 -1
- {brynq_sdk_elastic-2.2.0 → brynq_sdk_elastic-2.2.2}/setup.py +1 -1
- {brynq_sdk_elastic-2.2.0 → brynq_sdk_elastic-2.2.2}/brynq_sdk/elastic/__init__.py +0 -0
- {brynq_sdk_elastic-2.2.0 → brynq_sdk_elastic-2.2.2}/brynq_sdk_elastic.egg-info/SOURCES.txt +0 -0
- {brynq_sdk_elastic-2.2.0 → brynq_sdk_elastic-2.2.2}/brynq_sdk_elastic.egg-info/dependency_links.txt +0 -0
- {brynq_sdk_elastic-2.2.0 → brynq_sdk_elastic-2.2.2}/brynq_sdk_elastic.egg-info/not-zip-safe +0 -0
- {brynq_sdk_elastic-2.2.0 → brynq_sdk_elastic-2.2.2}/brynq_sdk_elastic.egg-info/requires.txt +0 -0
- {brynq_sdk_elastic-2.2.0 → brynq_sdk_elastic-2.2.2}/brynq_sdk_elastic.egg-info/top_level.txt +0 -0
- {brynq_sdk_elastic-2.2.0 → brynq_sdk_elastic-2.2.2}/setup.cfg +0 -0
|
@@ -17,6 +17,7 @@ class Elastic:
|
|
|
17
17
|
try:
|
|
18
18
|
self.verify = False
|
|
19
19
|
self.disabled = disabled
|
|
20
|
+
self.timeout = 60 # seconds before request to elastic fails
|
|
20
21
|
elasticsearch_host = os.getenv("ELASTIC_HOST")
|
|
21
22
|
elasticsearch_port = os.getenv("ELASTIC_PORT")
|
|
22
23
|
kibana_port = os.getenv("KIBANA_PORT")
|
|
@@ -75,7 +76,7 @@ class Elastic:
|
|
|
75
76
|
return 'Healthy connection established with elasticsearch!'
|
|
76
77
|
|
|
77
78
|
try:
|
|
78
|
-
health = requests.get(url=f'{self.elasticsearch_host}/_cat/health?', headers=self.elastic_headers, verify=self.verify).status_code
|
|
79
|
+
health = requests.get(url=f'{self.elasticsearch_host}/_cat/health?', headers=self.elastic_headers, verify=self.verify, timeout=self.timeout).status_code
|
|
79
80
|
if health != 200:
|
|
80
81
|
raise ConnectionError(f"Elasticsearch cluster health check failed with status code: {health}")
|
|
81
82
|
else:
|
|
@@ -96,29 +97,34 @@ class Elastic:
|
|
|
96
97
|
:param space_name: The name of the space
|
|
97
98
|
:return: The status of the creation of the space
|
|
98
99
|
"""
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
100
|
+
try:
|
|
101
|
+
if self.disabled:
|
|
102
|
+
return 'Space creation disabled'
|
|
103
|
+
|
|
104
|
+
url = f'{self.kibana_host}/api/spaces/space'
|
|
105
|
+
data = {
|
|
106
|
+
"id": space_name,
|
|
107
|
+
"name": space_name,
|
|
108
|
+
"description": f"This is the space for {space_name}",
|
|
109
|
+
"color": "#aabbcc",
|
|
110
|
+
"initials": space_name[0:2].upper(),
|
|
111
|
+
"disabledFeatures": [],
|
|
112
|
+
}
|
|
111
113
|
|
|
112
|
-
|
|
114
|
+
response = requests.head(url=url + fr'/{space_name}', headers=self.kibana_headers, verify=self.verify)
|
|
113
115
|
|
|
114
|
-
if response.status_code == 200:
|
|
115
|
-
return f'Index \'{space_name}\' already exists'
|
|
116
|
-
else:
|
|
117
|
-
response = requests.post(url=url, headers=self.kibana_headers, data=json.dumps(data), verify=self.verify)
|
|
118
116
|
if response.status_code == 200:
|
|
119
|
-
return f'
|
|
117
|
+
return f'Index \'{space_name}\' already exists'
|
|
120
118
|
else:
|
|
121
|
-
|
|
119
|
+
response = requests.post(url=url, headers=self.kibana_headers, data=json.dumps(data), verify=self.verify)
|
|
120
|
+
if response.status_code == 200:
|
|
121
|
+
return f'space {space_name} created'
|
|
122
|
+
else:
|
|
123
|
+
raise ConnectionError(f'Could not create space {space_name} with status code: {response.status_code}. Response: {response.text}')
|
|
124
|
+
except:
|
|
125
|
+
message = "Could not create space, since this is not strictly necessary to write logs, continue without it"
|
|
126
|
+
print(message)
|
|
127
|
+
return message
|
|
122
128
|
|
|
123
129
|
def create_data_view(self, space_name: str, view_name: str, name: str, time_field: str) -> str:
|
|
124
130
|
"""
|
|
@@ -128,29 +134,34 @@ class Elastic:
|
|
|
128
134
|
:param time_field: The name of the time field
|
|
129
135
|
:return: The status of the creation of the data view
|
|
130
136
|
"""
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
"
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
137
|
+
try:
|
|
138
|
+
if self.disabled:
|
|
139
|
+
return 'Data view creation disabled'
|
|
140
|
+
|
|
141
|
+
url = f'{self.kibana_host}/s/{space_name}/api/data_views/data_view'
|
|
142
|
+
data = {
|
|
143
|
+
"data_view": {
|
|
144
|
+
"title": f'{view_name}*',
|
|
145
|
+
"id": f'{view_name}',
|
|
146
|
+
"name": f'{name}',
|
|
147
|
+
"timeFieldName": time_field
|
|
148
|
+
}
|
|
141
149
|
}
|
|
142
|
-
}
|
|
143
150
|
|
|
144
|
-
|
|
151
|
+
response = requests.head(url=url + fr'/{view_name}', headers=self.kibana_headers, verify=self.verify)
|
|
145
152
|
|
|
146
|
-
if response.status_code == 200:
|
|
147
|
-
return f'Data view \'{view_name}\' already exists'
|
|
148
|
-
else:
|
|
149
|
-
response = requests.post(url=url, headers=self.kibana_headers, data=json.dumps(data), verify=self.verify)
|
|
150
153
|
if response.status_code == 200:
|
|
151
|
-
return f'
|
|
154
|
+
return f'Data view \'{view_name}\' already exists'
|
|
152
155
|
else:
|
|
153
|
-
|
|
156
|
+
response = requests.post(url=url, headers=self.kibana_headers, data=json.dumps(data), verify=self.verify)
|
|
157
|
+
if response.status_code == 200:
|
|
158
|
+
return f'data view {view_name} created'
|
|
159
|
+
else:
|
|
160
|
+
raise ConnectionError(f'Could not create data view {view_name} with status code: {response.status_code}. Response: {response.text}')
|
|
161
|
+
except:
|
|
162
|
+
message = "Could not create data view, since this is not strictly necessary to write logs, continue without it"
|
|
163
|
+
print(message)
|
|
164
|
+
return message
|
|
154
165
|
|
|
155
166
|
def get_all_docs_from_index(self, index: str) -> pd.DataFrame:
|
|
156
167
|
"""
|
|
@@ -175,7 +186,7 @@ class Elastic:
|
|
|
175
186
|
|
|
176
187
|
# initial request
|
|
177
188
|
params = {"size": size, "scroll": "10m"}
|
|
178
|
-
response = requests.get(url=url, headers=self.elastic_headers, params=params, verify=self.verify).json()
|
|
189
|
+
response = requests.get(url=url, headers=self.elastic_headers, params=params, verify=self.verify, timeout=self.timeout).json()
|
|
179
190
|
|
|
180
191
|
# next requests until finished
|
|
181
192
|
scroll_id = response['_scroll_id']
|
|
@@ -248,44 +259,49 @@ class Elastic:
|
|
|
248
259
|
:param index: one or more index names in a list.
|
|
249
260
|
:return: The response of the request to elasticsearch
|
|
250
261
|
"""
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
'
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
'kibana': [{
|
|
267
|
-
'feature': {
|
|
268
|
-
'dashboard': ['read'],
|
|
269
|
-
'discover': ['read']
|
|
262
|
+
try:
|
|
263
|
+
if self.disabled:
|
|
264
|
+
return 'Role creation disabled'
|
|
265
|
+
|
|
266
|
+
url = f'{self.kibana_host}/api/security/role/{role_name}'
|
|
267
|
+
# Set the body
|
|
268
|
+
body = {
|
|
269
|
+
'elasticsearch': {
|
|
270
|
+
'cluster': ['transport_client'],
|
|
271
|
+
'indices': [
|
|
272
|
+
{
|
|
273
|
+
'names': [index],
|
|
274
|
+
'privileges': ['read', 'write', 'read_cross_cluster', 'view_index_metadata', 'index']
|
|
275
|
+
}
|
|
276
|
+
]
|
|
270
277
|
},
|
|
271
|
-
'
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
278
|
+
'kibana': [{
|
|
279
|
+
'feature': {
|
|
280
|
+
'dashboard': ['read'],
|
|
281
|
+
'discover': ['read']
|
|
282
|
+
},
|
|
283
|
+
'spaces': [role_name],
|
|
284
|
+
}],
|
|
285
|
+
'metadata': {
|
|
286
|
+
'version': 1
|
|
287
|
+
}
|
|
275
288
|
}
|
|
276
|
-
|
|
277
|
-
body = json.dumps(body)
|
|
289
|
+
body = json.dumps(body)
|
|
278
290
|
|
|
279
|
-
|
|
291
|
+
response = requests.head(url=url, headers=self.kibana_headers, verify=self.verify)
|
|
280
292
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
else:
|
|
284
|
-
response = requests.put(url=url, data=body, headers=self.kibana_headers, verify=self.verify)
|
|
285
|
-
if response.status_code == 204:
|
|
286
|
-
return f'Role {role_name} created'
|
|
293
|
+
if response.status_code == 200:
|
|
294
|
+
return f'Role \'{role_name}\' already exists'
|
|
287
295
|
else:
|
|
288
|
-
|
|
296
|
+
response = requests.put(url=url, data=body, headers=self.kibana_headers, verify=self.verify)
|
|
297
|
+
if response.status_code == 204:
|
|
298
|
+
return f'Role {role_name} created'
|
|
299
|
+
else:
|
|
300
|
+
raise ConnectionError(f'Could not create role {role_name} with status code: {response.status_code}. Response: {response.text}')
|
|
301
|
+
except:
|
|
302
|
+
message = "Could not create role, since this is not strictly necessary to write logs, continue without it"
|
|
303
|
+
print(message)
|
|
304
|
+
return message
|
|
289
305
|
|
|
290
306
|
def get_indices(self) -> dict:
|
|
291
307
|
"""
|
|
@@ -341,7 +357,7 @@ class Elastic:
|
|
|
341
357
|
|
|
342
358
|
url = f'{self.elasticsearch_host}/{index_name}/_doc/'
|
|
343
359
|
body = json.dumps(document)
|
|
344
|
-
response = requests.post(url=url, data=body, headers=self.elastic_headers, verify=self.verify)
|
|
360
|
+
response = requests.post(url=url, data=body, headers=self.elastic_headers, verify=self.verify, timeout=self.timeout)
|
|
345
361
|
return response
|
|
346
362
|
|
|
347
363
|
def get_document(self, index_name: str, document_id: str) -> requests.Response:
|
|
@@ -355,7 +371,7 @@ class Elastic:
|
|
|
355
371
|
return None
|
|
356
372
|
|
|
357
373
|
url = f'{self.elasticsearch_host}/{index_name}/_doc/{document_id}'
|
|
358
|
-
response = requests.get(url=url, headers=self.elastic_headers, verify=self.verify)
|
|
374
|
+
response = requests.get(url=url, headers=self.elastic_headers, verify=self.verify, timeout=self.timeout)
|
|
359
375
|
return response
|
|
360
376
|
|
|
361
377
|
def delete_document(self, index_name: str, document_id: str) -> requests.Response:
|
|
@@ -369,7 +385,7 @@ class Elastic:
|
|
|
369
385
|
return None
|
|
370
386
|
|
|
371
387
|
url = f'{self.elasticsearch_host}/{index_name}/_doc/{document_id}'
|
|
372
|
-
response = requests.delete(url=url, headers=self.elastic_headers, verify=self.verify)
|
|
388
|
+
response = requests.delete(url=url, headers=self.elastic_headers, verify=self.verify, timeout=self.timeout)
|
|
373
389
|
return response
|
|
374
390
|
|
|
375
391
|
def task_execution_log(self, information: dict) -> requests.Response:
|
|
@@ -384,7 +400,7 @@ class Elastic:
|
|
|
384
400
|
# Add new document
|
|
385
401
|
url = f'{self.elasticsearch_host}/task_execution_log_{self.client_user}/_doc/'
|
|
386
402
|
body = json.dumps(information)
|
|
387
|
-
response = requests.post(url=url, data=body, headers=self.elastic_headers, verify=self.verify)
|
|
403
|
+
response = requests.post(url=url, data=body, headers=self.elastic_headers, verify=self.verify, timeout=self.timeout)
|
|
388
404
|
return response
|
|
389
405
|
|
|
390
406
|
@staticmethod
|
|
File without changes
|
|
File without changes
|
{brynq_sdk_elastic-2.2.0 → brynq_sdk_elastic-2.2.2}/brynq_sdk_elastic.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{brynq_sdk_elastic-2.2.0 → brynq_sdk_elastic-2.2.2}/brynq_sdk_elastic.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|