From b9515357fa158b7b82938207f58240e5336eb1ec Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 21 Apr 2016 15:16:28 +0200 Subject: [PATCH] Migrated from indexed scripts to store scripts Original commit: elastic/x-pack-elasticsearch@a0218f1c9e25c16dcefd286786a5e2ffee9f9de4 --- .../messy/tests/MessyTestUtils.java | 9 ++++++++- .../messy/tests/ScriptConditionTests.java | 4 ++-- .../elasticsearch/messy/tests/TransformIT.java | 6 +++++- .../messy/tests/SearchInputIT.java | 16 ++++++++++------ .../messy/tests/SearchTransformIT.java | 17 +++++++++++------ .../messy/tests/ShieldCachePermissionIT.java | 9 +++++---- .../smoketest/WatcherTemplateTests.java | 7 ++++++- .../org/elasticsearch/transport/actions | 6 +++--- .../elasticsearch/watcher/support/Script.java | 6 +++--- .../support/init/proxy/ScriptServiceProxy.java | 17 +++++++---------- .../watcher/support/text/TextTemplate.java | 8 ++++---- .../condition/script/ScriptConditionTests.java | 4 ++-- .../watcher/support/WatcherUtilsTests.java | 2 +- .../watcher/support/text/TextTemplateTests.java | 6 +++--- .../AbstractWatcherIntegrationTestCase.java | 3 ++- .../watcher/test/WatcherTestUtils.java | 9 ++++++++- .../test/integration/BasicWatcherTests.java | 7 +++---- .../transform/TransformIntegrationTests.java | 6 +++++- .../transform/script/ScriptTransformTests.java | 6 +++--- 19 files changed, 91 insertions(+), 57 deletions(-) diff --git a/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/MessyTestUtils.java b/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/MessyTestUtils.java index 653d4b794d9..7f1510ec1a5 100644 --- a/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/MessyTestUtils.java +++ b/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/MessyTestUtils.java @@ -6,6 +6,9 @@ package org.elasticsearch.messy.tests; 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.settings.Settings; import org.elasticsearch.env.Environment; @@ -19,6 +22,7 @@ import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy; import org.junit.Ignore; +import org.mockito.Mockito; import java.util.Arrays; import java.util.HashSet; @@ -43,8 +47,11 @@ public final class MessyTestUtils { ); 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); 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); } } diff --git a/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/ScriptConditionTests.java b/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/ScriptConditionTests.java index b0db5da4114..556b1dc52a4 100644 --- a/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/ScriptConditionTests.java +++ b/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/ScriptConditionTests.java @@ -124,7 +124,7 @@ public class ScriptConditionTests extends ESTestCase { ScriptType scriptType = randomFrom(ScriptType.values()); String script; switch (scriptType) { - case INDEXED: + case STORED: case FILE: script = "nonExisting_script"; break; @@ -212,7 +212,7 @@ public class ScriptConditionTests extends ESTestCase { case FILE: builder.field("file", script); break; - case INDEXED: + case STORED: builder.field("id", script); break; default: diff --git a/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/TransformIT.java b/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/TransformIT.java index 6fc32aecf91..e48017531ec 100644 --- a/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/TransformIT.java +++ b/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/TransformIT.java @@ -7,6 +7,7 @@ package org.elasticsearch.messy.tests; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.io.Streams; 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(); } else if (randomBoolean()) { 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(); script = Script.indexed("_id").lang("groovy").build(); } else { diff --git a/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/SearchInputIT.java b/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/SearchInputIT.java index bf916cffa67..d941ac4f802 100644 --- a/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/SearchInputIT.java +++ b/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/SearchInputIT.java @@ -5,9 +5,10 @@ */ 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.SearchType; +import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; @@ -192,14 +193,17 @@ public class SearchInputIT extends ESIntegTestCase { public void testSearchIndexedTemplate() throws Exception { WatchExecutionContext ctx = createContext(); - PutIndexedScriptRequest indexedScriptRequest = client().preparePutIndexedScript("mustache","test-template", - TEMPLATE_QUERY).request(); - assertThat(client().putIndexedScript(indexedScriptRequest).actionGet().isCreated(), is(true)); + PutStoredScriptRequest indexedScriptRequest = client().admin().cluster().preparePutStoredScript() + .setId("test-template") + .setScriptLang("mustache") + .setSource(new BytesArray(TEMPLATE_QUERY)) + .request(); + assertThat(client().admin().cluster().putStoredScript(indexedScriptRequest).actionGet().isAcknowledged(), is(true)); Map params = new HashMap<>(); 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(); SearchRequest request = client().prepareSearch().setSearchType(ExecutableSearchInput.DEFAULT_SEARCH_TYPE) @@ -209,7 +213,7 @@ public class SearchInputIT extends ESIntegTestCase { Template resultTemplate = executedResult.executedRequest().template(); assertThat(resultTemplate, notNullValue()); assertThat(resultTemplate.getScript(), equalTo("test-template")); - assertThat(resultTemplate.getType(), equalTo(ScriptType.INDEXED)); + assertThat(resultTemplate.getType(), equalTo(ScriptType.STORED)); } public void testSearchOnDiskTemplate() throws Exception { diff --git a/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/SearchTransformIT.java b/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/SearchTransformIT.java index 1519bea48ab..ee5f7e57920 100644 --- a/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/SearchTransformIT.java +++ b/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/SearchTransformIT.java @@ -5,12 +5,13 @@ */ 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.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.Requests; import org.elasticsearch.common.Base64; +import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.Streams; 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}}\"," + "\"include_lower\":true,\"include_upper\":true}}}]}}}"; - PutIndexedScriptRequest indexedScriptRequest = client().preparePutIndexedScript("mustache", "test-script", templateQuery).request(); - assertThat(client().putIndexedScript(indexedScriptRequest).actionGet().isCreated(), is(true)); + PutStoredScriptRequest indexedScriptRequest = client().admin().cluster().preparePutStoredScript() + .setId("mustache") + .setScriptLang("test-script") + .setSource(new BytesArray(templateQuery)) + .request(); + assertThat(client().admin().cluster().putStoredScript(indexedScriptRequest).actionGet().isAcknowledged(), is(true)); Map params = new HashMap<>(); params.put("seconds_param", "30s"); @@ -397,7 +402,7 @@ public class SearchTransformIT extends ESIntegTestCase { BytesReference templateSource = jsonBuilder() .value(TextTemplate.indexed("test-script").params(params).build()) .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() .prepareSearch() @@ -411,7 +416,7 @@ public class SearchTransformIT extends ESIntegTestCase { Template resultTemplate = result.executedRequest().template(); assertThat(resultTemplate, notNullValue()); assertThat(resultTemplate.getScript(), equalTo("test-script")); - assertThat(resultTemplate.getType(), equalTo(ScriptType.INDEXED)); + assertThat(resultTemplate.getType(), equalTo(ScriptType.STORED)); } public void testSearchOnDiskTemplate() throws Exception { @@ -466,7 +471,7 @@ public class SearchTransformIT extends ESIntegTestCase { new ExecutableAlwaysCondition(logger), null, null, - new ExecutableActions(new ArrayList()), + new ExecutableActions(new ArrayList<>()), null, new WatchStatus( new DateTime(40000, UTC), emptyMap())), new DateTime(60000, UTC), diff --git a/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/ShieldCachePermissionIT.java b/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/ShieldCachePermissionIT.java index ba3471e61c5..2266c798528 100644 --- a/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/ShieldCachePermissionIT.java +++ b/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/ShieldCachePermissionIT.java @@ -9,6 +9,7 @@ import org.elasticsearch.Version; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.search.SearchPhaseExecutionException; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.indices.TermsLookup; import org.elasticsearch.plugins.Plugin; @@ -72,7 +73,7 @@ public class ShieldCachePermissionIT extends ShieldIntegTestCase { public void loadData() { index("data", "a", "1", "{ \"name\": \"John\", \"token\": \"token1\" }"); 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" + " \"query\": {\n" + " \"exists\": {\n" + @@ -80,7 +81,7 @@ public class ShieldCachePermissionIT extends ShieldIntegTestCase { " }\n" + " }\n" + " }\n" + - "}") + "}")) .setScriptLang("mustache") .setId("testTemplate") .execute().actionGet(); @@ -110,7 +111,7 @@ public class ShieldCachePermissionIT extends ShieldIntegTestCase { public void testThatScriptServiceDoesntLeakData() { 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.singletonMap("name", "token"))) .execute().actionGet(); 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, new SecuredString("changeme".toCharArray())))) .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.singletonMap("name", "token"))) .execute().actionGet(); fail("search phase exception should have been thrown! response was:\n" + response.toString()); diff --git a/elasticsearch/qa/smoke-test-watcher-with-mustache/src/test/java/org/elasticsearch/smoketest/WatcherTemplateTests.java b/elasticsearch/qa/smoke-test-watcher-with-mustache/src/test/java/org/elasticsearch/smoketest/WatcherTemplateTests.java index 4098ae21366..75b6b155756 100644 --- a/elasticsearch/qa/smoke-test-watcher-with-mustache/src/test/java/org/elasticsearch/smoketest/WatcherTemplateTests.java +++ b/elasticsearch/qa/smoke-test-watcher-with-mustache/src/test/java/org/elasticsearch/smoketest/WatcherTemplateTests.java @@ -6,6 +6,9 @@ package org.elasticsearch.smoketest; 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.settings.Settings; import org.elasticsearch.common.xcontent.XContentType; @@ -59,7 +62,9 @@ public class WatcherTemplateTests extends ESTestCase { ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, registry); ScriptService scriptService = new ScriptService(setting, environment, engines, resourceWatcherService, scriptEngineRegistry, 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 { diff --git a/elasticsearch/x-pack/shield/src/test/resources/org/elasticsearch/transport/actions b/elasticsearch/x-pack/shield/src/test/resources/org/elasticsearch/transport/actions index 376da2fd067..d2d08bf1977 100644 --- a/elasticsearch/x-pack/shield/src/test/resources/org/elasticsearch/transport/actions +++ b/elasticsearch/x-pack/shield/src/test/resources/org/elasticsearch/transport/actions @@ -62,7 +62,7 @@ indices:data/read/mpercolate indices:data/read/msearch indices:data/read/mtv indices:data/read/percolate -indices:data/read/script/get +cluster:admin/script/get indices:data/read/scroll indices:data/read/scroll/clear indices:data/read/search @@ -70,8 +70,8 @@ indices:data/read/tv indices:data/write/bulk indices:data/write/delete indices:data/write/index -indices:data/write/script/delete -indices:data/write/script/put +cluster:admin/script/delete +cluster:admin/script/put indices:data/write/update cluster:monitor/xpack/info cluster:monitor/xpack/license/get diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/Script.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/Script.java index c54facdb863..811ace3c28b 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/Script.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/Script.java @@ -94,7 +94,7 @@ public class Script implements ToXContent { builder.field(Field.FILE.getPreferredName(), script); break; 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); } if (lang != null) { @@ -141,7 +141,7 @@ public class Script implements ToXContent { token); } } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.ID)) { - type = ScriptType.INDEXED; + type = ScriptType.STORED; if (token == XContentParser.Token.VALUE_STRING) { script = parser.text(); } else { @@ -240,7 +240,7 @@ public class Script implements ToXContent { public static class Indexed extends Builder { public Indexed(String id) { - super(id, ScriptType.INDEXED); + super(id, ScriptType.STORED); } @Override diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/init/proxy/ScriptServiceProxy.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/init/proxy/ScriptServiceProxy.java index 5da4d7c87e3..3df32ca2524 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/init/proxy/ScriptServiceProxy.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/init/proxy/ScriptServiceProxy.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.watcher.support.init.proxy; +import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.inject.Injector; import org.elasticsearch.script.CompiledScript; import org.elasticsearch.script.ExecutableScript; @@ -27,14 +28,16 @@ public class ScriptServiceProxy implements LazyInitializable { private ScriptService service; private SecurityContext securityContext; + private ClusterService clusterService; /** * 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(); proxy.service = service; proxy.securityContext = SecurityContext.Insecure.INSTANCE; + proxy.clusterService = clusterService; return proxy; } @@ -42,16 +45,16 @@ public class ScriptServiceProxy implements LazyInitializable { public void init(Injector injector) { this.service = injector.getInstance(ScriptService.class); this.securityContext = injector.getInstance(SecurityContext.class); + this.clusterService = injector.getInstance(ClusterService.class); } public CompiledScript compile(Script script) { - return securityContext.executeAs(XPackUser.INSTANCE, () -> - compile(new org.elasticsearch.script.Script(script.script(), script.type(), script.lang(), script.params()), emptyMap())); + return compile(new org.elasticsearch.script.Script(script.script(), script.type(), script.lang(), script.params()), emptyMap()); } public CompiledScript compile(org.elasticsearch.script.Script script, Map compileParams) { 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 vars) { @@ -59,12 +62,6 @@ public class ScriptServiceProxy implements LazyInitializable { 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"); private static class WatcherScriptContext implements ScriptContext { diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/text/TextTemplate.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/text/TextTemplate.java index 37f4d3aaec2..d8159f67283 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/text/TextTemplate.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/text/TextTemplate.java @@ -99,8 +99,8 @@ public class TextTemplate implements ToXContent { case FILE: builder.field(Field.FILE.getPreferredName(), template); break; - default: // INDEXED - assert type == ScriptType.INDEXED : "template type [" + type + "] is not supported"; + default: // STORED + assert type == ScriptType.STORED : "template type [" + type + "] is not supported"; builder.field(Field.ID.getPreferredName(), template); } if (this.params != null) { @@ -145,7 +145,7 @@ public class TextTemplate implements ToXContent { currentFieldName, token); } } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.ID)) { - type = ScriptType.INDEXED; + type = ScriptType.STORED; if (token == XContentParser.Token.VALUE_STRING) { template = parser.text(); } else { @@ -242,7 +242,7 @@ public class TextTemplate implements ToXContent { public static class Indexed extends Builder { public Indexed(String id) { - super(id, ScriptType.INDEXED); + super(id, ScriptType.STORED); } @Override diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/condition/script/ScriptConditionTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/condition/script/ScriptConditionTests.java index daec5020dc3..2c325fb2024 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/condition/script/ScriptConditionTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/condition/script/ScriptConditionTests.java @@ -124,7 +124,7 @@ public class ScriptConditionTests extends ESTestCase { ScriptType scriptType = randomFrom(ScriptType.values()); String script; switch (scriptType) { - case INDEXED: + case STORED: case FILE: script = "nonExisting_script"; break; @@ -212,7 +212,7 @@ public class ScriptConditionTests extends ESTestCase { case FILE: builder.field("file", script); break; - case INDEXED: + case STORED: builder.field("id", script); break; default: diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/WatcherUtilsTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/WatcherUtilsTests.java index 0425e3403a5..131f08c59b6 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/WatcherUtilsTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/WatcherUtilsTests.java @@ -125,7 +125,7 @@ public class WatcherUtilsTests extends ESTestCase { Template template = randomFrom( new Template(text, ScriptType.INLINE, 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); } diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/text/TextTemplateTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/text/TextTemplateTests.java index 2731fcd8501..2185815f4cc 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/text/TextTemplateTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/text/TextTemplateTests.java @@ -114,7 +114,7 @@ public class TextTemplateTests extends ESTestCase { case FILE: builder.field("file", template.getTemplate()); break; - case INDEXED: + case STORED: builder.field("id", template.getTemplate()); } builder.field("params", template.getParams()); @@ -173,7 +173,7 @@ public class TextTemplateTests extends ESTestCase { public void testParserInvalidMissingText() throws Exception { XContentBuilder builder = jsonBuilder().startObject() - .field("type", ScriptType.INDEXED) + .field("type", ScriptType.STORED) .startObject("params").endObject() .endObject(); BytesReference bytes = builder.bytes(); @@ -195,7 +195,7 @@ public class TextTemplateTests extends ESTestCase { switch (type) { case INLINE: return TextTemplate.inline(text); case FILE: return TextTemplate.file(text); - case INDEXED: return TextTemplate.indexed(text); + case STORED: return TextTemplate.indexed(text); default: throw illegalArgument("unsupported script type [{}]", type); } diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/AbstractWatcherIntegrationTestCase.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/AbstractWatcherIntegrationTestCase.java index c57ac04e35d..03931ff18c5 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/AbstractWatcherIntegrationTestCase.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/AbstractWatcherIntegrationTestCase.java @@ -668,7 +668,8 @@ public abstract class AbstractWatcherIntegrationTestCase extends ESIntegTestCase "test:\n" + // a user for the test infra. " 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'," + - " '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" + " - names: '*'\n" + " privileges: [ all ]\n" + diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/WatcherTestUtils.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/WatcherTestUtils.java index 7bd9ffef2af..4d9758c9e57 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/WatcherTestUtils.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/WatcherTestUtils.java @@ -8,6 +8,9 @@ package org.elasticsearch.watcher.test; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchType; 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.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; @@ -67,6 +70,7 @@ import org.elasticsearch.watcher.watch.Watch; import org.elasticsearch.watcher.watch.WatchStatus; import org.hamcrest.Matcher; import org.joda.time.DateTime; +import org.mockito.Mockito; import javax.mail.internet.AddressException; import java.io.IOException; @@ -248,8 +252,11 @@ public final class WatcherTestUtils { ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.emptyList()); 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(), - new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings)); + new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings), + clusterService); } public static SearchType getRandomSupportedSearchType() { diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java index d97157a1da2..c6616b66132 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java @@ -257,14 +257,13 @@ public class BasicWatcherTests extends AbstractWatcherIntegrationTestCase { public void testConditionSearchWithIndexedTemplate() throws Exception { SearchSourceBuilder searchSourceBuilder = searchSource().query(matchQuery("level", "a")); - client().preparePutIndexedScript() + client().admin().cluster().preparePutStoredScript() .setScriptLang("mustache") .setId("my-template") - .setSource(jsonBuilder().startObject().field("template").value(searchSourceBuilder).endObject()) + .setSource(jsonBuilder().startObject().field("template").value(searchSourceBuilder).endObject().bytes()) .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"); // TODO (2.0 upgrade): move back to BytesReference instead of coverting to a string searchRequest.template(template); diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/transform/TransformIntegrationTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/transform/TransformIntegrationTests.java index f6d0a439cf8..de3344394e1 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/transform/TransformIntegrationTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/transform/TransformIntegrationTests.java @@ -8,6 +8,7 @@ package org.elasticsearch.watcher.transform; import org.apache.lucene.util.LuceneTestCase.AwaitsFix; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.io.Streams; 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(); } else if (randomBoolean()) { 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(); script = Script.indexed("_id").lang("groovy").build(); } else { diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/transform/script/ScriptTransformTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/transform/script/ScriptTransformTests.java index 63585cc9c31..71b0dbc4801 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/transform/script/ScriptTransformTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/transform/script/ScriptTransformTests.java @@ -174,7 +174,7 @@ public class ScriptTransformTests extends ESTestCase { ScriptType scriptType = randomFrom(ScriptType.values()); String script; switch (scriptType) { - case INDEXED: + case STORED: case FILE: script = "nonExisting_script"; break; @@ -227,7 +227,7 @@ public class ScriptTransformTests extends ESTestCase { switch (type) { case INLINE: return Script.inline(script); case FILE: return Script.file(script); - case INDEXED: return Script.indexed(script); + case STORED: return Script.indexed(script); default: throw illegalArgument("unsupported script type [{}]", type); } @@ -237,7 +237,7 @@ public class ScriptTransformTests extends ESTestCase { switch (type) { case INLINE: return "inline"; case FILE: return "file"; - case INDEXED: return "id"; + case STORED: return "id"; default: throw illegalArgument("unsupported script type [{}]", type); }