diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/DeleteEnrichPolicyAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/DeleteEnrichPolicyAction.java new file mode 100644 index 00000000000..f852f217cda --- /dev/null +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/DeleteEnrichPolicyAction.java @@ -0,0 +1,73 @@ +/* + * 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.action; + +import org.elasticsearch.action.Action; +import org.elasticsearch.action.ActionRequestValidationException; +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 java.io.IOException; +import java.util.Objects; + +public class DeleteEnrichPolicyAction extends Action { + + public static final DeleteEnrichPolicyAction INSTANCE = new DeleteEnrichPolicyAction(); + public static final String NAME = "cluster:admin/xpack/enrich/delete"; + + protected DeleteEnrichPolicyAction() { + super(NAME); + } + + @Override + public AcknowledgedResponse newResponse() { + return new AcknowledgedResponse(); + } + + public static class Request extends MasterNodeRequest { + + private final String name; + + public Request(String name) { + this.name = Objects.requireNonNull(name, "name cannot be null"); + } + + public Request(StreamInput in) throws IOException { + super(in); + this.name = in.readString(); + } + + public String getName() { + return name; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeString(name); + } + + @Override + public ActionRequestValidationException validate() { + return null; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Request request = (Request) o; + return name.equals(request.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } + } +} diff --git a/x-pack/plugin/enrich/qa/rest/src/test/java/org/elasticsearch/xpack/enrich/EnrichIT.java b/x-pack/plugin/enrich/qa/rest/src/test/java/org/elasticsearch/xpack/enrich/EnrichIT.java index cce65a53a3d..9faf345618d 100644 --- a/x-pack/plugin/enrich/qa/rest/src/test/java/org/elasticsearch/xpack/enrich/EnrichIT.java +++ b/x-pack/plugin/enrich/qa/rest/src/test/java/org/elasticsearch/xpack/enrich/EnrichIT.java @@ -15,15 +15,28 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.rest.ESRestTestCase; +import org.junit.After; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.util.List; import java.util.Map; import static org.hamcrest.Matchers.equalTo; public class EnrichIT extends ESRestTestCase { + @After + private void deletePolicies() throws Exception { + Map responseMap = toMap(client().performRequest(new Request("GET", "/_enrich/policy"))); + @SuppressWarnings("unchecked") + List> policies = (List>) responseMap.get("policies"); + + for (Map entry: policies) { + client().performRequest(new Request("DELETE", "/_enrich/policy/" + entry.get("name"))); + } + } + // TODO: update this test when policy runner is ready public void testBasicFlow() throws Exception { // Create the policy: @@ -72,6 +85,10 @@ public class EnrichIT extends ESRestTestCase { assertThat(_source.get("host"), equalTo("elastic.co")); assertThat(_source.get("global_rank"), equalTo(25)); assertThat(_source.get("tld_rank"), equalTo(7)); + + // Delete policy: + Request deletePolicyRequest = new Request("DELETE", "/_enrich/policy/my_policy"); + assertOK(client().performRequest(deletePolicyRequest)); } private static Map toMap(Response response) throws IOException { 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 40934c35785..a51a930b6f3 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 @@ -21,3 +21,8 @@ - match: { policies.0.enrich_key: baz } - match: { policies.0.enrich_values: ["a", "b"] } - match: { policies.0.schedule: "*/120" } + + - do: + enrich.delete_policy: + name: policy-crud + - is_true: acknowledged 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 4b9e9151b57..c7083e7bbb6 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 @@ -10,8 +10,8 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.MetaData; -import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.cluster.node.DiscoveryNodes; +import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.ClusterSettings; @@ -25,10 +25,13 @@ import org.elasticsearch.plugins.IngestPlugin; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestHandler; +import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.action.ListEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction; +import org.elasticsearch.xpack.enrich.action.TransportDeleteEnrichPolicyAction; import org.elasticsearch.xpack.enrich.action.TransportListEnrichPolicyAction; import org.elasticsearch.xpack.enrich.action.TransportPutEnrichPolicyAction; +import org.elasticsearch.xpack.enrich.rest.RestDeleteEnrichPolicyAction; import org.elasticsearch.xpack.enrich.rest.RestListEnrichPolicyAction; import org.elasticsearch.xpack.enrich.rest.RestPutEnrichPolicyAction; @@ -71,6 +74,7 @@ public class EnrichPlugin extends Plugin implements ActionPlugin, IngestPlugin { } return Arrays.asList( + new ActionHandler<>(DeleteEnrichPolicyAction.INSTANCE, TransportDeleteEnrichPolicyAction.class), new ActionHandler<>(ListEnrichPolicyAction.INSTANCE, TransportListEnrichPolicyAction.class), new ActionHandler<>(PutEnrichPolicyAction.INSTANCE, TransportPutEnrichPolicyAction.class) ); @@ -85,6 +89,7 @@ public class EnrichPlugin extends Plugin implements ActionPlugin, IngestPlugin { } return Arrays.asList( + new RestDeleteEnrichPolicyAction(settings, restController), new RestListEnrichPolicyAction(settings, restController), new RestPutEnrichPolicyAction(settings, restController) ); diff --git a/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportDeleteEnrichPolicyAction.java b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportDeleteEnrichPolicyAction.java new file mode 100644 index 00000000000..dd7ebc6a497 --- /dev/null +++ b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportDeleteEnrichPolicyAction.java @@ -0,0 +1,61 @@ +/* + * 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.action; + +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.support.ActionFilters; +import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.action.support.master.TransportMasterNodeAction; +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.block.ClusterBlockException; +import org.elasticsearch.cluster.block.ClusterBlockLevel; +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; +import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction; +import org.elasticsearch.xpack.enrich.EnrichStore; + +public class TransportDeleteEnrichPolicyAction extends TransportMasterNodeAction { + + @Inject + public TransportDeleteEnrichPolicyAction(TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver) { + super(DeleteEnrichPolicyAction.NAME, transportService, clusterService, threadPool, actionFilters, + DeleteEnrichPolicyAction.Request::new, indexNameExpressionResolver); + } + + @Override + protected String executor() { + return ThreadPool.Names.SAME; + } + + @Override + protected AcknowledgedResponse newResponse() { + throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable"); + } + + @Override + protected void masterOperation(DeleteEnrichPolicyAction.Request request, ClusterState state, + ActionListener listener) throws Exception { + EnrichStore.deletePolicy(request.getName(), clusterService, e -> { + if (e == null) { + listener.onResponse(new AcknowledgedResponse(true)); + } else { + listener.onFailure(e); + } + }); + } + + @Override + protected ClusterBlockException checkBlock(DeleteEnrichPolicyAction.Request request, ClusterState state) { + return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_WRITE); + } +} diff --git a/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/rest/RestDeleteEnrichPolicyAction.java b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/rest/RestDeleteEnrichPolicyAction.java new file mode 100644 index 00000000000..b779e01d141 --- /dev/null +++ b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/rest/RestDeleteEnrichPolicyAction.java @@ -0,0 +1,35 @@ +/* + * 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.rest; + +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.rest.BaseRestHandler; +import org.elasticsearch.rest.RestController; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction; + +import java.io.IOException; + +public class RestDeleteEnrichPolicyAction extends BaseRestHandler { + + public RestDeleteEnrichPolicyAction(final Settings settings, final RestController controller) { + super(settings); + controller.registerHandler(RestRequest.Method.DELETE, "/_enrich/policy/{name}", this); + } + + @Override + public String getName() { + return "delete_enrich_policy"; + } + + @Override + protected RestChannelConsumer prepareRequest(final RestRequest restRequest, final NodeClient client) throws IOException { + final DeleteEnrichPolicyAction.Request request = new DeleteEnrichPolicyAction.Request(restRequest.param("name")); + return channel -> client.execute(DeleteEnrichPolicyAction.INSTANCE, request, new RestToXContentListener<>(channel)); + } +} diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/DeleteEnrichPolicyActionRequestTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/DeleteEnrichPolicyActionRequestTests.java new file mode 100644 index 00000000000..e5a32919198 --- /dev/null +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/DeleteEnrichPolicyActionRequestTests.java @@ -0,0 +1,22 @@ +/* + * 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.action; + +import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction; + +public class DeleteEnrichPolicyActionRequestTests extends AbstractWireSerializingTestCase { + @Override + protected DeleteEnrichPolicyAction.Request createTestInstance() { + return new DeleteEnrichPolicyAction.Request(randomAlphaOfLength(4)); + } + + @Override + protected Writeable.Reader instanceReader() { + return DeleteEnrichPolicyAction.Request::new; + } +} diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/api/enrich.delete_policy.json b/x-pack/plugin/src/test/resources/rest-api-spec/api/enrich.delete_policy.json new file mode 100644 index 00000000000..9dff98aee02 --- /dev/null +++ b/x-pack/plugin/src/test/resources/rest-api-spec/api/enrich.delete_policy.json @@ -0,0 +1,19 @@ +{ + "enrich.delete_policy": { + "documentation": "https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-delete-policy.html", + "methods": [ "DELETE" ], + "url": { + "path": "/_enrich/policy/{name}", + "paths": ["/_enrich/policy/{name}"], + "parts": { + "name": { + "type" : "string", + "description" : "The name of the enrich policy" + } + }, + "params": { + } + }, + "body": null + } +}