Prior to creating the indices, I need to create the index templates.
import requests
from requests.auth import HTTPBasicAuth
import json
from time import sleep
def serialize_sets(obj):
if isinstance(obj, set):
return list(obj)
return obj
listIgnoredTemplates = ['.watch-history', '.watch-history-1', '.watch-history-2', '.watch-history-3', '.watch-history-4', '.watch-history-5', '.watch-history-6', '.watch-history-7', '.watch-history-8', '.watch-history-9', '.watch-history-10', '.watch-history-11', 'ilm-history', 'ilm-history_2', 'tenant_template', '.monitoring-logstash']
# Get all roles from prod & list users in those roles
r = requests.get(f"https://elasticsearch.example.com:9200/_template", auth = HTTPBasicAuth('something', 'something'), verify=False)
dictAllTemplates= r.json()
for item in dictAllTemplates.items():
if item[0] not in listIgnoredTemplates:
if item[1].get('settings').get('index'):
iShards = (item[1].get('settings').get('index').get('number_of_shards'))
iReplicas = (item[1].get('settings').get('index').get('number_of_replicas'))
else:
iShards = 3
iReplicas = 1
if iShards is None:
iShards = 3
if iReplicas is None:
iReplicas = 1
if item[1].get('settings').get('index') and item[1].get('settings').get('index').get('lifecycle'):
jsonAddTemplate = {
"index_patterns": item[1].get('index_patterns'),
"template": {
"aliases": {
item[1].get('settings').get('index').get('lifecycle').get('rollover_alias'): {}
},
"settings": {
"number_of_shards": iShards,
"number_of_replicas": iReplicas
},
"mappings": item[1].get('mappings')
}
}
else:
jsonAddTemplate = {
"index_patterns": item[1].get('index_patterns'),
"template": {
"settings": {
"number_of_shards": iShards,
"number_of_replicas": iReplicas
},
"mappings": item[1].get('mappings')
}
}
r2 = requests.put(f"https://opensearch.example.com:9200/_index_template/{item[0]}", json=jsonAddTemplate, auth = HTTPBasicAuth('something', 'something'), verify=False)
print(r2.text)
print(r2.status_code)
sleep(2)