brynq-sdk-elastic 2.0.1__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 1.0
2
2
  Name: brynq_sdk_elastic
3
- Version: 2.0.1
3
+ Version: 2.2.0
4
4
  Summary: elastic wrapper from BrynQ
5
5
  Home-page: UNKNOWN
6
6
  Author: BrynQ
@@ -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
- elasticsearch_host = os.getenv("ELASTIC_HOST", "localhost")
20
- elasticsearch_port = os.getenv("ELASTIC_PORT", "9200")
21
- kibana_port = os.getenv("KIBANA_PORT", "5601")
22
- elastic_token = os.getenv('ELASTIC_API_KEY', None)
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 os.getenv("ELASTIC_HOST") is None:
33
- warnings.warn("Environment variable ELASTIC_HOST is not set. Using default: localhost")
34
- if os.getenv("KIBANA_PORT") is None:
35
- warnings.warn("Environment variable ELASTIC_PORT is not set. Using default: 9200")
36
- if os.getenv("KIBANA_PORT") is None:
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 os.getenv("ELASTIC_SPACE") is None:
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.get_health()
58
- self.create_space(space_name=self.space_name)
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,6 +71,9 @@ class Elastic:
65
71
  :return: if the connection is established or not
66
72
  """
67
73
  # Get the health of the database connection
74
+ if self.disabled:
75
+ return 'Healthy connection established with elasticsearch!'
76
+
68
77
  try:
69
78
  health = requests.get(url=f'{self.elasticsearch_host}/_cat/health?', headers=self.elastic_headers, verify=self.verify).status_code
70
79
  if health != 200:
@@ -87,6 +96,9 @@ class Elastic:
87
96
  :param space_name: The name of the space
88
97
  :return: The status of the creation of the space
89
98
  """
99
+ if self.disabled:
100
+ return 'Space creation disabled'
101
+
90
102
  url = f'{self.kibana_host}/api/spaces/space'
91
103
  data = {
92
104
  "id": space_name,
@@ -116,6 +128,9 @@ class Elastic:
116
128
  :param time_field: The name of the time field
117
129
  :return: The status of the creation of the data view
118
130
  """
131
+ if self.disabled:
132
+ return 'Data view creation disabled'
133
+
119
134
  url = f'{self.kibana_host}/s/{space_name}/api/data_views/data_view'
120
135
  data = {
121
136
  "data_view": {
@@ -143,6 +158,9 @@ class Elastic:
143
158
  :param index: the name of the index
144
159
  :return: The response of the request to elasticsearch
145
160
  """
161
+ if self.disabled:
162
+ return pd.DataFrame()
163
+
146
164
  size = 10000
147
165
 
148
166
  # Get all indices with the given index from the function parameter. For each day a new index.
@@ -185,6 +203,9 @@ class Elastic:
185
203
  :param index_name: The index you want to delete
186
204
  :return: The response of the request to elasticsearch
187
205
  """
206
+ if self.disabled:
207
+ return 'Index deletion disabled'
208
+
188
209
  # Check if index exists
189
210
  url = f'{self.elasticsearch_host}/{index_name}'
190
211
  response = requests.head(url=url, headers=self.elastic_headers, verify=self.verify)
@@ -205,6 +226,9 @@ class Elastic:
205
226
  :param index_name: The name of the desired index
206
227
  :return: The response of the request to elasticsearch
207
228
  """
229
+ if self.disabled:
230
+ return 'Index creation disabled'
231
+
208
232
  url = f'{self.elasticsearch_host}/{index_name}'
209
233
  response = requests.head(url=url, headers=self.elastic_headers, verify=self.verify)
210
234
 
@@ -224,6 +248,9 @@ class Elastic:
224
248
  :param index: one or more index names in a list.
225
249
  :return: The response of the request to elasticsearch
226
250
  """
251
+ if self.disabled:
252
+ return 'Role creation disabled'
253
+
227
254
  url = f'{self.kibana_host}/api/security/role/{role_name}'
228
255
  # Set the body
229
256
  body = {
@@ -265,6 +292,9 @@ class Elastic:
265
292
  Get all the indices in the elasticsearch instance
266
293
  :return: A dictionary with all the indices
267
294
  """
295
+ if self.disabled:
296
+ return {}
297
+
268
298
  indices = requests.get(url=f'{self.elasticsearch_host}/_cat/indices?format=json', headers=self.elastic_headers, verify=self.verify).json()
269
299
  return indices
270
300
 
@@ -277,6 +307,9 @@ class Elastic:
277
307
  :param roles: Give the roles to which the user belongs in a list. Most often the same role_name as the user_name
278
308
  :return: The response of the request to elasticsearch
279
309
  """
310
+ if self.disabled:
311
+ return 'User creation disabled'
312
+
280
313
  url = f'{self.elasticsearch_host}/_security/user/{user_name}'
281
314
  body = {
282
315
  'password': f'{password}',
@@ -303,6 +336,9 @@ class Elastic:
303
336
  :param document: The document to be posted
304
337
  :return: The response of the request to elasticsearch
305
338
  """
339
+ if self.disabled:
340
+ return None
341
+
306
342
  url = f'{self.elasticsearch_host}/{index_name}/_doc/'
307
343
  body = json.dumps(document)
308
344
  response = requests.post(url=url, data=body, headers=self.elastic_headers, verify=self.verify)
@@ -315,6 +351,9 @@ class Elastic:
315
351
  :param document_id: The id of the document to be retrieved
316
352
  :return: The response of the request to elasticsearch
317
353
  """
354
+ if self.disabled:
355
+ return None
356
+
318
357
  url = f'{self.elasticsearch_host}/{index_name}/_doc/{document_id}'
319
358
  response = requests.get(url=url, headers=self.elastic_headers, verify=self.verify)
320
359
  return response
@@ -326,6 +365,9 @@ class Elastic:
326
365
  :param document_id: The id of the document to be deleted
327
366
  :return: The response of the request to elasticsearch
328
367
  """
368
+ if self.disabled:
369
+ return None
370
+
329
371
  url = f'{self.elasticsearch_host}/{index_name}/_doc/{document_id}'
330
372
  response = requests.delete(url=url, headers=self.elastic_headers, verify=self.verify)
331
373
  return response
@@ -336,6 +378,9 @@ class Elastic:
336
378
  :param information: the information to be inserted into the database.
337
379
  :return: the response of the post request
338
380
  """
381
+ if self.disabled:
382
+ return None
383
+
339
384
  # Add new document
340
385
  url = f'{self.elasticsearch_host}/task_execution_log_{self.client_user}/_doc/'
341
386
  body = json.dumps(information)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 1.0
2
2
  Name: brynq-sdk-elastic
3
- Version: 2.0.1
3
+ Version: 2.2.0
4
4
  Summary: elastic wrapper from BrynQ
5
5
  Home-page: UNKNOWN
6
6
  Author: BrynQ
@@ -3,7 +3,7 @@ from setuptools import setup
3
3
 
4
4
  setup(
5
5
  name='brynq_sdk_elastic',
6
- version='2.0.1',
6
+ version='2.2.0',
7
7
  description='elastic wrapper from BrynQ',
8
8
  long_description='elastic wrapper from BrynQ',
9
9
  author='BrynQ',