From 77eed9e6a0ccc428bfca4bfc7c1884afda2cc9bb Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Tue, 28 May 2019 14:47:50 -0500 Subject: [PATCH] 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. --- .../enrich/action/GetEnrichPolicyAction.java | 132 ++++++++++++++++++ .../rest-api-spec/test/enrich/10_basic.yml | 8 ++ .../xpack/enrich/EnrichPlugin.java | 5 + .../TransportGetEnrichPolicyAction.java | 71 ++++++++++ .../rest/RestGetEnrichPolicyAction.java | 35 +++++ .../GetEnrichPolicyActionRequestTests.java | 23 +++ .../GetEnrichPolicyActionResponseTests.java | 45 ++++++ .../rest-api-spec/api/enrich.get_policy.json | 19 +++ 8 files changed, 338 insertions(+) create mode 100644 x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/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/RestGetEnrichPolicyAction.java create mode 100644 x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/GetEnrichPolicyActionRequestTests.java create mode 100644 x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/GetEnrichPolicyActionResponseTests.java create mode 100644 x-pack/plugin/src/test/resources/rest-api-spec/api/enrich.get_policy.json diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/GetEnrichPolicyAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/GetEnrichPolicyAction.java new file mode 100644 index 00000000000..9a3a5c8d9a6 --- /dev/null +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/GetEnrichPolicyAction.java @@ -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 { + + 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 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 { + + 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); + } + } +} diff --git a/x-pack/plugin/enrich/qa/rest/src/test/resources/rest-api-spec/test/enrich/10_basic.yml b/x-pack/plugin/enrich/qa/rest/src/test/resources/rest-api-spec/test/enrich/10_basic.yml index eb2c71cf375..42d0020049a 100644 --- a/x-pack/plugin/enrich/qa/rest/src/test/resources/rest-api-spec/test/enrich/10_basic.yml +++ b/x-pack/plugin/enrich/qa/rest/src/test/resources/rest-api-spec/test/enrich/10_basic.yml @@ -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 } 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 d2713017073..029a39356a0 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 @@ -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), 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 00000000000..a0c6039ee31 --- /dev/null +++ b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportGetEnrichPolicyAction.java @@ -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 { + + @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 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); + } + +} diff --git a/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/rest/RestGetEnrichPolicyAction.java b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/rest/RestGetEnrichPolicyAction.java new file mode 100644 index 00000000000..4cbd7be5a88 --- /dev/null +++ b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/rest/RestGetEnrichPolicyAction.java @@ -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)); + } +} diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/GetEnrichPolicyActionRequestTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/GetEnrichPolicyActionRequestTests.java new file mode 100644 index 00000000000..6d940279b16 --- /dev/null +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/GetEnrichPolicyActionRequestTests.java @@ -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 { + + @Override + protected GetEnrichPolicyAction.Request createTestInstance() { + return new GetEnrichPolicyAction.Request(randomAlphaOfLength(5)); + } + + @Override + protected Writeable.Reader instanceReader() { + return GetEnrichPolicyAction.Request::new; + } +} diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/GetEnrichPolicyActionResponseTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/GetEnrichPolicyActionResponseTests.java new file mode 100644 index 00000000000..74a63484c60 --- /dev/null +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/GetEnrichPolicyActionResponseTests.java @@ -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 { + + @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 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()); + } +} diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/api/enrich.get_policy.json b/x-pack/plugin/src/test/resources/rest-api-spec/api/enrich.get_policy.json new file mode 100644 index 00000000000..8d5664ee7c2 --- /dev/null +++ b/x-pack/plugin/src/test/resources/rest-api-spec/api/enrich.get_policy.json @@ -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 + } +}