Add enrich policy GET API (#41384)
This commit wires up the Rest calls and Transport calls for GET enrich policy, as well as tests and rest spec additions.
This commit is contained in:
parent
be60125a4e
commit
77eed9e6a0
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
* 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 = Objects.requireNonNull(policy, "policy cannot be null");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public EnrichPolicy getPolicy() {
|
||||
return policy;
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,6 +16,14 @@
|
|||
name: policy-crud
|
||||
- is_true: acknowledged
|
||||
|
||||
- do:
|
||||
enrich.get_policy:
|
||||
name: policy-crud
|
||||
- match: { type: exact_match }
|
||||
- match: { indices: ["bar*"] }
|
||||
- match: { enrich_key: baz }
|
||||
- match: { enrich_values: ["a", "b"] }
|
||||
|
||||
- do:
|
||||
enrich.list_policy: {}
|
||||
- length: { policies: 1 }
|
||||
|
|
|
@ -33,14 +33,17 @@ import org.elasticsearch.threadpool.ThreadPool;
|
|||
import org.elasticsearch.watcher.ResourceWatcherService;
|
||||
import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.core.enrich.action.ListEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.enrich.action.TransportDeleteEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.enrich.action.TransportExecuteEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.enrich.action.TransportGetEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.enrich.action.TransportListEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.enrich.action.TransportPutEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.enrich.rest.RestDeleteEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.enrich.rest.RestExecuteEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.enrich.rest.RestGetEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.enrich.rest.RestListEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.enrich.rest.RestPutEnrichPolicyAction;
|
||||
|
||||
|
@ -81,6 +84,7 @@ public class EnrichPlugin extends Plugin implements ActionPlugin, IngestPlugin {
|
|||
}
|
||||
|
||||
return Arrays.asList(
|
||||
new ActionHandler<>(GetEnrichPolicyAction.INSTANCE, TransportGetEnrichPolicyAction.class),
|
||||
new ActionHandler<>(DeleteEnrichPolicyAction.INSTANCE, TransportDeleteEnrichPolicyAction.class),
|
||||
new ActionHandler<>(ListEnrichPolicyAction.INSTANCE, TransportListEnrichPolicyAction.class),
|
||||
new ActionHandler<>(PutEnrichPolicyAction.INSTANCE, TransportPutEnrichPolicyAction.class),
|
||||
|
@ -97,6 +101,7 @@ public class EnrichPlugin extends Plugin implements ActionPlugin, IngestPlugin {
|
|||
}
|
||||
|
||||
return Arrays.asList(
|
||||
new RestGetEnrichPolicyAction(settings, restController),
|
||||
new RestDeleteEnrichPolicyAction(settings, restController),
|
||||
new RestListEnrichPolicyAction(settings, restController),
|
||||
new RestPutEnrichPolicyAction(settings, restController),
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* 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.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.EnrichPolicy;
|
||||
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
|
||||
import org.elasticsearch.xpack.enrich.EnrichStore;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TransportGetEnrichPolicyAction extends TransportMasterNodeReadAction<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(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void masterOperation(GetEnrichPolicyAction.Request request,
|
||||
ClusterState state,
|
||||
ActionListener<GetEnrichPolicyAction.Response> listener) throws Exception {
|
||||
final EnrichPolicy policy = EnrichStore.getPolicy(request.getName(), state);
|
||||
if (policy == null) {
|
||||
throw new ResourceNotFoundException("Policy [{}] was not found", request.getName());
|
||||
}
|
||||
listener.onResponse(new GetEnrichPolicyAction.Response(policy));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ClusterBlockException checkBlock(GetEnrichPolicyAction.Request request, ClusterState state) {
|
||||
return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ);
|
||||
}
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* 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.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.test.AbstractSerializingTestCase;
|
||||
import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
|
||||
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.assertEqualPolicies;
|
||||
import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.randomEnrichPolicy;
|
||||
|
||||
public class GetEnrichPolicyActionResponseTests extends AbstractSerializingTestCase<GetEnrichPolicyAction.Response> {
|
||||
|
||||
@Override
|
||||
protected GetEnrichPolicyAction.Response doParseInstance(XContentParser parser) throws IOException {
|
||||
EnrichPolicy policy = EnrichPolicy.fromXContent(parser);
|
||||
return new GetEnrichPolicyAction.Response(policy);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GetEnrichPolicyAction.Response createTestInstance() {
|
||||
EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON);
|
||||
return new GetEnrichPolicyAction.Response(policy);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Writeable.Reader<GetEnrichPolicyAction.Response> instanceReader() {
|
||||
return GetEnrichPolicyAction.Response::new;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void assertEqualInstances(GetEnrichPolicyAction.Response expectedInstance, GetEnrichPolicyAction.Response newInstance) {
|
||||
assertNotSame(expectedInstance, newInstance);
|
||||
// the tests shuffle around the policy query source xcontent type, so this is needed here
|
||||
assertEqualPolicies(expectedInstance.getPolicy(), newInstance.getPolicy());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"enrich.get_policy": {
|
||||
"documentation": "https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-get-policy.html",
|
||||
"methods": [ "GET" ],
|
||||
"url": {
|
||||
"path": "/_enrich/policy/{name}",
|
||||
"paths": ["/_enrich/policy/{name}"],
|
||||
"parts": {
|
||||
"name": {
|
||||
"type" : "string",
|
||||
"description" : "The name of the enrich policy"
|
||||
}
|
||||
},
|
||||
"params": {
|
||||
}
|
||||
},
|
||||
"body": null
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue