Skip to content

Commit

Permalink
Delete Template: When deleting with * and no templates exists, don't 404
Browse files Browse the repository at this point in the history
closes #3723
  • Loading branch information
kimchy committed Sep 18, 2013
1 parent 0515efd commit c44000f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Expand Up @@ -78,6 +78,11 @@ public ClusterState execute(ClusterState currentState) {
}
}
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());
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/elasticsearch/common/regex/Regex.java
Expand Up @@ -29,11 +29,10 @@
*
*/
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

Expand All @@ -44,6 +43,10 @@ public static boolean isSimpleMatchPattern(String str) {
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
Expand Down Expand Up @@ -166,7 +169,7 @@ public static String flagsToString(int flags) {
}
if ((flags & Pattern.COMMENTS) != 0) {
sb.append("COMMENTS|");
}
}
if ((flags & UNICODE_CHARACTER_CLASS) != 0) {
sb.append("UNICODE_CHAR_CLASS|");
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -152,6 +152,10 @@ public void testDeleteIndexTemplate() throws Exception {
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
Expand Down

0 comments on commit c44000f

Please sign in to comment.