-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add enrich policy list API #41553
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
Add enrich policy list API #41553
Changes from all commits
096f61e
4508318
ba0e5be
7852583
b1b5a81
eaf5661
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* 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.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 Action<ListEnrichPolicyAction.Response> { | ||
|
||
public static final ListEnrichPolicyAction INSTANCE = new ListEnrichPolicyAction(); | ||
public static final String NAME = "cluster:admin/xpack/enrich/list"; | ||
|
||
private ListEnrichPolicyAction() { | ||
super(NAME); | ||
} | ||
|
||
@Override | ||
public Response newResponse() { | ||
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable"); | ||
} | ||
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); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.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.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 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); | ||
} | ||
|
||
|
||
} |
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.ListEnrichPolicyAction; | ||
|
||
import java.io.IOException; | ||
|
||
public class RestListEnrichPolicyAction extends BaseRestHandler { | ||
|
||
public RestListEnrichPolicyAction(final Settings settings, final RestController controller) { | ||
super(settings); | ||
controller.registerHandler(RestRequest.Method.GET, "/_enrich/policy", this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. plural? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if w go with this then i suggest we change all of the URIs, which i like, but I assumed we did not want to go against for now. This is how I view it. You are enacting on a single or multiple policies at any time. So, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok :) Lets leave this for now and decide whether we want this in a later stage of development. Btw I just check get mappings api (returns a hash and not a list) and there both singular and plural is supported. But it looks like most apis don't mix plural with singular nouns when it comes returning a single item or multiple (get pipeline, get repository, get tasks etc). Anyhow, language wise (i'm not a native speaker) plural made sense to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO plural makes sesne for all cases. It reads easier to me, a list of policies |
||
} | ||
|
||
@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)); | ||
} | ||
} |
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.
maybe also add a unit test for this? (now that it is public)