diff --git a/src/main/java/org/elasticsearch/alerts/actions/IndexAlertAction.java b/src/main/java/org/elasticsearch/alerts/actions/IndexAlertAction.java index 8c2a8b38346..2fb36409d21 100644 --- a/src/main/java/org/elasticsearch/alerts/actions/IndexAlertAction.java +++ b/src/main/java/org/elasticsearch/alerts/actions/IndexAlertAction.java @@ -66,7 +66,7 @@ public class IndexAlertAction implements AlertAction, ToXContent { try { XContentBuilder resultBuilder = XContentFactory.jsonBuilder().prettyPrint(); resultBuilder.startObject(); - //resultBuilder = alertResult.searchResponse.toXContent(resultBuilder, ToXContent.EMPTY_PARAMS); + resultBuilder = alertResult.getSearchResponse().toXContent(resultBuilder, ToXContent.EMPTY_PARAMS); resultBuilder.field("timestamp", alertResult.getFireTime()); resultBuilder.endObject(); indexRequest.source(resultBuilder); diff --git a/src/main/java/org/elasticsearch/alerts/client/AlertsClient.java b/src/main/java/org/elasticsearch/alerts/client/AlertsClient.java index 10a53a4d2e2..82692c6cf09 100644 --- a/src/main/java/org/elasticsearch/alerts/client/AlertsClient.java +++ b/src/main/java/org/elasticsearch/alerts/client/AlertsClient.java @@ -8,13 +8,11 @@ package org.elasticsearch.alerts.client; import org.elasticsearch.action.*; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.TransportAction; -import org.elasticsearch.action.update.TransportUpdateAction; -import org.elasticsearch.alerts.Alert; import org.elasticsearch.alerts.AlertManager; -import org.elasticsearch.alerts.transport.actions.create.*; +import org.elasticsearch.alerts.transport.actions.index.*; import org.elasticsearch.alerts.transport.actions.delete.*; import org.elasticsearch.alerts.transport.actions.get.*; -import org.elasticsearch.alerts.transport.actions.update.*; +import org.elasticsearch.client.Client; import org.elasticsearch.client.support.Headers; import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.common.inject.Inject; @@ -36,22 +34,21 @@ public class AlertsClient implements AlertsClientInterface { Settings settings, Headers headers, ActionFilters filters, - TransportService transportService, ClusterService clusterService, AlertManager alertManager) { + TransportService transportService, ClusterService clusterService, AlertManager alertManager, + Client client) { this.headers = headers; internalActions = new HashMap<>(); this.threadPool = threadPool; - internalActions.put(CreateAlertAction.INSTANCE, new TransportCreateAlertAction(settings, - CreateAlertAction.NAME, transportService, clusterService, threadPool, filters, alertManager)); + internalActions.put(IndexAlertAction.INSTANCE, new TransportIndexAlertAction(settings, + IndexAlertAction.NAME, transportService, clusterService, threadPool, filters, alertManager)); internalActions.put(GetAlertAction.INSTANCE, new TransportGetAlertAction(settings, - GetAlertAction.NAME, transportService, clusterService, threadPool, filters, alertManager)); + GetAlertAction.NAME, threadPool, filters, client)); internalActions.put(DeleteAlertAction.INSTANCE, new TransportDeleteAlertAction(settings, DeleteAlertAction.NAME, transportService, clusterService, threadPool, filters, alertManager)); - internalActions.put(UpdateAlertAction.INSTANCE, new TransportUpdateAlertAction(settings, - UpdateAlertAction.NAME, transportService, clusterService, threadPool, filters, alertManager)); } @@ -99,47 +96,25 @@ public class AlertsClient implements AlertsClientInterface { @Override - public CreateAlertRequestBuilder prepareCreateAlert(Alert alert) { - return new CreateAlertRequestBuilder(this, alert); + public IndexAlertRequestBuilder prepareCreateAlert(String alertName) { + return new IndexAlertRequestBuilder(this, alertName); } @Override - public CreateAlertRequestBuilder prepareCreateAlert() { - return new CreateAlertRequestBuilder(this, null); + public IndexAlertRequestBuilder prepareCreateAlert() { + return new IndexAlertRequestBuilder(this, null); } @Override - public void createAlert(CreateAlertRequest request, ActionListener response) { - execute(CreateAlertAction.INSTANCE, request, response); + public void createAlert(IndexAlertRequest request, ActionListener response) { + execute(IndexAlertAction.INSTANCE, request, response); } @Override - public ActionFuture createAlert(CreateAlertRequest request) { - return execute(CreateAlertAction.INSTANCE, request); + public ActionFuture createAlert(IndexAlertRequest request) { + return execute(IndexAlertAction.INSTANCE, request); } - - @Override - public UpdateAlertRequestBuilder prepareUpdateAlert(Alert alert) { - return new UpdateAlertRequestBuilder(this, alert); - } - - @Override - public UpdateAlertRequestBuilder prepareUpdateAlert() { - return new UpdateAlertRequestBuilder(this); - } - - @Override - public void updateAlert(UpdateAlertRequest request, ActionListener response) { - execute(UpdateAlertAction.INSTANCE, request, response); - } - - @Override - public ActionFuture updateAlert(UpdateAlertRequest request) { - return execute(UpdateAlertAction.INSTANCE, request); - } - - @SuppressWarnings("unchecked") @Override public > ActionFuture execute(Action action, Request request) { diff --git a/src/main/java/org/elasticsearch/alerts/client/AlertsClientInterface.java b/src/main/java/org/elasticsearch/alerts/client/AlertsClientInterface.java index 24090828e0e..8ec8cb69f3a 100644 --- a/src/main/java/org/elasticsearch/alerts/client/AlertsClientInterface.java +++ b/src/main/java/org/elasticsearch/alerts/client/AlertsClientInterface.java @@ -6,23 +6,16 @@ package org.elasticsearch.alerts.client; import org.elasticsearch.action.*; -import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse; -import org.elasticsearch.alerts.Alert; -import org.elasticsearch.alerts.transport.actions.create.CreateAlertRequest; -import org.elasticsearch.alerts.transport.actions.create.CreateAlertRequestBuilder; -import org.elasticsearch.alerts.transport.actions.create.CreateAlertResponse; +import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequest; +import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequestBuilder; +import org.elasticsearch.alerts.transport.actions.index.IndexAlertResponse; import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest; import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequestBuilder; import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse; import org.elasticsearch.alerts.transport.actions.get.GetAlertRequest; import org.elasticsearch.alerts.transport.actions.get.GetAlertRequestBuilder; import org.elasticsearch.alerts.transport.actions.get.GetAlertResponse; -import org.elasticsearch.alerts.transport.actions.update.UpdateAlertRequest; -import org.elasticsearch.alerts.transport.actions.update.UpdateAlertRequestBuilder; -import org.elasticsearch.alerts.transport.actions.update.UpdateAlertResponse; -import org.elasticsearch.client.Client; import org.elasticsearch.client.ElasticsearchClient; -import org.elasticsearch.common.lease.Releasable; /** */ @@ -46,24 +39,13 @@ public interface AlertsClientInterface extends ElasticsearchClient deleteAlert(DeleteAlertRequest request); - CreateAlertRequestBuilder prepareCreateAlert(Alert alert); + IndexAlertRequestBuilder prepareCreateAlert(String alertName); - CreateAlertRequestBuilder prepareCreateAlert(); - - public void createAlert(CreateAlertRequest request, ActionListener response); - - ActionFuture createAlert(CreateAlertRequest request); - - - UpdateAlertRequestBuilder prepareUpdateAlert(Alert alert); - - UpdateAlertRequestBuilder prepareUpdateAlert(); - - public void updateAlert(UpdateAlertRequest request, ActionListener response); - - ActionFuture updateAlert(UpdateAlertRequest request); + IndexAlertRequestBuilder prepareCreateAlert(); + public void createAlert(IndexAlertRequest request, ActionListener response); + ActionFuture createAlert(IndexAlertRequest request); } diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/create/CreateAlertAction.java b/src/main/java/org/elasticsearch/alerts/transport/actions/create/CreateAlertAction.java deleted file mode 100644 index 105a934e294..00000000000 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/create/CreateAlertAction.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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.alerts.transport.actions.create; - -import org.elasticsearch.action.ClientAction; -import org.elasticsearch.alerts.client.AlertsClient; -import org.elasticsearch.alerts.client.AlertsClientAction; -import org.elasticsearch.alerts.client.AlertsClientInterface; -import org.elasticsearch.client.Client; - -/** - */ -public class CreateAlertAction extends AlertsClientAction { - - public static final CreateAlertAction INSTANCE = new CreateAlertAction(); - public static final String NAME = "indices:data/write/alert/create"; - - private CreateAlertAction() { - super(NAME); - } - - - @Override - public CreateAlertRequestBuilder newRequestBuilder(AlertsClientInterface client) { - return new CreateAlertRequestBuilder(client); - } - - @Override - public CreateAlertResponse newResponse() { - return new CreateAlertResponse(); - } -} diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/create/CreateAlertRequest.java b/src/main/java/org/elasticsearch/alerts/transport/actions/create/CreateAlertRequest.java deleted file mode 100644 index 410339049da..00000000000 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/create/CreateAlertRequest.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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.alerts.transport.actions.create; - - -import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.action.ValidateActions; -import org.elasticsearch.action.support.master.MasterNodeOperationRequest; -import org.elasticsearch.alerts.Alert; -import org.elasticsearch.alerts.triggers.AlertTrigger; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.joda.time.DateTime; -import org.elasticsearch.common.lucene.uid.Versions; -import org.elasticsearch.common.unit.TimeValue; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; - -/** - */ -public class CreateAlertRequest extends MasterNodeOperationRequest { - - private Alert alert; - - @Override - public ActionRequestValidationException validate() { - ActionRequestValidationException validationException = null; - if (alert == null) { - validationException = ValidateActions.addValidationError("alert is missing", validationException); - } - return validationException; - } - - - CreateAlertRequest() { - } - - - public CreateAlertRequest(Alert alert) { - this.alert = alert; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - alert = new Alert(); - alert.readFrom(in); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - alert.writeTo(out); - } - - - public Alert alert() { - return alert; - } - - public void Alert(Alert alert) { - this.alert = alert; - } -} diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/create/CreateAlertRequestBuilder.java b/src/main/java/org/elasticsearch/alerts/transport/actions/create/CreateAlertRequestBuilder.java deleted file mode 100644 index 91f3e02d6f7..00000000000 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/create/CreateAlertRequestBuilder.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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.alerts.transport.actions.create; - -import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.ActionRequestBuilder; -import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; -import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder; -import org.elasticsearch.alerts.Alert; -import org.elasticsearch.alerts.client.AlertsClient; -import org.elasticsearch.alerts.client.AlertsClientInterface; -import org.elasticsearch.client.Client; - -/** - */ -public class CreateAlertRequestBuilder - extends MasterNodeOperationRequestBuilder { - - - public CreateAlertRequestBuilder(AlertsClientInterface client) { - super(client, new CreateAlertRequest(null)); - } - - - public CreateAlertRequestBuilder(AlertsClientInterface client, Alert alert) { - super(client, new CreateAlertRequest(alert)); - } - - @Override - protected void doExecute(ActionListener listener) { - client.createAlert(request, listener); - } -} diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/create/TransportCreateAlertAction.java b/src/main/java/org/elasticsearch/alerts/transport/actions/create/TransportCreateAlertAction.java deleted file mode 100644 index 613d2b52a63..00000000000 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/create/TransportCreateAlertAction.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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.alerts.transport.actions.create; - -import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.index.IndexResponse; -import org.elasticsearch.action.support.ActionFilters; -import org.elasticsearch.action.support.HandledTransportAction; -import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction; -import org.elasticsearch.alerts.Alert; -import org.elasticsearch.alerts.AlertManager; -import org.elasticsearch.alerts.AlertsStore; -import org.elasticsearch.alerts.actions.AlertAction; -import org.elasticsearch.alerts.actions.AlertActionManager; -import org.elasticsearch.alerts.triggers.TriggerManager; -import org.elasticsearch.cluster.ClusterService; -import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.cluster.block.ClusterBlockException; -import org.elasticsearch.cluster.block.ClusterBlockLevel; -import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.threadpool.ThreadPool; -import org.elasticsearch.transport.TransportService; -import org.quartz.Trigger; - -import java.util.ArrayList; - -/** - */ -public class TransportCreateAlertAction extends TransportMasterNodeOperationAction { - - private final AlertManager alertManager; - - @Inject - public TransportCreateAlertAction(Settings settings, String actionName, TransportService transportService, - ClusterService clusterService, ThreadPool threadPool, ActionFilters actionFilters, - AlertManager alertManager) { - super(settings, actionName, transportService, clusterService, threadPool, actionFilters); - this.alertManager = alertManager; - } - - @Override - protected String executor() { - return ThreadPool.Names.MANAGEMENT; - } - - @Override - protected CreateAlertRequest newRequest() { - return new CreateAlertRequest(); - } - - @Override - protected CreateAlertResponse newResponse() { - return new CreateAlertResponse(); - } - - @Override - protected void masterOperation(CreateAlertRequest request, ClusterState state, ActionListener listener) throws ElasticsearchException { - try { - IndexResponse indexResponse = alertManager.addAlert(request.alert()); - listener.onResponse(new CreateAlertResponse(indexResponse)); - } catch (Exception e) { - listener.onFailure(e); - } - } - - @Override - protected ClusterBlockException checkBlock(CreateAlertRequest request, ClusterState state) { - if (!alertManager.isStarted()) { - return new ClusterBlockException(null); - } - return state.blocks().indicesBlockedException(ClusterBlockLevel.WRITE, new String[]{AlertsStore.ALERT_INDEX, AlertActionManager.ALERT_HISTORY_INDEX}); - - } - -} diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/get/GetAlertRequest.java b/src/main/java/org/elasticsearch/alerts/transport/actions/get/GetAlertRequest.java index 997453c19a6..5296090b50b 100644 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/get/GetAlertRequest.java +++ b/src/main/java/org/elasticsearch/alerts/transport/actions/get/GetAlertRequest.java @@ -22,7 +22,7 @@ import java.io.IOException; /** */ -public class GetAlertRequest extends MasterNodeOperationRequest implements IndicesRequest { +public class GetAlertRequest extends ActionRequest implements IndicesRequest { private String alertName; private long version = Versions.MATCH_ANY; diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/get/GetAlertRequestBuilder.java b/src/main/java/org/elasticsearch/alerts/transport/actions/get/GetAlertRequestBuilder.java index 342f3913f9e..1e5fbb5c5c8 100644 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/get/GetAlertRequestBuilder.java +++ b/src/main/java/org/elasticsearch/alerts/transport/actions/get/GetAlertRequestBuilder.java @@ -17,7 +17,7 @@ import org.elasticsearch.index.VersionType; * A delete document action request builder. */ public class GetAlertRequestBuilder - extends MasterNodeOperationRequestBuilder { + extends ActionRequestBuilder { public GetAlertRequestBuilder(AlertsClientInterface client, String alertName) { diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/get/GetAlertResponse.java b/src/main/java/org/elasticsearch/alerts/transport/actions/get/GetAlertResponse.java index cd89e097a8c..0505db8e97d 100644 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/get/GetAlertResponse.java +++ b/src/main/java/org/elasticsearch/alerts/transport/actions/get/GetAlertResponse.java @@ -6,6 +6,7 @@ package org.elasticsearch.alerts.transport.actions.get; import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.alerts.Alert; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -16,44 +17,38 @@ import java.io.IOException; */ public class GetAlertResponse extends ActionResponse { private boolean found = false; - private Alert alert = null; + private GetResponse getResponse; public GetAlertResponse() { } - public boolean found() { - return this.found; + public GetAlertResponse(GetResponse getResponse) { + this.getResponse = getResponse; } - public void found(boolean found) { - this.found = found; + public void getResponse(GetResponse getResponse) { + this.getResponse = getResponse; } - public Alert alert() { - return alert; - } - - public void alert(Alert alert){ - this.alert = alert; + public GetResponse getResponse() { + return this.getResponse; } @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); - found = in.readBoolean(); - if (found) { - alert = new Alert(); - alert.readFrom(in); + if (in.readBoolean()) { + getResponse = GetResponse.readGetResponse(in); } } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - out.writeBoolean(found); - if (found && alert != null){ - alert.writeTo(out); + out.writeBoolean(getResponse != null); + if (getResponse != null) { + getResponse.writeTo(out); } } } diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/get/TransportGetAlertAction.java b/src/main/java/org/elasticsearch/alerts/transport/actions/get/TransportGetAlertAction.java index 32b26affc92..66b7ceee6ce 100644 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/get/TransportGetAlertAction.java +++ b/src/main/java/org/elasticsearch/alerts/transport/actions/get/TransportGetAlertAction.java @@ -7,7 +7,10 @@ package org.elasticsearch.alerts.transport.actions.get; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.get.GetRequest; +import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.support.ActionFilters; +import org.elasticsearch.action.support.TransportAction; import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction; import org.elasticsearch.alerts.Alert; import org.elasticsearch.alerts.AlertManager; @@ -15,6 +18,7 @@ import org.elasticsearch.alerts.AlertsStore; import org.elasticsearch.alerts.actions.AlertActionManager; import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest; import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse; +import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.block.ClusterBlockException; @@ -27,53 +31,27 @@ import org.elasticsearch.transport.TransportService; /** * Performs the delete operation. */ -public class TransportGetAlertAction extends TransportMasterNodeOperationAction { +public class TransportGetAlertAction extends TransportAction { - private final AlertManager alertManager; + private final Client client; @Inject - public TransportGetAlertAction(Settings settings, String actionName, TransportService transportService, - ClusterService clusterService, ThreadPool threadPool, ActionFilters actionFilters, - AlertManager alertManager) { - super(settings, actionName, transportService, clusterService, threadPool, actionFilters); - this.alertManager = alertManager; + public TransportGetAlertAction(Settings settings, String actionName, ThreadPool threadPool, + ActionFilters actionFilters, Client client) { + super(settings, actionName, threadPool, actionFilters); + this.client = client; } @Override - protected String executor() { - return ThreadPool.Names.MANAGEMENT; - } - - @Override - protected GetAlertRequest newRequest() { - return new GetAlertRequest(); - } - - @Override - protected GetAlertResponse newResponse() { - return new GetAlertResponse(); - } - - @Override - protected void masterOperation(GetAlertRequest request, ClusterState state, ActionListener listener) throws ElasticsearchException { + protected void doExecute(GetAlertRequest request, ActionListener listener) { try { - Alert alert = alertManager.getAlert(request.alertName()); - GetAlertResponse response = new GetAlertResponse(); - response.found(alert != null); - response.alert(alert); + GetResponse getResponse = client.prepareGet(AlertsStore.ALERT_INDEX, AlertsStore.ALERT_TYPE, request.alertName()) + .setVersion(request.version()) + .setVersionType(request.versionType()).execute().actionGet(); + GetAlertResponse response = new GetAlertResponse(getResponse); listener.onResponse(response); } catch (Exception e) { listener.onFailure(e); } } - - @Override - protected ClusterBlockException checkBlock(GetAlertRequest request, ClusterState state) { - if (!alertManager.isStarted()) { - return new ClusterBlockException(null); - } - return state.blocks().indicesBlockedException(ClusterBlockLevel.WRITE, new String[]{AlertsStore.ALERT_INDEX, AlertActionManager.ALERT_HISTORY_INDEX}); - } - - } diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/index/IndexAlertAction.java b/src/main/java/org/elasticsearch/alerts/transport/actions/index/IndexAlertAction.java new file mode 100644 index 00000000000..fcd74524910 --- /dev/null +++ b/src/main/java/org/elasticsearch/alerts/transport/actions/index/IndexAlertAction.java @@ -0,0 +1,32 @@ +/* + * 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.alerts.transport.actions.index; + +import org.elasticsearch.alerts.client.AlertsClientAction; +import org.elasticsearch.alerts.client.AlertsClientInterface; + +/** + */ +public class IndexAlertAction extends AlertsClientAction { + + public static final IndexAlertAction INSTANCE = new IndexAlertAction(); + public static final String NAME = "indices:data/write/alert/create"; + + private IndexAlertAction() { + super(NAME); + } + + + @Override + public IndexAlertRequestBuilder newRequestBuilder(AlertsClientInterface client) { + return new IndexAlertRequestBuilder(client); + } + + @Override + public IndexAlertResponse newResponse() { + return new IndexAlertResponse(); + } +} diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/update/UpdateAlertRequest.java b/src/main/java/org/elasticsearch/alerts/transport/actions/index/IndexAlertRequest.java similarity index 51% rename from src/main/java/org/elasticsearch/alerts/transport/actions/update/UpdateAlertRequest.java rename to src/main/java/org/elasticsearch/alerts/transport/actions/index/IndexAlertRequest.java index 309675ee510..f32a5c4b0d4 100644 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/update/UpdateAlertRequest.java +++ b/src/main/java/org/elasticsearch/alerts/transport/actions/index/IndexAlertRequest.java @@ -3,12 +3,13 @@ * 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.alerts.transport.actions.update; +package org.elasticsearch.alerts.transport.actions.index; + import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ValidateActions; import org.elasticsearch.action.support.master.MasterNodeOperationRequest; -import org.elasticsearch.alerts.Alert; +import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -16,49 +17,58 @@ import java.io.IOException; /** */ -public class UpdateAlertRequest extends MasterNodeOperationRequest { - - private Alert alert; +public class IndexAlertRequest extends MasterNodeOperationRequest { + private BytesReference alertSource; + private String alertName; @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; - if (alert == null) { - validationException = ValidateActions.addValidationError("alert is missing", validationException); + if (alertName == null) { + validationException = ValidateActions.addValidationError("alertName is missing", validationException); + } + if (alertSource == null) { + validationException = ValidateActions.addValidationError("alertSource is missing", validationException); } return validationException; } - UpdateAlertRequest() { + + IndexAlertRequest() { } - public UpdateAlertRequest(Alert alert) { - this.alert = alert; + + public IndexAlertRequest(BytesReference alertSource) { + this.alertSource = alertSource; } @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); - alert = new Alert(); - alert.readFrom(in); + alertSource = in.readBytesReference(); } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - alert.writeTo(out); + alertSource.writeTo(out); } - public Alert alert() { - return alert; + public String alertName() { + return alertName; } - public void Alert(Alert alert) { - this.alert = alert; + public void alertName(String alertName) { + this.alertName = alertName; } + public BytesReference alertSource() { + return alertSource; + } - + public void alertSource(BytesReference alertSource) { + this.alertSource = alertSource; + } } diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/index/IndexAlertRequestBuilder.java b/src/main/java/org/elasticsearch/alerts/transport/actions/index/IndexAlertRequestBuilder.java new file mode 100644 index 00000000000..af047055370 --- /dev/null +++ b/src/main/java/org/elasticsearch/alerts/transport/actions/index/IndexAlertRequestBuilder.java @@ -0,0 +1,45 @@ +/* + * 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.alerts.transport.actions.index; + +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder; +import org.elasticsearch.alerts.client.AlertsClientInterface; +import org.elasticsearch.common.bytes.BytesReference; + +/** + */ +public class IndexAlertRequestBuilder + extends MasterNodeOperationRequestBuilder { + + + public IndexAlertRequestBuilder(AlertsClientInterface client) { + super(client, new IndexAlertRequest()); + } + + + public IndexAlertRequestBuilder(AlertsClientInterface client, String alertName) { + super(client, new IndexAlertRequest()); + request.alertName(alertName); + } + + public IndexAlertRequestBuilder setAlertName(String alertName){ + request.alertName(alertName); + return this; + } + + public IndexAlertRequestBuilder setAlertSource(BytesReference alertSource) { + request.alertSource(alertSource); + return this; + } + + + @Override + protected void doExecute(ActionListener listener) { + client.createAlert(request, listener); + } +} diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/create/CreateAlertResponse.java b/src/main/java/org/elasticsearch/alerts/transport/actions/index/IndexAlertResponse.java similarity index 85% rename from src/main/java/org/elasticsearch/alerts/transport/actions/create/CreateAlertResponse.java rename to src/main/java/org/elasticsearch/alerts/transport/actions/index/IndexAlertResponse.java index 18008d232b3..dfe632021d3 100644 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/create/CreateAlertResponse.java +++ b/src/main/java/org/elasticsearch/alerts/transport/actions/index/IndexAlertResponse.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.alerts.transport.actions.create; +package org.elasticsearch.alerts.transport.actions.index; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.index.IndexResponse; @@ -14,14 +14,14 @@ import java.io.IOException; /** */ -public class CreateAlertResponse extends ActionResponse { +public class IndexAlertResponse extends ActionResponse { private IndexResponse indexResponse; - public CreateAlertResponse(IndexResponse indexResponse) { + public IndexAlertResponse(IndexResponse indexResponse) { this.indexResponse = indexResponse; } - public CreateAlertResponse() { + public IndexAlertResponse() { indexResponse = null; } diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/update/TransportUpdateAlertAction.java b/src/main/java/org/elasticsearch/alerts/transport/actions/index/TransportIndexAlertAction.java similarity index 60% rename from src/main/java/org/elasticsearch/alerts/transport/actions/update/TransportUpdateAlertAction.java rename to src/main/java/org/elasticsearch/alerts/transport/actions/index/TransportIndexAlertAction.java index 89a327f6bf0..289ac807cb5 100644 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/update/TransportUpdateAlertAction.java +++ b/src/main/java/org/elasticsearch/alerts/transport/actions/index/TransportIndexAlertAction.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.alerts.transport.actions.update; +package org.elasticsearch.alerts.transport.actions.index; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; @@ -17,19 +17,21 @@ import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.cluster.block.ClusterBlockLevel; +import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; /** */ -public class TransportUpdateAlertAction extends TransportMasterNodeOperationAction { +public class TransportIndexAlertAction extends TransportMasterNodeOperationAction { private final AlertManager alertManager; - public TransportUpdateAlertAction(Settings settings, String actionName, TransportService transportService, - ClusterService clusterService, ThreadPool threadPool, ActionFilters actionFilters, - AlertManager alertManager) { + @Inject + public TransportIndexAlertAction(Settings settings, String actionName, TransportService transportService, + ClusterService clusterService, ThreadPool threadPool, ActionFilters actionFilters, + AlertManager alertManager) { super(settings, actionName, transportService, clusterService, threadPool, actionFilters); this.alertManager = alertManager; } @@ -40,31 +42,32 @@ public class TransportUpdateAlertAction extends TransportMasterNodeOperationActi } @Override - protected UpdateAlertRequest newRequest() { - return new UpdateAlertRequest(); + protected IndexAlertRequest newRequest() { + return new IndexAlertRequest(); } @Override - protected UpdateAlertResponse newResponse() { - return new UpdateAlertResponse(); + protected IndexAlertResponse newResponse() { + return new IndexAlertResponse(); } @Override - protected void masterOperation(UpdateAlertRequest request, ClusterState state, ActionListener listener) throws ElasticsearchException { + protected void masterOperation(IndexAlertRequest request, ClusterState state, ActionListener listener) throws ElasticsearchException { try { - IndexResponse indexResponse = alertManager.updateAlert(request.alert(), true); - listener.onResponse(new UpdateAlertResponse(indexResponse)); + IndexResponse indexResponse = alertManager.addAlert(request.alertName(), request.alertSource()); + listener.onResponse(new IndexAlertResponse(indexResponse)); } catch (Exception e) { listener.onFailure(e); } } @Override - protected ClusterBlockException checkBlock(UpdateAlertRequest request, ClusterState state) { + protected ClusterBlockException checkBlock(IndexAlertRequest request, ClusterState state) { if (!alertManager.isStarted()) { return new ClusterBlockException(null); } return state.blocks().indicesBlockedException(ClusterBlockLevel.WRITE, new String[]{AlertsStore.ALERT_INDEX, AlertActionManager.ALERT_HISTORY_INDEX}); + } } diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/update/UpdateAlertAction.java b/src/main/java/org/elasticsearch/alerts/transport/actions/update/UpdateAlertAction.java deleted file mode 100644 index 468da5f6391..00000000000 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/update/UpdateAlertAction.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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.alerts.transport.actions.update; - -import org.elasticsearch.action.ClientAction; -import org.elasticsearch.alerts.client.AlertsClientAction; -import org.elasticsearch.alerts.client.AlertsClientInterface; -import org.elasticsearch.client.Client; - -/** - */ -public class UpdateAlertAction extends AlertsClientAction { - - public static final UpdateAlertAction INSTANCE = new UpdateAlertAction(); - public static final String NAME = "indices:data/write/alert/update"; - - private UpdateAlertAction() { - super(NAME); - } - - - @Override - public UpdateAlertRequestBuilder newRequestBuilder(AlertsClientInterface client) { - return new UpdateAlertRequestBuilder(client); - } - - @Override - public UpdateAlertResponse newResponse() { - return new UpdateAlertResponse(); - } - -} diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/update/UpdateAlertRequestBuilder.java b/src/main/java/org/elasticsearch/alerts/transport/actions/update/UpdateAlertRequestBuilder.java deleted file mode 100644 index 137ee20007f..00000000000 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/update/UpdateAlertRequestBuilder.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.alerts.transport.actions.update; - -import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.ActionRequestBuilder; -import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder; -import org.elasticsearch.alerts.Alert; -import org.elasticsearch.alerts.client.AlertsClient; -import org.elasticsearch.alerts.client.AlertsClientInterface; -import org.elasticsearch.client.Client; - -/** - */ -public class UpdateAlertRequestBuilder extends MasterNodeOperationRequestBuilder - { - - - public UpdateAlertRequestBuilder(AlertsClientInterface client) { - super(client, new UpdateAlertRequest()); - } - - public UpdateAlertRequestBuilder(AlertsClientInterface client, Alert alert) { - super(client, new UpdateAlertRequest(alert)); - } - - @Override - protected void doExecute(ActionListener listener) { - client.updateAlert(request, listener); - } -} diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/update/UpdateAlertResponse.java b/src/main/java/org/elasticsearch/alerts/transport/actions/update/UpdateAlertResponse.java deleted file mode 100644 index cdf402756b3..00000000000 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/update/UpdateAlertResponse.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.alerts.transport.actions.update; - -import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.action.index.IndexResponse; -import org.elasticsearch.action.update.UpdateResponse; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; - -import java.io.IOException; - -/** - */ -public class UpdateAlertResponse extends ActionResponse { - - private IndexResponse indexResponse; - - public UpdateAlertResponse() { - indexResponse = null; - } - - public UpdateAlertResponse(IndexResponse indexResponse) { - this.indexResponse = indexResponse; - } - - public IndexResponse updateResponse() { - return indexResponse; - } - - public void indexResponse(IndexResponse indexResponse) { - this.indexResponse = indexResponse; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - if (in.readBoolean()) { - indexResponse = new IndexResponse(); - indexResponse.readFrom(in); - } - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeBoolean(indexResponse != null); - if (indexResponse != null) { - indexResponse.writeTo(out); - } - } - -} diff --git a/src/test/java/org/elasticsearch/alerts/BasicAlertingTest.java b/src/test/java/org/elasticsearch/alerts/BasicAlertingTest.java index 9426312cc34..ec1832bc421 100644 --- a/src/test/java/org/elasticsearch/alerts/BasicAlertingTest.java +++ b/src/test/java/org/elasticsearch/alerts/BasicAlertingTest.java @@ -10,8 +10,8 @@ import org.elasticsearch.alerts.actions.*; import org.elasticsearch.alerts.client.AlertsClient; import org.elasticsearch.alerts.client.AlertsClientInterface; import org.elasticsearch.alerts.plugin.AlertsPlugin; -import org.elasticsearch.alerts.transport.actions.create.CreateAlertRequest; -import org.elasticsearch.alerts.transport.actions.create.CreateAlertResponse; +import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequest; +import org.elasticsearch.alerts.transport.actions.index.IndexAlertResponse; import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest; import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse; import org.elasticsearch.alerts.triggers.AlertTrigger; @@ -20,7 +20,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.script.ScriptService; @@ -124,10 +126,11 @@ public class BasicAlertingTest extends ElasticsearchIntegrationTest { ); AlertsClientInterface alertsClient = internalCluster().getInstance(AlertsClient.class, internalCluster().getMasterName()); + XContentBuilder jsonBuilder = XContentFactory.jsonBuilder(); + alert.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS); - //alertManager.addAlert("my-first-alert", jsonBuilder().value(alert).bytes()); - CreateAlertRequest alertRequest = new CreateAlertRequest(alert); - CreateAlertResponse alertsResponse = alertsClient.createAlert(alertRequest).actionGet(); + IndexAlertRequest alertRequest = alertsClient.prepareCreateAlert().setAlertName("my-first-alert").setAlertSource(jsonBuilder.bytes()).request(); + IndexAlertResponse alertsResponse = alertsClient.createAlert(alertRequest).actionGet(); assertNotNull(alertsResponse.indexResponse()); assertTrue(alertsResponse.indexResponse().isCreated()); diff --git a/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java b/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java index 9b611daeda6..24eb455cc48 100644 --- a/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java +++ b/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java @@ -15,14 +15,12 @@ import org.elasticsearch.alerts.AlertsStore; import org.elasticsearch.alerts.client.AlertsClient; import org.elasticsearch.alerts.client.AlertsClientInterface; import org.elasticsearch.alerts.plugin.AlertsPlugin; -import org.elasticsearch.alerts.transport.actions.create.CreateAlertRequest; -import org.elasticsearch.alerts.transport.actions.create.CreateAlertResponse; +import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequest; +import org.elasticsearch.alerts.transport.actions.index.IndexAlertResponse; import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest; import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse; import org.elasticsearch.alerts.transport.actions.get.GetAlertRequest; import org.elasticsearch.alerts.transport.actions.get.GetAlertResponse; -import org.elasticsearch.alerts.transport.actions.update.UpdateAlertRequest; -import org.elasticsearch.alerts.transport.actions.update.UpdateAlertResponse; import org.elasticsearch.alerts.triggers.AlertTrigger; import org.elasticsearch.alerts.triggers.ScriptedAlertTrigger; import org.elasticsearch.common.io.stream.BytesStreamOutput; @@ -31,8 +29,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.joda.FormatDateTimeFormatter; import org.elasticsearch.common.joda.time.DateTime; import org.elasticsearch.common.joda.time.DateTimeZone; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.*; import org.elasticsearch.index.mapper.core.DateFieldMapper; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.script.ScriptService; @@ -41,13 +38,11 @@ import org.elasticsearch.search.internal.InternalSearchHits; import org.elasticsearch.search.internal.InternalSearchResponse; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.junit.Test; import java.io.IOException; import java.util.*; -import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import static org.hamcrest.core.Is.is; import java.util.ArrayList; @@ -211,25 +206,20 @@ public class AlertActionsTest extends ElasticsearchIntegrationTest { true ); + XContentBuilder jsonBuilder = XContentFactory.jsonBuilder(); + alert.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS); AlertsClientInterface alertsClient = internalCluster().getInstance(AlertsClient.class, internalCluster().getMasterName()); - CreateAlertRequest alertRequest = new CreateAlertRequest(alert); - CreateAlertResponse alertsResponse = alertsClient.createAlert(alertRequest).actionGet(); + IndexAlertRequest alertRequest = alertsClient.prepareCreateAlert().setAlertName("my-first-alert").setAlertSource(jsonBuilder.bytes()).request(); + IndexAlertResponse alertsResponse = alertsClient.createAlert(alertRequest).actionGet(); assertNotNull(alertsResponse.indexResponse()); assertTrue(alertsResponse.indexResponse().isCreated()); GetAlertRequest getAlertRequest = new GetAlertRequest(alert.alertName()); GetAlertResponse getAlertResponse = alertsClient.getAlert(getAlertRequest).actionGet(); - assertTrue(getAlertResponse.found()); - assertEquals(alert.schedule(), getAlertResponse.alert().schedule()); - - String schedule = "0/10 * * * * ? *"; - alert.schedule(schedule); - UpdateAlertRequest updateAlertRequest = new UpdateAlertRequest(alert); - UpdateAlertResponse updateAlertResponse = alertsClient.updateAlert(updateAlertRequest).actionGet(); - assertNotNull(updateAlertResponse.updateResponse()); - assertFalse(updateAlertResponse.updateResponse().isCreated()); + assertTrue(getAlertResponse.getResponse().isExists()); + assertEquals(getAlertResponse.getResponse().getSourceAsMap().get("schedule").toString(), "0/5 * * * * ? *"); DeleteAlertRequest deleteAlertRequest = new DeleteAlertRequest(alert.alertName()); DeleteAlertResponse deleteAlertResponse = alertsClient.deleteAlert(deleteAlertRequest).actionGet(); @@ -237,10 +227,8 @@ public class AlertActionsTest extends ElasticsearchIntegrationTest { assertTrue(deleteAlertResponse.deleteResponse().isFound()); getAlertResponse = alertsClient.getAlert(getAlertRequest).actionGet(); - assertFalse(getAlertResponse.found()); + assertFalse(getAlertResponse.getResponse().isExists()); - updateAlertResponse = alertsClient.updateAlert(updateAlertRequest).actionGet(); - assertNull(updateAlertResponse.updateResponse()); } }