"""Opensearch connector.""" import boto3 from opensearchpy import AWSV4SignerAuth, OpenSearch, RequestsHttpConnection from src.configs import opensearch as config def get_client(): """Get OpenSearch client instance. Uses connection variables from the environment defined in config. Returns: mixed: OpenSearch client instance or None if not configured. """ if not (config.OPENSEARCH_HOST and config.OPENSEARCH_PORT): return session = boto3.session.Session() credentials = boto3.Session().get_credentials() auth = AWSV4SignerAuth(credentials, session.region_name, 'es') return OpenSearch( hosts=[{ 'host': config.OPENSEARCH_HOST, 'port': config.OPENSEARCH_PORT, 'timeout': config.OPENSEARCH_DEFAULT_TIMEOUT, }], http_auth=auth, use_ssl=True, verify_certs=True, connection_class=RequestsHttpConnection, pool_maxsize=config.OPENSEARCH_POOL_MAXSIZE ) os_client = get_client()