Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
sathishkumar-p committed Jan 18, 2024
1 parent b4a2972 commit fae87ab
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions elastic_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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":{
Expand All @@ -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
Expand Down

0 comments on commit fae87ab

Please sign in to comment.