diff options
author | Ryan Ernst <ryan@iernst.net> | 2017-06-01 17:53:48 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-01 17:53:48 -0700 |
commit | 8d88b943728d1a54fc0dab919dead9dde51774aa (patch) | |
tree | 0777b4a6362be45838094516a79e51e0a2d51f23 /core | |
parent | 39e59b49b16a8f82b174ba8066c4769b06a37eb0 (diff) |
Scripting: Add optional context parameter to put stored script requests (#25014)
This commit adds an optional `context` url parameter to the put stored
script request. When a context is specified, the script is compiled
against that context before storing, as a validation the script will
work when used in that context.
Diffstat (limited to 'core')
4 files changed, 28 insertions, 10 deletions
diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequest.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequest.java index ca26df75de..d35a26d9c3 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequest.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequest.java @@ -38,6 +38,7 @@ public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptR private String id; private String lang; + private String context; private BytesReference content; private XContentType xContentType; @@ -45,10 +46,11 @@ public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptR super(); } - public PutStoredScriptRequest(String id, String lang, BytesReference content, XContentType xContentType) { + public PutStoredScriptRequest(String id, String lang, String context, BytesReference content, XContentType xContentType) { super(); this.id = id; this.lang = lang; + this.context = context; this.content = content; this.xContentType = Objects.requireNonNull(xContentType); } @@ -94,6 +96,15 @@ public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptR return this; } + public String context() { + return context; + } + + public PutStoredScriptRequest context(String context) { + this.context = context; + return this; + } + public BytesReference content() { return content; } @@ -128,6 +139,9 @@ public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptR } else { xContentType = XContentFactory.xContentType(content); } + if (in.getVersion().onOrAfter(Version.V_6_0_0_alpha2)) { + context = in.readOptionalString(); + } } @Override @@ -140,6 +154,9 @@ public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptR if (out.getVersion().onOrAfter(Version.V_5_3_0)) { xContentType.writeTo(out); } + if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha2)) { + out.writeOptionalString(context); + } } @Override diff --git a/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutStoredScriptAction.java b/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutStoredScriptAction.java index 358c3656ce..a7e9c6c8e8 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutStoredScriptAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutStoredScriptAction.java @@ -50,6 +50,7 @@ public class RestPutStoredScriptAction extends BaseRestHandler { public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { String id = request.param("id"); String lang = request.param("lang"); + String context = request.param("context"); // In the case where only {lang} is not null, we make it {id} because of // name ordering issues in the handlers' paths. @@ -65,7 +66,7 @@ public class RestPutStoredScriptAction extends BaseRestHandler { "specifying lang [" + lang + "] as part of the url path is deprecated, use request content instead"); } - PutStoredScriptRequest putRequest = new PutStoredScriptRequest(id, lang, content, request.getXContentType()); + PutStoredScriptRequest putRequest = new PutStoredScriptRequest(id, lang, context, content, request.getXContentType()); return channel -> client.admin().cluster().putStoredScript(putRequest, new AcknowledgedRestListener<>(channel)); } } diff --git a/core/src/main/java/org/elasticsearch/script/ScriptService.java b/core/src/main/java/org/elasticsearch/script/ScriptService.java index 2634cbda8d..981d952d63 100644 --- a/core/src/main/java/org/elasticsearch/script/ScriptService.java +++ b/core/src/main/java/org/elasticsearch/script/ScriptService.java @@ -431,14 +431,12 @@ public class ScriptService extends AbstractComponent implements Closeable, Clust } else if (isAnyContextEnabled() == false) { throw new IllegalArgumentException( "cannot put [" + ScriptType.STORED + "] script, no script contexts are enabled"); - } else { - // TODO: executable context here is just a placeholder, replace with optional context name passed into PUT stored script req - Object compiled = scriptEngine.compile(request.id(), source.getCode(), ExecutableScript.CONTEXT, Collections.emptyMap()); - - if (compiled == null) { - throw new IllegalArgumentException("failed to parse/compile stored script [" + request.id() + "]" + - (source.getCode() == null ? "" : " using code [" + source.getCode() + "]")); + } else if (request.context() != null) { + ScriptContext<?> context = contexts.get(request.context()); + if (context == null) { + throw new IllegalArgumentException("Unknown context [" + request.context() + "]"); } + scriptEngine.compile(request.id(), source.getCode(), context, Collections.emptyMap()); } } catch (ScriptException good) { throw good; diff --git a/core/src/test/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequestTests.java b/core/src/test/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequestTests.java index aec8349ea8..a60722d3f4 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequestTests.java +++ b/core/src/test/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequestTests.java @@ -32,7 +32,8 @@ import java.util.Base64; public class PutStoredScriptRequestTests extends ESTestCase { public void testSerialization() throws IOException { - PutStoredScriptRequest storedScriptRequest = new PutStoredScriptRequest("foo", "bar", new BytesArray("{}"), XContentType.JSON); + PutStoredScriptRequest storedScriptRequest = + new PutStoredScriptRequest("foo", "bar", "context", new BytesArray("{}"), XContentType.JSON); assertEquals(XContentType.JSON, storedScriptRequest.xContentType()); try (BytesStreamOutput output = new BytesStreamOutput()) { @@ -44,6 +45,7 @@ public class PutStoredScriptRequestTests extends ESTestCase { assertEquals(XContentType.JSON, serialized.xContentType()); assertEquals(storedScriptRequest.lang(), serialized.lang()); assertEquals(storedScriptRequest.id(), serialized.id()); + assertEquals(storedScriptRequest.context(), serialized.context()); } } } |