Skip to content

Instantly share code, notes, and snippets.

@ishritam
Created August 17, 2020 12:32
Show Gist options
  • Save ishritam/02295837df0e6867bcd206e5d6822897 to your computer and use it in GitHub Desktop.
Save ishritam/02295837df0e6867bcd206e5d6822897 to your computer and use it in GitHub Desktop.
import os, base64, re, logging
from elasticsearch import Elasticsearch
# Log transport details (optional):
logging.basicConfig(level=logging.INFO)
# Parse the auth and host from env:
BONSAI_URL = "Copy the url from the bonsai dashbord's credentials menu"
bonsai = os.environ[BONSAI_URL]
auth = re.search('https\:\/\/(.*)\@', bonsai).group(1).split(':')
host = bonsai.replace('https://%s:%s@' % (auth[0], auth[1]), '')
# optional port
match = re.search('(:\d+)', host)
if match:
p = match.group(0)
host = host.replace(p, '')
port = int(p.split(':')[1])
else:
port=443
# Connect to cluster over SSL using auth for best security:
es_header = [{
'host': host,
'port': port,
'use_ssl': True,
'http_auth': (auth[0],auth[1])
}]
# Instantiate the new Elasticsearch connection:
es = Elasticsearch(es_header)
# Verify that Python can talk to Bonsai (optional):
es.ping()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment