Skip to content

Add enrich policy GET API #41384

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 18 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -39,6 +39,11 @@ private XPackSettings() {
}


/**
* 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,127 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.core.enrich.action;

import org.elasticsearch.action.Action;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ActionResponse;
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 org.elasticsearch.xpack.core.enrich.EnrichPolicy;

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 = "cluster: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 {

private final EnrichPolicy policy;

public Response(EnrichPolicy policy) {
this.policy = policy;
}

public Response(StreamInput in) throws IOException {
policy = new EnrichPolicy(in);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
policy.writeTo(out);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
{
policy.toXContent(builder, params);
}
builder.endObject();

return builder;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Response response = (Response) o;
return policy.equals(response.policy);
}

@Override
public int hashCode() {
return Objects.hash(policy);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,83 @@
*/
package org.elasticsearch.xpack.enrich;

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
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.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.NodeEnvironment;
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.script.ScriptService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.watcher.ResourceWatcherService;
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
import org.elasticsearch.xpack.enrich.action.TransportGetEnrichPolicyAction;
import org.elasticsearch.xpack.enrich.rest.RestGetEnrichPolicyAction;

import java.util.Arrays;
import java.util.Collection;
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 RestGetEnrichPolicyAction(settings, restController)
);
}

@Override
public Collection<Object> createComponents(Client client,
ClusterService clusterService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* A components that provides access and stores an enrich policy.
*/
public final class EnrichStore {
public class EnrichStore {

private final ClusterService clusterService;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.enrich.action;

import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.master.TransportMasterNodeReadAction;
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.Strings;
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.action.GetEnrichPolicyAction;
import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
import org.elasticsearch.xpack.enrich.EnrichStore;

import java.io.IOException;

public class TransportGetEnrichPolicyAction extends TransportMasterNodeReadAction<GetEnrichPolicyAction.Request,
GetEnrichPolicyAction.Response> {

private final EnrichStore enrichStore;

@Inject
public TransportGetEnrichPolicyAction(TransportService transportService,
ClusterService clusterService,
ThreadPool threadPool,
ActionFilters actionFilters,
IndexNameExpressionResolver indexNameExpressionResolver,
EnrichStore enrichStore) {
super(GetEnrichPolicyAction.NAME, transportService, clusterService, threadPool, actionFilters,
GetEnrichPolicyAction.Request::new, indexNameExpressionResolver);
this.enrichStore = enrichStore;
}

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

@Override
protected void masterOperation(GetEnrichPolicyAction.Request request,
ClusterState state,
ActionListener<GetEnrichPolicyAction.Response> listener) throws Exception {
final EnrichPolicy policy = getPolicy(request.getName(), enrichStore);
listener.onResponse(new GetEnrichPolicyAction.Response(policy));
}

static EnrichPolicy getPolicy(String name, EnrichStore store) {
if (Strings.isNullOrEmpty(name)) {
throw new ResourceNotFoundException("name is missing");
}
var policy = store.getPolicy(name);
if (policy == null) {
throw new ResourceNotFoundException("policy [{}] is missing", name);
}
return policy;
Copy link
Member

Choose a reason for hiding this comment

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

I think the two if statements in this method should be merged with EnrichStore#getPolicy(...) and also tests should move the EnrichStoreTests. Since the static method will basically by doing nothing, it can be merged with masterOperation(...)

}

@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,35 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
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.action.GetEnrichPolicyAction;

import java.io.IOException;

public class RestGetEnrichPolicyAction extends BaseRestHandler {

public RestGetEnrichPolicyAction(final Settings settings, final RestController controller) {
super(settings);
controller.registerHandler(RestRequest.Method.GET, "/_enrich/policy/{name}", this);
}

@Override
public String getName() {
return "get_enrich_policy";
}

@Override
protected RestChannelConsumer prepareRequest(final RestRequest restRequest, final NodeClient client) throws IOException {
final GetEnrichPolicyAction.Request request = new GetEnrichPolicyAction.Request(restRequest.param("name"));
return channel -> client.execute(GetEnrichPolicyAction.INSTANCE, request, new RestToXContentListener<>(channel));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected EnrichPolicy createXContextTestInstance(XContentType xContentType) {
return randomEnrichPolicy(xContentType);
}

static EnrichPolicy randomEnrichPolicy(XContentType xContentType) {
public static EnrichPolicy randomEnrichPolicy(XContentType xContentType) {
final QueryBuilder queryBuilder;
if (randomBoolean()) {
queryBuilder = new MatchAllQueryBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.enrich.action;

import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;

public class GetEnrichPolicyActionRequestTests extends AbstractWireSerializingTestCase<GetEnrichPolicyAction.Request> {
@Override
protected GetEnrichPolicyAction.Request createTestInstance() {
return new GetEnrichPolicyAction.Request(randomAlphaOfLength(5));
}

@Override
protected Writeable.Reader<GetEnrichPolicyAction.Request> instanceReader() {
return GetEnrichPolicyAction.Request::new;
}
}
Loading