New file
Some checks failed
gl/check check status: failure (afa9936eded5c8206ab81824aac4520fc69efaef)
Some checks failed
gl/check check status: failure (afa9936eded5c8206ab81824aac4520fc69efaef)
This commit is contained in:
parent
c39be95a60
commit
afa9936ede
98
tools/index_metadata.py
Normal file
98
tools/index_metadata.py
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
import otc_metadata
|
||||||
|
import argparse
|
||||||
|
import logging
|
||||||
|
from opensearchpy import helpers as os_helpers
|
||||||
|
from opensearchpy import OpenSearch
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
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>',
|
||||||
|
help='Username for the connection.'
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--password',
|
||||||
|
metavar='<password>',
|
||||||
|
help='Password for the connection.'
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
|
data = getData(
|
||||||
|
environment=args.target_environment
|
||||||
|
)
|
||||||
|
|
||||||
|
indexedData = indexData(
|
||||||
|
deleteIndex=args.delete_index,
|
||||||
|
hosts=args.hosts,
|
||||||
|
index=args.index,
|
||||||
|
username=args.username,
|
||||||
|
password=args.password,
|
||||||
|
data=data
|
||||||
|
)
|
||||||
|
|
||||||
|
def getData(environment):
|
||||||
|
metadata = otc_metadata.Services()
|
||||||
|
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 == True:
|
||||||
|
client.indices.delete(index=index, ignore=[400, 404])
|
||||||
|
|
||||||
|
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)
|
Loading…
x
Reference in New Issue
Block a user