Consolidate enrich list all and get by name APIs (#45705)

The get and list APIs are a single API in this commit. Whether
requesting one named policy or all policies, a list of policies is
returened. The list API code has all been removed and the GET api is
what remains, which contains much of the list response code.
This commit is contained in:
Michael Basnight 2019-08-20 10:05:45 -05:00
parent 5ea0985711
commit e3373d349b
18 changed files with 323 additions and 345 deletions

View File

@ -45,10 +45,13 @@ public class EnrichIT extends ESRestHighLevelClientTestCase {
Response getPolicyResponse = highLevelClient().getLowLevelClient().performRequest(getPolicyRequest); Response getPolicyResponse = highLevelClient().getLowLevelClient().performRequest(getPolicyRequest);
assertThat(getPolicyResponse.getHttpResponse().getStatusLine().getStatusCode(), equalTo(200)); assertThat(getPolicyResponse.getHttpResponse().getStatusLine().getStatusCode(), equalTo(200));
Map<String, Object> responseBody = toMap(getPolicyResponse); Map<String, Object> responseBody = toMap(getPolicyResponse);
assertThat(responseBody.get("type"), equalTo(putPolicyRequest.getType())); @SuppressWarnings("unchecked")
assertThat(responseBody.get("indices"), equalTo(putPolicyRequest.getIndices())); List<Map<String, Object>> responsePolicies = (List<Map<String, Object>>) responseBody.get("policies");
assertThat(responseBody.get("match_field"), equalTo(putPolicyRequest.getMatchField())); assertThat(responsePolicies.size(), equalTo(1));
assertThat(responseBody.get("enrich_fields"), equalTo(putPolicyRequest.getEnrichFields())); assertThat(responsePolicies.get(0).get("type"), equalTo(putPolicyRequest.getType()));
assertThat(responsePolicies.get(0).get("indices"), equalTo(putPolicyRequest.getIndices()));
assertThat(responsePolicies.get(0).get("match_field"), equalTo(putPolicyRequest.getMatchField()));
assertThat(responsePolicies.get(0).get("enrich_fields"), equalTo(putPolicyRequest.getEnrichFields()));
} }
private static Map<String, Object> toMap(Response response) throws IOException { private static Map<String, Object> toMap(Response response) throws IOException {

View File

@ -967,7 +967,6 @@ Also there are several APIs in order to manage and execute enrich policies:
* <<get-policy-api,Get policy api>>. * <<get-policy-api,Get policy api>>.
* <<delete-policy-api,Delete policy api>>. * <<delete-policy-api,Delete policy api>>.
* <<execute-policy-api,Execute policy api>>. * <<execute-policy-api,Execute policy api>>.
* <<list-policies-api,List policies api>>.
If security is enabled then the user managing enrich policies will need to have If security is enabled then the user managing enrich policies will need to have
the `enrich_user` builtin role. Also the user will need to have read privileges the `enrich_user` builtin role. Also the user will need to have read privileges
@ -1008,7 +1007,7 @@ Response:
The get policy api allows a policy to be retrieved by id. The get policy api allows a policy to be retrieved by id.
Request" Request:
[source,js] [source,js]
-------------------------------------------------- --------------------------------------------------
@ -1022,18 +1021,27 @@ Response:
[source,js] [source,js]
-------------------------------------------------- --------------------------------------------------
{ {
"type": "exact_match", "policies": [
"indices": ["users"], {
"match_field": "email", "name" : "my-policy",
"enrich_fields": ["first_name", "last_name", "address", "city", "zip", "state"] "type" : "exact_match",
"indices" : ["users"],
"match_field" : "email",
"enrich_fields" : [
"first_name",
"last_name",
"address",
"city",
"zip",
"state"
]
}
]
} }
-------------------------------------------------- --------------------------------------------------
// TESTRESPONSE // TESTRESPONSE
[[list-policies-api]] The get policy api allows all policies to be returned.
==== List Policies API
The list policies api allows all policies to be returned.
Request: Request:

View File

@ -16,7 +16,11 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.enrich.EnrichPolicy; import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Collectors;
public class GetEnrichPolicyAction extends ActionType<GetEnrichPolicyAction.Response> { public class GetEnrichPolicyAction extends ActionType<GetEnrichPolicyAction.Response> {
@ -31,6 +35,8 @@ public class GetEnrichPolicyAction extends ActionType<GetEnrichPolicyAction.Resp
private String name; private String name;
public Request() { }
public Request(String name) { public Request(String name) {
this.name = name; this.name = name;
} }
@ -75,34 +81,43 @@ public class GetEnrichPolicyAction extends ActionType<GetEnrichPolicyAction.Resp
public static class Response extends ActionResponse implements ToXContentObject { public static class Response extends ActionResponse implements ToXContentObject {
private final EnrichPolicy policy; private final List<EnrichPolicy.NamedPolicy> policies;
public Response(EnrichPolicy policy) { public Response(Map<String, EnrichPolicy> policies) {
this.policy = Objects.requireNonNull(policy, "policy cannot be null"); Objects.requireNonNull(policies, "policies cannot be null");
// use a treemap to guarantee ordering in the set, then transform it to the list of named policies
this.policies = new TreeMap<>(policies).entrySet().stream()
.map(entry -> new EnrichPolicy.NamedPolicy(entry.getKey(), entry.getValue())).collect(Collectors.toList());
} }
public Response(StreamInput in) throws IOException { public Response(StreamInput in) throws IOException {
policy = new EnrichPolicy(in); policies = in.readList(EnrichPolicy.NamedPolicy::new);
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
policy.writeTo(out); out.writeList(policies);
} }
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(); builder.startObject();
{ {
policy.toXContent(builder, params); builder.startArray("policies");
{
for (EnrichPolicy.NamedPolicy policy: policies) {
policy.toXContent(builder, params);
}
}
builder.endArray();
} }
builder.endObject(); builder.endObject();
return builder; return builder;
} }
public EnrichPolicy getPolicy() { public List<EnrichPolicy.NamedPolicy> getPolicies() {
return policy; return policies;
} }
@Override @Override
@ -110,12 +125,12 @@ public class GetEnrichPolicyAction extends ActionType<GetEnrichPolicyAction.Resp
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
Response response = (Response) o; Response response = (Response) o;
return policy.equals(response.policy); return policies.equals(response.policies);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(policy); return Objects.hash(policies);
} }
} }
} }

View File

@ -1,102 +0,0 @@
/*
* 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.ActionType;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.support.master.MasterNodeRequest;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
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.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Collectors;
public class ListEnrichPolicyAction extends ActionType<ListEnrichPolicyAction.Response> {
public static final ListEnrichPolicyAction INSTANCE = new ListEnrichPolicyAction();
public static final String NAME = "cluster:admin/xpack/enrich/list";
private ListEnrichPolicyAction() {
super(NAME, Response::new);
}
public static class Request extends MasterNodeRequest<ListEnrichPolicyAction.Request> {
public Request() {}
public Request(StreamInput in) throws IOException {
super(in);
}
@Override
public ActionRequestValidationException validate() {
return null;
}
}
public static class Response extends ActionResponse implements ToXContentObject {
private final List<EnrichPolicy.NamedPolicy> policies;
public Response(Map<String, EnrichPolicy> policies) {
Objects.requireNonNull(policies, "policies cannot be null");
// use a treemap to guarantee ordering in the set, then transform it to the list of named policies
this.policies = new TreeMap<>(policies).entrySet().stream()
.map(entry -> new EnrichPolicy.NamedPolicy(entry.getKey(), entry.getValue())).collect(Collectors.toList());
}
public Response(StreamInput in) throws IOException {
policies = in.readList(EnrichPolicy.NamedPolicy::new);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeList(policies);
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
{
builder.startArray("policies");
{
for (EnrichPolicy.NamedPolicy policy: policies) {
policy.toXContent(builder, params);
}
}
builder.endArray();
}
builder.endObject();
return builder;
}
public List<EnrichPolicy.NamedPolicy> getPolicies() {
return policies;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Response response = (Response) o;
return policies.equals(response.policies);
}
@Override
public int hashCode() {
return Objects.hash(policies);
}
}
}

View File

@ -11,7 +11,6 @@ import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction;
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction;
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction; 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.core.enrich.action.PutEnrichPolicyAction;
import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.transport.TransportRequest;
import org.elasticsearch.xpack.core.security.authz.permission.ClusterPermission; import org.elasticsearch.xpack.core.security.authz.permission.ClusterPermission;
@ -185,7 +184,6 @@ public class PrivilegeTests extends ESTestCase {
verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, DeleteEnrichPolicyAction.NAME); verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, DeleteEnrichPolicyAction.NAME);
verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, ExecuteEnrichPolicyAction.NAME); verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, ExecuteEnrichPolicyAction.NAME);
verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, GetEnrichPolicyAction.NAME); verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, GetEnrichPolicyAction.NAME);
verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, ListEnrichPolicyAction.NAME);
verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, PutEnrichPolicyAction.NAME); verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, PutEnrichPolicyAction.NAME);
verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, "cluster:admin/xpack/enrich/brand_new_api"); verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, "cluster:admin/xpack/enrich/brand_new_api");
verifyClusterActionDenied(ClusterPrivilegeResolver.MANAGE_ENRICH, "cluster:admin/xpack/whatever"); verifyClusterActionDenied(ClusterPrivilegeResolver.MANAGE_ENRICH, "cluster:admin/xpack/whatever");

View File

@ -19,13 +19,15 @@
- do: - do:
enrich.get_policy: enrich.get_policy:
name: policy-crud name: policy-crud
- match: { type: exact_match } - length: { policies: 1 }
- match: { indices: ["bar*"] } - match: { policies.0.name: policy-crud }
- match: { match_field: baz } - match: { policies.0.type: exact_match }
- match: { enrich_fields: ["a", "b"] } - match: { policies.0.indices: ["bar*"] }
- match: { policies.0.match_field: baz }
- match: { policies.0.enrich_fields: ["a", "b"] }
- do: - do:
enrich.list_policy: {} enrich.get_policy: {}
- length: { policies: 1 } - length: { policies: 1 }
- match: { policies.0.name: policy-crud } - match: { policies.0.name: policy-crud }
- match: { policies.0.type: exact_match } - match: { policies.0.type: exact_match }

View File

@ -38,19 +38,16 @@ import org.elasticsearch.xpack.core.XPackPlugin;
import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction;
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction;
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction; 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.core.enrich.action.PutEnrichPolicyAction;
import org.elasticsearch.xpack.enrich.action.CoordinatorProxyAction; import org.elasticsearch.xpack.enrich.action.CoordinatorProxyAction;
import org.elasticsearch.xpack.enrich.action.EnrichShardMultiSearchAction; import org.elasticsearch.xpack.enrich.action.EnrichShardMultiSearchAction;
import org.elasticsearch.xpack.enrich.action.TransportDeleteEnrichPolicyAction; import org.elasticsearch.xpack.enrich.action.TransportDeleteEnrichPolicyAction;
import org.elasticsearch.xpack.enrich.action.TransportExecuteEnrichPolicyAction; import org.elasticsearch.xpack.enrich.action.TransportExecuteEnrichPolicyAction;
import org.elasticsearch.xpack.enrich.action.TransportGetEnrichPolicyAction; 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.action.TransportPutEnrichPolicyAction;
import org.elasticsearch.xpack.enrich.rest.RestDeleteEnrichPolicyAction; import org.elasticsearch.xpack.enrich.rest.RestDeleteEnrichPolicyAction;
import org.elasticsearch.xpack.enrich.rest.RestExecuteEnrichPolicyAction; import org.elasticsearch.xpack.enrich.rest.RestExecuteEnrichPolicyAction;
import org.elasticsearch.xpack.enrich.rest.RestGetEnrichPolicyAction; import org.elasticsearch.xpack.enrich.rest.RestGetEnrichPolicyAction;
import org.elasticsearch.xpack.enrich.rest.RestListEnrichPolicyAction;
import org.elasticsearch.xpack.enrich.rest.RestPutEnrichPolicyAction; import org.elasticsearch.xpack.enrich.rest.RestPutEnrichPolicyAction;
import java.util.Arrays; import java.util.Arrays;
@ -115,7 +112,6 @@ public class EnrichPlugin extends Plugin implements ActionPlugin, IngestPlugin {
return Arrays.asList( return Arrays.asList(
new ActionHandler<>(GetEnrichPolicyAction.INSTANCE, TransportGetEnrichPolicyAction.class), new ActionHandler<>(GetEnrichPolicyAction.INSTANCE, TransportGetEnrichPolicyAction.class),
new ActionHandler<>(DeleteEnrichPolicyAction.INSTANCE, TransportDeleteEnrichPolicyAction.class), new ActionHandler<>(DeleteEnrichPolicyAction.INSTANCE, TransportDeleteEnrichPolicyAction.class),
new ActionHandler<>(ListEnrichPolicyAction.INSTANCE, TransportListEnrichPolicyAction.class),
new ActionHandler<>(PutEnrichPolicyAction.INSTANCE, TransportPutEnrichPolicyAction.class), new ActionHandler<>(PutEnrichPolicyAction.INSTANCE, TransportPutEnrichPolicyAction.class),
new ActionHandler<>(ExecuteEnrichPolicyAction.INSTANCE, TransportExecuteEnrichPolicyAction.class), new ActionHandler<>(ExecuteEnrichPolicyAction.INSTANCE, TransportExecuteEnrichPolicyAction.class),
new ActionHandler<>(CoordinatorProxyAction.INSTANCE, CoordinatorProxyAction.TransportAction.class), new ActionHandler<>(CoordinatorProxyAction.INSTANCE, CoordinatorProxyAction.TransportAction.class),
@ -134,7 +130,6 @@ public class EnrichPlugin extends Plugin implements ActionPlugin, IngestPlugin {
return Arrays.asList( return Arrays.asList(
new RestGetEnrichPolicyAction(restController), new RestGetEnrichPolicyAction(restController),
new RestDeleteEnrichPolicyAction(restController), new RestDeleteEnrichPolicyAction(restController),
new RestListEnrichPolicyAction(restController),
new RestPutEnrichPolicyAction(restController), new RestPutEnrichPolicyAction(restController),
new RestExecuteEnrichPolicyAction(restController) new RestExecuteEnrichPolicyAction(restController)
); );

View File

@ -23,6 +23,8 @@ import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
import org.elasticsearch.xpack.enrich.EnrichStore; import org.elasticsearch.xpack.enrich.EnrichStore;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.Map;
public class TransportGetEnrichPolicyAction extends TransportMasterNodeReadAction<GetEnrichPolicyAction.Request, public class TransportGetEnrichPolicyAction extends TransportMasterNodeReadAction<GetEnrichPolicyAction.Request,
GetEnrichPolicyAction.Response> { GetEnrichPolicyAction.Response> {
@ -55,11 +57,18 @@ public class TransportGetEnrichPolicyAction extends TransportMasterNodeReadActio
protected void masterOperation(GetEnrichPolicyAction.Request request, protected void masterOperation(GetEnrichPolicyAction.Request request,
ClusterState state, ClusterState state,
ActionListener<GetEnrichPolicyAction.Response> listener) throws Exception { ActionListener<GetEnrichPolicyAction.Response> listener) throws Exception {
final EnrichPolicy policy = EnrichStore.getPolicy(request.getName(), state); Map<String, EnrichPolicy> policies;
if (policy == null) { if (request.getName() == null || request.getName().isEmpty()) {
throw new ResourceNotFoundException("Policy [{}] was not found", request.getName()); policies = EnrichStore.getPolicies(state);
} else {
EnrichPolicy policy = EnrichStore.getPolicy(request.getName(), state);
if (policy == null) {
throw new ResourceNotFoundException("Policy [{}] was not found", request.getName());
}
policies = Collections.singletonMap(request.getName(), policy);
} }
listener.onResponse(new GetEnrichPolicyAction.Response(policy)); listener.onResponse(new GetEnrichPolicyAction.Response(policies));
} }
@Override @Override

View File

@ -1,67 +0,0 @@
/*
* 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.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.EnrichPolicy;
import org.elasticsearch.xpack.core.enrich.action.ListEnrichPolicyAction;
import org.elasticsearch.xpack.enrich.EnrichStore;
import java.io.IOException;
import java.util.Map;
public class TransportListEnrichPolicyAction
extends TransportMasterNodeAction<ListEnrichPolicyAction.Request, ListEnrichPolicyAction.Response> {
@Inject
public TransportListEnrichPolicyAction(TransportService transportService,
ClusterService clusterService,
ThreadPool threadPool,
ActionFilters actionFilters,
IndexNameExpressionResolver indexNameExpressionResolver) {
super(ListEnrichPolicyAction.NAME, transportService, clusterService, threadPool, actionFilters,
ListEnrichPolicyAction.Request::new, indexNameExpressionResolver);
}
@Override
protected String executor() {
return ThreadPool.Names.SAME;
}
@Override
protected ListEnrichPolicyAction.Response read(StreamInput in) throws IOException {
return new ListEnrichPolicyAction.Response(in);
}
protected ListEnrichPolicyAction.Response newResponse() {
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
}
@Override
protected void masterOperation(ListEnrichPolicyAction.Request request, ClusterState state,
ActionListener<ListEnrichPolicyAction.Response> listener) throws Exception {
Map<String, EnrichPolicy> policies = EnrichStore.getPolicies(clusterService.state());
listener.onResponse(new ListEnrichPolicyAction.Response(policies));
}
@Override
protected ClusterBlockException checkBlock(ListEnrichPolicyAction.Request request, ClusterState state) {
return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_WRITE);
}
}

View File

@ -18,6 +18,7 @@ public class RestGetEnrichPolicyAction extends BaseRestHandler {
public RestGetEnrichPolicyAction(final RestController controller) { public RestGetEnrichPolicyAction(final RestController controller) {
controller.registerHandler(RestRequest.Method.GET, "/_enrich/policy/{name}", this); controller.registerHandler(RestRequest.Method.GET, "/_enrich/policy/{name}", this);
controller.registerHandler(RestRequest.Method.GET, "/_enrich/policy", this);
} }
@Override @Override

View File

@ -1,33 +0,0 @@
/*
* 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.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.ListEnrichPolicyAction;
import java.io.IOException;
public class RestListEnrichPolicyAction extends BaseRestHandler {
public RestListEnrichPolicyAction(final RestController controller) {
controller.registerHandler(RestRequest.Method.GET, "/_enrich/policy", this);
}
@Override
public String getName() {
return "list_enrich_policy";
}
@Override
protected RestChannelConsumer prepareRequest(final RestRequest restRequest, final NodeClient client) throws IOException {
final ListEnrichPolicyAction.Request request = new ListEnrichPolicyAction.Request();
return channel -> client.execute(ListEnrichPolicyAction.INSTANCE, request, new RestToXContentListener<>(channel));
}
}

View File

@ -27,7 +27,6 @@ import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction;
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction;
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction; 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.core.enrich.action.PutEnrichPolicyAction;
import java.util.ArrayList; import java.util.ArrayList;
@ -82,9 +81,10 @@ public class EnrichMultiNodeIT extends ESIntegTestCase {
client().execute(PutEnrichPolicyAction.INSTANCE, request).actionGet(); client().execute(PutEnrichPolicyAction.INSTANCE, request).actionGet();
client().execute(ExecuteEnrichPolicyAction.INSTANCE, new ExecuteEnrichPolicyAction.Request(policyName)).actionGet(); client().execute(ExecuteEnrichPolicyAction.INSTANCE, new ExecuteEnrichPolicyAction.Request(policyName)).actionGet();
EnrichPolicy result = EnrichPolicy.NamedPolicy result =
client().execute(GetEnrichPolicyAction.INSTANCE, new GetEnrichPolicyAction.Request(policyName)).actionGet().getPolicy(); client().execute(GetEnrichPolicyAction.INSTANCE,
assertThat(result, equalTo(enrichPolicy)); new GetEnrichPolicyAction.Request(policyName)).actionGet().getPolicies().get(0);
assertThat(result, equalTo(new EnrichPolicy.NamedPolicy(policyName, enrichPolicy)));
String enrichIndexPrefix = EnrichPolicy.getBaseName(policyName) + "*"; String enrichIndexPrefix = EnrichPolicy.getBaseName(policyName) + "*";
refresh(enrichIndexPrefix); refresh(enrichIndexPrefix);
SearchResponse searchResponse = client().search(new SearchRequest(enrichIndexPrefix)).actionGet(); SearchResponse searchResponse = client().search(new SearchRequest(enrichIndexPrefix)).actionGet();
@ -92,8 +92,8 @@ public class EnrichMultiNodeIT extends ESIntegTestCase {
assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long) numDocsInSourceIndex)); assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long) numDocsInSourceIndex));
} }
ListEnrichPolicyAction.Response response = GetEnrichPolicyAction.Response response =
client().execute(ListEnrichPolicyAction.INSTANCE, new ListEnrichPolicyAction.Request()).actionGet(); client().execute(GetEnrichPolicyAction.INSTANCE, new GetEnrichPolicyAction.Request()).actionGet();
assertThat(response.getPolicies().size(), equalTo(numPolicies)); assertThat(response.getPolicies().size(), equalTo(numPolicies));
for (int i = 0; i < numPolicies; i++) { for (int i = 0; i < numPolicies; i++) {
@ -101,7 +101,7 @@ public class EnrichMultiNodeIT extends ESIntegTestCase {
client().execute(DeleteEnrichPolicyAction.INSTANCE, new DeleteEnrichPolicyAction.Request(policyName)).actionGet(); client().execute(DeleteEnrichPolicyAction.INSTANCE, new DeleteEnrichPolicyAction.Request(policyName)).actionGet();
} }
response = client().execute(ListEnrichPolicyAction.INSTANCE, new ListEnrichPolicyAction.Request()).actionGet(); response = client().execute(GetEnrichPolicyAction.INSTANCE, new GetEnrichPolicyAction.Request()).actionGet();
assertThat(response.getPolicies().size(), equalTo(0)); assertThat(response.getPolicies().size(), equalTo(0));
} }

View File

@ -11,7 +11,7 @@ import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.XPackSettings;
import org.elasticsearch.xpack.core.enrich.EnrichPolicy; import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
import org.elasticsearch.xpack.core.enrich.action.ListEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;
import java.util.Arrays; import java.util.Arrays;
@ -63,8 +63,8 @@ public class EnrichRestartIT extends ESIntegTestCase {
} }
private static void verifyPolicies(int numPolicies, EnrichPolicy enrichPolicy) { private static void verifyPolicies(int numPolicies, EnrichPolicy enrichPolicy) {
ListEnrichPolicyAction.Response response = GetEnrichPolicyAction.Response response =
client().execute(ListEnrichPolicyAction.INSTANCE, new ListEnrichPolicyAction.Request()).actionGet(); client().execute(GetEnrichPolicyAction.INSTANCE, new GetEnrichPolicyAction.Request()).actionGet();
assertThat(response.getPolicies().size(), equalTo(numPolicies)); assertThat(response.getPolicies().size(), equalTo(numPolicies));
for (int i = 0; i < numPolicies; i++) { for (int i = 0; i < numPolicies; i++) {
String policyName = POLICY_NAME + i; String policyName = POLICY_NAME + i;

View File

@ -13,22 +13,41 @@ import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.assertEqualPolicies; import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.assertEqualPolicies;
import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.randomEnrichPolicy; import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.randomEnrichPolicy;
import static org.hamcrest.core.IsEqual.equalTo;
public class GetEnrichPolicyActionResponseTests extends AbstractSerializingTestCase<GetEnrichPolicyAction.Response> { public class GetEnrichPolicyActionResponseTests extends AbstractSerializingTestCase<GetEnrichPolicyAction.Response> {
@Override @Override
protected GetEnrichPolicyAction.Response doParseInstance(XContentParser parser) throws IOException { protected GetEnrichPolicyAction.Response doParseInstance(XContentParser parser) throws IOException {
EnrichPolicy policy = EnrichPolicy.fromXContent(parser); Map<String, EnrichPolicy> policies = new HashMap<>();
return new GetEnrichPolicyAction.Response(policy); assert parser.nextToken() == XContentParser.Token.START_OBJECT;
assert parser.nextToken() == XContentParser.Token.FIELD_NAME;
assert parser.currentName().equals("policies");
assert parser.nextToken() == XContentParser.Token.START_ARRAY;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
assert token == XContentParser.Token.START_OBJECT;
EnrichPolicy.NamedPolicy policy = EnrichPolicy.NamedPolicy.fromXContent(parser);
policies.put(policy.getName(), policy.getPolicy());
}
return new GetEnrichPolicyAction.Response(policies);
} }
@Override @Override
protected GetEnrichPolicyAction.Response createTestInstance() { protected GetEnrichPolicyAction.Response createTestInstance() {
EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON); Map<String, EnrichPolicy> items = new HashMap<>();
return new GetEnrichPolicyAction.Response(policy); for (int i = 0; i < randomIntBetween(0, 3); i++) {
EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON);
items.put(randomAlphaOfLength(3), policy);
}
return new GetEnrichPolicyAction.Response(items);
} }
@Override @Override
@ -40,6 +59,14 @@ public class GetEnrichPolicyActionResponseTests extends AbstractSerializingTestC
protected void assertEqualInstances(GetEnrichPolicyAction.Response expectedInstance, GetEnrichPolicyAction.Response newInstance) { protected void assertEqualInstances(GetEnrichPolicyAction.Response expectedInstance, GetEnrichPolicyAction.Response newInstance) {
assertNotSame(expectedInstance, newInstance); assertNotSame(expectedInstance, newInstance);
// the tests shuffle around the policy query source xcontent type, so this is needed here // the tests shuffle around the policy query source xcontent type, so this is needed here
assertEqualPolicies(expectedInstance.getPolicy(), newInstance.getPolicy()); assertThat(expectedInstance.getPolicies().size(), equalTo(newInstance.getPolicies().size()));
// since the backing store is a treemap the list will be sorted so we can just check each
// instance is the same
for (int i = 0; i < expectedInstance.getPolicies().size(); i++) {
EnrichPolicy.NamedPolicy expected = expectedInstance.getPolicies().get(i);
EnrichPolicy.NamedPolicy newed = newInstance.getPolicies().get(i);
assertThat(expected.getName(), equalTo(newed.getName()));
assertEqualPolicies(expected.getPolicy(), newed.getPolicy());
}
} }
} }

View File

@ -1,72 +0,0 @@
/*
* 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.ListEnrichPolicyAction;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.assertEqualPolicies;
import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.randomEnrichPolicy;
import static org.hamcrest.Matchers.equalTo;
public class ListEnrichPolicyActionResponseTests extends AbstractSerializingTestCase<ListEnrichPolicyAction.Response> {
@Override
protected ListEnrichPolicyAction.Response doParseInstance(XContentParser parser) throws IOException {
Map<String, EnrichPolicy> policies = new HashMap<>();
assert parser.nextToken() == XContentParser.Token.START_OBJECT;
assert parser.nextToken() == XContentParser.Token.FIELD_NAME;
assert parser.currentName().equals("policies");
assert parser.nextToken() == XContentParser.Token.START_ARRAY;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
assert token == XContentParser.Token.START_OBJECT;
EnrichPolicy.NamedPolicy policy = EnrichPolicy.NamedPolicy.fromXContent(parser);
policies.put(policy.getName(), policy.getPolicy());
}
return new ListEnrichPolicyAction.Response(policies);
}
@Override
protected ListEnrichPolicyAction.Response createTestInstance() {
Map<String, EnrichPolicy> items = new HashMap<>();
for (int i = 0; i < randomIntBetween(0, 3); i++) {
EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON);
items.put(randomAlphaOfLength(3), policy);
}
return new ListEnrichPolicyAction.Response(items);
}
@Override
protected Writeable.Reader<ListEnrichPolicyAction.Response> instanceReader() {
return ListEnrichPolicyAction.Response::new;
}
@Override
protected void assertEqualInstances(ListEnrichPolicyAction.Response expectedInstance, ListEnrichPolicyAction.Response newInstance) {
assertThat(expectedInstance.getPolicies().size(), equalTo(newInstance.getPolicies().size()));
for (EnrichPolicy.NamedPolicy expectedPolicy: expectedInstance.getPolicies()) {
// contains and indexOf cannot be used here as the query source may be represented differently, so we need to check
// if the name is the same and if it is, use that to ensure the policies are the same
Optional<EnrichPolicy.NamedPolicy> maybePolicy = newInstance.getPolicies().stream()
.filter(p -> p.getName().equals(expectedPolicy.getName())).findFirst();
assertTrue(maybePolicy.isPresent());
EnrichPolicy.NamedPolicy newPolicy = maybePolicy.get();
assertEqualPolicies(expectedPolicy.getPolicy(), newPolicy.getPolicy());
assertThat(expectedPolicy.getName(), equalTo(newPolicy.getName()));
}
}
}

View File

@ -0,0 +1,209 @@
/*
* 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.master.AcknowledgedResponse;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction;
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
import org.elasticsearch.xpack.enrich.AbstractEnrichTestCase;
import org.junit.After;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.assertEqualPolicies;
import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.randomEnrichPolicy;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
public class TransportGetEnrichPolicyActionTests extends AbstractEnrichTestCase {
@After
private void cleanupPolicies() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<GetEnrichPolicyAction.Response> reference = new AtomicReference<>();
final TransportGetEnrichPolicyAction transportAction = node().injector().getInstance(TransportGetEnrichPolicyAction.class);
transportAction.execute(null,
new GetEnrichPolicyAction.Request(),
new ActionListener<GetEnrichPolicyAction.Response>() {
@Override
public void onResponse(GetEnrichPolicyAction.Response response) {
reference.set(response);
latch.countDown();
}
public void onFailure(final Exception e) {
fail();
}
});
latch.await();
assertNotNull(reference.get());
GetEnrichPolicyAction.Response response = reference.get();
for (EnrichPolicy.NamedPolicy policy: response.getPolicies()) {
final CountDownLatch loopLatch = new CountDownLatch(1);
final AtomicReference<AcknowledgedResponse> loopReference = new AtomicReference<>();
final TransportDeleteEnrichPolicyAction deleteAction = node().injector().getInstance(TransportDeleteEnrichPolicyAction.class);
deleteAction.execute(null,
new DeleteEnrichPolicyAction.Request(policy.getName()),
new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
loopReference.set(acknowledgedResponse);
loopLatch.countDown();
}
public void onFailure(final Exception e) {
fail();
}
});
loopLatch.await();
assertNotNull(loopReference.get());
assertTrue(loopReference.get().isAcknowledged());
}
}
public void testListPolicies() throws InterruptedException {
EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON);
ClusterService clusterService = getInstanceFromNode(ClusterService.class);
String name = "my-policy";
AtomicReference<Exception> error = saveEnrichPolicy(name, policy, clusterService);
assertThat(error.get(), nullValue());
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<GetEnrichPolicyAction.Response> reference = new AtomicReference<>();
final TransportGetEnrichPolicyAction transportAction = node().injector().getInstance(TransportGetEnrichPolicyAction.class);
transportAction.execute(null,
// empty or null should return the same
randomBoolean() ? new GetEnrichPolicyAction.Request() : new GetEnrichPolicyAction.Request(""),
new ActionListener<GetEnrichPolicyAction.Response>() {
@Override
public void onResponse(GetEnrichPolicyAction.Response response) {
reference.set(response);
latch.countDown();
}
public void onFailure(final Exception e) {
fail();
}
});
latch.await();
assertNotNull(reference.get());
GetEnrichPolicyAction.Response response = reference.get();
assertThat(response.getPolicies().size(), equalTo(1));
EnrichPolicy.NamedPolicy actualPolicy = response.getPolicies().get(0);
assertThat(name, equalTo(actualPolicy.getName()));
assertEqualPolicies(policy, actualPolicy.getPolicy());
}
public void testListEmptyPolicies() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<GetEnrichPolicyAction.Response> reference = new AtomicReference<>();
final TransportGetEnrichPolicyAction transportAction = node().injector().getInstance(TransportGetEnrichPolicyAction.class);
transportAction.execute(null,
new GetEnrichPolicyAction.Request(),
new ActionListener<GetEnrichPolicyAction.Response>() {
@Override
public void onResponse(GetEnrichPolicyAction.Response response) {
reference.set(response);
latch.countDown();
}
public void onFailure(final Exception e) {
fail();
}
});
latch.await();
assertNotNull(reference.get());
GetEnrichPolicyAction.Response response = reference.get();
assertThat(response.getPolicies().size(), equalTo(0));
}
public void testGetPolicy() throws InterruptedException {
EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON);
ClusterService clusterService = getInstanceFromNode(ClusterService.class);
String name = "my-policy";
AtomicReference<Exception> error = saveEnrichPolicy(name, policy, clusterService);
assertThat(error.get(), nullValue());
// save a second one to verify the count below on GET
error = saveEnrichPolicy("something-else", randomEnrichPolicy(XContentType.JSON), clusterService);
assertThat(error.get(), nullValue());
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<GetEnrichPolicyAction.Response> reference = new AtomicReference<>();
final TransportGetEnrichPolicyAction transportAction = node().injector().getInstance(TransportGetEnrichPolicyAction.class);
transportAction.execute(null,
new GetEnrichPolicyAction.Request(name),
new ActionListener<GetEnrichPolicyAction.Response>() {
@Override
public void onResponse(GetEnrichPolicyAction.Response response) {
reference.set(response);
latch.countDown();
}
public void onFailure(final Exception e) {
fail();
}
});
latch.await();
assertNotNull(reference.get());
GetEnrichPolicyAction.Response response = reference.get();
assertThat(response.getPolicies().size(), equalTo(1));
EnrichPolicy.NamedPolicy actualPolicy = response.getPolicies().get(0);
assertThat(name, equalTo(actualPolicy.getName()));
assertEqualPolicies(policy, actualPolicy.getPolicy());
}
public void testGetPolicyThrowsError() throws InterruptedException {
EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON);
ClusterService clusterService = getInstanceFromNode(ClusterService.class);
String name = "my-policy";
AtomicReference<Exception> error = saveEnrichPolicy(name, policy, clusterService);
assertThat(error.get(), nullValue());
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Exception> reference = new AtomicReference<>();
final TransportGetEnrichPolicyAction transportAction = node().injector().getInstance(TransportGetEnrichPolicyAction.class);
transportAction.execute(null,
new GetEnrichPolicyAction.Request("non-exists"),
new ActionListener<GetEnrichPolicyAction.Response>() {
@Override
public void onResponse(GetEnrichPolicyAction.Response response) {
fail();
}
public void onFailure(final Exception e) {
reference.set(e);
latch.countDown();
}
});
latch.await();
assertNotNull(reference.get());
assertThat(reference.get(), instanceOf(ResourceNotFoundException.class));
assertThat(reference.get().getMessage(),
equalTo("Policy [non-exists] was not found"));
}
}

View File

@ -4,8 +4,7 @@
"stability" : "stable", "stability" : "stable",
"methods": [ "GET" ], "methods": [ "GET" ],
"url": { "url": {
"path": "/_enrich/policy/{name}", "paths": ["/_enrich/policy/{name}", "/_enrich/policy/"],
"paths": ["/_enrich/policy/{name}"],
"parts": { "parts": {
"name": { "name": {
"type" : "string", "type" : "string",

View File

@ -1,14 +0,0 @@
{
"enrich.list_policy": {
"documentation": "https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-list-policy.html",
"stability" : "stable",
"methods": [ "GET" ],
"url": {
"path": "/_enrich/policy",
"paths": ["/_enrich/policy"],
"parts": {},
"params": {}
},
"body": null
}
}