Browse Source

Delete Template: When deleting with * and no templates exists, don't 404
closes #3723

Shay Banon 12 years ago
parent
commit
6a04c16932

+ 5 - 0
src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexTemplateService.java

@@ -78,6 +78,11 @@ public class MetaDataIndexTemplateService extends AbstractComponent {
                     }
                 }
                 if (templateNames.isEmpty()) {
+                    // if its a match all pattern, and no templates are found (we have none), don't
+                    // fail with index missing...
+                    if (Regex.isMatchAllPattern(request.name)) {
+                        return currentState;
+                    }
                     throw new IndexTemplateMissingException(request.name);
                 }
                 MetaData.Builder metaData = MetaData.builder().metaData(currentState.metaData());

+ 6 - 3
src/main/java/org/elasticsearch/common/regex/Regex.java

@@ -29,11 +29,10 @@ import java.util.regex.Pattern;
  *
  */
 public class Regex {
-    
+
     /**
      * This Regex / {@link Pattern} flag is supported from Java 7 on.
      * If set on a Java6 JVM the flag will be ignored.
-     * 
      */
     public static final int UNICODE_CHARACTER_CLASS = 0x100; // supported in JAVA7
 
@@ -44,6 +43,10 @@ public class Regex {
         return str.indexOf('*') != -1;
     }
 
+    public static boolean isMatchAllPattern(String str) {
+        return str.equals("*");
+    }
+
     /**
      * Match a String against the given pattern, supporting the following simple
      * pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy" matches (with an
@@ -179,7 +182,7 @@ public class Regex {
         }
         if ((flags & Pattern.COMMENTS) != 0) {
             sb.append("COMMENTS|");
-        } 
+        }
         if ((flags & UNICODE_CHARACTER_CLASS) != 0) {
             sb.append("UNICODE_CHAR_CLASS|");
         }

+ 5 - 1
src/test/java/org/elasticsearch/indices/template/SimpleIndexTemplateTests.java

@@ -20,13 +20,13 @@
 package org.elasticsearch.indices.template;
 
 import com.google.common.collect.Lists;
+import org.elasticsearch.AbstractSharedClusterTest;
 import org.elasticsearch.action.ActionRequestValidationException;
 import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.common.Priority;
 import org.elasticsearch.common.xcontent.XContentFactory;
 import org.elasticsearch.indices.IndexTemplateAlreadyExistsException;
-import org.elasticsearch.AbstractSharedClusterTest;
 import org.junit.Test;
 
 import java.util.Arrays;
@@ -152,6 +152,10 @@ public class SimpleIndexTemplateTests extends AbstractSharedClusterTest {
         logger.info("--> delete template*");
         admin().indices().prepareDeleteTemplate("template*").execute().actionGet();
         assertThat(admin().cluster().prepareState().execute().actionGet().getState().metaData().templates().size(), equalTo(0));
+
+        logger.info("--> delete * with no templates, make sure we don't get a failure");
+        admin().indices().prepareDeleteTemplate("*").execute().actionGet();
+        assertThat(admin().cluster().prepareState().execute().actionGet().getState().metaData().templates().size(), equalTo(0));
     }
 
     @Test