Some checks failed
gl/check check status: failure (f2e01cf1ca558ae8a19e55b014aea6c67ba23af5)
130 lines
3.1 KiB
Python
130 lines
3.1 KiB
Python
import otc_metadata
|
|
import argparse
|
|
import logging
|
|
from opensearchpy import OpenSearch
|
|
import json
|
|
|
|
|
|
metadata = otc_metadata.Services()
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser(
|
|
description="Create Index data for search inside OpenSearch"
|
|
)
|
|
parser.add_argument(
|
|
"--target-environment",
|
|
required=True,
|
|
help="Environment to be used as a source",
|
|
)
|
|
parser.add_argument(
|
|
'--delete-index',
|
|
action='store_true',
|
|
help='Option deletes old index with the same name and creates new '
|
|
'one.'
|
|
)
|
|
parser.add_argument(
|
|
'--hosts',
|
|
metavar='<host:port>',
|
|
nargs='+',
|
|
default=['localhost:9200'],
|
|
help='Provide one or multiple host:port values '
|
|
'separated by space for multiple hosts.\n'
|
|
'Default: localhost:9200'
|
|
)
|
|
parser.add_argument(
|
|
'--index',
|
|
metavar='<index>',
|
|
default='test-index',
|
|
help="OpenSearch / ElasticSearch index name.\n"
|
|
'Default: test-index'
|
|
)
|
|
parser.add_argument(
|
|
'--username',
|
|
metavar='<username>',
|
|
required=True,
|
|
help='Username for the connection.'
|
|
)
|
|
parser.add_argument(
|
|
'--password',
|
|
metavar='<password>',
|
|
required=True,
|
|
help='Password for the connection.'
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
return args
|
|
|
|
|
|
def main():
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
args = parse_args()
|
|
|
|
logging.debug("Obtaining data from otc_metadata")
|
|
data = getData(
|
|
environment=args.target_environment
|
|
)
|
|
|
|
logging.debug("Indexing data into OpenSearch")
|
|
indexData(
|
|
deleteIndex=args.delete_index,
|
|
hosts=args.hosts,
|
|
index=args.index,
|
|
username=args.username,
|
|
password=args.password,
|
|
data=data
|
|
)
|
|
|
|
|
|
def getData(environment):
|
|
return metadata.service_types_with_doc_types(
|
|
environment=environment
|
|
)
|
|
|
|
|
|
def indexData(deleteIndex, hosts, index, username, password, data):
|
|
hosts = generate_os_host_list(hosts)
|
|
client = OpenSearch(
|
|
hosts=hosts,
|
|
http_compress=True,
|
|
http_auth=(username, password),
|
|
use_ssl=True,
|
|
verify_certs=True,
|
|
ssl_assert_hostname=False,
|
|
ssl_show_warn=False
|
|
)
|
|
|
|
if deleteIndex is True:
|
|
logging.debug("Deleting Index")
|
|
delete_index(client, index)
|
|
|
|
logging.debug("Started creating Index")
|
|
create_index(client, index, data)
|
|
logging.debug("Finished creating Index")
|
|
|
|
|
|
def generate_os_host_list(hosts):
|
|
host_list = []
|
|
for host in hosts:
|
|
raw_host = host.split(':')
|
|
if len(raw_host) != 2:
|
|
raise Exception('--hosts parameter does not match the following '
|
|
'format: hostname:port')
|
|
json_host = {'host': raw_host[0], 'port': int(raw_host[1])}
|
|
host_list.append(json_host)
|
|
return host_list
|
|
|
|
|
|
def create_index(client, index, data):
|
|
client.indices.create(index=index)
|
|
return client.index(index=index, body=data)
|
|
|
|
|
|
|
|
def delete_index(client, index):
|
|
return client.indices.delete(index=index, ignore=[400, 404])
|
|
|
|
|
|
main()
|