diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/XPackPlugin.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/XPackPlugin.java index 19687fde244..356557745dd 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/XPackPlugin.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/XPackPlugin.java @@ -233,7 +233,6 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin, I extensionsService.getExtensions())); components.addAll(monitoring.createComponents(internalClient, threadPool, clusterService, licenseService, sslService)); - components.addAll(watcher.createComponents(getClock(), scriptService)); // watcher http stuff Map httpAuthFactories = new HashMap<>(); @@ -245,8 +244,13 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin, I final HttpClient httpClient = new HttpClient(settings, httpAuthRegistry, sslService); components.add(httpClient); - components.addAll(createNotificationComponents(clusterService.getClusterSettings(), httpClient, - httpTemplateParser, scriptService, httpAuthRegistry)); + Collection notificationComponents = createNotificationComponents(clusterService.getClusterSettings(), httpClient, + httpTemplateParser, scriptService, httpAuthRegistry); + components.addAll(notificationComponents); + + components.addAll(watcher.createComponents(getClock(), scriptService, internalClient, searchRequestParsers, licenseState, + httpClient, components)); + // just create the reloader as it will pull all of the loaded ssl configurations and start watching them new SSLConfigurationReloader(settings, env, sslService, resourceWatcherService); diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java index 720d348c06d..8451dce5a45 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java @@ -20,18 +20,44 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.EsExecutors; +import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.plugins.ActionPlugin; import org.elasticsearch.plugins.ScriptPlugin; import org.elasticsearch.rest.RestHandler; import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptSettings; +import org.elasticsearch.search.SearchRequestParsers; import org.elasticsearch.threadpool.ExecutorBuilder; import org.elasticsearch.threadpool.FixedExecutorBuilder; import org.elasticsearch.xpack.XPackPlugin; import org.elasticsearch.xpack.XPackSettings; +import org.elasticsearch.xpack.common.http.HttpClient; +import org.elasticsearch.xpack.common.http.HttpRequestTemplate; +import org.elasticsearch.xpack.common.text.TextTemplateEngine; +import org.elasticsearch.xpack.notification.email.EmailService; +import org.elasticsearch.xpack.notification.email.attachment.EmailAttachmentsParser; +import org.elasticsearch.xpack.notification.hipchat.HipChatService; +import org.elasticsearch.xpack.notification.pagerduty.PagerDutyService; +import org.elasticsearch.xpack.notification.slack.SlackService; +import org.elasticsearch.xpack.security.InternalClient; import org.elasticsearch.xpack.support.clock.Clock; -import org.elasticsearch.xpack.watcher.actions.WatcherActionModule; +import org.elasticsearch.xpack.watcher.actions.ActionFactory; +import org.elasticsearch.xpack.watcher.actions.ActionRegistry; +import org.elasticsearch.xpack.watcher.actions.email.EmailAction; +import org.elasticsearch.xpack.watcher.actions.email.EmailActionFactory; +import org.elasticsearch.xpack.watcher.actions.hipchat.HipChatAction; +import org.elasticsearch.xpack.watcher.actions.hipchat.HipChatActionFactory; +import org.elasticsearch.xpack.watcher.actions.index.IndexAction; +import org.elasticsearch.xpack.watcher.actions.index.IndexActionFactory; +import org.elasticsearch.xpack.watcher.actions.logging.LoggingAction; +import org.elasticsearch.xpack.watcher.actions.logging.LoggingActionFactory; +import org.elasticsearch.xpack.watcher.actions.pagerduty.PagerDutyAction; +import org.elasticsearch.xpack.watcher.actions.pagerduty.PagerDutyActionFactory; +import org.elasticsearch.xpack.watcher.actions.slack.SlackAction; +import org.elasticsearch.xpack.watcher.actions.slack.SlackActionFactory; +import org.elasticsearch.xpack.watcher.actions.webhook.WebhookAction; +import org.elasticsearch.xpack.watcher.actions.webhook.WebhookActionFactory; import org.elasticsearch.xpack.watcher.client.WatcherClientModule; import org.elasticsearch.xpack.watcher.condition.AlwaysCondition; import org.elasticsearch.xpack.watcher.condition.ArrayCompareCondition; @@ -58,7 +84,12 @@ import org.elasticsearch.xpack.watcher.rest.action.RestWatchServiceAction; import org.elasticsearch.xpack.watcher.rest.action.RestWatcherStatsAction; import org.elasticsearch.xpack.watcher.support.WatcherIndexTemplateRegistry; import org.elasticsearch.xpack.watcher.support.WatcherIndexTemplateRegistry.TemplateConfig; -import org.elasticsearch.xpack.watcher.transform.TransformModule; +import org.elasticsearch.xpack.watcher.transform.TransformFactory; +import org.elasticsearch.xpack.watcher.transform.TransformRegistry; +import org.elasticsearch.xpack.watcher.transform.script.ScriptTransform; +import org.elasticsearch.xpack.watcher.transform.script.ScriptTransformFactory; +import org.elasticsearch.xpack.watcher.transform.search.SearchTransform; +import org.elasticsearch.xpack.watcher.transform.search.SearchTransformFactory; import org.elasticsearch.xpack.watcher.transport.actions.ack.AckWatchAction; import org.elasticsearch.xpack.watcher.transport.actions.ack.TransportAckWatchAction; import org.elasticsearch.xpack.watcher.transport.actions.activate.ActivateWatchAction; @@ -90,6 +121,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; +import java.util.stream.Collectors; import static java.util.Collections.emptyList; @@ -124,7 +156,9 @@ public class Watcher implements ActionPlugin, ScriptPlugin { validAutoCreateIndex(settings); } - public Collection createComponents(Clock clock, ScriptService scriptService) { + public Collection createComponents(Clock clock, ScriptService scriptService, InternalClient internalClient, + SearchRequestParsers searchRequestParsers, XPackLicenseState licenseState, + HttpClient httpClient, Collection components) { final Map parsers = new HashMap<>(); parsers.put(AlwaysCondition.TYPE, (c, id, p, upgrade) -> AlwaysCondition.parse(id, p)); parsers.put(NeverCondition.TYPE, (c, id, p, upgrade) -> NeverCondition.parse(id, p)); @@ -133,7 +167,40 @@ public class Watcher implements ActionPlugin, ScriptPlugin { String defaultLegacyScriptLanguage = ScriptSettings.getLegacyDefaultLang(settings); parsers.put(ScriptCondition.TYPE, (c, id, p, upgrade) -> ScriptCondition.parse(scriptService, id, p, upgrade, defaultLegacyScriptLanguage)); - return Collections.singleton(new ConditionRegistry(Collections.unmodifiableMap(parsers), clock)); + + final ConditionRegistry conditionRegistry = new ConditionRegistry(Collections.unmodifiableMap(parsers), clock); + final Map transformFactories = new HashMap<>(); + transformFactories.put(ScriptTransform.TYPE, new ScriptTransformFactory(settings, scriptService)); + transformFactories.put(SearchTransform.TYPE, new SearchTransformFactory(settings, internalClient, searchRequestParsers, + scriptService)); + final TransformRegistry transformRegistry = new TransformRegistry(settings, Collections.unmodifiableMap(transformFactories)); + + final Map actionFactoryMap = new HashMap<>(); + TextTemplateEngine templateEngine = getService(TextTemplateEngine.class, components); + actionFactoryMap.put(EmailAction.TYPE, new EmailActionFactory(settings, getService(EmailService.class, components), templateEngine, + getService(EmailAttachmentsParser.class, components))); + actionFactoryMap.put(WebhookAction.TYPE, new WebhookActionFactory(settings, httpClient, + getService(HttpRequestTemplate.Parser.class, components), templateEngine)); + actionFactoryMap.put(IndexAction.TYPE, new IndexActionFactory(settings, internalClient)); + actionFactoryMap.put(LoggingAction.TYPE, new LoggingActionFactory(settings, templateEngine)); + actionFactoryMap.put(HipChatAction.TYPE, new HipChatActionFactory(settings, templateEngine, + getService(HipChatService.class, components))); + actionFactoryMap.put(SlackAction.TYPE, new SlackActionFactory(settings, templateEngine, + getService(SlackService.class, components))); + actionFactoryMap.put(PagerDutyAction.TYPE, new PagerDutyActionFactory(settings, templateEngine, + getService(PagerDutyService.class, components))); + final ActionRegistry registry = new ActionRegistry(actionFactoryMap, conditionRegistry, transformRegistry, clock, licenseState); + return Collections.singleton(registry); + } + + private T getService(Class serviceClass, Collection services) { + List collect = services.stream().filter(o -> o.getClass() == serviceClass).collect(Collectors.toList()); + if (collect.isEmpty()) { + throw new IllegalArgumentException("no service for class " + serviceClass.getName()); + } else if (collect.size() > 1) { + throw new IllegalArgumentException("more than one service for class " + serviceClass.getName()); + } + return (T) collect.get(0); } public Collection nodeModules() { @@ -142,11 +209,9 @@ public class Watcher implements ActionPlugin, ScriptPlugin { if (enabled && transportClient == false) { modules.add(new WatchModule()); modules.add(new WatcherClientModule()); - modules.add(new TransformModule()); modules.add(new TriggerModule(settings)); modules.add(new ScheduleModule()); modules.add(new InputModule()); - modules.add(new WatcherActionModule()); modules.add(new HistoryModule()); modules.add(new ExecutionModule()); } @@ -295,6 +360,4 @@ public class Watcher implements ActionPlugin, ScriptPlugin { " that any future history indices after 6 months with the pattern " + "[.watcher-history-YYYY.MM.dd] are allowed to be created", value); } - - } diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionFactory.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionFactory.java index 102f4aadd54..84213c267c8 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionFactory.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionFactory.java @@ -13,7 +13,7 @@ import java.io.IOException; /** * Parses xcontent to a concrete action of the same type. */ -public abstract class ActionFactory> { +public abstract class ActionFactory { protected final Logger actionLogger; @@ -21,20 +21,8 @@ public abstract class ActionFactory parsers, ConditionRegistry conditionRegistry, TransformRegistry transformRegistry, Clock clock, @@ -60,11 +58,17 @@ public class ActionRegistry { throw new ElasticsearchParseException("could not parse action [{}] for watch [{}]. {}", id, watchId, error); } } else if (token == XContentParser.Token.START_OBJECT && id != null) { - actions.add(ActionWrapper.parse(watchId, id, parser, this, conditionRegistry, transformRegistry, clock, - licenseState, upgradeActionSource)); + actions.add(ActionWrapper.parse(watchId, id, parser, this, clock, licenseState, upgradeActionSource)); } } return actions; } + public TransformRegistry getTransformRegistry() { + return transformRegistry; + } + + public ConditionRegistry getConditionRegistry() { + return conditionRegistry; + } } diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionWrapper.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionWrapper.java index 09a1d2d9653..9ecbbb6838e 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionWrapper.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionWrapper.java @@ -20,13 +20,11 @@ import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.support.clock.Clock; import org.elasticsearch.xpack.watcher.actions.throttler.ActionThrottler; import org.elasticsearch.xpack.watcher.actions.throttler.Throttler; -import org.elasticsearch.xpack.watcher.condition.ConditionRegistry; import org.elasticsearch.xpack.watcher.condition.Condition; import org.elasticsearch.xpack.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.watcher.support.WatcherDateTimeUtils; import org.elasticsearch.xpack.watcher.transform.ExecutableTransform; import org.elasticsearch.xpack.watcher.transform.Transform; -import org.elasticsearch.xpack.watcher.transform.TransformRegistry; import org.elasticsearch.xpack.watcher.watch.Payload; import org.elasticsearch.xpack.watcher.watch.Watch; @@ -195,9 +193,8 @@ public class ActionWrapper implements ToXContent { return builder.endObject(); } - static ActionWrapper parse(String watchId, String actionId, XContentParser parser, - ActionRegistry actionRegistry, ConditionRegistry conditionRegistry, TransformRegistry transformRegistry, - Clock clock, XPackLicenseState licenseState, boolean upgradeActionSource) throws IOException { + static ActionWrapper parse(String watchId, String actionId, XContentParser parser, ActionRegistry actionRegistry, Clock clock, + XPackLicenseState licenseState, boolean upgradeActionSource) throws IOException { assert parser.currentToken() == XContentParser.Token.START_OBJECT; @@ -213,9 +210,9 @@ public class ActionWrapper implements ToXContent { currentFieldName = parser.currentName(); } else { if (ParseFieldMatcher.STRICT.match(currentFieldName, Watch.Field.CONDITION)) { - condition = conditionRegistry.parseExecutable(watchId, parser, upgradeActionSource); + condition = actionRegistry.getConditionRegistry().parseExecutable(watchId, parser, upgradeActionSource); } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Transform.Field.TRANSFORM)) { - transform = transformRegistry.parse(watchId, parser, upgradeActionSource); + transform = actionRegistry.getTransformRegistry().parse(watchId, parser, upgradeActionSource); } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Throttler.Field.THROTTLE_PERIOD)) { throttlePeriod = timeValueMillis(parser.longValue()); } else if (ParseFieldMatcher.STRICT.match(currentFieldName, Throttler.Field.THROTTLE_PERIOD_HUMAN)) { diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/WatcherActionModule.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/WatcherActionModule.java deleted file mode 100644 index af7db9e3612..00000000000 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/WatcherActionModule.java +++ /dev/null @@ -1,57 +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.xpack.watcher.actions; - -import org.elasticsearch.common.inject.AbstractModule; -import org.elasticsearch.common.inject.multibindings.MapBinder; -import org.elasticsearch.xpack.watcher.actions.email.EmailAction; -import org.elasticsearch.xpack.watcher.actions.email.EmailActionFactory; -import org.elasticsearch.xpack.watcher.actions.hipchat.HipChatAction; -import org.elasticsearch.xpack.watcher.actions.hipchat.HipChatActionFactory; -import org.elasticsearch.xpack.watcher.actions.index.IndexAction; -import org.elasticsearch.xpack.watcher.actions.index.IndexActionFactory; -import org.elasticsearch.xpack.watcher.actions.logging.LoggingAction; -import org.elasticsearch.xpack.watcher.actions.logging.LoggingActionFactory; -import org.elasticsearch.xpack.watcher.actions.pagerduty.PagerDutyAction; -import org.elasticsearch.xpack.watcher.actions.pagerduty.PagerDutyActionFactory; -import org.elasticsearch.xpack.watcher.actions.slack.SlackAction; -import org.elasticsearch.xpack.watcher.actions.slack.SlackActionFactory; -import org.elasticsearch.xpack.watcher.actions.webhook.WebhookAction; -import org.elasticsearch.xpack.watcher.actions.webhook.WebhookActionFactory; - -import java.util.HashMap; -import java.util.Map; - -public class WatcherActionModule extends AbstractModule { - - private final Map> parsers = new HashMap<>(); - - public WatcherActionModule() { - registerAction(EmailAction.TYPE, EmailActionFactory.class); - registerAction(WebhookAction.TYPE, WebhookActionFactory.class); - registerAction(IndexAction.TYPE, IndexActionFactory.class); - registerAction(LoggingAction.TYPE, LoggingActionFactory.class); - registerAction(HipChatAction.TYPE, HipChatActionFactory.class); - registerAction(SlackAction.TYPE, SlackActionFactory.class); - registerAction(PagerDutyAction.TYPE, PagerDutyActionFactory.class); - } - - public void registerAction(String type, Class parserType) { - parsers.put(type, parserType); - } - - @Override - protected void configure() { - MapBinder parsersBinder = MapBinder.newMapBinder(binder(), String.class, ActionFactory.class); - for (Map.Entry> entry : parsers.entrySet()) { - bind(entry.getValue()).asEagerSingleton(); - parsersBinder.addBinding(entry.getKey()).to(entry.getValue()); - } - - bind(ActionRegistry.class).asEagerSingleton(); - - } -} diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionFactory.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionFactory.java index b725816f741..c364596948c 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionFactory.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionFactory.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.xpack.watcher.actions.email; -import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentParser; @@ -17,14 +16,13 @@ import org.elasticsearch.xpack.notification.email.attachment.EmailAttachmentsPar import java.io.IOException; -public class EmailActionFactory extends ActionFactory { +public class EmailActionFactory extends ActionFactory { private final EmailService emailService; private final TextTemplateEngine templateEngine; private final HtmlSanitizer htmlSanitizer; private final EmailAttachmentsParser emailAttachmentsParser; - @Inject public EmailActionFactory(Settings settings, EmailService emailService, TextTemplateEngine templateEngine, EmailAttachmentsParser emailAttachmentsParser) { super(Loggers.getLogger(ExecutableEmailAction.class, settings)); @@ -35,18 +33,9 @@ public class EmailActionFactory extends ActionFactory { +public class HipChatActionFactory extends ActionFactory { private final TextTemplateEngine templateEngine; private final HipChatService hipchatService; - @Inject public HipChatActionFactory(Settings settings, TextTemplateEngine templateEngine, HipChatService hipchatService) { super(Loggers.getLogger(ExecutableHipChatAction.class, settings)); this.templateEngine = templateEngine; @@ -30,25 +27,10 @@ public class HipChatActionFactory extends ActionFactory { +public class IndexActionFactory extends ActionFactory { private final WatcherClientProxy client; private final TimeValue defaultTimeout; - @Inject public IndexActionFactory(Settings settings, InternalClient client) { this(settings, new WatcherClientProxy(settings, client)); } @@ -33,17 +31,7 @@ public class IndexActionFactory extends ActionFactory { +public class LoggingActionFactory extends ActionFactory { private final Settings settings; private final TextTemplateEngine templateEngine; - @Inject public LoggingActionFactory(Settings settings, TextTemplateEngine templateEngine) { super(Loggers.getLogger(ExecutableLoggingAction.class, settings)); this.settings = settings; @@ -27,17 +25,9 @@ public class LoggingActionFactory extends ActionFactory { +public class PagerDutyActionFactory extends ActionFactory { private final TextTemplateEngine templateEngine; private final PagerDutyService pagerDutyService; - @Inject public PagerDutyActionFactory(Settings settings, TextTemplateEngine templateEngine, PagerDutyService pagerDutyService) { super(Loggers.getLogger(ExecutableHipChatAction.class, settings)); this.templateEngine = templateEngine; @@ -31,24 +27,9 @@ public class PagerDutyActionFactory extends ActionFactory { +public class SlackActionFactory extends ActionFactory { private final TextTemplateEngine templateEngine; private final SlackService slackService; - @Inject public SlackActionFactory(Settings settings, TextTemplateEngine templateEngine, SlackService slackService) { super(Loggers.getLogger(ExecutableHipChatAction.class, settings)); this.templateEngine = templateEngine; @@ -30,23 +26,9 @@ public class SlackActionFactory extends ActionFactory { +public class WebhookActionFactory extends ActionFactory { private final HttpClient httpClient; private final HttpRequestTemplate.Parser requestTemplateParser; private final TextTemplateEngine templateEngine; - @Inject public WebhookActionFactory(Settings settings, HttpClient httpClient, HttpRequestTemplate.Parser requestTemplateParser, TextTemplateEngine templateEngine) { @@ -33,17 +31,9 @@ public class WebhookActionFactory extends ActionFactory> factories = new HashMap<>(); - - public void registerTransform(String payloadType, Class parserType) { - factories.put(payloadType, parserType); - } - - @Override - protected void configure() { - MapBinder mbinder = MapBinder.newMapBinder(binder(), String.class, TransformFactory.class); - - bind(SearchTransformFactory.class).asEagerSingleton(); - mbinder.addBinding(SearchTransform.TYPE).to(SearchTransformFactory.class); - - bind(ScriptTransformFactory.class).asEagerSingleton(); - mbinder.addBinding(ScriptTransform.TYPE).to(ScriptTransformFactory.class); - - for (Map.Entry> entry : factories.entrySet()) { - bind(entry.getValue()).asEagerSingleton(); - mbinder.addBinding(entry.getKey()).to(entry.getValue()); - } - } -} diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transform/TransformRegistry.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transform/TransformRegistry.java index 6267cac5d69..48e9e3cd73f 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transform/TransformRegistry.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transform/TransformRegistry.java @@ -6,7 +6,6 @@ package org.elasticsearch.xpack.watcher.transform; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.xpack.watcher.transform.chain.ChainTransform; @@ -21,7 +20,6 @@ public class TransformRegistry { private final Map factories; - @Inject public TransformRegistry(Settings settings, Map factories) { Map map = new HashMap<>(factories); map.put(ChainTransform.TYPE, new ChainTransformFactory(settings, this)); @@ -46,7 +44,7 @@ public class TransformRegistry { return transform; } - public ExecutableTransform parse(String watchId, String type, XContentParser parser, + private ExecutableTransform parse(String watchId, String type, XContentParser parser, boolean upgradeTransformSource) throws IOException { TransformFactory factory = factories.get(type); if (factory == null) { diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transform/chain/ChainTransform.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transform/chain/ChainTransform.java index 75a2ab54eaf..c5da27e0934 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transform/chain/ChainTransform.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transform/chain/ChainTransform.java @@ -68,7 +68,7 @@ public class ChainTransform implements Transform { return builder.endArray(); } - public static ChainTransform parse(String watchId, XContentParser parser, TransformRegistry transformRegistry, + static ChainTransform parse(String watchId, XContentParser parser, TransformRegistry transformRegistry, boolean upgradeSource) throws IOException { XContentParser.Token token = parser.currentToken(); if (token != XContentParser.Token.START_ARRAY) { diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformFactory.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformFactory.java index 06eec5a20fb..8e451680c89 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformFactory.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformFactory.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.xpack.watcher.transform.script; -import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentParser; @@ -20,7 +19,6 @@ public class ScriptTransformFactory extends TransformFactory> pluginTypes() { - List> types = new ArrayList<>(); - types.addAll(super.pluginTypes()); - types.add(ErrorActionPlugin.class); - return Collections.unmodifiableList(types); - } - /** * This test makes sure that when an action encounters an error it should * not be subject to throttling. Also, the ack status of the action in the @@ -53,13 +33,16 @@ public class ActionErrorIntegrationTests extends AbstractWatcherIntegrationTestC * fails. */ public void testErrorInAction() throws Exception { + createIndex("foo"); + client().admin().indices().prepareUpdateSettings("foo").setSettings(Settings.builder().put("index.blocks.write", true)).get(); + PutWatchResponse putWatchResponse = watcherClient().preparePutWatch("_id").setSource(watchBuilder() .trigger(schedule(interval("10m"))) // adding an action that throws an error and is associated with a 60 minute throttle period // with such a period, on successful execution we other executions of the watch will be // throttled within the hour... but on failed execution there should be no throttling - .addAction("_action", TimeValue.timeValueMinutes(60), new ErrorAction.Builder())) + .addAction("_action", TimeValue.timeValueMinutes(60), IndexAction.builder("foo", "bar"))) .get(); assertThat(putWatchResponse.isCreated(), is(true)); @@ -104,83 +87,4 @@ public class ActionErrorIntegrationTests extends AbstractWatcherIntegrationTestC XContentSource watch = getWatchResponse.getSource(); watch.getValue("status.actions._action.ack.awaits_successful_execution"); } - - - - public static class ErrorActionPlugin extends Plugin { - - public void onModule(WatcherActionModule module) { - module.registerAction(ErrorAction.TYPE, ErrorAction.Factory.class); - } - } - - public static class ErrorAction implements Action { - - static final String TYPE = "error"; - - @Override - public String type() { - return TYPE; - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject().endObject(); - } - - public static class Result extends Action.Result { - public Result() { - super(TYPE, Status.FAILURE); - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject().endObject(); - } - } - - public static class Executable extends ExecutableAction { - - public Executable(ErrorAction action, Logger logger) { - super(action, logger); - } - - @Override - public Action.Result execute(String actionId, WatchExecutionContext context, Payload payload) throws Exception { - throw new RuntimeException("dummy error"); - } - } - - public static class Factory extends ActionFactory { - - @Inject - public Factory(Settings settings) { - super(Loggers.getLogger(Executable.class, settings)); - } - - @Override - public String type() { - return TYPE; - } - - @Override - public ErrorAction parseAction(String watchId, String actionId, XContentParser parser) throws IOException { - assert parser.currentToken() == XContentParser.Token.START_OBJECT; - assert parser.nextToken() == XContentParser.Token.END_OBJECT; - return new ErrorAction(); - } - - @Override - public Executable createExecutable(ErrorAction action) { - return new Executable(action, actionLogger); - } - } - - public static class Builder implements Action.Builder { - @Override - public ErrorAction build() { - return new ErrorAction(); - } - } - } } diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/hipchat/HipChatActionFactoryTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/hipchat/HipChatActionFactoryTests.java index f2cf006c33e..3308753476d 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/hipchat/HipChatActionFactoryTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/hipchat/HipChatActionFactoryTests.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.watcher.actions.hipchat; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -16,6 +17,8 @@ import org.elasticsearch.xpack.notification.hipchat.HipChatAccount; import org.elasticsearch.xpack.notification.hipchat.HipChatService; import org.junit.Before; +import java.util.Collections; + import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.hipchatAction; import static org.hamcrest.Matchers.is; @@ -43,24 +46,20 @@ public class HipChatActionFactoryTests extends ESTestCase { XContentParser parser = JsonXContent.jsonXContent.createParser(jsonBuilder.bytes()); parser.nextToken(); - HipChatAction parsedAction = factory.parseAction("_w1", "_a1", parser); - assertThat(parsedAction, is(action)); + ExecutableHipChatAction parsedAction = factory.parseExecutable("_w1", "_a1", parser); + assertThat(parsedAction.action(), is(action)); verify(account, times(1)).validateParsedTemplate("_w1", "_a1", action.message); } public void testParseActionUnknownAccount() throws Exception { - when(hipchatService.getAccount("_unknown")).thenReturn(null); - + hipchatService = new HipChatService(Settings.EMPTY, null, new ClusterSettings(Settings.EMPTY, + Collections.singleton(HipChatService.HIPCHAT_ACCOUNT_SETTING))); + factory = new HipChatActionFactory(Settings.EMPTY, mock(TextTemplateEngine.class), hipchatService); HipChatAction action = hipchatAction("_unknown", "_body").build(); XContentBuilder jsonBuilder = jsonBuilder().value(action); XContentParser parser = JsonXContent.jsonXContent.createParser(jsonBuilder.bytes()); parser.nextToken(); - try { - factory.parseAction("_w1", "_a1", parser); - fail("Expected ElasticsearchParseException"); - } catch (ElasticsearchParseException e) { - assertThat(e.getMessage(), is("could not parse [hipchat] action [_w1]. unknown hipchat account [_unknown]")); - } + expectThrows(IllegalArgumentException.class, () -> factory.parseExecutable("_w1", "_a1", parser)); } } diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionFactoryTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionFactoryTests.java index 843a9860b49..9feab67bcc2 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionFactoryTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionFactoryTests.java @@ -5,7 +5,7 @@ */ package org.elasticsearch.xpack.watcher.actions.pagerduty; -import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -16,6 +16,8 @@ import org.elasticsearch.xpack.notification.pagerduty.PagerDutyAccount; import org.elasticsearch.xpack.notification.pagerduty.PagerDutyService; import org.junit.Before; +import java.util.Collections; + import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.triggerPagerDutyAction; import static org.hamcrest.Matchers.is; @@ -43,20 +45,18 @@ public class PagerDutyActionFactoryTests extends ESTestCase { XContentParser parser = JsonXContent.jsonXContent.createParser(jsonBuilder.bytes()); parser.nextToken(); - PagerDutyAction parsedAction = factory.parseAction("_w1", "_a1", parser); + PagerDutyAction parsedAction = PagerDutyAction.parse("_w1", "_a1", parser); assertThat(parsedAction, is(action)); } public void testParseActionUnknownAccount() throws Exception { - try { - when(service.getAccount("_unknown")).thenReturn(null); - - PagerDutyAction action = triggerPagerDutyAction("_unknown", "_body").build(); - XContentBuilder jsonBuilder = jsonBuilder().value(action); - XContentParser parser = JsonXContent.jsonXContent.createParser(jsonBuilder.bytes()); - parser.nextToken(); - factory.parseAction("_w1", "_a1", parser); - fail("Expected ElasticsearchParseException due to unknown account"); - } catch (ElasticsearchParseException e) {} + factory = new PagerDutyActionFactory(Settings.EMPTY, mock(TextTemplateEngine.class), new PagerDutyService(Settings.EMPTY, null, + new ClusterSettings(Settings.EMPTY, Collections.singleton(PagerDutyService.PAGERDUTY_ACCOUNT_SETTING)))); + PagerDutyAction action = triggerPagerDutyAction("_unknown", "_body").build(); + XContentBuilder jsonBuilder = jsonBuilder().value(action); + XContentParser parser = JsonXContent.jsonXContent.createParser(jsonBuilder.bytes()); + parser.nextToken(); + expectThrows(IllegalArgumentException.class, () -> + factory.parseExecutable("_w1", "_a1", parser)); } } diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionFactoryTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionFactoryTests.java index 58aa3c661a8..6a1e6740edc 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionFactoryTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionFactoryTests.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.watcher.actions.slack; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -16,6 +17,8 @@ import org.elasticsearch.xpack.notification.slack.SlackAccount; import org.elasticsearch.xpack.notification.slack.SlackService; import org.junit.Before; +import java.util.Collections; + import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.slackAction; import static org.elasticsearch.xpack.notification.slack.message.SlackMessageTests.createRandomTemplate; @@ -42,22 +45,18 @@ public class SlackActionFactoryTests extends ESTestCase { XContentParser parser = JsonXContent.jsonXContent.createParser(jsonBuilder.bytes()); parser.nextToken(); - SlackAction parsedAction = factory.parseAction("_w1", "_a1", parser); + SlackAction parsedAction = SlackAction.parse("_w1", "_a1", parser); assertThat(parsedAction, is(action)); } public void testParseActionUnknownAccount() throws Exception { - when(service.getAccount("_unknown")).thenReturn(null); - + SlackService service = new SlackService(Settings.EMPTY, null, new ClusterSettings(Settings.EMPTY, + Collections.singleton(SlackService.SLACK_ACCOUNT_SETTING))); + factory = new SlackActionFactory(Settings.EMPTY, mock(TextTemplateEngine.class), service); SlackAction action = slackAction("_unknown", createRandomTemplate()).build(); XContentBuilder jsonBuilder = jsonBuilder().value(action); XContentParser parser = JsonXContent.jsonXContent.createParser(jsonBuilder.bytes()); parser.nextToken(); - try { - factory.parseAction("_w1", "_a1", parser); - fail("Expected ElasticsearchParseException"); - } catch (ElasticsearchParseException e) { - assertThat(e.getMessage(), is("could not parse [slack] action [_w1]. unknown slack account [_unknown]")); - } + expectThrows(IllegalArgumentException.class, () -> factory.parseExecutable("_w1", "_a1", parser)); } } diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchTests.java index 6c070c33725..e21aea320eb 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchTests.java @@ -201,8 +201,7 @@ public class WatchTests extends ESTestCase { BytesReference bytes = XContentFactory.jsonBuilder().value(watch).bytes(); logger.info("{}", bytes.utf8ToString()); - Watch.Parser watchParser = new Watch.Parser(settings, conditionRegistry, triggerService, transformRegistry, actionRegistry, - inputRegistry, null, clock); + Watch.Parser watchParser = new Watch.Parser(settings, triggerService, actionRegistry, inputRegistry, null, clock); Watch parsedWatch = watchParser.parse("_name", includeStatus, bytes); @@ -238,8 +237,7 @@ public class WatchTests extends ESTestCase { .startObject() .startArray("actions").endArray() .endObject(); - Watch.Parser watchParser = new Watch.Parser(settings, conditionRegistry, triggerService, transformRegistry, actionRegistry, - inputRegistry, null, clock); + Watch.Parser watchParser = new Watch.Parser(settings, triggerService, actionRegistry, inputRegistry, null, clock); try { watchParser.parse("failure", false, jsonBuilder.bytes()); fail("This watch should fail to parse as actions is an array"); @@ -265,8 +263,7 @@ public class WatchTests extends ESTestCase { .field(ScheduleTrigger.TYPE, schedule(schedule).build()) .endObject(); builder.endObject(); - Watch.Parser watchParser = new Watch.Parser(settings, conditionRegistry, triggerService, transformRegistry, actionRegistry, - inputRegistry, null, SystemClock.INSTANCE); + Watch.Parser watchParser = new Watch.Parser(settings, triggerService, actionRegistry, inputRegistry, null, SystemClock.INSTANCE); Watch watch = watchParser.parse("failure", false, builder.bytes()); assertThat(watch, notNullValue()); assertThat(watch.trigger(), instanceOf(ScheduleTrigger.class)); @@ -287,8 +284,7 @@ public class WatchTests extends ESTestCase { InputRegistry inputRegistry = registry(SearchInput.TYPE); TransformRegistry transformRegistry = transformRegistry(); ActionRegistry actionRegistry = registry(Collections.emptyList(), conditionRegistry, transformRegistry); - Watch.Parser watchParser = new Watch.Parser(settings, conditionRegistry, triggerService, transformRegistry, actionRegistry, - inputRegistry, null, SystemClock.INSTANCE); + Watch.Parser watchParser = new Watch.Parser(settings, triggerService, actionRegistry, inputRegistry, null, SystemClock.INSTANCE); IndicesQueriesRegistry queryRegistry = new IndicesQueriesRegistry(); QueryParser queryParser1 = MatchAllQueryBuilder::fromXContent;