Repository: dadoonet/elasticsearch-beyonder
Pull request #175 (2021-06-18 10:00:05)

Move templates dir to `_templates`
`_template` dir has been deprecated by `_templates` dir.


Patch:
diff --git a/README.md b/README.md
index 8c3f1d3..e3eff02 100644
--- a/README.md
+++ b/README.md
@@ -51,6 +51,7 @@ Release notes
 ------
 
 * `_pipeline` dir has been deprecated by `_pipelines` dir.
+* `_template` dir has been deprecated by `_templates` dir.
 * `force` parameter is not applied anymore to pipelines. So pipelines are always updated.
 * `force` parameter is not applied anymore to templates, component templates and index templates. So they are always updated.
 * method `start(RestClient client, String root, boolean merge, boolean force)` is now deprecated as the `merge` parameter
@@ -326,7 +327,7 @@ This method is deprecated as Elasticsearch has deprecated [legacy templates](htt
 Sometimes it's useful to define a template mapping that will automatically be applied to new indices created. 
 
 For example, if you planned to have indexes per year for twitter feeds (twitter2012, twitter2013, twitter2014) and you want
-to define a template named `twitter_template`, you can add a file named `elasticsearch/_template/twitter_template.json`
+to define a template named `twitter_template`, you can add a file named `elasticsearch/_templates/twitter_template.json`
 in your project:
 
 ```json
diff --git a/src/main/java/fr/pilato/elasticsearch/tools/ElasticsearchBeyonder.java b/src/main/java/fr/pilato/elasticsearch/tools/ElasticsearchBeyonder.java
index ae67089..753ad8c 100644
--- a/src/main/java/fr/pilato/elasticsearch/tools/ElasticsearchBeyonder.java
+++ b/src/main/java/fr/pilato/elasticsearch/tools/ElasticsearchBeyonder.java
@@ -119,7 +119,14 @@ public class ElasticsearchBeyonder {
 		logger.info("starting automatic settings/mappings discovery");
 
 		// create legacy templates
-		List<String> templateNames = ResourceList.getResourceNames(root, Defaults.TemplateDir);
+		List<String> templateNames = ResourceList.getResourceNames(root, Defaults.TemplatesDir);
+		if (templateNames.isEmpty()) {
+			// We check for deprecated resources
+			templateNames = ResourceList.getResourceNames(root, Defaults.TemplateDir);
+			if (!templateNames.isEmpty()) {
+				logger.warn("{} dir has been deprecated. Please use {} dir instead.", Defaults.TemplateDir, Defaults.TemplatesDir);
+			}
+		}
 		for (String templateName : templateNames) {
 			logger.warn("Legacy Templates are deprecated in Elasticsearch. Switch to Index Templates instead by using {}/{}{}",
 					Defaults.IndexTemplatesDir, templateName, Defaults.JsonFileExtension);
@@ -185,13 +192,20 @@ public class ElasticsearchBeyonder {
 	public static void start(Client client, String root) throws Exception {
 		logger.info("starting automatic settings/mappings discovery");
 
-		// TODO make it a parameter
-		boolean merge = Defaults.MergeMappings;
 		boolean force = Defaults.ForceCreation;
 
-		// create templates
-		List<String> templateNames = ResourceList.getResourceNames(root, Defaults.TemplateDir);
+		// create legacy templates
+		List<String> templateNames = ResourceList.getResourceNames(root, Defaults.TemplatesDir);
+		if (templateNames.isEmpty()) {
+			// We check for deprecated resources
+			templateNames = ResourceList.getResourceNames(root, Defaults.TemplateDir);
+			if (!templateNames.isEmpty()) {
+				logger.warn("{} dir has been deprecated. Please use {} dir instead.", Defaults.TemplateDir, Defaults.TemplatesDir);
+			}
+		}
 		for (String templateName : templateNames) {
+			logger.warn("Legacy Templates are deprecated in Elasticsearch. Switch to Index Templates instead by using {}/{}{}",
+					Defaults.IndexTemplatesDir, templateName, Defaults.JsonFileExtension);
 			createTemplate(client, root, templateName);
 		}
 
diff --git a/src/main/java/fr/pilato/elasticsearch/tools/updaters/ElasticsearchTemplateUpdater.java b/src/main/java/fr/pilato/elasticsearch/tools/updaters/ElasticsearchTemplateUpdater.java
index b01a643..23843e6 100644
--- a/src/main/java/fr/pilato/elasticsearch/tools/updaters/ElasticsearchTemplateUpdater.java
+++ b/src/main/java/fr/pilato/elasticsearch/tools/updaters/ElasticsearchTemplateUpdater.java
@@ -52,7 +52,13 @@ public class ElasticsearchTemplateUpdater {
 	 */
 	@Deprecated
 	public static void createTemplate(Client client, String root, String template) throws Exception {
-		String json = getJsonContent(root, SettingsFinder.Defaults.TemplateDir, template);
+		String json = getJsonContent(root, SettingsFinder.Defaults.TemplatesDir, template);
+		if (json == null) {
+			logger.warn("Please rename [{}/{}/{}{}] to [{}/{}/{}{}].",
+					root, SettingsFinder.Defaults.TemplateDir, template, SettingsFinder.Defaults.JsonFileExtension,
+					root, SettingsFinder.Defaults.TemplatesDir, template, SettingsFinder.Defaults.JsonFileExtension);
+			json = getJsonContent(root, SettingsFinder.Defaults.TemplateDir, template);
+		}
 		createTemplateWithJsonInElasticsearch(client, template, json);
 	}
 
@@ -93,20 +99,14 @@ public class ElasticsearchTemplateUpdater {
 	 */
 	@Deprecated
 	public static void createTemplate(RestClient client, String root, String template) throws Exception {
-		String json = getJsonContent(root, SettingsFinder.Defaults.TemplateDir, template);
-		createTemplateWithJsonInElasticsearch(client, template, json);
-	}
+		String json = getJsonContent(root, SettingsFinder.Defaults.TemplatesDir, template);
+		if (json == null) {
+			logger.warn("Please rename [{}/{}/{}{}] to [{}/{}/{}{}].",
+					root, SettingsFinder.Defaults.TemplateDir, template, SettingsFinder.Defaults.JsonFileExtension,
+					root, SettingsFinder.Defaults.TemplatesDir, template, SettingsFinder.Defaults.JsonFileExtension);
+			json = getJsonContent(root, SettingsFinder.Defaults.TemplateDir, template);
+		}
 
-	/**
-	 * Create a new legacy template in Elasticsearch
-	 * @param client Elasticsearch client
-	 * @param template Template name
-	 * @param json JSon content for the template
-	 * @param force set it to true if you want to force cleaning template before adding it
-     * @throws Exception if something goes wrong
-	 */
-	@Deprecated
-	public static void createTemplateWithJson(RestClient client, String template, String json, boolean force) throws Exception {
 		createTemplateWithJsonInElasticsearch(client, template, json);
 	}
 
diff --git a/src/main/java/fr/pilato/elasticsearch/tools/util/ResourceList.java b/src/main/java/fr/pilato/elasticsearch/tools/util/ResourceList.java
index f12eb5d..02d285e 100644
--- a/src/main/java/fr/pilato/elasticsearch/tools/util/ResourceList.java
+++ b/src/main/java/fr/pilato/elasticsearch/tools/util/ResourceList.java
@@ -183,6 +183,7 @@ public class ResourceList {
                 if (!key.equals(SettingsFinder.Defaults.IndexTemplatesDir) &&
                         !key.equals(SettingsFinder.Defaults.ComponentTemplatesDir) &&
                         !key.equals(SettingsFinder.Defaults.TemplateDir) &&
+                        !key.equals(SettingsFinder.Defaults.TemplatesDir) &&
                         !key.equals(SettingsFinder.Defaults.PipelineDir) &&
                         !key.equals(SettingsFinder.Defaults.PipelinesDir) &&
                         !keys.contains(key)) {
diff --git a/src/main/java/fr/pilato/elasticsearch/tools/util/SettingsFinder.java b/src/main/java/fr/pilato/elasticsearch/tools/util/SettingsFinder.java
index 257609c..8859de9 100644
--- a/src/main/java/fr/pilato/elasticsearch/tools/util/SettingsFinder.java
+++ b/src/main/java/fr/pilato/elasticsearch/tools/util/SettingsFinder.java
@@ -31,7 +31,9 @@ public class SettingsFinder {
 		public static String IndexSettingsFileName = "_settings";
 		public static String UpdateIndexSettingsFileName = "_update_settings";
 		public static String UpdateIndexMappingFileName = "_update_mapping";
+		@Deprecated
 		public static String TemplateDir = "_template";
+		public static String TemplatesDir = "_templates";
 		public static String IndexTemplatesDir = "_index_templates";
 		public static String ComponentTemplatesDir = "_component_templates";
 		public static String PipelinesDir = "_pipelines";


Test patch:
diff --git a/src/test/java/fr/pilato/elasticsearch/tools/AbstractBeyonderTest.java b/src/test/java/fr/pilato/elasticsearch/tools/AbstractBeyonderTest.java
index 6b88498..c7a3d8c 100644
--- a/src/test/java/fr/pilato/elasticsearch/tools/AbstractBeyonderTest.java
+++ b/src/test/java/fr/pilato/elasticsearch/tools/AbstractBeyonderTest.java
@@ -151,9 +151,9 @@ public abstract class AbstractBeyonderTest {
     }
 
     @Test
-    public void testTemplate() throws Exception {
+    public void testTemplates() throws Exception {
         // 1 template
-        testBeyonder("models/template",
+        testBeyonder("models/templates",
                 null,
                 singletonList("twitter_template"), null, null, null);
     }
diff --git a/src/test/java/fr/pilato/elasticsearch/tools/BeyonderRestIT.java b/src/test/java/fr/pilato/elasticsearch/tools/BeyonderRestIT.java
index 5cbd206..5a7effd 100644
--- a/src/test/java/fr/pilato/elasticsearch/tools/BeyonderRestIT.java
+++ b/src/test/java/fr/pilato/elasticsearch/tools/BeyonderRestIT.java
@@ -284,6 +284,10 @@ public class BeyonderRestIT extends AbstractBeyonderTest {
                 singletonList("template_1"), null);
     }
 
+    @Test
+    public void testDeprecatedTemplate() throws Exception {
+        testBeyonder("models/template", null, singletonList("twitter_template"), null, null, null);
+    }
 
     private String getMapping(String indexName) throws IOException {
         HttpEntity response = client.performRequest(new Request("GET", indexName + "/_mapping")).getEntity();
diff --git a/src/test/java/fr/pilato/elasticsearch/tools/BeyonderTransportIT.java b/src/test/java/fr/pilato/elasticsearch/tools/BeyonderTransportIT.java
index 658d3dc..6be0cfa 100644
--- a/src/test/java/fr/pilato/elasticsearch/tools/BeyonderTransportIT.java
+++ b/src/test/java/fr/pilato/elasticsearch/tools/BeyonderTransportIT.java
@@ -159,6 +159,11 @@ public class BeyonderTransportIT extends AbstractBeyonderTest {
         }
     }
 
+    @Test
+    public void testDeprecatedTemplate() throws Exception {
+        testBeyonder("models/template", null, singletonList("twitter_template"), null, null, null);
+    }
+
     protected void testBeyonder(String root,
                                 List<String> indices,
                                 List<String> templates,
diff --git a/src/test/java/fr/pilato/elasticsearch/tools/BeyonderUnitTest.java b/src/test/java/fr/pilato/elasticsearch/tools/BeyonderUnitTest.java
index 013c7cf..2795a89 100644
--- a/src/test/java/fr/pilato/elasticsearch/tools/BeyonderUnitTest.java
+++ b/src/test/java/fr/pilato/elasticsearch/tools/BeyonderUnitTest.java
@@ -68,9 +68,9 @@ public class BeyonderUnitTest extends AbstractBeyonderTest {
             assertThat(indexNames, emptyIterable());
         }
 
-        check(ResourceList.getResourceNames(root, SettingsFinder.Defaults.TemplateDir), templates, (name) -> {
+        check(ResourceList.getResourceNames(root, SettingsFinder.Defaults.TemplatesDir), templates, (name) -> {
             try {
-                return getJsonContent(root, SettingsFinder.Defaults.TemplateDir, name);
+                return getJsonContent(root, SettingsFinder.Defaults.TemplatesDir, name);
             } catch (IOException e) {
                 throw new RuntimeException("Our test is failing...");
             }
diff --git a/src/test/resources/models/templates/_templates/twitter_template.json b/src/test/resources/models/templates/_templates/twitter_template.json
new file mode 100644
index 0000000..f015004
--- /dev/null
+++ b/src/test/resources/models/templates/_templates/twitter_template.json
@@ -0,0 +1,14 @@
+{
+    "template" : "twee*",
+    "settings" : {
+        "number_of_shards" : 1
+    },
+    "mappings" : {
+        "properties" : {
+            "message" : {
+                "type" : "text",
+                "store" : true
+            }
+        }
+    }
+}
diff --git a/src/test/resources/transport/models/template/_template/twitter_template.json b/src/test/resources/transport/models/template/_template/twitter_template.json
index 73bf296..ea53cc3 100644
--- a/src/test/resources/transport/models/template/_template/twitter_template.json
+++ b/src/test/resources/transport/models/template/_template/twitter_template.json
@@ -1,5 +1,5 @@
 {
-    "template" : "twee*",
+    "index_patterns" : "twee*",
     "settings" : {
         "number_of_shards" : 1
     },
diff --git a/src/test/resources/transport/models/templates/_templates/twitter_template.json b/src/test/resources/transport/models/templates/_templates/twitter_template.json
new file mode 100644
index 0000000..ea53cc3
--- /dev/null
+++ b/src/test/resources/transport/models/templates/_templates/twitter_template.json
@@ -0,0 +1,16 @@
+{
+    "index_patterns" : "twee*",
+    "settings" : {
+        "number_of_shards" : 1
+    },
+    "mappings" : {
+        "_doc": {
+            "properties" : {
+                "message" : {
+                    "type" : "text",
+                    "store" : true
+                }
+            }
+        }
+    }
+}