DeleteAlert : Add the delete response to the DeleteAlertResponse
This commit adds the delete response to the DeleteAlertResponse. Original commit: elastic/x-pack-elasticsearch@a5cc31f321
This commit is contained in:
parent
47a78648df
commit
41821c39c6
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,8 +60,7 @@ public class TransportDeleteAlertAction extends TransportMasterNodeOperationActi
|
|||
@Override
|
||||
protected void masterOperation(DeleteAlertRequest request, ClusterState state, ActionListener<DeleteAlertResponse> 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);
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue