-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add a Painless Context REST API #39382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 30 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
7b3aacb
Add skeleton for context action.
jdconrad 759eb4e
Store PainlessScriptEngine.t
jdconrad 1c1edba
Merge branch 'master' into docapi
jdconrad cf6b680
Add methods to access lookups for doc api.
jdconrad 613cfe6
Merge branch 'master' into docapi
jdconrad 0c48a73
Merge branch 'master' into docapi
jdconrad 1ae7533
Convert to use HandledRestAction
jdconrad 28b7cde
Progress on API for returning methods
jdconrad eafa7b9
Revent prettyprint
jdconrad fed13ea
Merge branch 'master' into docapi
jdconrad eab2c38
Add a Painless Context REST API
jdconrad 13b3133
Fix style
jdconrad f519aba
Merge branch 'master' into docapi
jdconrad f1f4e5d
Merge branch 'master' into docapi
jdconrad 9afc790
Move to action package.
jdconrad 1f81c2c
Merge branch 'master' into docapi
jdconrad e4644d4
Merge branch 'master' into docapi
jdconrad 2e40711
Add POJOs to handle serialization and response XContent.
jdconrad 8ad179f
Update serialization.
jdconrad 46f7504
Merge branch 'master' into docapi
jdconrad c480335
Merge branch 'master' into docapi
jdconrad c586417
Merge branch 'master' into docapi
jdconrad c54fe76
Merge branch 'master' into docapi
jdconrad a74a4dd
Add serialization tests.
jdconrad c0effbe
Fix generics.
jdconrad dd43131
Merge branch 'master' into docapi
jdconrad 7f3d4a1
Merge branch 'master' into docapi
jdconrad 7f218eb
Add yaml tests.
jdconrad a651b4b
Add rest spec
jdconrad f9ac3b1
Fix naming convention test.
jdconrad 3b06a61
Merge branch 'master' into docapi
jdconrad 79b1221
Add imported info to class, add matches to yaml tests, and update
jdconrad 2b22fb3
Fix ignored imports in ANTLR files.
jdconrad 17a3efc
Merge branch 'master' into docapi
jdconrad 3a88b15
Merge branch 'master' into docapi
jdconrad 5e97670
Response to PR comments.
jdconrad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 230 additions & 0 deletions
230
.../lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.painless.action; | ||
|
|
||
| import org.elasticsearch.action.Action; | ||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.ActionRequest; | ||
| import org.elasticsearch.action.ActionRequestValidationException; | ||
| import org.elasticsearch.action.ActionResponse; | ||
| import org.elasticsearch.action.support.ActionFilters; | ||
| import org.elasticsearch.action.support.HandledTransportAction; | ||
| import org.elasticsearch.client.node.NodeClient; | ||
| import org.elasticsearch.common.ParseField; | ||
| import org.elasticsearch.common.inject.Inject; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.io.stream.Writeable; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.common.xcontent.ToXContentObject; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.painless.PainlessScriptEngine; | ||
| import org.elasticsearch.painless.lookup.PainlessLookup; | ||
| import org.elasticsearch.rest.BaseRestHandler; | ||
| import org.elasticsearch.rest.RestController; | ||
| import org.elasticsearch.rest.RestRequest; | ||
| import org.elasticsearch.rest.action.RestToXContentListener; | ||
| import org.elasticsearch.script.ScriptContext; | ||
| import org.elasticsearch.tasks.Task; | ||
| import org.elasticsearch.transport.TransportService; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static org.elasticsearch.rest.RestRequest.Method.GET; | ||
|
|
||
| /** | ||
| * Internal REST API for querying context information about Painless whitelists. | ||
| * Commands include the following: | ||
| * <ul> | ||
| * <li> GET /_scripts/painless/_context -- retrieves a list of contexts </li> | ||
| * <li> GET /_scripts/painless/_context?context=%name% -- | ||
| * retrieves all available information about the API for this specific context</li> | ||
| * </ul> | ||
| */ | ||
| public class PainlessContextAction extends Action<PainlessContextAction.Response> { | ||
|
|
||
| public static final PainlessContextAction INSTANCE = new PainlessContextAction(); | ||
| private static final String NAME = "cluster:admin/scripts/painless/context"; | ||
|
|
||
| private static final String SCRIPT_CONTEXT_NAME_PARAM = "context"; | ||
|
|
||
| private PainlessContextAction() { | ||
| super(NAME); | ||
| } | ||
|
|
||
| @Override | ||
| public Response newResponse() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public Writeable.Reader<Response> getResponseReader() { | ||
| return Response::new; | ||
| } | ||
|
|
||
| public static class Request extends ActionRequest { | ||
|
|
||
| private String scriptContextName; | ||
|
|
||
| public Request() { | ||
| scriptContextName = null; | ||
| } | ||
|
|
||
| public Request(StreamInput in) throws IOException { | ||
| super(in); | ||
| scriptContextName = in.readString(); | ||
| } | ||
|
|
||
| public void setScriptContextName(String scriptContextName) { | ||
| this.scriptContextName = scriptContextName; | ||
| } | ||
|
|
||
| public String getScriptContextName() { | ||
| return scriptContextName; | ||
| } | ||
|
|
||
| @Override | ||
| public ActionRequestValidationException validate() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public void readFrom(StreamInput in) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| super.writeTo(out); | ||
| out.writeString(scriptContextName); | ||
| } | ||
| } | ||
|
|
||
| public static class Response extends ActionResponse implements ToXContentObject { | ||
|
|
||
| public static final ParseField CONTEXTS = new ParseField("contexts"); | ||
|
|
||
| private final Map<String, PainlessContextInfo> scriptContextNamesToPainlessContextInfos; | ||
| private final String scriptContextName; | ||
|
|
||
| public Response(Map<String, PainlessContextInfo> scriptContextNamesToPainlessContextInfos, String scriptContextName) { | ||
| this.scriptContextNamesToPainlessContextInfos = Collections.unmodifiableMap(scriptContextNamesToPainlessContextInfos); | ||
| this.scriptContextName = scriptContextName; | ||
| } | ||
|
|
||
| public Response(StreamInput in) throws IOException { | ||
| super(in); | ||
| scriptContextNamesToPainlessContextInfos = | ||
| Collections.unmodifiableMap(in.readMap(StreamInput::readString, PainlessContextInfo::new)); | ||
| scriptContextName = in.readString(); | ||
| } | ||
|
|
||
| @Override | ||
| public void readFrom(StreamInput in) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| super.writeTo(out); | ||
| out.writeMap(scriptContextNamesToPainlessContextInfos, StreamOutput::writeString, (o, v) -> v.writeTo(o)); | ||
| out.writeString(scriptContextName); | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
| if (scriptContextName == null) { | ||
| builder.startObject(); | ||
| builder.field(CONTEXTS.getPreferredName(), scriptContextNamesToPainlessContextInfos.keySet()); | ||
| builder.endObject(); | ||
| } else { | ||
| scriptContextNamesToPainlessContextInfos.get(scriptContextName).toXContent(builder, params); | ||
| } | ||
|
|
||
| return builder; | ||
| } | ||
| } | ||
|
|
||
| public static class TransportAction extends HandledTransportAction<Request, Response> { | ||
|
|
||
| PainlessScriptEngine painlessScriptEngine; | ||
|
|
||
| @Inject | ||
| public TransportAction(TransportService transportService, ActionFilters actionFilters, PainlessScriptEngine painlessScriptEngine) { | ||
| super(NAME, transportService, actionFilters, (Writeable.Reader<Request>)Request::new); | ||
| this.painlessScriptEngine = painlessScriptEngine; | ||
| } | ||
|
|
||
| @Override | ||
| protected void doExecute(Task task, Request request, ActionListener<Response> listener) { | ||
| Map<String, PainlessContextInfo> scriptContextNamesToPainlessContextInfos = new HashMap<>(); | ||
| String scriptContextName = request.getScriptContextName(); | ||
|
|
||
| if (request.scriptContextName == null) { | ||
| for (ScriptContext<?> scriptContext : painlessScriptEngine.getContextsToLookups().keySet()) { | ||
| scriptContextNamesToPainlessContextInfos.put(scriptContext.name, null); | ||
| } | ||
| } else { | ||
| ScriptContext<?> scriptContext = null; | ||
| PainlessLookup painlessLookup = null; | ||
|
|
||
| for (Map.Entry<ScriptContext<?>, PainlessLookup> contextLookupEntry : | ||
| painlessScriptEngine.getContextsToLookups().entrySet()) { | ||
| if (contextLookupEntry.getKey().name.equals(scriptContextName)) { | ||
| scriptContext = contextLookupEntry.getKey(); | ||
| painlessLookup = contextLookupEntry.getValue(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (scriptContext == null || painlessLookup == null) { | ||
| throw new IllegalArgumentException("script context [" + scriptContextName + "] not found"); | ||
| } | ||
|
|
||
| scriptContextNamesToPainlessContextInfos.put(scriptContext.name, new PainlessContextInfo(scriptContext, painlessLookup)); | ||
| } | ||
|
|
||
| listener.onResponse(new Response(scriptContextNamesToPainlessContextInfos, scriptContextName)); | ||
| } | ||
| } | ||
|
|
||
| public static class RestAction extends BaseRestHandler { | ||
|
|
||
| public RestAction(Settings settings, RestController controller) { | ||
| super(settings); | ||
| controller.registerHandler(GET, "/_scripts/painless/_context", this); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "_scripts_painless_context"; | ||
| } | ||
|
|
||
| @Override | ||
| protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) { | ||
| Request request = new Request(); | ||
| request.setScriptContextName(restRequest.param(SCRIPT_CONTEXT_NAME_PARAM)); | ||
| return channel -> client.executeLocally(INSTANCE, request, new RestToXContentListener<>(channel)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe make this field final?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.