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
This commit is contained in:
Martijn van Groningen 2019-09-30 14:36:53 +02:00
parent bb3e9cb908
commit fe937ea4b8
No known key found for this signature in database
GPG Key ID: AB236F4FCF2AF12A
7 changed files with 100 additions and 71 deletions

View File

@ -34,9 +34,18 @@ public final class GetPolicyResponse {
args -> new GetPolicyResponse((List<NamedPolicy>) args[0]) args -> new GetPolicyResponse((List<NamedPolicy>) args[0])
); );
@SuppressWarnings("unchecked")
private static final ConstructingObjectParser<NamedPolicy, Void> CONFIG_PARSER = new ConstructingObjectParser<>(
"config",
true,
args -> (NamedPolicy) args[0]
);
static { static {
PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), 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<NamedPolicy> policies; private final List<NamedPolicy> policies;

View File

@ -91,6 +91,7 @@ The API returns the following response:
{ {
"policies": [ "policies": [
{ {
"config": {
"match": { "match": {
"name" : "my-policy", "name" : "my-policy",
"indices" : ["users"], "indices" : ["users"],
@ -104,6 +105,7 @@ The API returns the following response:
] ]
} }
} }
}
] ]
} }
-------------------------------------------------- --------------------------------------------------
@ -125,6 +127,7 @@ The API returns the following response:
{ {
"policies": [ "policies": [
{ {
"config": {
"match": { "match": {
"name" : "my-policy", "name" : "my-policy",
"indices" : ["users"], "indices" : ["users"],
@ -137,8 +140,10 @@ The API returns the following response:
"state" "state"
] ]
} }
}
}, },
{ {
"config": {
"match": { "match": {
"name" : "other-policy", "name" : "other-policy",
"indices" : ["users"], "indices" : ["users"],
@ -152,6 +157,7 @@ The API returns the following response:
] ]
} }
} }
}
] ]
} }
-------------------------------------------------- --------------------------------------------------
@ -174,6 +180,7 @@ The API returns the following response:
{ {
"policies": [ "policies": [
{ {
"config": {
"match": { "match": {
"name" : "my-policy", "name" : "my-policy",
"indices" : ["users"], "indices" : ["users"],
@ -186,8 +193,10 @@ The API returns the following response:
"state" "state"
] ]
} }
}
}, },
{ {
"config": {
"match": { "match": {
"name" : "other-policy", "name" : "other-policy",
"indices" : ["users"], "indices" : ["users"],
@ -201,6 +210,7 @@ The API returns the following response:
] ]
} }
} }
}
] ]
} }
-------------------------------------------------- --------------------------------------------------

View File

@ -15,7 +15,6 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser.ValueType; import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper; 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"); static final ParseField NAME = new ParseField("name");
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -324,14 +323,12 @@ public final class EnrichPolicy implements Writeable, ToXContentFragment {
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.startObject(policy.type); builder.startObject(policy.type);
{ {
builder.field(NAME.getPreferredName(), name); builder.field(NAME.getPreferredName(), name);
policy.toInnerXContent(builder, params); policy.toInnerXContent(builder, params);
} }
builder.endObject(); builder.endObject();
builder.endObject();
return builder; return builder;
} }

View File

@ -105,9 +105,17 @@ public class GetEnrichPolicyAction extends ActionType<GetEnrichPolicyAction.Resp
{ {
builder.startArray("policies"); builder.startArray("policies");
{ {
for (EnrichPolicy.NamedPolicy policy: policies) { for (EnrichPolicy.NamedPolicy policy : policies) {
builder.startObject();
{
builder.startObject("config");
{
policy.toXContent(builder, params); policy.toXContent(builder, params);
} }
builder.endObject();
}
builder.endObject();
}
} }
builder.endArray(); builder.endArray();
} }

View File

@ -36,7 +36,8 @@ public abstract class CommonEnrichRestTestCase extends ESRestTestCase {
List<Map<?,?>> policies = (List<Map<?,?>>) responseMap.get("policies"); List<Map<?,?>> policies = (List<Map<?,?>>) responseMap.get("policies");
for (Map<?, ?> entry: 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)));
} }
} }

View File

@ -20,18 +20,18 @@
enrich.get_policy: enrich.get_policy:
name: policy-crud name: policy-crud
- length: { policies: 1 } - length: { policies: 1 }
- match: { policies.0.match.name: policy-crud } - match: { policies.0.config.match.name: policy-crud }
- match: { policies.0.match.indices: ["bar*"] } - match: { policies.0.config.match.indices: ["bar*"] }
- match: { policies.0.match.match_field: baz } - match: { policies.0.config.match.match_field: baz }
- match: { policies.0.match.enrich_fields: ["a", "b"] } - match: { policies.0.config.match.enrich_fields: ["a", "b"] }
- do: - do:
enrich.get_policy: {} enrich.get_policy: {}
- length: { policies: 1 } - length: { policies: 1 }
- match: { policies.0.match.name: policy-crud } - match: { policies.0.config.match.name: policy-crud }
- match: { policies.0.match.indices: ["bar*"] } - match: { policies.0.config.match.indices: ["bar*"] }
- match: { policies.0.match.match_field: baz } - match: { policies.0.config.match.match_field: baz }
- match: { policies.0.match.enrich_fields: ["a", "b"] } - match: { policies.0.config.match.enrich_fields: ["a", "b"] }
- do: - do:
enrich.stats: {} enrich.stats: {}

View File

@ -33,8 +33,12 @@ public class GetEnrichPolicyActionResponseTests extends AbstractSerializingTestC
XContentParser.Token token; XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
assert token == XContentParser.Token.START_OBJECT; 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); EnrichPolicy.NamedPolicy policy = EnrichPolicy.NamedPolicy.fromXContent(parser);
policies.put(policy.getName(), policy.getPolicy()); policies.put(policy.getName(), policy.getPolicy());
assert parser.nextToken() == XContentParser.Token.END_OBJECT;
} }
return new GetEnrichPolicyAction.Response(policies); return new GetEnrichPolicyAction.Response(policies);