From b434bddf1ac87d389bf36fd84729a68a4d6f5204 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Mon, 8 Apr 2019 22:37:41 -0500 Subject: [PATCH] Add rest and transport plumbing Also wired up a bit more of the plugin and tested against a live cluster. --- .../xpack/core/XPackSettings.java | 4 + .../core/enrich/GetEnrichPolicyAction.java | 102 ++++++++++++++++++ .../xpack/enrich/EnrichPlugin.java | 54 +++++++++- .../TransportGetEnrichPolicyAction.java | 59 ++++++++++ .../rest/RestGenEnrichPolicyAction.java | 33 ++++++ 5 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/GetEnrichPolicyAction.java create mode 100644 x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportGetEnrichPolicyAction.java create mode 100644 x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/rest/RestGenEnrichPolicyAction.java diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java index 0eeb173b8b84e..fd77aec1e3441 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java @@ -38,6 +38,10 @@ private XPackSettings() { throw new IllegalStateException("Utility class should not be instantiated"); } + /** + * Setting for controlling whether or not enrich is enabled. + */ + public static final Setting ENRICH_ENABLED_SETTING = Setting.boolSetting("xpack.enrich.enabled", true, Property.NodeScope); /** * Setting for controlling whether or not CCR is enabled. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/GetEnrichPolicyAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/GetEnrichPolicyAction.java new file mode 100644 index 0000000000000..d3e4e7d45ac5f --- /dev/null +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/GetEnrichPolicyAction.java @@ -0,0 +1,102 @@ +package org.elasticsearch.xpack.core.enrich; + +import org.elasticsearch.action.Action; +import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.action.support.master.MasterNodeReadRequest; +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.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; + +import java.io.IOException; +import java.util.Objects; + +public class GetEnrichPolicyAction extends Action { + public static final GetEnrichPolicyAction INSTANCE = new GetEnrichPolicyAction(); + public static final String NAME = "indices:admin/xpack/enrich/get"; + + private GetEnrichPolicyAction() { + super(NAME); + } + + @Override + public Response newResponse() { + throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable"); + } + + @Override + public Writeable.Reader getResponseReader() { + return Response::new; + } + + public static class Request extends MasterNodeReadRequest { + + private String name; + + public Request(String name) { + this.name = name; + } + + public Request(StreamInput in) throws IOException { + super(in); + this.name = in.readOptionalString(); + } + + @Override + public ActionRequestValidationException validate() { + return null; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeOptionalString(name); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Request request = (Request) o; + return Objects.equals(name, request.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } + } + + public static class Response extends ActionResponse implements ToXContentObject { + + public Response() { + + } + + public Response(StreamInput in) throws IOException { + super.readFrom(in); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + // placeholder until more data is returned + return new AcknowledgedResponse(true).toXContent(builder, params); + } + } +} diff --git a/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichPlugin.java b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichPlugin.java index a3b4cb85a19e5..ce01ebb70ab80 100644 --- a/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichPlugin.java +++ b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichPlugin.java @@ -5,17 +5,69 @@ */ package org.elasticsearch.xpack.enrich; +import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; +import org.elasticsearch.cluster.node.DiscoveryNodes; +import org.elasticsearch.common.settings.ClusterSettings; +import org.elasticsearch.common.settings.IndexScopedSettings; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.settings.SettingsFilter; import org.elasticsearch.ingest.Processor; +import org.elasticsearch.plugins.ActionPlugin; import org.elasticsearch.plugins.IngestPlugin; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.rest.RestController; +import org.elasticsearch.rest.RestHandler; +import org.elasticsearch.xpack.core.enrich.GetEnrichPolicyAction; +import org.elasticsearch.xpack.enrich.action.TransportGetEnrichPolicyAction; +import org.elasticsearch.xpack.enrich.rest.RestGenEnrichPolicyAction; +import java.util.Arrays; import java.util.Collections; +import java.util.List; import java.util.Map; +import java.util.function.Supplier; -public class EnrichPlugin extends Plugin implements IngestPlugin { +import static java.util.Collections.emptyList; +import static org.elasticsearch.xpack.core.XPackSettings.ENRICH_ENABLED_SETTING; + +public class EnrichPlugin extends Plugin implements ActionPlugin, IngestPlugin { + + private final Settings settings; + private final Boolean enabled; + + public EnrichPlugin(final Settings settings) { + this.settings = settings; + this.enabled = ENRICH_ENABLED_SETTING.get(settings); + } @Override public Map getProcessors(Processor.Parameters parameters) { return Collections.emptyMap(); } + + public List> getActions() { + if (enabled == false) { + return emptyList(); + } + + return Arrays.asList( + new ActionHandler<>(GetEnrichPolicyAction.INSTANCE, TransportGetEnrichPolicyAction.class) + ); + } + + + public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster) { + if (enabled == false) { + return emptyList(); + } + + return Arrays.asList( + new RestGenEnrichPolicyAction(settings, restController) + ); + } } diff --git a/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportGetEnrichPolicyAction.java b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportGetEnrichPolicyAction.java new file mode 100644 index 0000000000000..7cc15dd3f1d00 --- /dev/null +++ b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportGetEnrichPolicyAction.java @@ -0,0 +1,59 @@ +package org.elasticsearch.xpack.enrich.action; + +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.support.ActionFilters; +import org.elasticsearch.action.support.master.TransportMasterNodeAction; +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.block.ClusterBlockException; +import org.elasticsearch.cluster.block.ClusterBlockLevel; +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; +import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xpack.core.enrich.GetEnrichPolicyAction; + +import java.io.IOException; + +public class TransportGetEnrichPolicyAction extends TransportMasterNodeAction { + + @Inject + public TransportGetEnrichPolicyAction(TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver) { + super(GetEnrichPolicyAction.NAME, transportService, clusterService, threadPool, actionFilters, + GetEnrichPolicyAction.Request::new, indexNameExpressionResolver); + } + + @Override + protected String executor() { + return ThreadPool.Names.SAME; + } + + @Override + protected GetEnrichPolicyAction.Response newResponse() { + throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable"); + } + + @Override + protected GetEnrichPolicyAction.Response read(StreamInput in) throws IOException { + return new GetEnrichPolicyAction.Response(); + } + + @Override + protected void masterOperation(GetEnrichPolicyAction.Request request, + ClusterState state, + ActionListener listener) throws Exception { + listener.onResponse(new GetEnrichPolicyAction.Response()); + } + + @Override + protected ClusterBlockException checkBlock(GetEnrichPolicyAction.Request request, ClusterState state) { + return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ); + } + +} diff --git a/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/rest/RestGenEnrichPolicyAction.java b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/rest/RestGenEnrichPolicyAction.java new file mode 100644 index 0000000000000..be8137918f493 --- /dev/null +++ b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/rest/RestGenEnrichPolicyAction.java @@ -0,0 +1,33 @@ +package org.elasticsearch.xpack.enrich.rest; + +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.rest.BaseRestHandler; +import org.elasticsearch.rest.RestController; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xpack.core.enrich.GetEnrichPolicyAction; + +import java.io.IOException; + +public class RestGenEnrichPolicyAction extends BaseRestHandler { + + public RestGenEnrichPolicyAction(final Settings settings, final RestController controller) { + super(settings); + controller.registerHandler(RestRequest.Method.GET, "/{index}/_enrich", this); + } + + @Override + public String getName() { + return "get_enrich_policy"; + } + + @Override + protected RestChannelConsumer prepareRequest(final RestRequest restRequest, final NodeClient client) throws IOException { + // need to parse out the index param as it may refer to > 1 index. + final GetEnrichPolicyAction.Request request = new GetEnrichPolicyAction.Request(restRequest.param("index")); + return channel -> client.execute(GetEnrichPolicyAction.INSTANCE, request, new RestToXContentListener<>(channel)); + } +} + +