Clean up of Script.
Closes elastic/elasticsearch#3982 Original commit: elastic/x-pack-elasticsearch@96c94ae8d5
This commit is contained in:
parent
2081399738
commit
0977935989
|
@ -15,6 +15,7 @@ import org.elasticsearch.script.Script;
|
||||||
import org.elasticsearch.script.ScriptType;
|
import org.elasticsearch.script.ScriptType;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@ -27,8 +28,6 @@ import java.util.Objects;
|
||||||
*/
|
*/
|
||||||
public class TextTemplate implements ToXContent {
|
public class TextTemplate implements ToXContent {
|
||||||
|
|
||||||
public static final String DEFAULT_TEMPLATE_LANG = "mustache";
|
|
||||||
|
|
||||||
private final Script script;
|
private final Script script;
|
||||||
private final String inlineTemplate;
|
private final String inlineTemplate;
|
||||||
|
|
||||||
|
@ -39,7 +38,14 @@ public class TextTemplate implements ToXContent {
|
||||||
|
|
||||||
public TextTemplate(String template, @Nullable XContentType contentType, ScriptType type,
|
public TextTemplate(String template, @Nullable XContentType contentType, ScriptType type,
|
||||||
@Nullable Map<String, Object> params) {
|
@Nullable Map<String, Object> params) {
|
||||||
this.script = new Script(template, type, DEFAULT_TEMPLATE_LANG, params, contentType);
|
Map<String, String> options = new HashMap<>();
|
||||||
|
if (contentType != null) {
|
||||||
|
options.put(Script.CONTENT_TYPE_OPTION, contentType.mediaType());
|
||||||
|
}
|
||||||
|
if (params == null) {
|
||||||
|
params = new HashMap<>();
|
||||||
|
}
|
||||||
|
this.script = new Script(type, Script.DEFAULT_TEMPLATE_LANG, template, options, params);
|
||||||
this.inlineTemplate = null;
|
this.inlineTemplate = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,11 +59,21 @@ public class TextTemplate implements ToXContent {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTemplate() {
|
public String getTemplate() {
|
||||||
return script != null ? script.getScript() : inlineTemplate;
|
return script != null ? script.getIdOrCode() : inlineTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public XContentType getContentType() {
|
public XContentType getContentType() {
|
||||||
return script != null ? script.getContentType() : null;
|
if (script == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String mediaType = script.getOptions().get(Script.CONTENT_TYPE_OPTION);
|
||||||
|
|
||||||
|
if (mediaType == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return XContentType.fromMediaTypeOrFormat(mediaType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScriptType getType() {
|
public ScriptType getType() {
|
||||||
|
@ -97,7 +113,7 @@ public class TextTemplate implements ToXContent {
|
||||||
if (parser.currentToken() == XContentParser.Token.VALUE_STRING) {
|
if (parser.currentToken() == XContentParser.Token.VALUE_STRING) {
|
||||||
return new TextTemplate(parser.text());
|
return new TextTemplate(parser.text());
|
||||||
} else {
|
} else {
|
||||||
return new TextTemplate(Script.parse(parser, ParseFieldMatcher.STRICT, DEFAULT_TEMPLATE_LANG));
|
return new TextTemplate(Script.parse(parser, ParseFieldMatcher.STRICT, Script.DEFAULT_TEMPLATE_LANG));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,11 @@ public class TextTemplateEngine extends AbstractComponent {
|
||||||
}
|
}
|
||||||
mergedModel.putAll(model);
|
mergedModel.putAll(model);
|
||||||
|
|
||||||
Script script = new Script(template, textTemplate.getType(), "mustache", mergedModel, textTemplate.getContentType());
|
Map<String, String> options = new HashMap<>();
|
||||||
|
if (textTemplate.getContentType() != null) {
|
||||||
|
options.put(Script.CONTENT_TYPE_OPTION, textTemplate.getContentType().mediaType());
|
||||||
|
}
|
||||||
|
Script script = new Script(textTemplate.getType(), "mustache", template, options, mergedModel);
|
||||||
CompiledScript compiledScript = service.compile(script, Watcher.SCRIPT_CONTEXT, compileParams);
|
CompiledScript compiledScript = service.compile(script, Watcher.SCRIPT_CONTEXT, compileParams);
|
||||||
ExecutableScript executable = service.executable(compiledScript, model);
|
ExecutableScript executable = service.executable(compiledScript, model);
|
||||||
Object result = executable.run();
|
Object result = executable.run();
|
||||||
|
|
|
@ -317,8 +317,8 @@ public class SecurityIndexSearcherWrapper extends IndexSearcherWrapper {
|
||||||
userModel.put("metadata", Collections.unmodifiableMap(user.metadata()));
|
userModel.put("metadata", Collections.unmodifiableMap(user.metadata()));
|
||||||
params.put("_user", userModel);
|
params.put("_user", userModel);
|
||||||
// Always enforce mustache script lang:
|
// Always enforce mustache script lang:
|
||||||
script = new Script(script.getScript(), script.getType(), "mustache", params, script.getContentType());
|
script = new Script(script.getType(), "mustache", script.getIdOrCode(), script.getOptions(), params);
|
||||||
ExecutableScript executable = scriptService.executable(script, ScriptContext.Standard.SEARCH, Collections.emptyMap());
|
ExecutableScript executable = scriptService.executable(script, ScriptContext.Standard.SEARCH);
|
||||||
return (BytesReference) executable.run();
|
return (BytesReference) executable.run();
|
||||||
} else {
|
} else {
|
||||||
return querySource;
|
return querySource;
|
||||||
|
|
|
@ -28,6 +28,7 @@ import org.elasticsearch.xpack.common.text.TextTemplate;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
@ -54,13 +55,13 @@ public class WatcherSearchTemplateRequest implements ToXContent {
|
||||||
this.indicesOptions = indicesOptions;
|
this.indicesOptions = indicesOptions;
|
||||||
// Here we convert a watch search request body into an inline search template,
|
// Here we convert a watch search request body into an inline search template,
|
||||||
// this way if any Watcher related context variables are used, they will get resolved.
|
// this way if any Watcher related context variables are used, they will get resolved.
|
||||||
this.template = new Script(searchSource.utf8ToString(), ScriptType.INLINE, TextTemplate.DEFAULT_TEMPLATE_LANG, null);
|
this.template = new Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, searchSource.utf8ToString(), Collections.emptyMap());
|
||||||
this.searchSource = null;
|
this.searchSource = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public WatcherSearchTemplateRequest(String[] indices, String[] types, SearchType searchType, IndicesOptions indicesOptions,
|
public WatcherSearchTemplateRequest(String[] indices, String[] types, SearchType searchType, IndicesOptions indicesOptions,
|
||||||
Script template) {
|
Script template) {
|
||||||
assert template == null || TextTemplate.DEFAULT_TEMPLATE_LANG.equals(template.getLang());
|
assert template == null || Script.DEFAULT_TEMPLATE_LANG.equals(template.getLang());
|
||||||
this.indices = indices;
|
this.indices = indices;
|
||||||
this.types = types;
|
this.types = types;
|
||||||
this.searchType = searchType;
|
this.searchType = searchType;
|
||||||
|
@ -118,7 +119,7 @@ public class WatcherSearchTemplateRequest implements ToXContent {
|
||||||
if (template != null) {
|
if (template != null) {
|
||||||
return template;
|
return template;
|
||||||
} else {
|
} else {
|
||||||
return new Script(searchSource.utf8ToString(), ScriptType.INLINE, TextTemplate.DEFAULT_TEMPLATE_LANG, null);
|
return new Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, searchSource.utf8ToString(), Collections.emptyMap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,7 +272,7 @@ public class WatcherSearchTemplateRequest implements ToXContent {
|
||||||
indicesOptions = IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandOpen, expandClosed,
|
indicesOptions = IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandOpen, expandClosed,
|
||||||
DEFAULT_INDICES_OPTIONS);
|
DEFAULT_INDICES_OPTIONS);
|
||||||
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, TEMPLATE_FIELD)) {
|
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, TEMPLATE_FIELD)) {
|
||||||
template = Script.parse(parser, ParseFieldMatcher.STRICT, TextTemplate.DEFAULT_TEMPLATE_LANG);
|
template = Script.parse(parser, ParseFieldMatcher.STRICT, Script.DEFAULT_TEMPLATE_LANG);
|
||||||
} else {
|
} else {
|
||||||
throw new ElasticsearchParseException("could not read search request. unexpected object field [" +
|
throw new ElasticsearchParseException("could not read search request. unexpected object field [" +
|
||||||
currentFieldName + "]");
|
currentFieldName + "]");
|
||||||
|
|
|
@ -55,8 +55,8 @@ public class WatcherSearchTemplateService extends AbstractComponent {
|
||||||
watcherContextParams.putAll(source.getParams());
|
watcherContextParams.putAll(source.getParams());
|
||||||
}
|
}
|
||||||
// Templates are always of lang mustache:
|
// Templates are always of lang mustache:
|
||||||
Script template = new Script(source.getScript(), source.getType(), "mustache", watcherContextParams,
|
Script template = new Script(source.getType(), "mustache", source.getIdOrCode(), source.getOptions(), watcherContextParams
|
||||||
source.getContentType());
|
);
|
||||||
CompiledScript compiledScript = scriptService.compile(template, Watcher.SCRIPT_CONTEXT, Collections.emptyMap());
|
CompiledScript compiledScript = scriptService.compile(template, Watcher.SCRIPT_CONTEXT, Collections.emptyMap());
|
||||||
return (BytesReference) scriptService.executable(compiledScript, template.getParams()).run();
|
return (BytesReference) scriptService.executable(compiledScript, template.getParams()).run();
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,6 +109,16 @@ public class WatcherXContentParser implements XContentParser {
|
||||||
return parser.mapOrdered();
|
return parser.mapOrdered();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, String> mapStrings() throws IOException {
|
||||||
|
return parser.mapStrings();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, String> mapStringsOrdered() throws IOException {
|
||||||
|
return parser.mapStringsOrdered();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Object> list() throws IOException {
|
public List<Object> list() throws IOException {
|
||||||
return parser.list();
|
return parser.list();
|
||||||
|
|
|
@ -118,7 +118,7 @@ public class XContentSource implements ToXContent {
|
||||||
|
|
||||||
public static void writeTo(XContentSource source, StreamOutput out) throws IOException {
|
public static void writeTo(XContentSource source, StreamOutput out) throws IOException {
|
||||||
out.writeBytesReference(source.bytes);
|
out.writeBytesReference(source.bytes);
|
||||||
XContentType.writeTo(source.contentType, out);
|
source.contentType.writeTo(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object data() {
|
private Object data() {
|
||||||
|
|
|
@ -86,7 +86,7 @@ public class LatchScriptEngine implements ScriptEngineService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Script latchScript() {
|
public static Script latchScript() {
|
||||||
return new Script("", ScriptType.INLINE, NAME, Collections.emptyMap());
|
return new Script(ScriptType.INLINE, NAME, "", Collections.emptyMap());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.common.text;
|
package org.elasticsearch.xpack.common.text;
|
||||||
|
|
||||||
import org.elasticsearch.ElasticsearchParseException;
|
import org.elasticsearch.common.ParsingException;
|
||||||
import org.elasticsearch.common.bytes.BytesReference;
|
import org.elasticsearch.common.bytes.BytesReference;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
|
@ -27,6 +27,7 @@ import java.util.Map;
|
||||||
import static java.util.Collections.singletonMap;
|
import static java.util.Collections.singletonMap;
|
||||||
import static java.util.Collections.unmodifiableMap;
|
import static java.util.Collections.unmodifiableMap;
|
||||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.hamcrest.Matchers.notNullValue;
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
|
@ -58,7 +59,7 @@ public class TextTemplateTests extends ESTestCase {
|
||||||
ScriptType type = randomFrom(ScriptType.values());
|
ScriptType type = randomFrom(ScriptType.values());
|
||||||
|
|
||||||
CompiledScript compiledScript = mock(CompiledScript.class);
|
CompiledScript compiledScript = mock(CompiledScript.class);
|
||||||
when(service.compile(new Script(templateText, type, lang, merged), Watcher.SCRIPT_CONTEXT,
|
when(service.compile(new Script(type, lang, templateText, merged), Watcher.SCRIPT_CONTEXT,
|
||||||
Collections.singletonMap("content_type", "text/plain"))).thenReturn(compiledScript);
|
Collections.singletonMap("content_type", "text/plain"))).thenReturn(compiledScript);
|
||||||
when(service.executable(compiledScript, model)).thenReturn(script);
|
when(service.executable(compiledScript, model)).thenReturn(script);
|
||||||
when(script.run()).thenReturn("rendered_text");
|
when(script.run()).thenReturn("rendered_text");
|
||||||
|
@ -74,7 +75,7 @@ public class TextTemplateTests extends ESTestCase {
|
||||||
ScriptType scriptType = randomFrom(ScriptType.values());
|
ScriptType scriptType = randomFrom(ScriptType.values());
|
||||||
|
|
||||||
CompiledScript compiledScript = mock(CompiledScript.class);
|
CompiledScript compiledScript = mock(CompiledScript.class);
|
||||||
when(service.compile(new Script(templateText, scriptType, lang, model), Watcher.SCRIPT_CONTEXT,
|
when(service.compile(new Script(scriptType, lang, templateText, model), Watcher.SCRIPT_CONTEXT,
|
||||||
Collections.singletonMap("content_type", "text/plain"))).thenReturn(compiledScript);
|
Collections.singletonMap("content_type", "text/plain"))).thenReturn(compiledScript);
|
||||||
when(service.executable(compiledScript, model)).thenReturn(script);
|
when(service.executable(compiledScript, model)).thenReturn(script);
|
||||||
when(script.run()).thenReturn("rendered_text");
|
when(script.run()).thenReturn("rendered_text");
|
||||||
|
@ -88,7 +89,7 @@ public class TextTemplateTests extends ESTestCase {
|
||||||
Map<String, Object> model = singletonMap("key", "model_val");
|
Map<String, Object> model = singletonMap("key", "model_val");
|
||||||
|
|
||||||
CompiledScript compiledScript = mock(CompiledScript.class);
|
CompiledScript compiledScript = mock(CompiledScript.class);
|
||||||
when(service.compile(new Script(templateText, ScriptType.INLINE, lang, model), Watcher.SCRIPT_CONTEXT,
|
when(service.compile(new Script(ScriptType.INLINE, lang, templateText, model), Watcher.SCRIPT_CONTEXT,
|
||||||
Collections.singletonMap("content_type", "text/plain"))).thenReturn(compiledScript);
|
Collections.singletonMap("content_type", "text/plain"))).thenReturn(compiledScript);
|
||||||
when(service.executable(compiledScript, model)).thenReturn(script);
|
when(service.executable(compiledScript, model)).thenReturn(script);
|
||||||
when(script.run()).thenReturn("rendered_text");
|
when(script.run()).thenReturn("rendered_text");
|
||||||
|
@ -143,8 +144,8 @@ public class TextTemplateTests extends ESTestCase {
|
||||||
try {
|
try {
|
||||||
TextTemplate.parse(parser);
|
TextTemplate.parse(parser);
|
||||||
fail("expected parse exception when encountering an unknown field");
|
fail("expected parse exception when encountering an unknown field");
|
||||||
} catch (ElasticsearchParseException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
assertThat(e.getMessage(), is("unexpected field [unknown_field]"));
|
assertThat(e.getMessage(), containsString("[script] unknown field [unknown_field], parser not found"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,8 +161,8 @@ public class TextTemplateTests extends ESTestCase {
|
||||||
try {
|
try {
|
||||||
TextTemplate.parse(parser);
|
TextTemplate.parse(parser);
|
||||||
fail("expected parse exception when script type is unknown");
|
fail("expected parse exception when script type is unknown");
|
||||||
} catch (ElasticsearchParseException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
assertThat(e.getMessage(), is("unexpected field [template]"));
|
assertThat(e.getMessage(), is("[script] unknown field [template], parser not found"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,8 +177,8 @@ public class TextTemplateTests extends ESTestCase {
|
||||||
try {
|
try {
|
||||||
TextTemplate.parse(parser);
|
TextTemplate.parse(parser);
|
||||||
fail("expected parse exception when template text is missing");
|
fail("expected parse exception when template text is missing");
|
||||||
} catch (ElasticsearchParseException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
assertThat(e.getMessage(), is("unexpected field [type]"));
|
assertThat(e.getMessage(), containsString("[script] unknown field [type], parser not found"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -211,8 +211,8 @@ public class GraphTests extends ESSingleNodeTestCase {
|
||||||
//00s friends of beatles
|
//00s friends of beatles
|
||||||
grb.createNextHop(QueryBuilders.termQuery("decade", "00s")).addVertexRequest("people").size(100).minDocCount(1);
|
grb.createNextHop(QueryBuilders.termQuery("decade", "00s")).addVertexRequest("people").size(100).minDocCount(1);
|
||||||
// A query that should cause a timeout
|
// A query that should cause a timeout
|
||||||
ScriptQueryBuilder timeoutQuery = QueryBuilders.scriptQuery(new Script(NativeTestScriptedTimeout.TEST_NATIVE_SCRIPT_TIMEOUT,
|
ScriptQueryBuilder timeoutQuery = QueryBuilders.scriptQuery(new Script(ScriptType.INLINE, "native",
|
||||||
ScriptType.INLINE, "native", null));
|
NativeTestScriptedTimeout.TEST_NATIVE_SCRIPT_TIMEOUT, Collections.emptyMap()));
|
||||||
grb.createNextHop(timeoutQuery).addVertexRequest("people").size(100).minDocCount(1);
|
grb.createNextHop(timeoutQuery).addVertexRequest("people").size(100).minDocCount(1);
|
||||||
|
|
||||||
GraphExploreResponse response = grb.get();
|
GraphExploreResponse response = grb.get();
|
||||||
|
|
|
@ -587,24 +587,23 @@ public class SecurityIndexSearcherWrapperUnitTests extends ESTestCase {
|
||||||
};
|
};
|
||||||
|
|
||||||
ExecutableScript executableScript = mock(ExecutableScript.class);
|
ExecutableScript executableScript = mock(ExecutableScript.class);
|
||||||
when(scriptService.executable(any(Script.class), eq(ScriptContext.Standard.SEARCH), eq(Collections.emptyMap())))
|
when(scriptService.executable(any(Script.class), eq(ScriptContext.Standard.SEARCH))).thenReturn(executableScript);
|
||||||
.thenReturn(executableScript);
|
|
||||||
|
|
||||||
XContentBuilder builder = jsonBuilder();
|
XContentBuilder builder = jsonBuilder();
|
||||||
String query = new TermQueryBuilder("field", "{{_user.username}}").toXContent(builder, ToXContent.EMPTY_PARAMS).string();
|
String query = new TermQueryBuilder("field", "{{_user.username}}").toXContent(builder, ToXContent.EMPTY_PARAMS).string();
|
||||||
Script script = new Script(query, ScriptType.INLINE, null, Collections.singletonMap("custom", "value"));
|
Script script = new Script(ScriptType.INLINE, "mustache", query, Collections.singletonMap("custom", "value"));
|
||||||
builder = jsonBuilder().startObject().field("template");
|
builder = jsonBuilder().startObject().field("template");
|
||||||
script.toXContent(builder, ToXContent.EMPTY_PARAMS);
|
script.toXContent(builder, ToXContent.EMPTY_PARAMS);
|
||||||
BytesReference querySource = builder.endObject().bytes();
|
BytesReference querySource = builder.endObject().bytes();
|
||||||
|
|
||||||
securityIndexSearcherWrapper.evaluateTemplate(querySource);
|
securityIndexSearcherWrapper.evaluateTemplate(querySource);
|
||||||
ArgumentCaptor<Script> argument = ArgumentCaptor.forClass(Script.class);
|
ArgumentCaptor<Script> argument = ArgumentCaptor.forClass(Script.class);
|
||||||
verify(scriptService).executable(argument.capture(), eq(ScriptContext.Standard.SEARCH), eq(Collections.emptyMap()));
|
verify(scriptService).executable(argument.capture(), eq(ScriptContext.Standard.SEARCH));
|
||||||
Script usedScript = argument.getValue();
|
Script usedScript = argument.getValue();
|
||||||
assertThat(usedScript.getScript(), equalTo(script.getScript()));
|
assertThat(usedScript.getIdOrCode(), equalTo(script.getIdOrCode()));
|
||||||
assertThat(usedScript.getType(), equalTo(script.getType()));
|
assertThat(usedScript.getType(), equalTo(script.getType()));
|
||||||
assertThat(usedScript.getLang(), equalTo("mustache"));
|
assertThat(usedScript.getLang(), equalTo("mustache"));
|
||||||
assertThat(usedScript.getContentType(), equalTo(script.getContentType()));
|
assertThat(usedScript.getOptions(), equalTo(script.getOptions()));
|
||||||
assertThat(usedScript.getParams().size(), equalTo(2));
|
assertThat(usedScript.getParams().size(), equalTo(2));
|
||||||
assertThat(usedScript.getParams().get("custom"), equalTo("value"));
|
assertThat(usedScript.getParams().get("custom"), equalTo("value"));
|
||||||
|
|
||||||
|
|
|
@ -6,9 +6,9 @@
|
||||||
package org.elasticsearch.xpack.watcher.condition;
|
package org.elasticsearch.xpack.watcher.condition;
|
||||||
|
|
||||||
|
|
||||||
import org.elasticsearch.ElasticsearchParseException;
|
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
import org.elasticsearch.action.search.ShardSearchFailure;
|
import org.elasticsearch.action.search.ShardSearchFailure;
|
||||||
|
import org.elasticsearch.common.ParsingException;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
|
@ -104,7 +104,8 @@ public class ScriptConditionTests extends ESTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testExecuteMergedParams() throws Exception {
|
public void testExecuteMergedParams() throws Exception {
|
||||||
Script script = new Script("ctx.payload.hits.total > threshold", ScriptType.INLINE, null, singletonMap("threshold", 1));
|
Script script = new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG,
|
||||||
|
"ctx.payload.hits.total > threshold", singletonMap("threshold", 1));
|
||||||
ScriptCondition executable = new ScriptCondition(script, scriptService);
|
ScriptCondition executable = new ScriptCondition(script, scriptService);
|
||||||
SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 500L, new ShardSearchFailure[0]);
|
SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 500L, new ShardSearchFailure[0]);
|
||||||
WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.XContent(response));
|
WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.XContent(response));
|
||||||
|
@ -143,9 +144,9 @@ public class ScriptConditionTests extends ESTestCase {
|
||||||
try {
|
try {
|
||||||
ScriptCondition.parse(scriptService, "_id", parser, false, defaultScriptLang);
|
ScriptCondition.parse(scriptService, "_id", parser, false, defaultScriptLang);
|
||||||
fail("expected a condition exception trying to parse an invalid condition XContent");
|
fail("expected a condition exception trying to parse an invalid condition XContent");
|
||||||
} catch (ElasticsearchParseException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// TODO add these when the test if fixed
|
assertThat(e.getMessage(),
|
||||||
// assertThat(e.getMessage(), is("ASDF"));
|
containsString("must specify either code for an [inline] script or an id for a [stored] script or [file] script"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -348,7 +348,7 @@ public class ManualExecutionTests extends AbstractWatcherIntegrationTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testWatchExecutionDuration() throws Exception {
|
public void testWatchExecutionDuration() throws Exception {
|
||||||
Script script = new Script("sleep", ScriptType.INLINE, null, singletonMap("millis", 100L));
|
Script script = new Script(ScriptType.INLINE, WATCHER_LANG, "sleep", singletonMap("millis", 100L));
|
||||||
WatchSourceBuilder watchBuilder = watchBuilder()
|
WatchSourceBuilder watchBuilder = watchBuilder()
|
||||||
.trigger(schedule(cron("0 0 0 1 * ? 2099")))
|
.trigger(schedule(cron("0 0 0 1 * ? 2099")))
|
||||||
.input(simpleInput("foo", "bar"))
|
.input(simpleInput("foo", "bar"))
|
||||||
|
@ -364,7 +364,7 @@ public class ManualExecutionTests extends AbstractWatcherIntegrationTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testForceDeletionOfLongRunningWatch() throws Exception {
|
public void testForceDeletionOfLongRunningWatch() throws Exception {
|
||||||
Script script = new Script("sleep", ScriptType.INLINE, null, singletonMap("millis", 10000L));
|
Script script = new Script(ScriptType.INLINE, WATCHER_LANG, "sleep", singletonMap("millis", 10000L));
|
||||||
WatchSourceBuilder watchBuilder = watchBuilder()
|
WatchSourceBuilder watchBuilder = watchBuilder()
|
||||||
.trigger(schedule(cron("0 0 0 1 * ? 2099")))
|
.trigger(schedule(cron("0 0 0 1 * ? 2099")))
|
||||||
.input(simpleInput("foo", "bar"))
|
.input(simpleInput("foo", "bar"))
|
||||||
|
|
|
@ -265,7 +265,7 @@ public class HistoryActionConditionTests extends AbstractWatcherIntegrationTestC
|
||||||
* @return Never {@code null}
|
* @return Never {@code null}
|
||||||
*/
|
*/
|
||||||
private static Condition mockScriptCondition(String inlineScript) {
|
private static Condition mockScriptCondition(String inlineScript) {
|
||||||
Script script = new Script(inlineScript, ScriptType.INLINE, MockScriptPlugin.NAME, null, null);
|
Script script = new Script(ScriptType.INLINE, MockScriptPlugin.NAME, inlineScript, Collections.emptyMap());
|
||||||
return new ScriptCondition(script);
|
return new ScriptCondition(script);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -107,7 +107,7 @@ public class WatcherUtilsTests extends ESTestCase {
|
||||||
}
|
}
|
||||||
String text = randomAsciiOfLengthBetween(1, 5);
|
String text = randomAsciiOfLengthBetween(1, 5);
|
||||||
ScriptType scriptType = randomFrom(ScriptType.values());
|
ScriptType scriptType = randomFrom(ScriptType.values());
|
||||||
expectedTemplate = new Script(text, scriptType, "mustache", params);
|
expectedTemplate = new Script(scriptType, "mustache", text, params);
|
||||||
request = new WatcherSearchTemplateRequest(expectedIndices, expectedTypes, expectedSearchType,
|
request = new WatcherSearchTemplateRequest(expectedIndices, expectedTypes, expectedSearchType,
|
||||||
expectedIndicesOptions, expectedTemplate);
|
expectedIndicesOptions, expectedTemplate);
|
||||||
} else {
|
} else {
|
||||||
|
@ -134,11 +134,11 @@ public class WatcherUtilsTests extends ESTestCase {
|
||||||
assertNotNull(result.getTemplate());
|
assertNotNull(result.getTemplate());
|
||||||
assertThat(result.getTemplate().getLang(), equalTo("mustache"));
|
assertThat(result.getTemplate().getLang(), equalTo("mustache"));
|
||||||
if (expectedSource == null) {
|
if (expectedSource == null) {
|
||||||
assertThat(result.getTemplate().getScript(), equalTo(expectedTemplate.getScript()));
|
assertThat(result.getTemplate().getIdOrCode(), equalTo(expectedTemplate.getIdOrCode()));
|
||||||
assertThat(result.getTemplate().getType(), equalTo(expectedTemplate.getType()));
|
assertThat(result.getTemplate().getType(), equalTo(expectedTemplate.getType()));
|
||||||
assertThat(result.getTemplate().getParams(), equalTo(expectedTemplate.getParams()));
|
assertThat(result.getTemplate().getParams(), equalTo(expectedTemplate.getParams()));
|
||||||
} else {
|
} else {
|
||||||
assertThat(result.getTemplate().getScript(), equalTo(expectedSource.utf8ToString()));
|
assertThat(result.getTemplate().getIdOrCode(), equalTo(expectedSource.utf8ToString()));
|
||||||
assertThat(result.getTemplate().getType(), equalTo(ScriptType.INLINE));
|
assertThat(result.getTemplate().getType(), equalTo(ScriptType.INLINE));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -206,7 +206,7 @@ public class WatcherUtilsTests extends ESTestCase {
|
||||||
}
|
}
|
||||||
String text = randomAsciiOfLengthBetween(1, 5);
|
String text = randomAsciiOfLengthBetween(1, 5);
|
||||||
ScriptType scriptType = randomFrom(ScriptType.values());
|
ScriptType scriptType = randomFrom(ScriptType.values());
|
||||||
template = new Script(text, scriptType, "mustache", params);
|
template = new Script(scriptType, "mustache", text, params);
|
||||||
builder.field("template", template);
|
builder.field("template", template);
|
||||||
}
|
}
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
|
@ -228,7 +228,7 @@ public class WatcherUtilsTests extends ESTestCase {
|
||||||
if (template == null) {
|
if (template == null) {
|
||||||
assertThat(result.getTemplate(), nullValue());
|
assertThat(result.getTemplate(), nullValue());
|
||||||
} else {
|
} else {
|
||||||
assertThat(result.getTemplate().getScript(), equalTo(template.getScript()));
|
assertThat(result.getTemplate().getIdOrCode(), equalTo(template.getIdOrCode()));
|
||||||
assertThat(result.getTemplate().getType(), equalTo(template.getType()));
|
assertThat(result.getTemplate().getType(), equalTo(template.getType()));
|
||||||
assertThat(result.getTemplate().getParams(), equalTo(template.getParams()));
|
assertThat(result.getTemplate().getParams(), equalTo(template.getParams()));
|
||||||
assertThat(result.getTemplate().getLang(), equalTo("mustache"));
|
assertThat(result.getTemplate().getLang(), equalTo("mustache"));
|
||||||
|
|
|
@ -44,7 +44,7 @@ public class WatcherSearchTemplateRequestTests extends ESTestCase {
|
||||||
WatcherSearchTemplateRequest result = WatcherSearchTemplateRequest.fromXContent(
|
WatcherSearchTemplateRequest result = WatcherSearchTemplateRequest.fromXContent(
|
||||||
logger, parser, randomFrom(SearchType.values()), false, null, null, null);
|
logger, parser, randomFrom(SearchType.values()), false, null, null, null);
|
||||||
assertNotNull(result.getTemplate());
|
assertNotNull(result.getTemplate());
|
||||||
assertThat(result.getTemplate().getScript(), equalTo(expectedScript));
|
assertThat(result.getTemplate().getIdOrCode(), equalTo(expectedScript));
|
||||||
assertThat(result.getTemplate().getLang(), equalTo(expectedLang));
|
assertThat(result.getTemplate().getLang(), equalTo(expectedLang));
|
||||||
assertThat(result.getTemplate().getParams(), equalTo(expectedParams));
|
assertThat(result.getTemplate().getParams(), equalTo(expectedParams));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
|
@ -32,6 +32,8 @@ import org.joda.time.DateTime;
|
||||||
|
|
||||||
import java.time.Clock;
|
import java.time.Clock;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
||||||
|
@ -258,7 +260,7 @@ public class BasicWatcherTests extends AbstractWatcherIntegrationTestCase {
|
||||||
.setSource(jsonBuilder().startObject().field("template").value(searchSourceBuilder).endObject().bytes())
|
.setSource(jsonBuilder().startObject().field("template").value(searchSourceBuilder).endObject().bytes())
|
||||||
.get());
|
.get());
|
||||||
|
|
||||||
Script template = new Script("my-template", ScriptType.STORED, "mustache", null);
|
Script template = new Script(ScriptType.STORED, "mustache", "my-template", Collections.emptyMap());
|
||||||
WatcherSearchTemplateRequest searchRequest = new WatcherSearchTemplateRequest(new String[]{"events"}, new String[0],
|
WatcherSearchTemplateRequest searchRequest = new WatcherSearchTemplateRequest(new String[]{"events"}, new String[0],
|
||||||
SearchType.DEFAULT, WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, template);
|
SearchType.DEFAULT, WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, template);
|
||||||
testConditionSearch(searchRequest);
|
testConditionSearch(searchRequest);
|
||||||
|
|
|
@ -54,6 +54,7 @@ import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static java.util.Collections.emptyMap;
|
import static java.util.Collections.emptyMap;
|
||||||
|
@ -230,7 +231,7 @@ public class SearchTransformTests extends ESIntegTestCase {
|
||||||
}
|
}
|
||||||
if (templateName != null) {
|
if (templateName != null) {
|
||||||
assertThat(executable.transform().getRequest().getTemplate(),
|
assertThat(executable.transform().getRequest().getTemplate(),
|
||||||
equalTo(new Script("template1", ScriptType.FILE, "mustache", null)));
|
equalTo(new Script(ScriptType.FILE, "mustache", "template1", Collections.emptyMap())));
|
||||||
}
|
}
|
||||||
assertThat(executable.transform().getRequest().getSearchSource().utf8ToString(), equalTo("{\"query\":{\"match_all\":{}}}"));
|
assertThat(executable.transform().getRequest().getSearchSource().utf8ToString(), equalTo("{\"query\":{\"match_all\":{}}}"));
|
||||||
assertThat(executable.transform().getTimeout(), equalTo(readTimeout));
|
assertThat(executable.transform().getTimeout(), equalTo(readTimeout));
|
||||||
|
|
|
@ -25,6 +25,7 @@ import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -128,10 +129,10 @@ public class TransformIntegrationTests extends AbstractWatcherIntegrationTestCas
|
||||||
.setScriptLang("painless")
|
.setScriptLang("painless")
|
||||||
.setSource(new BytesArray("{\"script\" : \"['key3' : ctx.payload.key1 + ctx.payload.key2]\"}"))
|
.setSource(new BytesArray("{\"script\" : \"['key3' : ctx.payload.key1 + ctx.payload.key2]\"}"))
|
||||||
.get());
|
.get());
|
||||||
script = new Script("my-script", ScriptType.STORED, "painless", null);
|
script = new Script(ScriptType.STORED, "painless", "my-script", Collections.emptyMap());
|
||||||
} else {
|
} else {
|
||||||
logger.info("testing script transform with a file script");
|
logger.info("testing script transform with a file script");
|
||||||
script = new Script("my-script", ScriptType.FILE, "painless", null);
|
script = new Script(ScriptType.FILE, "painless", "my-script", Collections.emptyMap());
|
||||||
}
|
}
|
||||||
|
|
||||||
// put a watch that has watch level transform:
|
// put a watch that has watch level transform:
|
||||||
|
|
|
@ -66,7 +66,7 @@ public class ScriptTransformTests extends ESTestCase {
|
||||||
ScriptService service = mock(ScriptService.class);
|
ScriptService service = mock(ScriptService.class);
|
||||||
ScriptType type = randomFrom(ScriptType.values());
|
ScriptType type = randomFrom(ScriptType.values());
|
||||||
Map<String, Object> params = Collections.emptyMap();
|
Map<String, Object> params = Collections.emptyMap();
|
||||||
Script script = new Script("_script", type, "_lang", params);
|
Script script = new Script(type, "_lang", "_script", params);
|
||||||
CompiledScript compiledScript = mock(CompiledScript.class);
|
CompiledScript compiledScript = mock(CompiledScript.class);
|
||||||
when(service.compile(script, Watcher.SCRIPT_CONTEXT, Collections.emptyMap())).thenReturn(compiledScript);
|
when(service.compile(script, Watcher.SCRIPT_CONTEXT, Collections.emptyMap())).thenReturn(compiledScript);
|
||||||
ExecutableScriptTransform transform = new ExecutableScriptTransform(new ScriptTransform(script), logger, service);
|
ExecutableScriptTransform transform = new ExecutableScriptTransform(new ScriptTransform(script), logger, service);
|
||||||
|
@ -94,7 +94,7 @@ public class ScriptTransformTests extends ESTestCase {
|
||||||
ScriptService service = mock(ScriptService.class);
|
ScriptService service = mock(ScriptService.class);
|
||||||
ScriptType type = randomFrom(ScriptType.values());
|
ScriptType type = randomFrom(ScriptType.values());
|
||||||
Map<String, Object> params = Collections.emptyMap();
|
Map<String, Object> params = Collections.emptyMap();
|
||||||
Script script = new Script("_script", type, "_lang", params);
|
Script script = new Script(type, "_lang", "_script", params);
|
||||||
CompiledScript compiledScript = mock(CompiledScript.class);
|
CompiledScript compiledScript = mock(CompiledScript.class);
|
||||||
when(service.compile(script, Watcher.SCRIPT_CONTEXT, Collections.emptyMap())).thenReturn(compiledScript);
|
when(service.compile(script, Watcher.SCRIPT_CONTEXT, Collections.emptyMap())).thenReturn(compiledScript);
|
||||||
ExecutableScriptTransform transform = new ExecutableScriptTransform(new ScriptTransform(script), logger, service);
|
ExecutableScriptTransform transform = new ExecutableScriptTransform(new ScriptTransform(script), logger, service);
|
||||||
|
@ -120,7 +120,7 @@ public class ScriptTransformTests extends ESTestCase {
|
||||||
ScriptService service = mock(ScriptService.class);
|
ScriptService service = mock(ScriptService.class);
|
||||||
ScriptType type = randomFrom(ScriptType.values());
|
ScriptType type = randomFrom(ScriptType.values());
|
||||||
Map<String, Object> params = Collections.emptyMap();
|
Map<String, Object> params = Collections.emptyMap();
|
||||||
Script script = new Script("_script", type, "_lang", params);
|
Script script = new Script(type, "_lang", "_script", params);
|
||||||
CompiledScript compiledScript = mock(CompiledScript.class);
|
CompiledScript compiledScript = mock(CompiledScript.class);
|
||||||
when(service.compile(script, Watcher.SCRIPT_CONTEXT, Collections.emptyMap())).thenReturn(compiledScript);
|
when(service.compile(script, Watcher.SCRIPT_CONTEXT, Collections.emptyMap())).thenReturn(compiledScript);
|
||||||
ExecutableScriptTransform transform = new ExecutableScriptTransform(new ScriptTransform(script), logger, service);
|
ExecutableScriptTransform transform = new ExecutableScriptTransform(new ScriptTransform(script), logger, service);
|
||||||
|
@ -155,7 +155,7 @@ public class ScriptTransformTests extends ESTestCase {
|
||||||
XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes());
|
XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes());
|
||||||
parser.nextToken();
|
parser.nextToken();
|
||||||
ExecutableScriptTransform transform = new ScriptTransformFactory(Settings.EMPTY, service).parseExecutable("_id", parser, false);
|
ExecutableScriptTransform transform = new ScriptTransformFactory(Settings.EMPTY, service).parseExecutable("_id", parser, false);
|
||||||
Script script = new Script("_script", type, "_lang", singletonMap("key", "value"));
|
Script script = new Script(type, "_lang", "_script", singletonMap("key", "value"));
|
||||||
assertThat(transform.transform().getScript(), equalTo(script));
|
assertThat(transform.transform().getScript(), equalTo(script));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue