-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add enrich policy GET API #41384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add enrich policy GET API #41384
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
dddcfd2
Add enrich policy GET API
hub-cap 498490e
Merge branch 'enrich' into enrich_get_request
hub-cap 39e9bfc
Updates for review
hub-cap d74ea50
Merge remote-tracking branch 'upstream/enrich' into enrich_get_request
hub-cap 5edff9c
Fix the NPE
hub-cap b376205
removed accidental added comment
hub-cap d03ea96
Merge branch 'enrich' into enrich_get_request
hub-cap ba67648
Fix final
hub-cap ad5e3c5
Merge remote-tracking branch 'upstream/enrich' into enrich_get_request
hub-cap 805d857
Adding newlines
hub-cap f884f99
Merge branch 'enrich' of github.com:elastic/elasticsearch into enrich…
hub-cap fd81e1c
Merge branch 'enrich' of github.com:elastic/elasticsearch into enrich…
hub-cap 43b24c2
Merge remote-tracking branch 'upstream/enrich' into enrich_get_request
hub-cap e263634
Fix test rename variable
hub-cap 0b05736
Merge remote-tracking branch 'upstream/enrich' into enrich_get_request
hub-cap e97ffc5
Merge remote-tracking branch 'upstream/enrich' into enrich_get_request
hub-cap 6389307
Merge branch 'enrich' into enrich_get_request
hub-cap 03b0701
Merge branch 'enrich' of github.com:elastic/elasticsearch into enrich…
hub-cap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
.../core/src/main/java/org/elasticsearch/xpack/core/enrich/action/GetEnrichPolicyAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* 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 = policy; | ||
} | ||
|
||
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; | ||
} | ||
|
||
@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); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...h/src/main/java/org/elasticsearch/xpack/enrich/action/TransportGetEnrichPolicyAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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.Strings; | ||
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.action.GetEnrichPolicyAction; | ||
import org.elasticsearch.xpack.core.enrich.EnrichPolicy; | ||
import org.elasticsearch.xpack.enrich.EnrichStore; | ||
|
||
import java.io.IOException; | ||
|
||
public class TransportGetEnrichPolicyAction extends TransportMasterNodeReadAction<GetEnrichPolicyAction.Request, | ||
GetEnrichPolicyAction.Response> { | ||
|
||
private final EnrichStore enrichStore; | ||
|
||
@Inject | ||
public TransportGetEnrichPolicyAction(TransportService transportService, | ||
ClusterService clusterService, | ||
ThreadPool threadPool, | ||
ActionFilters actionFilters, | ||
IndexNameExpressionResolver indexNameExpressionResolver, | ||
EnrichStore enrichStore) { | ||
super(GetEnrichPolicyAction.NAME, transportService, clusterService, threadPool, actionFilters, | ||
GetEnrichPolicyAction.Request::new, indexNameExpressionResolver); | ||
this.enrichStore = enrichStore; | ||
} | ||
|
||
@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 = getPolicy(request.getName(), enrichStore); | ||
listener.onResponse(new GetEnrichPolicyAction.Response(policy)); | ||
} | ||
|
||
static EnrichPolicy getPolicy(String name, EnrichStore store) { | ||
if (Strings.isNullOrEmpty(name)) { | ||
throw new ResourceNotFoundException("name is missing"); | ||
} | ||
var policy = store.getPolicy(name); | ||
if (policy == null) { | ||
throw new ResourceNotFoundException("policy [{}] is missing", name); | ||
} | ||
return policy; | ||
} | ||
|
||
@Override | ||
protected ClusterBlockException checkBlock(GetEnrichPolicyAction.Request request, ClusterState state) { | ||
return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...n/enrich/src/main/java/org/elasticsearch/xpack/enrich/rest/RestGetEnrichPolicyAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...rc/test/java/org/elasticsearch/xpack/enrich/action/GetEnrichPolicyActionRequestTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* 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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the two if statements in this method should be merged with
EnrichStore#getPolicy(...)
and also tests should move theEnrichStoreTests
. Since the static method will basically by doing nothing, it can be merged withmasterOperation(...)