Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
Expand All @@ -41,9 +41,9 @@ public class GetPolicyResponseTests extends AbstractResponseTestCase<GetEnrichPo
@Override
protected GetEnrichPolicyAction.Response createServerTestInstance(XContentType xContentType) {
int numPolicies = randomIntBetween(0, 8);
Map<String, EnrichPolicy> policies = new HashMap<>(numPolicies);
List<EnrichPolicy.NamedPolicy> policies = new ArrayList<>();
for (int i = 0; i < numPolicies; i++) {
policies.put(randomAlphaOfLength(4), createRandomEnrichPolicy(xContentType));
policies.add(new EnrichPolicy.NamedPolicy(randomAlphaOfLength(4), createRandomEnrichPolicy(xContentType)));
}
return new GetEnrichPolicyAction.Response(policies);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public final class ClientHelper {
public static final String PERSISTENT_TASK_ORIGIN = "persistent_tasks";
public static final String ROLLUP_ORIGIN = "rollup";
public static final String TRANSFORM_ORIGIN = "transform";
public static final String ENRICH_ORIGIN = "enrich";

private ClientHelper() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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.ToXContentFragment;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
Expand All @@ -30,7 +30,7 @@
/**
* Represents an enrich policy including its configuration.
*/
public final class EnrichPolicy implements Writeable, ToXContentFragment {
public final class EnrichPolicy implements Writeable, ToXContentObject {

public static final String ENRICH_INDEX_NAME_BASE = ".enrich-";

Expand Down Expand Up @@ -175,12 +175,15 @@ public void writeTo(StreamOutput out) throws IOException {

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(type);
builder.startObject();
{
toInnerXContent(builder, params);
builder.startObject(type);
{
toInnerXContent(builder, params);
}
builder.endObject();
}
builder.endObject();
return builder;
return builder.endObject();
}

private void toInnerXContent(XContentBuilder builder, Params params) throws IOException {
Expand Down Expand Up @@ -271,7 +274,7 @@ public int hashCode() {
}
}

public static class NamedPolicy implements Writeable, ToXContentFragment {
public static class NamedPolicy implements Writeable, ToXContentObject {

static final ParseField NAME = new ParseField("name");
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -323,13 +326,20 @@ public void writeTo(StreamOutput out) throws IOException {

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
{
innerToXContent(builder, params);
}
return builder.endObject();
}

public void innerToXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(policy.type);
{
builder.field(NAME.getPreferredName(), name);
policy.toInnerXContent(builder, params);
}
builder.endObject();
return builder;
}

public static NamedPolicy fromXContent(XContentParser parser) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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;

import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.xpack.core.template.IndexTemplateConfig;
import org.elasticsearch.xpack.core.template.IndexTemplateRegistry;
import org.elasticsearch.xpack.core.template.LifecyclePolicyConfig;

import java.util.Collections;
import java.util.List;

import static org.elasticsearch.xpack.core.ClientHelper.ENRICH_ORIGIN;
import static org.elasticsearch.xpack.core.XPackSettings.ENRICH_ENABLED_SETTING;

public class EnrichTemplateRegistry extends IndexTemplateRegistry {
// history (please add a comment why you increased the version here)
// version 1: initial
public static final String INDEX_TEMPLATE_VERSION = "1";

public static final String ENRICH_TEMPLATE_VERSION_VARIABLE = "xpack.enrich.template.version";
public static final String ENRICH_TEMPLATE_NAME = ".enrich";

public static final IndexTemplateConfig TEMPLATE_ENRICH = new IndexTemplateConfig(
ENRICH_TEMPLATE_NAME,
"/enrich.json",
INDEX_TEMPLATE_VERSION,
ENRICH_TEMPLATE_VERSION_VARIABLE
);

private final Boolean enrichEnabled;

public EnrichTemplateRegistry(Settings nodeSettings, ClusterService clusterService, ThreadPool threadPool,
Client client, NamedXContentRegistry xContentRegistry) {
super(nodeSettings, clusterService, threadPool, client, xContentRegistry);
enrichEnabled = ENRICH_ENABLED_SETTING.get(nodeSettings);
}

@Override
protected List<IndexTemplateConfig> getTemplateConfigs() {
if (enrichEnabled == false) {
return Collections.emptyList();
}
return Collections.singletonList(TEMPLATE_ENRICH);
}

@Override
protected List<LifecyclePolicyConfig> getPolicyConfigs() {
return Collections.emptyList();
}

@Override
protected String getOrigin() {
return ENRICH_ORIGIN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/
package org.elasticsearch.xpack.core.enrich.action;

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.support.master.MasterNodeRequest;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;

Expand All @@ -24,7 +24,7 @@ private DeleteEnrichPolicyAction() {
super(NAME, AcknowledgedResponse::new);
}

public static class Request extends MasterNodeRequest<DeleteEnrichPolicyAction.Request> {
public static class Request extends ActionRequest {

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/
package org.elasticsearch.xpack.core.enrich.action;

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.ActionType;
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.xcontent.ToXContentObject;
Expand All @@ -18,11 +18,10 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class GetEnrichPolicyAction extends ActionType<GetEnrichPolicyAction.Response> {

Expand All @@ -33,7 +32,7 @@ private GetEnrichPolicyAction() {
super(NAME, Response::new);
}

public static class Request extends MasterNodeReadRequest<Request> {
public static class Request extends ActionRequest {

private final List<String> names;

Expand Down Expand Up @@ -83,11 +82,9 @@ public static class Response extends ActionResponse implements ToXContentObject

private final List<EnrichPolicy.NamedPolicy> policies;

public Response(Map<String, EnrichPolicy> policies) {
public Response(Collection<EnrichPolicy.NamedPolicy> 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());
this.policies = new LinkedList<>(policies);
}

public Response(StreamInput in) throws IOException {
Expand All @@ -110,7 +107,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
{
builder.startObject("config");
{
policy.toXContent(builder, params);
policy.innerToXContent(builder, params);
}
builder.endObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
package org.elasticsearch.xpack.core.enrich.action;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
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.XContentParser;
Expand All @@ -31,7 +31,7 @@ public static Request fromXContent(XContentParser parser, String name) throws IO
return new Request(name, EnrichPolicy.fromXContent(parser));
}

public static class Request extends MasterNodeRequest<PutEnrichPolicyAction.Request> {
public static class Request extends ActionRequest {

private final EnrichPolicy policy;
private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private static Map<String, RoleDescriptor> initializeReservedRoles() {
.build() }, null, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, null))
.put("enrich_user", new RoleDescriptor("enrich_user", new String[]{ "manage_enrich", "manage_ingest_pipelines", "monitor" },
new RoleDescriptor.IndicesPrivileges[]{ RoleDescriptor.IndicesPrivileges.builder()
.indices(".enrich-*")
.indices(".enrich-*", ".enrich")
.privileges("manage", "read", "write")
.build() }, null, MetadataUtils.DEFAULT_RESERVED_METADATA))
.immutableMap();
Expand Down
13 changes: 13 additions & 0 deletions x-pack/plugin/core/src/main/resources/enrich.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"index_patterns": [ ".enrich" ],
"settings": {
"index.number_of_shards": 1,
"index.number_of_replicas": 1
},
"mappings": {
"_doc": {
"dynamic" : "false",
"properties": { }
}
}
}
Loading