From b02d7ce2dff880ea899ae54ae6dd7684b7aa838c Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Mon, 26 Aug 2019 17:18:08 -0500 Subject: [PATCH 1/2] Add enrich transport client support This commit adds an enrich client, as well as a smoke test to validate the client works. --- .../elasticsearch/xpack/core/XPackClient.java | 7 ++ .../xpack/core/XPackClientPlugin.java | 17 +++-- .../core/enrich/client/EnrichClient.java | 70 +++++++++++++++++++ .../xpack/enrich/EnrichPlugin.java | 10 ++- x-pack/qa/transport-client-tests/build.gradle | 2 +- .../ESXPackSmokeClientTestCase.java | 2 +- .../client/EnrichTransportClientIT.java | 65 +++++++++++++++++ .../xpack/ml/client/MLTransportClientIT.java | 1 + 8 files changed, 166 insertions(+), 8 deletions(-) create mode 100644 x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/client/EnrichClient.java rename x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/{ml/client => }/ESXPackSmokeClientTestCase.java (99%) create mode 100644 x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/enrich/client/EnrichTransportClientIT.java diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClient.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClient.java index 411218781112b..439abc808cbbf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClient.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClient.java @@ -17,6 +17,7 @@ import org.elasticsearch.xpack.core.action.XPackInfoAction; import org.elasticsearch.xpack.core.action.XPackInfoRequestBuilder; import org.elasticsearch.xpack.core.ccr.client.CcrClient; +import org.elasticsearch.xpack.core.enrich.client.EnrichClient; import org.elasticsearch.xpack.core.frozen.action.FreezeIndexAction; import org.elasticsearch.xpack.core.ilm.client.ILMClient; import org.elasticsearch.xpack.core.ml.client.MachineLearningClient; @@ -43,6 +44,7 @@ public class XPackClient { private final WatcherClient watcherClient; private final MachineLearningClient machineLearning; private final ILMClient ilmClient; + private final EnrichClient enrichClient; public XPackClient(Client client) { this.client = Objects.requireNonNull(client, "client"); @@ -53,6 +55,7 @@ public XPackClient(Client client) { this.watcherClient = new WatcherClient(client); this.machineLearning = new MachineLearningClient(client); this.ilmClient = new ILMClient(client); + this.enrichClient = new EnrichClient(client); } public Client es() { @@ -87,6 +90,10 @@ public ILMClient ilmClient() { return ilmClient; } + public EnrichClient enrichClient() { + return enrichClient; + } + public XPackClient withHeaders(Map headers) { return new XPackClient(client.filterWithHeader(headers)); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java index d51c00036da66..cb0d7205ef32f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java @@ -6,8 +6,8 @@ package org.elasticsearch.xpack.core; import org.elasticsearch.Version; -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.ActionType; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.NamedDiff; import org.elasticsearch.cluster.metadata.MetaData; @@ -56,6 +56,10 @@ import org.elasticsearch.xpack.core.dataframe.transforms.SyncConfig; import org.elasticsearch.xpack.core.dataframe.transforms.TimeSyncConfig; import org.elasticsearch.xpack.core.deprecation.DeprecationInfoAction; +import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction; +import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction; +import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction; +import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction; import org.elasticsearch.xpack.core.flattened.FlattenedFeatureSetUsage; import org.elasticsearch.xpack.core.frozen.FrozenIndicesFeatureSetUsage; import org.elasticsearch.xpack.core.frozen.action.FreezeIndexAction; @@ -426,8 +430,13 @@ public List> getClientActions() { DeleteDataFrameTransformAction.INSTANCE, GetDataFrameTransformsAction.INSTANCE, GetDataFrameTransformsStatsAction.INSTANCE, - PreviewDataFrameTransformAction.INSTANCE - ); + PreviewDataFrameTransformAction.INSTANCE, + // enrich + DeleteEnrichPolicyAction.INSTANCE, + ExecuteEnrichPolicyAction.INSTANCE, + GetEnrichPolicyAction.INSTANCE, + PutEnrichPolicyAction.INSTANCE + ); } @Override @@ -586,7 +595,7 @@ public List getNamedXContent() { DataFrameTransformState::fromXContent), new NamedXContentRegistry.Entry(PersistentTaskState.class, new ParseField(DataFrameField.TASK_NAME), DataFrameTransformState::fromXContent) - ); + ); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/client/EnrichClient.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/client/EnrichClient.java new file mode 100644 index 0000000000000..d4cfdd1e5be4d --- /dev/null +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/client/EnrichClient.java @@ -0,0 +1,70 @@ +package org.elasticsearch.xpack.core.enrich.client; + +import org.elasticsearch.action.ActionFuture; +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.support.PlainActionFuture; +import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.client.ElasticsearchClient; +import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction; +import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction; +import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction; +import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction; + +import java.util.Objects; + +public class EnrichClient { + + private final ElasticsearchClient client; + + public EnrichClient(ElasticsearchClient client) { + this.client = Objects.requireNonNull(client, "client"); + } + + public void deleteEnrichPolicy( + final DeleteEnrichPolicyAction.Request request, + final ActionListener listener) { + client.execute(DeleteEnrichPolicyAction.INSTANCE, request, listener); + } + + public ActionFuture deleteEnrichPolicy(final DeleteEnrichPolicyAction.Request request) { + final PlainActionFuture listener = PlainActionFuture.newFuture(); + client.execute(DeleteEnrichPolicyAction.INSTANCE, request, listener); + return listener; + } + + public void executeEnrichPolicy( + final ExecuteEnrichPolicyAction.Request request, + final ActionListener listener) { + client.execute(ExecuteEnrichPolicyAction.INSTANCE, request, listener); + } + + public ActionFuture executeEnrichPolicy(final ExecuteEnrichPolicyAction.Request request) { + final PlainActionFuture listener = PlainActionFuture.newFuture(); + client.execute(ExecuteEnrichPolicyAction.INSTANCE, request, listener); + return listener; + } + + public void getEnrichPolicy( + final GetEnrichPolicyAction.Request request, + final ActionListener listener) { + client.execute(GetEnrichPolicyAction.INSTANCE, request, listener); + } + + public ActionFuture getEnrichPolicy(final GetEnrichPolicyAction.Request request) { + final PlainActionFuture listener = PlainActionFuture.newFuture(); + client.execute(GetEnrichPolicyAction.INSTANCE, request, listener); + return listener; + } + + public void putEnrichPolicy( + final PutEnrichPolicyAction.Request request, + final ActionListener listener) { + client.execute(PutEnrichPolicyAction.INSTANCE, request, listener); + } + + public ActionFuture putEnrichPolicy(final PutEnrichPolicyAction.Request request) { + final PlainActionFuture listener = PlainActionFuture.newFuture(); + client.execute(PutEnrichPolicyAction.INSTANCE, request, listener); + return listener; + } +} diff --git a/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichPlugin.java b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichPlugin.java index d0c9054da9697..f8efece29b69c 100644 --- a/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichPlugin.java +++ b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichPlugin.java @@ -58,6 +58,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Supplier; +import static java.util.Collections.emptyList; import static org.elasticsearch.xpack.core.XPackSettings.ENRICH_ENABLED_SETTING; public class EnrichPlugin extends Plugin implements ActionPlugin, IngestPlugin { @@ -89,10 +90,12 @@ public class EnrichPlugin extends Plugin implements ActionPlugin, IngestPlugin { private final Settings settings; private final Boolean enabled; + private final boolean transportClientMode; public EnrichPlugin(final Settings settings) { this.settings = settings; this.enabled = ENRICH_ENABLED_SETTING.get(settings); + this.transportClientMode = XPackPlugin.transportClientMode(settings); } @Override @@ -106,7 +109,7 @@ public Map getProcessors(Processor.Parameters paramet public List> getActions() { if (enabled == false) { - return Collections.emptyList(); + return emptyList(); } return Arrays.asList( @@ -124,7 +127,7 @@ public List getRestHandlers(Settings settings, RestController restC IndexNameExpressionResolver indexNameExpressionResolver, Supplier nodesInCluster) { if (enabled == false) { - return Collections.emptyList(); + return emptyList(); } return Arrays.asList( @@ -140,6 +143,9 @@ public Collection createComponents(Client client, ClusterService cluster ResourceWatcherService resourceWatcherService, ScriptService scriptService, NamedXContentRegistry xContentRegistry, Environment environment, NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry) { + if (enabled == false || transportClientMode) { + return emptyList(); + } EnrichPolicyLocks enrichPolicyLocks = new EnrichPolicyLocks(); EnrichPolicyExecutor enrichPolicyExecutor = new EnrichPolicyExecutor(settings, clusterService, client, threadPool, new IndexNameExpressionResolver(), enrichPolicyLocks, System::currentTimeMillis); diff --git a/x-pack/qa/transport-client-tests/build.gradle b/x-pack/qa/transport-client-tests/build.gradle index 12f0f4dc024e1..0fa48ac38f4ee 100644 --- a/x-pack/qa/transport-client-tests/build.gradle +++ b/x-pack/qa/transport-client-tests/build.gradle @@ -16,7 +16,7 @@ testingConventions { naming.clear() naming { IT { - baseClass 'org.elasticsearch.xpack.ml.client.ESXPackSmokeClientTestCase' + baseClass 'org.elasticsearch.xpack.ESXPackSmokeClientTestCase' } } } \ No newline at end of file diff --git a/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/ml/client/ESXPackSmokeClientTestCase.java b/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/ESXPackSmokeClientTestCase.java similarity index 99% rename from x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/ml/client/ESXPackSmokeClientTestCase.java rename to x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/ESXPackSmokeClientTestCase.java index 28267614dd36d..e8d886330ae04 100644 --- a/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/ml/client/ESXPackSmokeClientTestCase.java +++ b/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/ESXPackSmokeClientTestCase.java @@ -3,7 +3,7 @@ * 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.ml.client; +package org.elasticsearch.xpack; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; diff --git a/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/enrich/client/EnrichTransportClientIT.java b/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/enrich/client/EnrichTransportClientIT.java new file mode 100644 index 0000000000000..a3d5f222e890a --- /dev/null +++ b/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/enrich/client/EnrichTransportClientIT.java @@ -0,0 +1,65 @@ +/* + * 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.client; + +import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.client.Client; +import org.elasticsearch.xpack.ESXPackSmokeClientTestCase; +import org.elasticsearch.xpack.core.XPackClient; +import org.elasticsearch.xpack.core.enrich.EnrichPolicy; +import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction; +import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction; +import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction; +import org.elasticsearch.xpack.core.enrich.client.EnrichClient; +import org.hamcrest.Matchers; + +import java.io.IOException; +import java.util.Collections; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.Matchers.nullValue; + +public class EnrichTransportClientIT extends ESXPackSmokeClientTestCase { + + private static void assertEqualPolicies(EnrichPolicy expectedInstance, EnrichPolicy newInstance) { + assertThat(newInstance.getType(), Matchers.equalTo(expectedInstance.getType())); + if (newInstance.getQuery() != null) { + // testFromXContent, always shuffles the xcontent and then byte wise the query is different, so we check the parsed version: + assertThat(newInstance.getQuery().getQueryAsMap(), Matchers.equalTo(expectedInstance.getQuery().getQueryAsMap())); + } else { + assertThat(expectedInstance.getQuery(), nullValue()); + } + assertThat(newInstance.getIndices(), Matchers.equalTo(expectedInstance.getIndices())); + assertThat(newInstance.getMatchField(), Matchers.equalTo(expectedInstance.getMatchField())); + assertThat(newInstance.getEnrichFields(), Matchers.equalTo(expectedInstance.getEnrichFields())); + } + + public void testEnrichCrud() throws IOException { + Client client = getClient(); + XPackClient xPackClient = new XPackClient(client); + EnrichClient enrichClient = xPackClient.enrichClient(); + + EnrichPolicy policy = new EnrichPolicy("exact_match", null, Collections.emptyList(), "test", Collections.emptyList()); + String policyName = "my-policy"; + + AcknowledgedResponse acknowledgedResponse = enrichClient.putEnrichPolicy( + new PutEnrichPolicyAction.Request(policyName, + policy)).actionGet(); + + assertTrue(acknowledgedResponse.isAcknowledged()); + + GetEnrichPolicyAction.Response getResponse = enrichClient.getEnrichPolicy( + new GetEnrichPolicyAction.Request(policyName)).actionGet(); + + assertThat(getResponse.getPolicies().size(), equalTo(1)); + assertThat(policyName, equalTo(getResponse.getPolicies().get(0).getName())); + assertEqualPolicies(policy, getResponse.getPolicies().get(0).getPolicy()); + + acknowledgedResponse = enrichClient.deleteEnrichPolicy(new DeleteEnrichPolicyAction.Request(policyName)).actionGet(); + assertTrue(acknowledgedResponse.isAcknowledged()); + } +} diff --git a/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/ml/client/MLTransportClientIT.java b/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/ml/client/MLTransportClientIT.java index 1a4959c0be84a..41a0c27c5d745 100644 --- a/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/ml/client/MLTransportClientIT.java +++ b/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/ml/client/MLTransportClientIT.java @@ -10,6 +10,7 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.xpack.ESXPackSmokeClientTestCase; import org.elasticsearch.xpack.core.XPackClient; import org.elasticsearch.xpack.core.ml.action.CloseJobAction; import org.elasticsearch.xpack.core.ml.action.DeleteJobAction; From b0f46b8eec491c79c975e156be499ed6a2573570 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Tue, 27 Aug 2019 08:25:45 -0500 Subject: [PATCH 2/2] Fixing nits --- .../xpack/core/enrich/client/EnrichClient.java | 5 +++++ .../enrich/client/EnrichTransportClientIT.java | 13 ++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/client/EnrichClient.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/client/EnrichClient.java index d4cfdd1e5be4d..5bfff235a4731 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/client/EnrichClient.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/client/EnrichClient.java @@ -1,3 +1,8 @@ +/* + * 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.client; import org.elasticsearch.action.ActionFuture; diff --git a/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/enrich/client/EnrichTransportClientIT.java b/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/enrich/client/EnrichTransportClientIT.java index a3d5f222e890a..965c0cc1b03f6 100644 --- a/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/enrich/client/EnrichTransportClientIT.java +++ b/x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/enrich/client/EnrichTransportClientIT.java @@ -15,27 +15,26 @@ import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.client.EnrichClient; -import org.hamcrest.Matchers; import java.io.IOException; import java.util.Collections; -import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.nullValue; public class EnrichTransportClientIT extends ESXPackSmokeClientTestCase { private static void assertEqualPolicies(EnrichPolicy expectedInstance, EnrichPolicy newInstance) { - assertThat(newInstance.getType(), Matchers.equalTo(expectedInstance.getType())); + assertThat(newInstance.getType(), equalTo(expectedInstance.getType())); if (newInstance.getQuery() != null) { // testFromXContent, always shuffles the xcontent and then byte wise the query is different, so we check the parsed version: - assertThat(newInstance.getQuery().getQueryAsMap(), Matchers.equalTo(expectedInstance.getQuery().getQueryAsMap())); + assertThat(newInstance.getQuery().getQueryAsMap(), equalTo(expectedInstance.getQuery().getQueryAsMap())); } else { assertThat(expectedInstance.getQuery(), nullValue()); } - assertThat(newInstance.getIndices(), Matchers.equalTo(expectedInstance.getIndices())); - assertThat(newInstance.getMatchField(), Matchers.equalTo(expectedInstance.getMatchField())); - assertThat(newInstance.getEnrichFields(), Matchers.equalTo(expectedInstance.getEnrichFields())); + assertThat(newInstance.getIndices(), equalTo(expectedInstance.getIndices())); + assertThat(newInstance.getMatchField(), equalTo(expectedInstance.getMatchField())); + assertThat(newInstance.getEnrichFields(), equalTo(expectedInstance.getEnrichFields())); } public void testEnrichCrud() throws IOException {