From fae87abe097c74e5075ac22393f0d87eeffbdd01 Mon Sep 17 00:00:00 2001 From: PALANSA Date: Thu, 18 Jan 2024 16:52:29 +0530 Subject: [PATCH] test --- elastic_handler.py | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/elastic_handler.py b/elastic_handler.py index d71b851..83e3d14 100644 --- a/elastic_handler.py +++ b/elastic_handler.py @@ -7,51 +7,51 @@ from opensearchpy import OpenSearch from opensearchpy.helpers import bulk -ELASTIC_HOST = os.environ.get("INPUT_OPENSEARCH_HOST") -ELASTIC_API_KEY_ID = os.environ.get("INPUT_OPENSEARCH_USERNAME") -ELASTIC_API_KEY = os.environ.get("INPUT_OPENSEARCH_PASSWORD") -ELASTIC_INDEX = os.environ.get("INPUT_OPENSEARCH_INDEX") +OPENSEARCH_HOST = os.environ.get("INPUT_OPENSEARCH_HOST") +OPENSEARCH_USERNAME = os.environ.get("INPUT_OPENSEARCH_USERNAME") +OPENSEARCH_PASSWORD = os.environ.get("INPUT_OPENSEARCH_PASSWORD") +OPENSEARCH_INDEX = os.environ.get("INPUT_OPENSEARCH_INDEX") try: - assert ELASTIC_HOST not in (None, '') + assert OPENSEARCH_HOST not in (None, '') except: - output = "The input ELASTIC_HOST is not set" + output = "The input OPENSEARCH_HOST is not set" print(f"Error: {output}") sys.exit(-1) try: - assert ELASTIC_API_KEY_ID not in (None, '') + assert OPENSEARCH_USERNAME not in (None, '') except: - output = "The input ELASTIC_API_KEY_ID is not set" + output = "The input OPENSEARCH_USERNAME is not set" print(f"Error: {output}") sys.exit(-1) try: - assert ELASTIC_API_KEY not in (None, '') + assert OPENSEARCH_PASSWORD not in (None, '') except: - output = "The input ELASTIC_API_KEY is not set" + output = "The input OPENSEARCH_PASSWORD is not set" print(f"Error: {output}") sys.exit(-1) try: - assert ELASTIC_INDEX not in (None, '') + assert OPENSEARCH_INDEX not in (None, '') now = datetime.datetime.now() - elastic_index = f"{ELASTIC_INDEX}-{now.month}-{now.day}" + opensearch_index = f"{OPENSEARCH_INDEX}-{now.month}-{now.day}" except: - output = "The input ELASTIC_INDEX is not set" + output = "The input OPENSEARCH_INDEX is not set" print(f"Error: {output}") sys.exit(-1) try: es = OpenSearch( - [ELASTIC_HOST], - http_auth=(ELASTIC_API_KEY_ID, ELASTIC_API_KEY), + [OPENSEARCH_HOST], + http_auth=(OPENSEARCH_USERNAME, OPENSEARCH_PASSWORD), use_ssl=True, verify_certs=False, ssl_show_warn=False, ) -except elasticsearch.exceptions.AuthorizationException as exc: - output = "Authentication to elastic failed" +except opensearch.exceptions.AuthorizationException as exc: + output = "Authentication to opensearch failed" print(f"Error: {output}") sys.exit(-1) @@ -67,18 +67,18 @@ def emit(self, record): record_dict = record.__dict__ record_dict["@timestamp"] = int(record_dict.pop("created") * 1000) self.buffer.append({ - "_index": elastic_index, + "_index": opensearch_index, **record_dict }) except ValueError as e: - output = f"Error inserting to Elastic {str(e)}" + output = f"Error inserting to Opensearch {str(e)}" print(f"Error: {output}") print(f"::set-output name=result::{output}") return def flush(self): # if the index is not exist, create it with mapping: - if not es.indices.exists(index=elastic_index): + if not es.indices.exists(index=opensearch_index): mapping = ''' { "mappings":{ @@ -90,8 +90,8 @@ def flush(self): } } }''' - es.indices.create(index=elastic_index, body=mapping) - # commit the logs to elastic + es.indices.create(index=opensearch_index, body=mapping) + # commit the logs to opensearch bulk( client=es, actions=self.buffer