[ML] Check calendar exists before deleting event (elastic/x-pack-elasticsearch#3659)

Solves the event part of elastic/x-pack-elasticsearch#3620

Original commit: elastic/x-pack-elasticsearch@c79ca85c6e
This commit is contained in:
Dimitris Athanasiou 2018-01-22 13:02:42 +00:00 committed by GitHub
parent 928b6a6e04
commit f3282b559f
3 changed files with 39 additions and 37 deletions

View File

@ -12,7 +12,6 @@ import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetAction; import org.elasticsearch.action.get.GetAction;
import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.action.support.HandledTransportAction;
import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.action.support.WriteRequest;
@ -57,46 +56,39 @@ public class TransportDeleteCalendarEventAction extends HandledTransportAction<D
protected void doExecute(DeleteCalendarEventAction.Request request, ActionListener<DeleteCalendarEventAction.Response> listener) { protected void doExecute(DeleteCalendarEventAction.Request request, ActionListener<DeleteCalendarEventAction.Response> listener) {
final String eventId = request.getEventId(); final String eventId = request.getEventId();
GetRequest getRequest = new GetRequest(MlMetaIndex.INDEX_NAME, MlMetaIndex.TYPE, eventId); ActionListener<Calendar> calendarListener = ActionListener.wrap(
executeAsyncWithOrigin(client, ML_ORIGIN, GetAction.INSTANCE, getRequest, new ActionListener<GetResponse>() { calendar -> {
@Override GetRequest getRequest = new GetRequest(MlMetaIndex.INDEX_NAME, MlMetaIndex.TYPE, eventId);
public void onResponse(GetResponse getResponse) { executeAsyncWithOrigin(client, ML_ORIGIN, GetAction.INSTANCE, getRequest, ActionListener.wrap(
if (getResponse.isExists() == false) { getResponse -> {
listener.onFailure(new ResourceNotFoundException("No event with id [" + eventId + "]")); if (getResponse.isExists() == false) {
return; listener.onFailure(new ResourceNotFoundException("No event with id [" + eventId + "]"));
} return;
}
Map<String, Object> source = getResponse.getSourceAsMap(); Map<String, Object> source = getResponse.getSourceAsMap();
String calendarId = (String) source.get(Calendar.ID.getPreferredName()); String calendarId = (String) source.get(Calendar.ID.getPreferredName());
if (calendarId == null) { if (calendarId == null) {
listener.onFailure(ExceptionsHelper.badRequestException("Event [" + eventId + "] does not have a valid " listener.onFailure(ExceptionsHelper.badRequestException("Event [" + eventId + "] does not have a valid "
+ Calendar.ID.getPreferredName())); + Calendar.ID.getPreferredName()));
return; return;
} }
if (calendarId.equals(request.getCalendarId()) == false) { if (calendarId.equals(request.getCalendarId()) == false) {
listener.onFailure(ExceptionsHelper.badRequestException( listener.onFailure(ExceptionsHelper.badRequestException(
"Event [" + eventId + "] has " + Calendar.ID.getPreferredName() + "Event [" + eventId + "] has " + Calendar.ID.getPreferredName()
" [" + calendarId + "] which does not match the request " + Calendar.ID.getPreferredName() + + " [" + calendarId + "] which does not match the request "
" [" + request.getCalendarId() + "]")); + Calendar.ID.getPreferredName() + " [" + request.getCalendarId() + "]"));
return; return;
} }
ActionListener<Calendar> calendarListener = ActionListener.wrap( deleteEvent(eventId, calendar, listener);
calendar -> { }, listener::onFailure)
deleteEvent(eventId, calendar, listener); );
}, }, listener::onFailure);
listener::onFailure
);
jobProvider.calendar(calendarId, calendarListener); // Get the calendar first so we check the calendar exists before checking the event exists
} jobProvider.calendar(request.getCalendarId(), calendarListener);
@Override
public void onFailure(Exception e) {
listener.onFailure(e);
}
});
} }
private void deleteEvent(String eventId, Calendar calendar, ActionListener<DeleteCalendarEventAction.Response> listener) { private void deleteEvent(String eventId, Calendar calendar, ActionListener<DeleteCalendarEventAction.Response> listener) {

View File

@ -613,3 +613,12 @@
{ {
"events": [] "events": []
} }
---
"Test delete event from non existing calendar":
- do:
catch: /No calendar with id \[unknown\]/
xpack.ml.delete_calendar_event:
calendar_id: "unknown"
event_id: "event_1"

View File

@ -26,6 +26,7 @@ integTestRunner {
'ml/calendar_crud/Test PageParams with ID is invalid', 'ml/calendar_crud/Test PageParams with ID is invalid',
'ml/calendar_crud/Test post calendar events given empty events', 'ml/calendar_crud/Test post calendar events given empty events',
'ml/calendar_crud/Test put calendar given id contains invalid chars', 'ml/calendar_crud/Test put calendar given id contains invalid chars',
'ml/calendar_crud/Test delete event from non existing calendar',
'ml/custom_all_field/Test querying custom all field', 'ml/custom_all_field/Test querying custom all field',
'ml/datafeeds_crud/Test delete datafeed with missing id', 'ml/datafeeds_crud/Test delete datafeed with missing id',
'ml/datafeeds_crud/Test put datafeed referring to missing job_id', 'ml/datafeeds_crud/Test put datafeed referring to missing job_id',