Skip to content

Add rest and transport plumbing #40997

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> ENRICH_ENABLED_SETTING = Setting.boolSetting("xpack.enrich.enabled", true, Property.NodeScope);

/**
* Setting for controlling whether or not CCR is enabled.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GetEnrichPolicyAction.Response> {
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<Response> getResponseReader() {
return Response::new;
}

public static class Request extends MasterNodeReadRequest<Request> {

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Processor.Factory> getProcessors(Processor.Parameters parameters) {
return Collections.emptyMap();
}

public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
if (enabled == false) {
return emptyList();
}

return Arrays.asList(
new ActionHandler<>(GetEnrichPolicyAction.INSTANCE, TransportGetEnrichPolicyAction.class)
);
}


public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings,
IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter,
IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<DiscoveryNodes> nodesInCluster) {
if (enabled == false) {
return emptyList();
}

return Arrays.asList(
new RestGenEnrichPolicyAction(settings, restController)
);
}
}
Original file line number Diff line number Diff line change
@@ -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<GetEnrichPolicyAction.Request,
GetEnrichPolicyAction.Response> {

@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<GetEnrichPolicyAction.Response> listener) throws Exception {
listener.onResponse(new GetEnrichPolicyAction.Response());
}

@Override
protected ClusterBlockException checkBlock(GetEnrichPolicyAction.Request request, ClusterState state) {
return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ);
}

}
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RestGenEnrichPolicyAction -> RestGetEnrichPolicyAction


public RestGenEnrichPolicyAction(final Settings settings, final RestController controller) {
super(settings);
controller.registerHandler(RestRequest.Method.GET, "/{index}/_enrich", this);
Copy link
Member

@martijnvg martijnvg Apr 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have the following path structure: /_enrich/policy/{name}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, so completely remove the {index} and only put it in the payload of the policy itself, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the payload would contain the: source index, decorate fields etc.
The name (or id) of the policy would exist in the url path.

}

@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));
}
}