From 41821c39c6021b568abc1fca2d7effbe92713dee Mon Sep 17 00:00:00 2001 From: Brian Murphy Date: Wed, 5 Nov 2014 17:50:05 +0000 Subject: [PATCH] DeleteAlert : Add the delete response to the DeleteAlertResponse This commit adds the delete response to the DeleteAlertResponse. Original commit: elastic/x-pack-elasticsearch@a5cc31f32112eaf0bcedc11b111c8a630e59e611 --- .../elasticsearch/alerts/AlertManager.java | 11 +++++--- .../org/elasticsearch/alerts/AlertsStore.java | 22 +++++++++------ .../alerts/rest/AlertRestHandler.java | 5 ++-- .../actions/delete/DeleteAlertResponse.java | 28 ++++++++++++------- .../delete/TransportDeleteAlertAction.java | 3 +- .../alerts/BasicAlertingTest.java | 3 +- .../alerts/actions/AlertActionsTest.java | 3 +- 7 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/elasticsearch/alerts/AlertManager.java b/src/main/java/org/elasticsearch/alerts/AlertManager.java index f7f76f17f1a..6fbc115eeff 100644 --- a/src/main/java/org/elasticsearch/alerts/AlertManager.java +++ b/src/main/java/org/elasticsearch/alerts/AlertManager.java @@ -8,8 +8,11 @@ package org.elasticsearch.alerts; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchIllegalStateException; +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.alerts.actions.AlertActionManager; import org.elasticsearch.alerts.scheduler.AlertScheduler; +import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse; import org.elasticsearch.alerts.triggers.TriggerManager; import org.elasticsearch.alerts.triggers.TriggerResult; import org.elasticsearch.cluster.*; @@ -76,14 +79,14 @@ public class AlertManager extends AbstractComponent { } } - public boolean deleteAlert(String name) throws InterruptedException, ExecutionException { + + public DeleteResponse deleteAlert(String name) throws InterruptedException, ExecutionException { ensureStarted(); if (alertsStore.hasAlert(name)) { assert scheduler.remove(name); - alertsStore.deleteAlert(name); - return true; + return alertsStore.deleteAlert(name); } else { - return false; + return null; } } diff --git a/src/main/java/org/elasticsearch/alerts/AlertsStore.java b/src/main/java/org/elasticsearch/alerts/AlertsStore.java index 41ab682f0ed..c02a5e48e2b 100644 --- a/src/main/java/org/elasticsearch/alerts/AlertsStore.java +++ b/src/main/java/org/elasticsearch/alerts/AlertsStore.java @@ -7,6 +7,7 @@ package org.elasticsearch.alerts; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchIllegalArgumentException; +import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.index.IndexRequest; @@ -16,6 +17,7 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.alerts.actions.AlertAction; import org.elasticsearch.alerts.actions.AlertActionRegistry; +import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse; import org.elasticsearch.alerts.triggers.TriggerManager; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterState; @@ -153,17 +155,19 @@ public class AlertsStore extends AbstractComponent { /** * Deletes the alert with the specified name if exists */ - public void deleteAlert(String name) { + public DeleteResponse deleteAlert(String name) { Alert alert = alertMap.remove(name); - if (alert != null) { - DeleteRequest deleteRequest = new DeleteRequest(); - deleteRequest.id(name); - deleteRequest.index(ALERT_INDEX); - deleteRequest.type(ALERT_TYPE); - deleteRequest.version(alert.version()); - DeleteResponse deleteResponse = client.delete(deleteRequest).actionGet(); - assert deleteResponse.isFound(); + if (alert == null) { + return null; } + DeleteRequest deleteRequest = new DeleteRequest(); + deleteRequest.id(name); + deleteRequest.index(ALERT_INDEX); + deleteRequest.type(ALERT_TYPE); + deleteRequest.version(alert.version()); + DeleteResponse deleteResponse = client.delete(deleteRequest).actionGet(); + assert deleteResponse.isFound(); + return deleteResponse; } /** diff --git a/src/main/java/org/elasticsearch/alerts/rest/AlertRestHandler.java b/src/main/java/org/elasticsearch/alerts/rest/AlertRestHandler.java index 15e6b085987..78e55dd34c5 100644 --- a/src/main/java/org/elasticsearch/alerts/rest/AlertRestHandler.java +++ b/src/main/java/org/elasticsearch/alerts/rest/AlertRestHandler.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.alerts.rest; +import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.alerts.Alert; import org.elasticsearch.alerts.AlertManager; import org.elasticsearch.common.inject.Inject; @@ -92,9 +93,9 @@ public class AlertRestHandler implements RestHandler { } else if (request.method() == DELETE) { String alertName = request.param("name"); logger.warn("Deleting [{}]", alertName); - boolean successful = alertManager.deleteAlert(alertName); + DeleteResponse deleteResponse = alertManager.deleteAlert(alertName); XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); - builder.field("Success", successful); + builder.field("Success", deleteResponse != null); builder.field("alertName", alertName); restChannel.sendResponse(new BytesRestResponse(OK)); return true; diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/delete/DeleteAlertResponse.java b/src/main/java/org/elasticsearch/alerts/transport/actions/delete/DeleteAlertResponse.java index 745c8904736..50e7f62fade 100644 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/delete/DeleteAlertResponse.java +++ b/src/main/java/org/elasticsearch/alerts/transport/actions/delete/DeleteAlertResponse.java @@ -6,6 +6,8 @@ package org.elasticsearch.alerts.transport.actions.delete; import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -15,33 +17,39 @@ import java.io.IOException; */ public class DeleteAlertResponse extends ActionResponse { - private boolean success; + private DeleteResponse deleteResponse; public DeleteAlertResponse() { - success = false; } - public DeleteAlertResponse(boolean success) { - this.success = true; + public DeleteAlertResponse(@Nullable DeleteResponse deleteResponse) { + this.deleteResponse = deleteResponse; } - public boolean success() { - return success; + public DeleteResponse deleteResponse() { + return deleteResponse; } - public void success(boolean success) { - this.success = success; + public void deleteResponse(DeleteResponse deleteResponse){ + this.deleteResponse = deleteResponse; } + @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); - success = in.readBoolean(); + deleteResponse = new DeleteResponse(); + if (in.readBoolean()) { + deleteResponse.readFrom(in); + } } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - out.writeBoolean(success); + out.writeBoolean(deleteResponse != null); + if (deleteResponse != null) { + deleteResponse.writeTo(out); + } } } diff --git a/src/main/java/org/elasticsearch/alerts/transport/actions/delete/TransportDeleteAlertAction.java b/src/main/java/org/elasticsearch/alerts/transport/actions/delete/TransportDeleteAlertAction.java index 08bf486e063..e17aebda46d 100644 --- a/src/main/java/org/elasticsearch/alerts/transport/actions/delete/TransportDeleteAlertAction.java +++ b/src/main/java/org/elasticsearch/alerts/transport/actions/delete/TransportDeleteAlertAction.java @@ -60,8 +60,7 @@ public class TransportDeleteAlertAction extends TransportMasterNodeOperationActi @Override protected void masterOperation(DeleteAlertRequest request, ClusterState state, ActionListener listener) throws ElasticsearchException { try { - boolean success = alertManager.deleteAlert(request.alertName()); - listener.onResponse(new DeleteAlertResponse(success)); + listener.onResponse(new DeleteAlertResponse(alertManager.deleteAlert(request.alertName()))); } catch (Exception e) { listener.onFailure(e); } diff --git a/src/test/java/org/elasticsearch/alerts/BasicAlertingTest.java b/src/test/java/org/elasticsearch/alerts/BasicAlertingTest.java index b2ffddd548d..a01ba458b02 100644 --- a/src/test/java/org/elasticsearch/alerts/BasicAlertingTest.java +++ b/src/test/java/org/elasticsearch/alerts/BasicAlertingTest.java @@ -143,7 +143,8 @@ public class BasicAlertingTest extends ElasticsearchIntegrationTest { DeleteAlertRequest deleteAlertRequest = new DeleteAlertRequest(alert.alertName()); DeleteAlertResponse deleteAlertResponse = alertsClient.deleteAlert(deleteAlertRequest).actionGet(); - assertTrue(deleteAlertResponse.success()); + assertNotNull(deleteAlertResponse.deleteResponse()); + assertTrue(deleteAlertResponse.deleteResponse().isFound()); } diff --git a/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java b/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java index 062626faf92..11f6d4d19d1 100644 --- a/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java +++ b/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java @@ -231,7 +231,8 @@ public class AlertActionsTest extends ElasticsearchIntegrationTest { DeleteAlertRequest deleteAlertRequest = new DeleteAlertRequest(alert.alertName()); DeleteAlertResponse deleteAlertResponse = alertsClient.deleteAlert(deleteAlertRequest).actionGet(); - assertTrue(deleteAlertResponse.success()); + assertNotNull(deleteAlertResponse.deleteResponse()); + assertTrue(deleteAlertResponse.deleteResponse().isFound()); getAlertResponse = alertsClient.getAlert(getAlertRequest).actionGet(); assertFalse(getAlertResponse.found());