from Config.config import ElasticConfig
from elasticsearch import Elasticsearch
import json

def connect_elasticsearch():
    # Connect to cluster over SSL using auth for best security:
    es_header = [{
            'host': ElasticConfig.HOSTNAME,
            'port': ElasticConfig.PORT,
            'use_ssl': ElasticConfig.USESSL,
            'http_auth': (ElasticConfig.USERNAME, ElasticConfig.PASSWORD)
            }]
    es = Elasticsearch(es_header)
    return es

if __name__ == "__main__":
    es = connect_elasticsearch()
    # index_names = [ElasticConfig.NMAPINDEX, ElasticConfig.CVESEARCHINDEX, ElasticConfig.NSEINDEX, ElasticConfig.WAPPALYZERINDEX, ElasticConfig.ACUNETIX_SUMARY_INDEX, ElasticConfig.ACUNETIX_DETAIL_INDEX, ElasticConfig.SCAN_INDEX, ElasticConfig.NIKTO_INDEX, ElasticConfig.NESSUS_INDEX, ElasticConfig.VULNERABILITY_INDEX, ElasticConfig.USERS_INDEX]
    index_names = [ElasticConfig.NMAPINDEX, ElasticConfig.CVESEARCHINDEX, ElasticConfig.NSEINDEX, ElasticConfig.WAPPALYZERINDEX, ElasticConfig.ACUNETIX_SUMARY_INDEX, ElasticConfig.ACUNETIX_DETAIL_INDEX, ElasticConfig.SCAN_INDEX, ElasticConfig.NIKTO_INDEX, ElasticConfig.NESSUS_INDEX, ElasticConfig.VULNERABILITY_INDEX]
    for index in index_names:
        mapping = {}
        with open('MappingElasticSearch/{}_mapping.json'.format(index)) as f:
            mapping = f.read()
        
        mapping = json.loads(mapping)
        print(index)
        es.indices.delete(index=index, ignore=[400, 404])
        es.indices.create(index=index, body=mapping, include_type_name=True)
        # es.indices.put_mapping(index=index, doc_type="_doc", body=mapping, include_type_name=True)
        print("done " + index)

    