Added default `actions`

Until now you always needed to define the `actions` in a watch. Even if you didn't want any actions, you had to define and empty objects there.

Now, the `actions` is an optional field. When missing, we default to "no actions"

Original commit: elastic/x-pack-elasticsearch@3d1a961232
This commit is contained in:
uboness 2015-05-12 19:25:05 +02:00
parent 86262d5ca5
commit a59e157ca3
3 changed files with 39 additions and 19 deletions

View File

@ -5,6 +5,7 @@
*/
package org.elasticsearch.watcher.watch;
import com.google.common.collect.ImmutableList;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.bytes.BytesReference;
@ -23,6 +24,7 @@ import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.watcher.WatcherException;
import org.elasticsearch.watcher.actions.ActionRegistry;
import org.elasticsearch.watcher.actions.ActionWrapper;
import org.elasticsearch.watcher.actions.ExecutableActions;
import org.elasticsearch.watcher.condition.ConditionRegistry;
import org.elasticsearch.watcher.condition.ExecutableCondition;
@ -220,6 +222,7 @@ public class Watch implements TriggerEngine.Job, ToXContent {
private final ExecutableInput defaultInput;
private final ExecutableCondition defaultCondition;
private final ExecutableActions defaultActions;
private final TimeValue defaultThrottleTimePeriod;
@Inject
@ -239,6 +242,7 @@ public class Watch implements TriggerEngine.Job, ToXContent {
this.defaultInput = new ExecutableNoneInput(logger);
this.defaultCondition = new ExecutableAlwaysCondition(logger);
this.defaultActions = new ExecutableActions(ImmutableList.<ActionWrapper>of());
this.defaultThrottleTimePeriod = settings.getAsTime(DEFAULT_THROTTLE_PERIOD_SETTING, DEFAULT_THROTTLE_PERIOD);
}
@ -288,7 +292,7 @@ public class Watch implements TriggerEngine.Job, ToXContent {
Trigger trigger = null;
ExecutableInput input = defaultInput;
ExecutableCondition condition = defaultCondition;
ExecutableActions actions = null;
ExecutableActions actions = defaultActions;
ExecutableTransform transform = null;
Map<String, Object> metatdata = null;
Status status = null;
@ -338,10 +342,6 @@ public class Watch implements TriggerEngine.Job, ToXContent {
if (trigger == null) {
throw new WatcherException("could not parse watch [{}]. missing required field [{}]", id, TRIGGER_FIELD.getPreferredName());
}
if (actions == null) {
throw new WatcherException("could not parse watch [{}]. missing required field [{}]", id, ACTIONS_FIELD.getPreferredName());
}
return new Watch(id, clock, licenseService, trigger, input, condition, transform, actions, metatdata, throttlePeriod, status);
}

View File

@ -75,17 +75,6 @@ public class BootStrapTests extends AbstractWatcherIntegrationTests {
.endObject())
.get();
// no actions field:
client().prepareIndex(WatchStore.INDEX, WatchStore.DOC_TYPE, "_id1")
.setSource(jsonBuilder().startObject()
.startObject(Watch.Parser.TRIGGER_FIELD.getPreferredName())
.startObject("schedule")
.field("interval", "1s")
.endObject()
.endObject()
.endObject())
.get();
// invalid interval
client().prepareIndex(WatchStore.INDEX, WatchStore.DOC_TYPE, "_id2")
.setSource(jsonBuilder().startObject()

View File

@ -7,7 +7,6 @@ package org.elasticsearch.watcher.watch;
import com.carrotsearch.ant.tasks.junit4.dependencies.com.google.common.collect.ImmutableSet;
import com.carrotsearch.randomizedtesting.annotations.Repeat;
import com.carrotsearch.randomizedtesting.annotations.Seed;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.ImmutableList;
import org.elasticsearch.common.collect.ImmutableMap;
@ -50,6 +49,7 @@ import org.elasticsearch.watcher.input.ExecutableInput;
import org.elasticsearch.watcher.input.InputBuilders;
import org.elasticsearch.watcher.input.InputFactory;
import org.elasticsearch.watcher.input.InputRegistry;
import org.elasticsearch.watcher.input.none.ExecutableNoneInput;
import org.elasticsearch.watcher.input.search.ExecutableSearchInput;
import org.elasticsearch.watcher.input.search.SearchInput;
import org.elasticsearch.watcher.input.search.SearchInputFactory;
@ -99,8 +99,8 @@ import java.util.Map;
import static org.elasticsearch.watcher.input.InputBuilders.searchInput;
import static org.elasticsearch.watcher.test.WatcherTestUtils.matchAllRequest;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.elasticsearch.watcher.trigger.TriggerBuilders.schedule;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.mock;
public class WatchTests extends ElasticsearchTestCase {
@ -209,6 +209,37 @@ public class WatchTests extends ElasticsearchTestCase {
}
}
@Test
public void testParser_Defaults() throws Exception {
Schedule schedule = randomSchedule();
ScheduleRegistry scheduleRegistry = registry(schedule);
TriggerEngine triggerEngine = new ParseOnlyScheduleTriggerEngine(ImmutableSettings.EMPTY, scheduleRegistry, SystemClock.INSTANCE);
TriggerService triggerService = new TriggerService(ImmutableSettings.EMPTY, ImmutableSet.of(triggerEngine));
SecretService secretService = new SecretService.PlainText();
ConditionRegistry conditionRegistry = registry(new ExecutableAlwaysCondition(logger));
InputRegistry inputRegistry = registry(new ExecutableNoneInput(logger));
TransformRegistry transformRegistry = transformRegistry();
ExecutableActions actions = new ExecutableActions(ImmutableList.<ActionWrapper>of());
ActionRegistry actionRegistry = registry(actions, transformRegistry);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.startObject(Watch.Parser.TRIGGER_FIELD.getPreferredName())
.field(ScheduleTrigger.TYPE, schedule(schedule).build())
.endObject();
builder.endObject();
Watch.Parser watchParser = new Watch.Parser(settings, mock(LicenseService.class), conditionRegistry, triggerService, transformRegistry, actionRegistry, inputRegistry, SystemClock.INSTANCE, secretService);
Watch watch = watchParser.parse("failure", false, builder.bytes());
assertThat(watch, notNullValue());
assertThat(watch.trigger(), instanceOf(ScheduleTrigger.class));
assertThat(watch.input(), instanceOf(ExecutableNoneInput.class));
assertThat(watch.condition(), instanceOf(ExecutableAlwaysCondition.class));
assertThat(watch.transform(), nullValue());
assertThat(watch.actions(), notNullValue());
assertThat(watch.actions().count(), is(0));
}
private static Schedule randomSchedule() {
String type = randomFrom(CronSchedule.TYPE, HourlySchedule.TYPE, DailySchedule.TYPE, WeeklySchedule.TYPE, MonthlySchedule.TYPE, YearlySchedule.TYPE, IntervalSchedule.TYPE);
switch (type) {