From fe937ea4b8c702c105ab6a1b6afa598dac62f1da Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 30 Sep 2019 14:36:53 +0200 Subject: [PATCH] Add config namespace in get policy api response (#47162) Currently the policy config is placed directly in the json object of the toplevel `policies` array field. For example: ``` { "policies": [ { "match": { "name" : "my-policy", "indices" : ["users"], "match_field" : "email", "enrich_fields" : [ "first_name", "last_name", "city", "zip", "state" ] } } ] } ``` This change adds a `config` field in each policy json object: ``` { "policies": [ { "config": { "match": { "name" : "my-policy", "indices" : ["users"], "match_field" : "email", "enrich_fields" : [ "first_name", "last_name", "city", "zip", "state" ] } } } ] } ``` This allows us in the future to add other information about policies in the get policy api response. The UI will consume this API to build an overview of all policies. The UI may in the future include additional information about a policy and the plan is to include that in the get policy api, so that this information can be gathered in a single api call. An example of the information that is likely to be added is: * Last policy execution time * The status of a policy (executing, executed, unexecuted) * Information about the last failure if exists --- .../client/enrich/GetPolicyResponse.java | 11 +- .../apis/enrich/get-enrich-policy.asciidoc | 120 ++++++++++-------- .../xpack/core/enrich/EnrichPolicy.java | 5 +- .../enrich/action/GetEnrichPolicyAction.java | 12 +- .../test/enrich/CommonEnrichRestTestCase.java | 3 +- .../rest-api-spec/test/enrich/10_basic.yml | 16 +-- .../GetEnrichPolicyActionResponseTests.java | 4 + 7 files changed, 100 insertions(+), 71 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/GetPolicyResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/GetPolicyResponse.java index d65d05f9e4e..e09d4657828 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/GetPolicyResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/GetPolicyResponse.java @@ -34,9 +34,18 @@ public final class GetPolicyResponse { args -> new GetPolicyResponse((List) args[0]) ); + @SuppressWarnings("unchecked") + private static final ConstructingObjectParser CONFIG_PARSER = new ConstructingObjectParser<>( + "config", + true, + args -> (NamedPolicy) args[0] + ); + static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> NamedPolicy.fromXContent(p), new ParseField("policies")); + CONFIG_PARSER::apply, new ParseField("policies")); + CONFIG_PARSER.declareObject(ConstructingObjectParser.constructorArg(), + (p, c) -> NamedPolicy.fromXContent(p), new ParseField("config")); } private final List policies; diff --git a/docs/reference/ingest/apis/enrich/get-enrich-policy.asciidoc b/docs/reference/ingest/apis/enrich/get-enrich-policy.asciidoc index bf325c5aae4..b3c0fe8f2ff 100644 --- a/docs/reference/ingest/apis/enrich/get-enrich-policy.asciidoc +++ b/docs/reference/ingest/apis/enrich/get-enrich-policy.asciidoc @@ -91,17 +91,19 @@ The API returns the following response: { "policies": [ { - "match": { - "name" : "my-policy", - "indices" : ["users"], - "match_field" : "email", - "enrich_fields" : [ - "first_name", - "last_name", - "city", - "zip", - "state" - ] + "config": { + "match": { + "name" : "my-policy", + "indices" : ["users"], + "match_field" : "email", + "enrich_fields" : [ + "first_name", + "last_name", + "city", + "zip", + "state" + ] + } } } ] @@ -125,31 +127,35 @@ The API returns the following response: { "policies": [ { - "match": { - "name" : "my-policy", - "indices" : ["users"], - "match_field" : "email", - "enrich_fields" : [ - "first_name", - "last_name", - "city", - "zip", - "state" - ] + "config": { + "match": { + "name" : "my-policy", + "indices" : ["users"], + "match_field" : "email", + "enrich_fields" : [ + "first_name", + "last_name", + "city", + "zip", + "state" + ] + } } }, { - "match": { - "name" : "other-policy", - "indices" : ["users"], - "match_field" : "email", - "enrich_fields" : [ - "first_name", - "last_name", - "city", - "zip", - "state" - ] + "config": { + "match": { + "name" : "other-policy", + "indices" : ["users"], + "match_field" : "email", + "enrich_fields" : [ + "first_name", + "last_name", + "city", + "zip", + "state" + ] + } } } ] @@ -174,31 +180,35 @@ The API returns the following response: { "policies": [ { - "match": { - "name" : "my-policy", - "indices" : ["users"], - "match_field" : "email", - "enrich_fields" : [ - "first_name", - "last_name", - "city", - "zip", - "state" - ] + "config": { + "match": { + "name" : "my-policy", + "indices" : ["users"], + "match_field" : "email", + "enrich_fields" : [ + "first_name", + "last_name", + "city", + "zip", + "state" + ] + } } }, { - "match": { - "name" : "other-policy", - "indices" : ["users"], - "match_field" : "email", - "enrich_fields" : [ - "first_name", - "last_name", - "city", - "zip", - "state" - ] + "config": { + "match": { + "name" : "other-policy", + "indices" : ["users"], + "match_field" : "email", + "enrich_fields" : [ + "first_name", + "last_name", + "city", + "zip", + "state" + ] + } } } ] diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/EnrichPolicy.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/EnrichPolicy.java index aec1f0e7b80..34ee034326b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/EnrichPolicy.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/EnrichPolicy.java @@ -15,7 +15,6 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ObjectParser.ValueType; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; @@ -272,7 +271,7 @@ public final class EnrichPolicy implements Writeable, ToXContentFragment { } } - public static class NamedPolicy implements Writeable, ToXContent { + public static class NamedPolicy implements Writeable, ToXContentFragment { static final ParseField NAME = new ParseField("name"); @SuppressWarnings("unchecked") @@ -324,14 +323,12 @@ public final class EnrichPolicy implements Writeable, ToXContentFragment { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); builder.startObject(policy.type); { builder.field(NAME.getPreferredName(), name); policy.toInnerXContent(builder, params); } builder.endObject(); - builder.endObject(); return builder; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/GetEnrichPolicyAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/GetEnrichPolicyAction.java index cad5e260d31..c1543c8578b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/GetEnrichPolicyAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/GetEnrichPolicyAction.java @@ -105,8 +105,16 @@ public class GetEnrichPolicyAction extends ActionType> policies = (List>) responseMap.get("policies"); for (Map entry: policies) { - client().performRequest(new Request("DELETE", "/_enrich/policy/" + XContentMapValues.extractValue("match.name", entry))); + client().performRequest(new Request("DELETE", "/_enrich/policy/" + + XContentMapValues.extractValue("config.match.name", entry))); } } diff --git a/x-pack/plugin/enrich/qa/rest/src/test/resources/rest-api-spec/test/enrich/10_basic.yml b/x-pack/plugin/enrich/qa/rest/src/test/resources/rest-api-spec/test/enrich/10_basic.yml index eb8be92aa3e..c53026b49ce 100644 --- a/x-pack/plugin/enrich/qa/rest/src/test/resources/rest-api-spec/test/enrich/10_basic.yml +++ b/x-pack/plugin/enrich/qa/rest/src/test/resources/rest-api-spec/test/enrich/10_basic.yml @@ -20,18 +20,18 @@ enrich.get_policy: name: policy-crud - length: { policies: 1 } - - match: { policies.0.match.name: policy-crud } - - match: { policies.0.match.indices: ["bar*"] } - - match: { policies.0.match.match_field: baz } - - match: { policies.0.match.enrich_fields: ["a", "b"] } + - match: { policies.0.config.match.name: policy-crud } + - match: { policies.0.config.match.indices: ["bar*"] } + - match: { policies.0.config.match.match_field: baz } + - match: { policies.0.config.match.enrich_fields: ["a", "b"] } - do: enrich.get_policy: {} - length: { policies: 1 } - - match: { policies.0.match.name: policy-crud } - - match: { policies.0.match.indices: ["bar*"] } - - match: { policies.0.match.match_field: baz } - - match: { policies.0.match.enrich_fields: ["a", "b"] } + - match: { policies.0.config.match.name: policy-crud } + - match: { policies.0.config.match.indices: ["bar*"] } + - match: { policies.0.config.match.match_field: baz } + - match: { policies.0.config.match.enrich_fields: ["a", "b"] } - do: enrich.stats: {} diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/GetEnrichPolicyActionResponseTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/GetEnrichPolicyActionResponseTests.java index 4403649739a..e9181abec66 100644 --- a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/GetEnrichPolicyActionResponseTests.java +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/GetEnrichPolicyActionResponseTests.java @@ -33,8 +33,12 @@ public class GetEnrichPolicyActionResponseTests extends AbstractSerializingTestC XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { assert token == XContentParser.Token.START_OBJECT; + assert parser.nextToken() == XContentParser.Token.FIELD_NAME; + assert parser.currentName().equals("config"); + assert parser.nextToken() == XContentParser.Token.START_OBJECT; EnrichPolicy.NamedPolicy policy = EnrichPolicy.NamedPolicy.fromXContent(parser); policies.put(policy.getName(), policy.getPolicy()); + assert parser.nextToken() == XContentParser.Token.END_OBJECT; } return new GetEnrichPolicyAction.Response(policies);