diff --git a/tools/index_metadata.py b/tools/index_metadata.py new file mode 100644 index 00000000..83287ce0 --- /dev/null +++ b/tools/index_metadata.py @@ -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='', + 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='', + default='test-index', + help="OpenSearch / ElasticSearch index name.\n" + 'Default: test-index' + ) + parser.add_argument( + '--username', + metavar='', + help='Username for the connection.' + ) + parser.add_argument( + '--password', + metavar='', + 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) \ No newline at end of file