Migrated from indexed scripts to store scripts

Original commit: elastic/x-pack-elasticsearch@a0218f1c9e
This commit is contained in:
Martijn van Groningen 2016-04-21 15:16:28 +02:00
parent 276d5fbbca
commit b9515357fa
19 changed files with 91 additions and 57 deletions

View File

@ -6,6 +6,9 @@
package org.elasticsearch.messy.tests; package org.elasticsearch.messy.tests;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment; import org.elasticsearch.env.Environment;
@ -19,6 +22,7 @@ import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.watcher.ResourceWatcherService; import org.elasticsearch.watcher.ResourceWatcherService;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy; import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.junit.Ignore; import org.junit.Ignore;
import org.mockito.Mockito;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
@ -43,8 +47,11 @@ public final class MessyTestUtils {
); );
ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Arrays.asList(ScriptServiceProxy.INSTANCE)); ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Arrays.asList(ScriptServiceProxy.INSTANCE));
ClusterService clusterService = Mockito.mock(ClusterService.class);
Mockito.when(clusterService.state()).thenReturn(ClusterState.builder(new ClusterName("_name")).build());
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry); ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings), engineServiceSet, return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings), engineServiceSet,
new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings)); new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings),
clusterService);
} }
} }

View File

@ -124,7 +124,7 @@ public class ScriptConditionTests extends ESTestCase {
ScriptType scriptType = randomFrom(ScriptType.values()); ScriptType scriptType = randomFrom(ScriptType.values());
String script; String script;
switch (scriptType) { switch (scriptType) {
case INDEXED: case STORED:
case FILE: case FILE:
script = "nonExisting_script"; script = "nonExisting_script";
break; break;
@ -212,7 +212,7 @@ public class ScriptConditionTests extends ESTestCase {
case FILE: case FILE:
builder.field("file", script); builder.field("file", script);
break; break;
case INDEXED: case STORED:
builder.field("id", script); builder.field("id", script);
break; break;
default: default:

View File

@ -7,6 +7,7 @@ package org.elasticsearch.messy.tests;
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -77,7 +78,10 @@ public class TransformIT extends AbstractWatcherIntegrationTestCase {
script = Script.inline("return [key3 : ctx.payload.key1 + ctx.payload.key2]").lang("groovy").build(); script = Script.inline("return [key3 : ctx.payload.key1 + ctx.payload.key2]").lang("groovy").build();
} else if (randomBoolean()) { } else if (randomBoolean()) {
logger.info("testing script transform with an indexed script"); logger.info("testing script transform with an indexed script");
client().preparePutIndexedScript("groovy", "_id", "{\"script\" : \"return [key3 : ctx.payload.key1 + ctx.payload.key2]\"}") client().admin().cluster().preparePutStoredScript()
.setId("_id")
.setScriptLang("groovy")
.setSource(new BytesArray("{\"script\" : \"return [key3 : ctx.payload.key1 + ctx.payload.key2]\"}"))
.get(); .get();
script = Script.indexed("_id").lang("groovy").build(); script = Script.indexed("_id").lang("groovy").build();
} else { } else {

View File

@ -5,9 +5,10 @@
*/ */
package org.elasticsearch.messy.tests; package org.elasticsearch.messy.tests;
import org.elasticsearch.action.indexedscripts.put.PutIndexedScriptRequest; import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest;
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
@ -192,14 +193,17 @@ public class SearchInputIT extends ESIntegTestCase {
public void testSearchIndexedTemplate() throws Exception { public void testSearchIndexedTemplate() throws Exception {
WatchExecutionContext ctx = createContext(); WatchExecutionContext ctx = createContext();
PutIndexedScriptRequest indexedScriptRequest = client().preparePutIndexedScript("mustache","test-template", PutStoredScriptRequest indexedScriptRequest = client().admin().cluster().preparePutStoredScript()
TEMPLATE_QUERY).request(); .setId("test-template")
assertThat(client().putIndexedScript(indexedScriptRequest).actionGet().isCreated(), is(true)); .setScriptLang("mustache")
.setSource(new BytesArray(TEMPLATE_QUERY))
.request();
assertThat(client().admin().cluster().putStoredScript(indexedScriptRequest).actionGet().isAcknowledged(), is(true));
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("seconds_param", "30s"); params.put("seconds_param", "30s");
Template template = new Template("test-template", ScriptType.INDEXED, null, null, params); Template template = new Template("test-template", ScriptType.STORED, null, null, params);
jsonBuilder().value(TextTemplate.indexed("test-template").params(params).build()).bytes(); jsonBuilder().value(TextTemplate.indexed("test-template").params(params).build()).bytes();
SearchRequest request = client().prepareSearch().setSearchType(ExecutableSearchInput.DEFAULT_SEARCH_TYPE) SearchRequest request = client().prepareSearch().setSearchType(ExecutableSearchInput.DEFAULT_SEARCH_TYPE)
@ -209,7 +213,7 @@ public class SearchInputIT extends ESIntegTestCase {
Template resultTemplate = executedResult.executedRequest().template(); Template resultTemplate = executedResult.executedRequest().template();
assertThat(resultTemplate, notNullValue()); assertThat(resultTemplate, notNullValue());
assertThat(resultTemplate.getScript(), equalTo("test-template")); assertThat(resultTemplate.getScript(), equalTo("test-template"));
assertThat(resultTemplate.getType(), equalTo(ScriptType.INDEXED)); assertThat(resultTemplate.getType(), equalTo(ScriptType.STORED));
} }
public void testSearchOnDiskTemplate() throws Exception { public void testSearchOnDiskTemplate() throws Exception {

View File

@ -5,12 +5,13 @@
*/ */
package org.elasticsearch.messy.tests; package org.elasticsearch.messy.tests;
import org.elasticsearch.action.indexedscripts.put.PutIndexedScriptRequest; import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest;
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Requests; import org.elasticsearch.client.Requests;
import org.elasticsearch.common.Base64; import org.elasticsearch.common.Base64;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -388,8 +389,12 @@ public class SearchTransformIT extends ESIntegTestCase {
"{\"from\":\"{{ctx.trigger.scheduled_time}}||-{{seconds_param}}\",\"to\":\"{{ctx.trigger.scheduled_time}}\"," + "{\"from\":\"{{ctx.trigger.scheduled_time}}||-{{seconds_param}}\",\"to\":\"{{ctx.trigger.scheduled_time}}\"," +
"\"include_lower\":true,\"include_upper\":true}}}]}}}"; "\"include_lower\":true,\"include_upper\":true}}}]}}}";
PutIndexedScriptRequest indexedScriptRequest = client().preparePutIndexedScript("mustache", "test-script", templateQuery).request(); PutStoredScriptRequest indexedScriptRequest = client().admin().cluster().preparePutStoredScript()
assertThat(client().putIndexedScript(indexedScriptRequest).actionGet().isCreated(), is(true)); .setId("mustache")
.setScriptLang("test-script")
.setSource(new BytesArray(templateQuery))
.request();
assertThat(client().admin().cluster().putStoredScript(indexedScriptRequest).actionGet().isAcknowledged(), is(true));
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("seconds_param", "30s"); params.put("seconds_param", "30s");
@ -397,7 +402,7 @@ public class SearchTransformIT extends ESIntegTestCase {
BytesReference templateSource = jsonBuilder() BytesReference templateSource = jsonBuilder()
.value(TextTemplate.indexed("test-script").params(params).build()) .value(TextTemplate.indexed("test-script").params(params).build())
.bytes(); .bytes();
Template template = new Template("test-script", ScriptType.INDEXED, null, null, null); Template template = new Template("test-script", ScriptType.STORED, null, null, null);
SearchRequest request = client() SearchRequest request = client()
.prepareSearch() .prepareSearch()
@ -411,7 +416,7 @@ public class SearchTransformIT extends ESIntegTestCase {
Template resultTemplate = result.executedRequest().template(); Template resultTemplate = result.executedRequest().template();
assertThat(resultTemplate, notNullValue()); assertThat(resultTemplate, notNullValue());
assertThat(resultTemplate.getScript(), equalTo("test-script")); assertThat(resultTemplate.getScript(), equalTo("test-script"));
assertThat(resultTemplate.getType(), equalTo(ScriptType.INDEXED)); assertThat(resultTemplate.getType(), equalTo(ScriptType.STORED));
} }
public void testSearchOnDiskTemplate() throws Exception { public void testSearchOnDiskTemplate() throws Exception {
@ -466,7 +471,7 @@ public class SearchTransformIT extends ESIntegTestCase {
new ExecutableAlwaysCondition(logger), new ExecutableAlwaysCondition(logger),
null, null,
null, null,
new ExecutableActions(new ArrayList<ActionWrapper>()), new ExecutableActions(new ArrayList<>()),
null, null,
new WatchStatus( new DateTime(40000, UTC), emptyMap())), new WatchStatus( new DateTime(40000, UTC), emptyMap())),
new DateTime(60000, UTC), new DateTime(60000, UTC),

View File

@ -9,6 +9,7 @@ import org.elasticsearch.Version;
import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchPhaseExecutionException; import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.indices.TermsLookup; import org.elasticsearch.indices.TermsLookup;
import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.Plugin;
@ -72,7 +73,7 @@ public class ShieldCachePermissionIT extends ShieldIntegTestCase {
public void loadData() { public void loadData() {
index("data", "a", "1", "{ \"name\": \"John\", \"token\": \"token1\" }"); index("data", "a", "1", "{ \"name\": \"John\", \"token\": \"token1\" }");
index("tokens", "tokens", "1", "{ \"group\": \"1\", \"tokens\": [\"token1\", \"token2\"] }"); index("tokens", "tokens", "1", "{ \"group\": \"1\", \"tokens\": [\"token1\", \"token2\"] }");
client().preparePutIndexedScript().setOpType(IndexRequest.OpType.CREATE).setSource("{\n" + client().admin().cluster().preparePutStoredScript().setSource(new BytesArray("{\n" +
"\"template\": {\n" + "\"template\": {\n" +
" \"query\": {\n" + " \"query\": {\n" +
" \"exists\": {\n" + " \"exists\": {\n" +
@ -80,7 +81,7 @@ public class ShieldCachePermissionIT extends ShieldIntegTestCase {
" }\n" + " }\n" +
" }\n" + " }\n" +
" }\n" + " }\n" +
"}") "}"))
.setScriptLang("mustache") .setScriptLang("mustache")
.setId("testTemplate") .setId("testTemplate")
.execute().actionGet(); .execute().actionGet();
@ -110,7 +111,7 @@ public class ShieldCachePermissionIT extends ShieldIntegTestCase {
public void testThatScriptServiceDoesntLeakData() { public void testThatScriptServiceDoesntLeakData() {
SearchResponse response = client().prepareSearch("data").setTypes("a") SearchResponse response = client().prepareSearch("data").setTypes("a")
.setTemplate(new Template("testTemplate", ScriptService.ScriptType.INDEXED, MustacheScriptEngineService.NAME, null, .setTemplate(new Template("testTemplate", ScriptService.ScriptType.STORED, MustacheScriptEngineService.NAME, null,
Collections.<String, Object>singletonMap("name", "token"))) Collections.<String, Object>singletonMap("name", "token")))
.execute().actionGet(); .execute().actionGet();
assertThat(response.isTimedOut(), is(false)); assertThat(response.isTimedOut(), is(false));
@ -121,7 +122,7 @@ public class ShieldCachePermissionIT extends ShieldIntegTestCase {
response = client().filterWithHeader(Collections.singletonMap("Authorization", basicAuthHeaderValue(READ_ONE_IDX_USER, response = client().filterWithHeader(Collections.singletonMap("Authorization", basicAuthHeaderValue(READ_ONE_IDX_USER,
new SecuredString("changeme".toCharArray())))) new SecuredString("changeme".toCharArray()))))
.prepareSearch("data").setTypes("a") .prepareSearch("data").setTypes("a")
.setTemplate(new Template("testTemplate", ScriptService.ScriptType.INDEXED, MustacheScriptEngineService.NAME, null, .setTemplate(new Template("testTemplate", ScriptService.ScriptType.STORED, MustacheScriptEngineService.NAME, null,
Collections.<String, Object>singletonMap("name", "token"))) Collections.<String, Object>singletonMap("name", "token")))
.execute().actionGet(); .execute().actionGet();
fail("search phase exception should have been thrown! response was:\n" + response.toString()); fail("search phase exception should have been thrown! response was:\n" + response.toString());

View File

@ -6,6 +6,9 @@
package org.elasticsearch.smoketest; package org.elasticsearch.smoketest;
import com.fasterxml.jackson.core.io.JsonStringEncoder; import com.fasterxml.jackson.core.io.JsonStringEncoder;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
@ -59,7 +62,9 @@ public class WatcherTemplateTests extends ESTestCase {
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, registry); ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, registry);
ScriptService scriptService = new ScriptService(setting, environment, engines, resourceWatcherService, scriptEngineRegistry, ScriptService scriptService = new ScriptService(setting, environment, engines, resourceWatcherService, scriptEngineRegistry,
registry, scriptSettings); registry, scriptSettings);
engine = new DefaultTextTemplateEngine(Settings.EMPTY, ScriptServiceProxy.of(scriptService)); ClusterService clusterService = Mockito.mock(ClusterService.class);
Mockito.when(clusterService.state()).thenReturn(ClusterState.builder(new ClusterName("_name")).build());
engine = new DefaultTextTemplateEngine(Settings.EMPTY, ScriptServiceProxy.of(scriptService, clusterService));
} }
public void testEscaping() throws Exception { public void testEscaping() throws Exception {

View File

@ -62,7 +62,7 @@ indices:data/read/mpercolate
indices:data/read/msearch indices:data/read/msearch
indices:data/read/mtv indices:data/read/mtv
indices:data/read/percolate indices:data/read/percolate
indices:data/read/script/get cluster:admin/script/get
indices:data/read/scroll indices:data/read/scroll
indices:data/read/scroll/clear indices:data/read/scroll/clear
indices:data/read/search indices:data/read/search
@ -70,8 +70,8 @@ indices:data/read/tv
indices:data/write/bulk indices:data/write/bulk
indices:data/write/delete indices:data/write/delete
indices:data/write/index indices:data/write/index
indices:data/write/script/delete cluster:admin/script/delete
indices:data/write/script/put cluster:admin/script/put
indices:data/write/update indices:data/write/update
cluster:monitor/xpack/info cluster:monitor/xpack/info
cluster:monitor/xpack/license/get cluster:monitor/xpack/license/get

View File

@ -94,7 +94,7 @@ public class Script implements ToXContent {
builder.field(Field.FILE.getPreferredName(), script); builder.field(Field.FILE.getPreferredName(), script);
break; break;
default: default:
assert type == ScriptType.INDEXED : "script type [" + type + "] is not supported"; assert type == ScriptType.STORED : "script type [" + type + "] is not supported";
builder.field(Field.ID.getPreferredName(), script); builder.field(Field.ID.getPreferredName(), script);
} }
if (lang != null) { if (lang != null) {
@ -141,7 +141,7 @@ public class Script implements ToXContent {
token); token);
} }
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.ID)) { } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.ID)) {
type = ScriptType.INDEXED; type = ScriptType.STORED;
if (token == XContentParser.Token.VALUE_STRING) { if (token == XContentParser.Token.VALUE_STRING) {
script = parser.text(); script = parser.text();
} else { } else {
@ -240,7 +240,7 @@ public class Script implements ToXContent {
public static class Indexed extends Builder<Indexed> { public static class Indexed extends Builder<Indexed> {
public Indexed(String id) { public Indexed(String id) {
super(id, ScriptType.INDEXED); super(id, ScriptType.STORED);
} }
@Override @Override

View File

@ -5,6 +5,7 @@
*/ */
package org.elasticsearch.watcher.support.init.proxy; package org.elasticsearch.watcher.support.init.proxy;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Injector; import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.script.CompiledScript; import org.elasticsearch.script.CompiledScript;
import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.ExecutableScript;
@ -27,14 +28,16 @@ public class ScriptServiceProxy implements LazyInitializable {
private ScriptService service; private ScriptService service;
private SecurityContext securityContext; private SecurityContext securityContext;
private ClusterService clusterService;
/** /**
* Creates a proxy to the given script service (can be used for testing) * Creates a proxy to the given script service (can be used for testing)
*/ */
public static ScriptServiceProxy of(ScriptService service) { public static ScriptServiceProxy of(ScriptService service, ClusterService clusterService) {
ScriptServiceProxy proxy = new ScriptServiceProxy(); ScriptServiceProxy proxy = new ScriptServiceProxy();
proxy.service = service; proxy.service = service;
proxy.securityContext = SecurityContext.Insecure.INSTANCE; proxy.securityContext = SecurityContext.Insecure.INSTANCE;
proxy.clusterService = clusterService;
return proxy; return proxy;
} }
@ -42,16 +45,16 @@ public class ScriptServiceProxy implements LazyInitializable {
public void init(Injector injector) { public void init(Injector injector) {
this.service = injector.getInstance(ScriptService.class); this.service = injector.getInstance(ScriptService.class);
this.securityContext = injector.getInstance(SecurityContext.class); this.securityContext = injector.getInstance(SecurityContext.class);
this.clusterService = injector.getInstance(ClusterService.class);
} }
public CompiledScript compile(Script script) { public CompiledScript compile(Script script) {
return securityContext.executeAs(XPackUser.INSTANCE, () -> return compile(new org.elasticsearch.script.Script(script.script(), script.type(), script.lang(), script.params()), emptyMap());
compile(new org.elasticsearch.script.Script(script.script(), script.type(), script.lang(), script.params()), emptyMap()));
} }
public CompiledScript compile(org.elasticsearch.script.Script script, Map<String, String> compileParams) { public CompiledScript compile(org.elasticsearch.script.Script script, Map<String, String> compileParams) {
return securityContext.executeAs(XPackUser.INSTANCE, () -> return securityContext.executeAs(XPackUser.INSTANCE, () ->
service.compile(script, WatcherScriptContext.CTX, compileParams)); service.compile(script, WatcherScriptContext.CTX, compileParams, clusterService.state()));
} }
public ExecutableScript executable(CompiledScript compiledScript, Map<String, Object> vars) { public ExecutableScript executable(CompiledScript compiledScript, Map<String, Object> vars) {
@ -59,12 +62,6 @@ public class ScriptServiceProxy implements LazyInitializable {
service.executable(compiledScript, vars)); service.executable(compiledScript, vars));
} }
public ExecutableScript executable(org.elasticsearch.script.Script script) {
return securityContext.executeAs(XPackUser.INSTANCE, () ->
service.executable(script, WatcherScriptContext.CTX, emptyMap()));
}
public static final ScriptContext.Plugin INSTANCE = new ScriptContext.Plugin("xpack", "watch"); public static final ScriptContext.Plugin INSTANCE = new ScriptContext.Plugin("xpack", "watch");
private static class WatcherScriptContext implements ScriptContext { private static class WatcherScriptContext implements ScriptContext {

View File

@ -99,8 +99,8 @@ public class TextTemplate implements ToXContent {
case FILE: case FILE:
builder.field(Field.FILE.getPreferredName(), template); builder.field(Field.FILE.getPreferredName(), template);
break; break;
default: // INDEXED default: // STORED
assert type == ScriptType.INDEXED : "template type [" + type + "] is not supported"; assert type == ScriptType.STORED : "template type [" + type + "] is not supported";
builder.field(Field.ID.getPreferredName(), template); builder.field(Field.ID.getPreferredName(), template);
} }
if (this.params != null) { if (this.params != null) {
@ -145,7 +145,7 @@ public class TextTemplate implements ToXContent {
currentFieldName, token); currentFieldName, token);
} }
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.ID)) { } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.ID)) {
type = ScriptType.INDEXED; type = ScriptType.STORED;
if (token == XContentParser.Token.VALUE_STRING) { if (token == XContentParser.Token.VALUE_STRING) {
template = parser.text(); template = parser.text();
} else { } else {
@ -242,7 +242,7 @@ public class TextTemplate implements ToXContent {
public static class Indexed extends Builder<Indexed> { public static class Indexed extends Builder<Indexed> {
public Indexed(String id) { public Indexed(String id) {
super(id, ScriptType.INDEXED); super(id, ScriptType.STORED);
} }
@Override @Override

View File

@ -124,7 +124,7 @@ public class ScriptConditionTests extends ESTestCase {
ScriptType scriptType = randomFrom(ScriptType.values()); ScriptType scriptType = randomFrom(ScriptType.values());
String script; String script;
switch (scriptType) { switch (scriptType) {
case INDEXED: case STORED:
case FILE: case FILE:
script = "nonExisting_script"; script = "nonExisting_script";
break; break;
@ -212,7 +212,7 @@ public class ScriptConditionTests extends ESTestCase {
case FILE: case FILE:
builder.field("file", script); builder.field("file", script);
break; break;
case INDEXED: case STORED:
builder.field("id", script); builder.field("id", script);
break; break;
default: default:

View File

@ -125,7 +125,7 @@ public class WatcherUtilsTests extends ESTestCase {
Template template = randomFrom( Template template = randomFrom(
new Template(text, ScriptType.INLINE, null, null, params), new Template(text, ScriptType.INLINE, null, null, params),
new Template(text, ScriptType.FILE, null, null, params), new Template(text, ScriptType.FILE, null, null, params),
new Template(text, ScriptType.INDEXED, null, null, params) new Template(text, ScriptType.STORED, null, null, params)
); );
expectedRequest.template(template); expectedRequest.template(template);
} }

View File

@ -114,7 +114,7 @@ public class TextTemplateTests extends ESTestCase {
case FILE: case FILE:
builder.field("file", template.getTemplate()); builder.field("file", template.getTemplate());
break; break;
case INDEXED: case STORED:
builder.field("id", template.getTemplate()); builder.field("id", template.getTemplate());
} }
builder.field("params", template.getParams()); builder.field("params", template.getParams());
@ -173,7 +173,7 @@ public class TextTemplateTests extends ESTestCase {
public void testParserInvalidMissingText() throws Exception { public void testParserInvalidMissingText() throws Exception {
XContentBuilder builder = jsonBuilder().startObject() XContentBuilder builder = jsonBuilder().startObject()
.field("type", ScriptType.INDEXED) .field("type", ScriptType.STORED)
.startObject("params").endObject() .startObject("params").endObject()
.endObject(); .endObject();
BytesReference bytes = builder.bytes(); BytesReference bytes = builder.bytes();
@ -195,7 +195,7 @@ public class TextTemplateTests extends ESTestCase {
switch (type) { switch (type) {
case INLINE: return TextTemplate.inline(text); case INLINE: return TextTemplate.inline(text);
case FILE: return TextTemplate.file(text); case FILE: return TextTemplate.file(text);
case INDEXED: return TextTemplate.indexed(text); case STORED: return TextTemplate.indexed(text);
default: default:
throw illegalArgument("unsupported script type [{}]", type); throw illegalArgument("unsupported script type [{}]", type);
} }

View File

@ -668,7 +668,8 @@ public abstract class AbstractWatcherIntegrationTestCase extends ESIntegTestCase
"test:\n" + // a user for the test infra. "test:\n" + // a user for the test infra.
" cluster: [ 'cluster:monitor/nodes/info', 'cluster:monitor/state', 'cluster:monitor/health', 'cluster:monitor/stats'," + " cluster: [ 'cluster:monitor/nodes/info', 'cluster:monitor/state', 'cluster:monitor/health', 'cluster:monitor/stats'," +
" 'cluster:admin/settings/update', 'cluster:admin/repository/delete', 'cluster:monitor/nodes/liveness'," + " 'cluster:admin/settings/update', 'cluster:admin/repository/delete', 'cluster:monitor/nodes/liveness'," +
" 'indices:admin/template/get', 'indices:admin/template/put', 'indices:admin/template/delete' ]\n" + " 'indices:admin/template/get', 'indices:admin/template/put', 'indices:admin/template/delete'," +
" 'cluster:admin/script/put' ]\n" +
" indices:\n" + " indices:\n" +
" - names: '*'\n" + " - names: '*'\n" +
" privileges: [ all ]\n" + " privileges: [ all ]\n" +

View File

@ -8,6 +8,9 @@ package org.elasticsearch.watcher.test;
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
@ -67,6 +70,7 @@ import org.elasticsearch.watcher.watch.Watch;
import org.elasticsearch.watcher.watch.WatchStatus; import org.elasticsearch.watcher.watch.WatchStatus;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.mockito.Mockito;
import javax.mail.internet.AddressException; import javax.mail.internet.AddressException;
import java.io.IOException; import java.io.IOException;
@ -248,8 +252,11 @@ public final class WatcherTestUtils {
ScriptEngineRegistry scriptEngineRegistry = ScriptEngineRegistry scriptEngineRegistry =
new ScriptEngineRegistry(Collections.emptyList()); new ScriptEngineRegistry(Collections.emptyList());
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry); ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
ClusterService clusterService = Mockito.mock(ClusterService.class);
Mockito.when(clusterService.state()).thenReturn(ClusterState.builder(new ClusterName("_name")).build());
return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings), Collections.emptySet(), return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings), Collections.emptySet(),
new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings)); new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings),
clusterService);
} }
public static SearchType getRandomSupportedSearchType() { public static SearchType getRandomSupportedSearchType() {

View File

@ -257,14 +257,13 @@ public class BasicWatcherTests extends AbstractWatcherIntegrationTestCase {
public void testConditionSearchWithIndexedTemplate() throws Exception { public void testConditionSearchWithIndexedTemplate() throws Exception {
SearchSourceBuilder searchSourceBuilder = searchSource().query(matchQuery("level", "a")); SearchSourceBuilder searchSourceBuilder = searchSource().query(matchQuery("level", "a"));
client().preparePutIndexedScript() client().admin().cluster().preparePutStoredScript()
.setScriptLang("mustache") .setScriptLang("mustache")
.setId("my-template") .setId("my-template")
.setSource(jsonBuilder().startObject().field("template").value(searchSourceBuilder).endObject()) .setSource(jsonBuilder().startObject().field("template").value(searchSourceBuilder).endObject().bytes())
.get(); .get();
refresh();
Template template = new Template("my-template", ScriptType.INDEXED, null, null, null); Template template = new Template("my-template", ScriptType.STORED, null, null, null);
SearchRequest searchRequest = newInputSearchRequest("events"); SearchRequest searchRequest = newInputSearchRequest("events");
// TODO (2.0 upgrade): move back to BytesReference instead of coverting to a string // TODO (2.0 upgrade): move back to BytesReference instead of coverting to a string
searchRequest.template(template); searchRequest.template(template);

View File

@ -8,6 +8,7 @@ package org.elasticsearch.watcher.transform;
import org.apache.lucene.util.LuceneTestCase.AwaitsFix; import org.apache.lucene.util.LuceneTestCase.AwaitsFix;
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -68,7 +69,10 @@ public class TransformIntegrationTests extends AbstractWatcherIntegrationTestCas
script = Script.inline("return [key3 : ctx.payload.key1 + ctx.payload.key2]").lang("groovy").build(); script = Script.inline("return [key3 : ctx.payload.key1 + ctx.payload.key2]").lang("groovy").build();
} else if (randomBoolean()) { } else if (randomBoolean()) {
logger.info("testing script transform with an indexed script"); logger.info("testing script transform with an indexed script");
client().preparePutIndexedScript("groovy", "_id", "{\"script\" : \"return [key3 : ctx.payload.key1 + ctx.payload.key2]\"}") client().admin().cluster().preparePutStoredScript()
.setId("id")
.setScriptLang("groovy")
.setSource(new BytesArray("{\"script\" : \"return [key3 : ctx.payload.key1 + ctx.payload.key2]\"}"))
.get(); .get();
script = Script.indexed("_id").lang("groovy").build(); script = Script.indexed("_id").lang("groovy").build();
} else { } else {

View File

@ -174,7 +174,7 @@ public class ScriptTransformTests extends ESTestCase {
ScriptType scriptType = randomFrom(ScriptType.values()); ScriptType scriptType = randomFrom(ScriptType.values());
String script; String script;
switch (scriptType) { switch (scriptType) {
case INDEXED: case STORED:
case FILE: case FILE:
script = "nonExisting_script"; script = "nonExisting_script";
break; break;
@ -227,7 +227,7 @@ public class ScriptTransformTests extends ESTestCase {
switch (type) { switch (type) {
case INLINE: return Script.inline(script); case INLINE: return Script.inline(script);
case FILE: return Script.file(script); case FILE: return Script.file(script);
case INDEXED: return Script.indexed(script); case STORED: return Script.indexed(script);
default: default:
throw illegalArgument("unsupported script type [{}]", type); throw illegalArgument("unsupported script type [{}]", type);
} }
@ -237,7 +237,7 @@ public class ScriptTransformTests extends ESTestCase {
switch (type) { switch (type) {
case INLINE: return "inline"; case INLINE: return "inline";
case FILE: return "file"; case FILE: return "file";
case INDEXED: return "id"; case STORED: return "id";
default: default:
throw illegalArgument("unsupported script type [{}]", type); throw illegalArgument("unsupported script type [{}]", type);
} }