brynq-sdk-elastic 1.0.0__tar.gz → 2.0.1__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: 1.0.0
3
+ Version: 2.0.1
4
4
  Summary: elastic wrapper from BrynQ
5
5
  Home-page: UNKNOWN
6
6
  Author: BrynQ
@@ -1,3 +1,4 @@
1
+ import warnings
1
2
  import requests
2
3
  import json
3
4
  import datetime
@@ -15,17 +16,34 @@ class Elastic:
15
16
  """
16
17
  try:
17
18
  self.verify = False
18
- if os.getenv('BRYNQ_ENVIRONMENT') == 'prod':
19
- self.elasticsearch_host = f'https://{os.getenv("ELASTIC_HOST_LIVE", "localhost")}:{os.getenv("ELASTIC_PORT_LIVE", "9200")}'
20
- self.kibana_host = f'http://{os.getenv("ELASTIC_HOST_LIVE", "localhost")}:{os.getenv("KIBANA_PORT_LIVE", "5601")}'
21
- self.elastic_token = os.getenv('ELASTIC_API_KEY_LIVE', api_key)
22
- else:
23
- self.elasticsearch_host = f'https://{os.getenv("ELASTIC_HOST_STAGING", "localhost")}:{os.getenv("ELASTIC_PORT_STAGING", "9200")}'
24
- self.kibana_host = f'http://{os.getenv("ELASTIC_HOST_STAGING", "localhost")}:{os.getenv("KIBANA_PORT_STAGING", "5601")}'
25
- self.elastic_token = os.getenv('ELASTIC_API_KEY_STAGING', api_key)
26
-
27
- self.client_user = os.getenv('BRYNQ_CUSTOMER_NAME', 'default').lower().replace(' ', '_') if customer_name is None else customer_name.lower().replace(' ', '_')
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)
23
+
24
+ # Build the host URLs
25
+ self.elasticsearch_host = f'https://{elasticsearch_host}:{elasticsearch_port}'
26
+ self.kibana_host = f'http://{elasticsearch_host}:{kibana_port}'
27
+ self.elastic_token = elastic_token if elastic_token is not None else api_key
28
28
  self.space_name = os.getenv('ELASTIC_SPACE', 'default') if space_name is None else space_name
29
+ self.client_user = os.getenv('BRYNQ_SUBDOMAIN', 'default').lower().replace(' ', '_') if customer_name is None else customer_name.lower().replace(' ', '_')
30
+
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
+ if self.client_user == 'default':
43
+ warnings.warn("Environment variable BRYNQ_SUBDOMAIN is not set and customer_name is not specified. Using 'default'")
44
+
45
+ print(f"Elasticsearch running on: {elasticsearch_host}")
46
+
29
47
  self.timestamp = int(datetime.datetime.now().timestamp())
30
48
  self.elastic_headers = {
31
49
  'Content-Type': 'application/json',
@@ -47,11 +65,14 @@ class Elastic:
47
65
  :return: if the connection is established or not
48
66
  """
49
67
  # Get the health of the database connection
50
- health = requests.get(url=f'{self.elasticsearch_host}/_cat/health?', headers=self.elastic_headers, verify=self.verify).status_code
51
- if health != 200:
52
- raise ConnectionError('Elasticsearch cluster health check failed with status code: {}'.format(health))
53
- else:
54
- return 'Healthy connection established with elasticsearch!'
68
+ try:
69
+ health = requests.get(url=f'{self.elasticsearch_host}/_cat/health?', headers=self.elastic_headers, verify=self.verify).status_code
70
+ if health != 200:
71
+ raise ConnectionError(f"Elasticsearch cluster health check failed with status code: {health}")
72
+ else:
73
+ return 'Healthy connection established with elasticsearch!'
74
+ except Exception as e:
75
+ raise ConnectionError(f'Elasticsearch was not reachable, error is: {e}')
55
76
 
56
77
  def initialize_customer(self):
57
78
  # Creates the index for the user if it does not exist yet
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 1.0
2
2
  Name: brynq-sdk-elastic
3
- Version: 1.0.0
3
+ Version: 2.0.1
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='1.0.0',
6
+ version='2.0.1',
7
7
  description='elastic wrapper from BrynQ',
8
8
  long_description='elastic wrapper from BrynQ',
9
9
  author='BrynQ',