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])
);
@SuppressWarnings("unchecked")
private static final ConstructingObjectParser<NamedPolicy, Void> 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<NamedPolicy> policies;

View File

@ -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"
]
}
}
}
]

View File

@ -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;
}

View File

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

View File

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

View File

@ -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: {}

View File

@ -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);