Skip to content

Commit e3373d3

Browse files
committed
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.
1 parent 5ea0985 commit e3373d3

File tree

18 files changed

+323
-345
lines changed

18 files changed

+323
-345
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/EnrichIT.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ public void testCRUD() throws Exception {
4545
Response getPolicyResponse = highLevelClient().getLowLevelClient().performRequest(getPolicyRequest);
4646
assertThat(getPolicyResponse.getHttpResponse().getStatusLine().getStatusCode(), equalTo(200));
4747
Map<String, Object> responseBody = toMap(getPolicyResponse);
48-
assertThat(responseBody.get("type"), equalTo(putPolicyRequest.getType()));
49-
assertThat(responseBody.get("indices"), equalTo(putPolicyRequest.getIndices()));
50-
assertThat(responseBody.get("match_field"), equalTo(putPolicyRequest.getMatchField()));
51-
assertThat(responseBody.get("enrich_fields"), equalTo(putPolicyRequest.getEnrichFields()));
48+
@SuppressWarnings("unchecked")
49+
List<Map<String, Object>> responsePolicies = (List<Map<String, Object>>) responseBody.get("policies");
50+
assertThat(responsePolicies.size(), equalTo(1));
51+
assertThat(responsePolicies.get(0).get("type"), equalTo(putPolicyRequest.getType()));
52+
assertThat(responsePolicies.get(0).get("indices"), equalTo(putPolicyRequest.getIndices()));
53+
assertThat(responsePolicies.get(0).get("match_field"), equalTo(putPolicyRequest.getMatchField()));
54+
assertThat(responsePolicies.get(0).get("enrich_fields"), equalTo(putPolicyRequest.getEnrichFields()));
5255
}
5356

5457
private static Map<String, Object> toMap(Response response) throws IOException {

docs/reference/ingest/ingest-node.asciidoc

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,6 @@ Also there are several APIs in order to manage and execute enrich policies:
967967
* <<get-policy-api,Get policy api>>.
968968
* <<delete-policy-api,Delete policy api>>.
969969
* <<execute-policy-api,Execute policy api>>.
970-
* <<list-policies-api,List policies api>>.
971970

972971
If security is enabled then the user managing enrich policies will need to have
973972
the `enrich_user` builtin role. Also the user will need to have read privileges
@@ -1008,7 +1007,7 @@ Response:
10081007

10091008
The get policy api allows a policy to be retrieved by id.
10101009

1011-
Request"
1010+
Request:
10121011

10131012
[source,js]
10141013
--------------------------------------------------
@@ -1022,18 +1021,27 @@ Response:
10221021
[source,js]
10231022
--------------------------------------------------
10241023
{
1025-
"type": "exact_match",
1026-
"indices": ["users"],
1027-
"match_field": "email",
1028-
"enrich_fields": ["first_name", "last_name", "address", "city", "zip", "state"]
1024+
"policies": [
1025+
{
1026+
"name" : "my-policy",
1027+
"type" : "exact_match",
1028+
"indices" : ["users"],
1029+
"match_field" : "email",
1030+
"enrich_fields" : [
1031+
"first_name",
1032+
"last_name",
1033+
"address",
1034+
"city",
1035+
"zip",
1036+
"state"
1037+
]
1038+
}
1039+
]
10291040
}
10301041
--------------------------------------------------
10311042
// TESTRESPONSE
10321043

1033-
[[list-policies-api]]
1034-
==== List Policies API
1035-
1036-
The list policies api allows all policies to be returned.
1044+
The get policy api allows all policies to be returned.
10371045

10381046
Request:
10391047

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/GetEnrichPolicyAction.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
1717

1818
import java.io.IOException;
19+
import java.util.List;
20+
import java.util.Map;
1921
import java.util.Objects;
22+
import java.util.TreeMap;
23+
import java.util.stream.Collectors;
2024

2125
public class GetEnrichPolicyAction extends ActionType<GetEnrichPolicyAction.Response> {
2226

@@ -31,6 +35,8 @@ public static class Request extends MasterNodeReadRequest<Request> {
3135

3236
private String name;
3337

38+
public Request() { }
39+
3440
public Request(String name) {
3541
this.name = name;
3642
}
@@ -75,47 +81,56 @@ public int hashCode() {
7581

7682
public static class Response extends ActionResponse implements ToXContentObject {
7783

78-
private final EnrichPolicy policy;
84+
private final List<EnrichPolicy.NamedPolicy> policies;
7985

80-
public Response(EnrichPolicy policy) {
81-
this.policy = Objects.requireNonNull(policy, "policy cannot be null");
86+
public Response(Map<String, EnrichPolicy> policies) {
87+
Objects.requireNonNull(policies, "policies cannot be null");
88+
// use a treemap to guarantee ordering in the set, then transform it to the list of named policies
89+
this.policies = new TreeMap<>(policies).entrySet().stream()
90+
.map(entry -> new EnrichPolicy.NamedPolicy(entry.getKey(), entry.getValue())).collect(Collectors.toList());
8291
}
8392

8493
public Response(StreamInput in) throws IOException {
85-
policy = new EnrichPolicy(in);
94+
policies = in.readList(EnrichPolicy.NamedPolicy::new);
8695
}
8796

8897
@Override
8998
public void writeTo(StreamOutput out) throws IOException {
90-
policy.writeTo(out);
99+
out.writeList(policies);
91100
}
92101

93102
@Override
94103
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
95104
builder.startObject();
96105
{
97-
policy.toXContent(builder, params);
106+
builder.startArray("policies");
107+
{
108+
for (EnrichPolicy.NamedPolicy policy: policies) {
109+
policy.toXContent(builder, params);
110+
}
111+
}
112+
builder.endArray();
98113
}
99114
builder.endObject();
100115

101116
return builder;
102117
}
103118

104-
public EnrichPolicy getPolicy() {
105-
return policy;
119+
public List<EnrichPolicy.NamedPolicy> getPolicies() {
120+
return policies;
106121
}
107122

108123
@Override
109124
public boolean equals(Object o) {
110125
if (this == o) return true;
111126
if (o == null || getClass() != o.getClass()) return false;
112127
Response response = (Response) o;
113-
return policy.equals(response.policy);
128+
return policies.equals(response.policies);
114129
}
115130

116131
@Override
117132
public int hashCode() {
118-
return Objects.hash(policy);
133+
return Objects.hash(policies);
119134
}
120135
}
121136
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/ListEnrichPolicyAction.java

Lines changed: 0 additions & 102 deletions
This file was deleted.

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/PrivilegeTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction;
1212
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction;
1313
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
14-
import org.elasticsearch.xpack.core.enrich.action.ListEnrichPolicyAction;
1514
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;
1615
import org.elasticsearch.transport.TransportRequest;
1716
import org.elasticsearch.xpack.core.security.authz.permission.ClusterPermission;
@@ -185,7 +184,6 @@ public void testManageEnrichPrivilege() {
185184
verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, DeleteEnrichPolicyAction.NAME);
186185
verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, ExecuteEnrichPolicyAction.NAME);
187186
verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, GetEnrichPolicyAction.NAME);
188-
verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, ListEnrichPolicyAction.NAME);
189187
verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, PutEnrichPolicyAction.NAME);
190188
verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ENRICH, "cluster:admin/xpack/enrich/brand_new_api");
191189
verifyClusterActionDenied(ClusterPrivilegeResolver.MANAGE_ENRICH, "cluster:admin/xpack/whatever");

x-pack/plugin/enrich/qa/rest/src/test/resources/rest-api-spec/test/enrich/10_basic.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
- do:
2020
enrich.get_policy:
2121
name: policy-crud
22-
- match: { type: exact_match }
23-
- match: { indices: ["bar*"] }
24-
- match: { match_field: baz }
25-
- match: { enrich_fields: ["a", "b"] }
22+
- length: { policies: 1 }
23+
- match: { policies.0.name: policy-crud }
24+
- match: { policies.0.type: exact_match }
25+
- match: { policies.0.indices: ["bar*"] }
26+
- match: { policies.0.match_field: baz }
27+
- match: { policies.0.enrich_fields: ["a", "b"] }
2628

2729
- do:
28-
enrich.list_policy: {}
30+
enrich.get_policy: {}
2931
- length: { policies: 1 }
3032
- match: { policies.0.name: policy-crud }
3133
- match: { policies.0.type: exact_match }

x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichPlugin.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,16 @@
3838
import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction;
3939
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction;
4040
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
41-
import org.elasticsearch.xpack.core.enrich.action.ListEnrichPolicyAction;
4241
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;
4342
import org.elasticsearch.xpack.enrich.action.CoordinatorProxyAction;
4443
import org.elasticsearch.xpack.enrich.action.EnrichShardMultiSearchAction;
4544
import org.elasticsearch.xpack.enrich.action.TransportDeleteEnrichPolicyAction;
4645
import org.elasticsearch.xpack.enrich.action.TransportExecuteEnrichPolicyAction;
4746
import org.elasticsearch.xpack.enrich.action.TransportGetEnrichPolicyAction;
48-
import org.elasticsearch.xpack.enrich.action.TransportListEnrichPolicyAction;
4947
import org.elasticsearch.xpack.enrich.action.TransportPutEnrichPolicyAction;
5048
import org.elasticsearch.xpack.enrich.rest.RestDeleteEnrichPolicyAction;
5149
import org.elasticsearch.xpack.enrich.rest.RestExecuteEnrichPolicyAction;
5250
import org.elasticsearch.xpack.enrich.rest.RestGetEnrichPolicyAction;
53-
import org.elasticsearch.xpack.enrich.rest.RestListEnrichPolicyAction;
5451
import org.elasticsearch.xpack.enrich.rest.RestPutEnrichPolicyAction;
5552

5653
import java.util.Arrays;
@@ -115,7 +112,6 @@ public Map<String, Processor.Factory> getProcessors(Processor.Parameters paramet
115112
return Arrays.asList(
116113
new ActionHandler<>(GetEnrichPolicyAction.INSTANCE, TransportGetEnrichPolicyAction.class),
117114
new ActionHandler<>(DeleteEnrichPolicyAction.INSTANCE, TransportDeleteEnrichPolicyAction.class),
118-
new ActionHandler<>(ListEnrichPolicyAction.INSTANCE, TransportListEnrichPolicyAction.class),
119115
new ActionHandler<>(PutEnrichPolicyAction.INSTANCE, TransportPutEnrichPolicyAction.class),
120116
new ActionHandler<>(ExecuteEnrichPolicyAction.INSTANCE, TransportExecuteEnrichPolicyAction.class),
121117
new ActionHandler<>(CoordinatorProxyAction.INSTANCE, CoordinatorProxyAction.TransportAction.class),
@@ -134,7 +130,6 @@ public List<RestHandler> getRestHandlers(Settings settings, RestController restC
134130
return Arrays.asList(
135131
new RestGetEnrichPolicyAction(restController),
136132
new RestDeleteEnrichPolicyAction(restController),
137-
new RestListEnrichPolicyAction(restController),
138133
new RestPutEnrichPolicyAction(restController),
139134
new RestExecuteEnrichPolicyAction(restController)
140135
);

x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportGetEnrichPolicyAction.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.elasticsearch.xpack.enrich.EnrichStore;
2424

2525
import java.io.IOException;
26+
import java.util.Collections;
27+
import java.util.Map;
2628

2729
public class TransportGetEnrichPolicyAction extends TransportMasterNodeReadAction<GetEnrichPolicyAction.Request,
2830
GetEnrichPolicyAction.Response> {
@@ -55,11 +57,18 @@ protected GetEnrichPolicyAction.Response read(StreamInput in) throws IOException
5557
protected void masterOperation(GetEnrichPolicyAction.Request request,
5658
ClusterState state,
5759
ActionListener<GetEnrichPolicyAction.Response> listener) throws Exception {
58-
final EnrichPolicy policy = EnrichStore.getPolicy(request.getName(), state);
59-
if (policy == null) {
60-
throw new ResourceNotFoundException("Policy [{}] was not found", request.getName());
60+
Map<String, EnrichPolicy> policies;
61+
if (request.getName() == null || request.getName().isEmpty()) {
62+
policies = EnrichStore.getPolicies(state);
63+
} else {
64+
EnrichPolicy policy = EnrichStore.getPolicy(request.getName(), state);
65+
if (policy == null) {
66+
throw new ResourceNotFoundException("Policy [{}] was not found", request.getName());
67+
}
68+
policies = Collections.singletonMap(request.getName(), policy);
69+
6170
}
62-
listener.onResponse(new GetEnrichPolicyAction.Response(policy));
71+
listener.onResponse(new GetEnrichPolicyAction.Response(policies));
6372
}
6473

6574
@Override

0 commit comments

Comments
 (0)