def fetch_es_templates()

in helper-scripts/export_kibana_config.py [0:0]


    def fetch_es_templates(self):
        """
        Get ElasticSearch templates from API
        """
        for template_name in ES_TEMPLATES_LIST:
            try:
                print(f"# Fetching ES template: {template_name}")
                response = requests.get(
                    f"{ES_URL}/_template/{template_name}",
                    verify=False,
                    auth=(self.kibana_user, self.kibana_password),
                )
                raw_data = response.text.encode("utf-8")
                tmpl = json.loads(raw_data)
                export_file = os.path.join(
                    self.export_path,
                    f"{EXPORT_FILES_PREFIX_ES}template_{template_name}.json",
                )
                print(f"\tExporting index template {template_name}: {export_file}")
                with open(export_file, "w", encoding="utf-8") as template_file:
                    json.dump(
                        tmpl[template_name], template_file, indent=4, sort_keys=True
                    )
            except Exception as error:  # pylint: disable=broad-except
                print(f"!!! Error fetching ES template {template_name}: {error}")

        # Get ElasticSearch component templates from API
        response = requests.get(
            f"{ES_URL}/_component_template",
            verify=False,
            auth=(self.kibana_user, self.kibana_password),
        )
        json_data = response.json()
        if "component_templates" in json_data:
            for template in json_data["component_templates"]:
                if "redelk" in template["name"]:
                    export_file = os.path.join(
                        self.export_path,
                        f"{EXPORT_FILES_PREFIX_ES}component_template_{template['name']}.json",
                    )
                    print(
                        f"\tExporting component template {template['name']}: {export_file}"
                    )
                    with open(export_file, "w", encoding="utf-8") as template_file:
                        json.dump(template, template_file, indent=4, sort_keys=True)

        # Get ElasticSearch index templates from API
        response = requests.get(
            f"{ES_URL}/_index_template",
            verify=False,
            auth=(self.kibana_user, self.kibana_password),
        )
        json_data = response.json()
        if "index_templates" in json_data:
            for template in json_data["index_templates"]:
                if template["name"] in ES_INDEX_TEMPLATES_LIST:
                    export_file = os.path.join(
                        self.export_path,
                        f"{EXPORT_FILES_PREFIX_ES}index_template_{template['name']}.json",
                    )
                    print(
                        f"\tExporting index template {template['name']}: {export_file}"
                    )
                    with open(export_file, "w", encoding="utf-8") as template_file:
                        json.dump(template, template_file, indent=4, sort_keys=True)