Skip to content

Commit 1c28f30

Browse files
authored
Add enrich policy PUT API (#41383)
This commit wires up the Rest calls and Transport calls for PUT enrich policy, as well as tests and rest spec additions.
1 parent 284c508 commit 1c28f30

File tree

9 files changed

+311
-2
lines changed

9 files changed

+311
-2
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ private XPackSettings() {
3939
}
4040

4141

42+
/**
43+
* Setting for controlling whether or not enrich is enabled.
44+
*/
45+
public static final Setting<Boolean> ENRICH_ENABLED_SETTING = Setting.boolSetting("xpack.enrich.enabled", true, Property.NodeScope);
46+
4247
/**
4348
* Setting for controlling whether or not CCR is enabled.
4449
*/
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.core.enrich.action;
7+
8+
import org.elasticsearch.action.Action;
9+
import org.elasticsearch.action.ActionRequestValidationException;
10+
import org.elasticsearch.action.support.master.AcknowledgedResponse;
11+
import org.elasticsearch.action.support.master.MasterNodeRequest;
12+
import org.elasticsearch.common.io.stream.StreamInput;
13+
import org.elasticsearch.common.io.stream.StreamOutput;
14+
import org.elasticsearch.common.xcontent.XContentParser;
15+
import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
16+
17+
import java.io.IOException;
18+
import java.util.Objects;
19+
20+
public class PutEnrichPolicyAction extends Action<AcknowledgedResponse> {
21+
22+
public static final PutEnrichPolicyAction INSTANCE = new PutEnrichPolicyAction();
23+
public static final String NAME = "cluster:admin/xpack/enrich/put";
24+
25+
protected PutEnrichPolicyAction() {
26+
super(NAME);
27+
}
28+
29+
public static Request fromXContent(XContentParser parser, String name) throws IOException {
30+
return new Request(name, EnrichPolicy.fromXContent(parser));
31+
}
32+
33+
@Override
34+
public AcknowledgedResponse newResponse() {
35+
return new AcknowledgedResponse();
36+
}
37+
38+
public static class Request extends MasterNodeRequest<PutEnrichPolicyAction.Request> {
39+
40+
private final EnrichPolicy policy;
41+
private final String name;
42+
43+
public Request(String name, EnrichPolicy policy) {
44+
this.name = Objects.requireNonNull(name, "name cannot be null");
45+
this.policy = policy;
46+
}
47+
48+
public Request(StreamInput in) throws IOException {
49+
super(in);
50+
name = in.readString();
51+
policy = new EnrichPolicy(in);
52+
}
53+
54+
@Override
55+
public void writeTo(StreamOutput out) throws IOException {
56+
super.writeTo(out);
57+
out.writeString(name);
58+
policy.writeTo(out);
59+
}
60+
61+
public String getName() {
62+
return name;
63+
}
64+
65+
public EnrichPolicy getPolicy() {
66+
return policy;
67+
}
68+
69+
@Override
70+
public ActionRequestValidationException validate() {
71+
return null;
72+
}
73+
74+
@Override
75+
public boolean equals(Object o) {
76+
if (this == o) return true;
77+
if (o == null || getClass() != o.getClass()) return false;
78+
Request request = (Request) o;
79+
return policy.equals(request.policy) &&
80+
name.equals(request.name);
81+
}
82+
83+
@Override
84+
public int hashCode() {
85+
return Objects.hash(policy, name);
86+
}
87+
}
88+
}

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,75 @@
55
*/
66
package org.elasticsearch.xpack.enrich;
77

8+
import org.elasticsearch.action.ActionRequest;
9+
import org.elasticsearch.action.ActionResponse;
10+
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
811
import org.elasticsearch.cluster.metadata.MetaData;
12+
import org.elasticsearch.cluster.node.DiscoveryNodes;
913
import org.elasticsearch.common.ParseField;
1014
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
15+
import org.elasticsearch.common.settings.ClusterSettings;
16+
import org.elasticsearch.common.settings.IndexScopedSettings;
17+
import org.elasticsearch.common.settings.Settings;
18+
import org.elasticsearch.common.settings.SettingsFilter;
1119
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
1220
import org.elasticsearch.ingest.Processor;
21+
import org.elasticsearch.plugins.ActionPlugin;
1322
import org.elasticsearch.plugins.IngestPlugin;
1423
import org.elasticsearch.plugins.Plugin;
24+
import org.elasticsearch.rest.RestController;
25+
import org.elasticsearch.rest.RestHandler;
26+
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;
27+
import org.elasticsearch.xpack.enrich.action.TransportPutEnrichPolicyAction;
28+
import org.elasticsearch.xpack.enrich.rest.RestPutEnrichPolicyAction;
1529

30+
import java.util.Arrays;
1631
import java.util.Collections;
1732
import java.util.List;
1833
import java.util.Map;
34+
import java.util.function.Supplier;
1935

20-
public class EnrichPlugin extends Plugin implements IngestPlugin {
36+
import static java.util.Collections.emptyList;
37+
import static org.elasticsearch.xpack.core.XPackSettings.ENRICH_ENABLED_SETTING;
38+
39+
public class EnrichPlugin extends Plugin implements ActionPlugin, IngestPlugin {
40+
41+
private final Settings settings;
42+
private final Boolean enabled;
43+
44+
public EnrichPlugin(final Settings settings) {
45+
this.settings = settings;
46+
this.enabled = ENRICH_ENABLED_SETTING.get(settings);
47+
}
2148

2249
@Override
2350
public Map<String, Processor.Factory> getProcessors(Processor.Parameters parameters) {
2451
return Collections.emptyMap();
2552
}
2653

54+
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
55+
if (enabled == false) {
56+
return emptyList();
57+
}
58+
59+
return Arrays.asList(
60+
new ActionHandler<>(PutEnrichPolicyAction.INSTANCE, TransportPutEnrichPolicyAction.class)
61+
);
62+
}
63+
64+
public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings,
65+
IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter,
66+
IndexNameExpressionResolver indexNameExpressionResolver,
67+
Supplier<DiscoveryNodes> nodesInCluster) {
68+
if (enabled == false) {
69+
return emptyList();
70+
}
71+
72+
return Arrays.asList(
73+
new RestPutEnrichPolicyAction(settings, restController)
74+
);
75+
}
76+
2777
@Override
2878
public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
2979
return Collections.singletonList(new NamedWriteableRegistry.Entry(MetaData.Custom.class, EnrichMetadata.TYPE,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.enrich.action;
7+
8+
import org.elasticsearch.action.ActionListener;
9+
import org.elasticsearch.action.support.ActionFilters;
10+
import org.elasticsearch.action.support.master.AcknowledgedResponse;
11+
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
12+
import org.elasticsearch.cluster.ClusterState;
13+
import org.elasticsearch.cluster.block.ClusterBlockException;
14+
import org.elasticsearch.cluster.block.ClusterBlockLevel;
15+
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
16+
import org.elasticsearch.cluster.service.ClusterService;
17+
import org.elasticsearch.common.inject.Inject;
18+
import org.elasticsearch.threadpool.ThreadPool;
19+
import org.elasticsearch.transport.TransportService;
20+
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;
21+
import org.elasticsearch.xpack.enrich.EnrichStore;
22+
23+
public class TransportPutEnrichPolicyAction extends TransportMasterNodeAction<PutEnrichPolicyAction.Request, AcknowledgedResponse> {
24+
25+
@Inject
26+
public TransportPutEnrichPolicyAction(TransportService transportService,
27+
ClusterService clusterService,
28+
ThreadPool threadPool,
29+
ActionFilters actionFilters,
30+
IndexNameExpressionResolver indexNameExpressionResolver) {
31+
super(PutEnrichPolicyAction.NAME, transportService, clusterService, threadPool, actionFilters,
32+
PutEnrichPolicyAction.Request::new, indexNameExpressionResolver);
33+
}
34+
35+
@Override
36+
protected String executor() {
37+
return ThreadPool.Names.SAME;
38+
}
39+
40+
@Override
41+
protected AcknowledgedResponse newResponse() {
42+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
43+
}
44+
45+
@Override
46+
protected void masterOperation(PutEnrichPolicyAction.Request request, ClusterState state,
47+
ActionListener<AcknowledgedResponse> listener) {
48+
EnrichStore.putPolicy(request.getName(), request.getPolicy(), clusterService, e -> {
49+
if (e == null) {
50+
listener.onResponse(new AcknowledgedResponse(true));
51+
} else {
52+
listener.onFailure(e);
53+
}
54+
});
55+
}
56+
57+
@Override
58+
protected ClusterBlockException checkBlock(PutEnrichPolicyAction.Request request, ClusterState state) {
59+
return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_WRITE);
60+
}
61+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.enrich.rest;
7+
8+
import org.elasticsearch.client.node.NodeClient;
9+
import org.elasticsearch.common.settings.Settings;
10+
import org.elasticsearch.common.xcontent.XContentParser;
11+
import org.elasticsearch.rest.BaseRestHandler;
12+
import org.elasticsearch.rest.RestController;
13+
import org.elasticsearch.rest.RestRequest;
14+
import org.elasticsearch.rest.action.RestToXContentListener;
15+
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;
16+
17+
import java.io.IOException;
18+
19+
public class RestPutEnrichPolicyAction extends BaseRestHandler {
20+
21+
public RestPutEnrichPolicyAction(final Settings settings, final RestController controller) {
22+
super(settings);
23+
controller.registerHandler(RestRequest.Method.PUT, "/_enrich/policy/{name}", this);
24+
}
25+
26+
@Override
27+
public String getName() {
28+
return "put_enrich_policy";
29+
}
30+
31+
@Override
32+
protected RestChannelConsumer prepareRequest(final RestRequest restRequest, final NodeClient client) throws IOException {
33+
final PutEnrichPolicyAction.Request request = createRequest(restRequest);
34+
return channel -> client.execute(PutEnrichPolicyAction.INSTANCE, request, new RestToXContentListener<>(channel));
35+
}
36+
37+
static PutEnrichPolicyAction.Request createRequest(RestRequest restRequest) throws IOException {
38+
try (XContentParser parser = restRequest.contentOrSourceParamParser()) {
39+
return PutEnrichPolicyAction.fromXContent(parser, restRequest.param("name"));
40+
}
41+
}
42+
}

x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected EnrichPolicy createXContextTestInstance(XContentType xContentType) {
4343
return randomEnrichPolicy(xContentType);
4444
}
4545

46-
static EnrichPolicy randomEnrichPolicy(XContentType xContentType) {
46+
public static EnrichPolicy randomEnrichPolicy(XContentType xContentType) {
4747
final QueryBuilder queryBuilder;
4848
if (randomBoolean()) {
4949
queryBuilder = new MatchAllQueryBuilder();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.enrich.action;
7+
8+
import org.elasticsearch.common.io.stream.Writeable;
9+
import org.elasticsearch.common.xcontent.XContentType;
10+
import org.elasticsearch.test.AbstractWireSerializingTestCase;
11+
import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
12+
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;
13+
14+
import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.randomEnrichPolicy;
15+
16+
public class PutEnrichPolicyActionRequestTests extends AbstractWireSerializingTestCase<PutEnrichPolicyAction.Request> {
17+
18+
@Override
19+
protected PutEnrichPolicyAction.Request createTestInstance() {
20+
final EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON);
21+
return new PutEnrichPolicyAction.Request(randomAlphaOfLength(3), policy);
22+
}
23+
24+
@Override
25+
protected Writeable.Reader<PutEnrichPolicyAction.Request> instanceReader() {
26+
return PutEnrichPolicyAction.Request::new;
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"enrich.put_policy": {
3+
"documentation": "https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-put-policy.html",
4+
"methods": [ "PUT" ],
5+
"url": {
6+
"path": "/_enrich/policy/{name}",
7+
"paths": ["/_enrich/policy/{name}"],
8+
"parts": {
9+
"name": {
10+
"type" : "string",
11+
"description" : "The name of the enrich policy"
12+
}
13+
},
14+
"params": {
15+
}
16+
},
17+
"body": {
18+
"description": "The enrich policy to register"
19+
}
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"Test enrich crud apis":
3+
4+
- do:
5+
enrich.put_policy:
6+
name: policy-crud
7+
body:
8+
type: exact_match
9+
index_pattern: "bar*"
10+
enrich_key: baz
11+
enrich_values: ["a", "b"]
12+
schedule: "*/120"
13+
14+
- is_true: acknowledged

0 commit comments

Comments
 (0)