From 35ed56d1edc4cb10a0145ecbf885718f27da60df Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Wed, 19 Dec 2018 10:10:50 -0600 Subject: [PATCH 1/8] Watcher: Remove deprecated params.ctx When the script contexts were created in 6, the use of params.ctx was deprecated. This commit cleans up that code and ensures that params.ctx is null in both watcher script contexts. Relates: #34059 --- .../condition/WatcherConditionScript.java | 20 ++----------------- .../script/WatcherTransformScript.java | 20 ++----------------- .../condition/ScriptConditionTests.java | 8 +++----- .../script/ScriptTransformTests.java | 4 +--- 4 files changed, 8 insertions(+), 44 deletions(-) diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java index 1148cc6a58eb5..2977a92bb5e41 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java @@ -18,30 +18,14 @@ * A script to determine whether a watch should be run. */ public abstract class WatcherConditionScript { - public static final String[] PARAMETERS = {}; - - private static final Map DEPRECATIONS; - - static { - Map deprecations = new HashMap<>(); - deprecations.put( - "ctx", - "Accessing variable [ctx] via [params.ctx] from within a watcher_condition script " + - "is deprecated in favor of directly accessing [ctx]." - ); - DEPRECATIONS = Collections.unmodifiableMap(deprecations); - } private final Map params; // TODO: ctx should have its members extracted into execute parameters, but it needs to be a member for bwc access in params private final Map ctx; public WatcherConditionScript(Map params, WatchExecutionContext watcherContext) { - Map paramsWithCtx = new HashMap<>(params); - Map ctx = Variables.createCtx(watcherContext, watcherContext.payload()); - paramsWithCtx.put("ctx", ctx); - this.params = new ParameterMap(Collections.unmodifiableMap(paramsWithCtx), DEPRECATIONS); - this.ctx = ctx; + this.params = params; + this.ctx = Variables.createCtx(watcherContext, watcherContext.payload());; } public abstract boolean execute(); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java index 6d84c32578bc0..27aec14f70c30 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java @@ -19,30 +19,14 @@ * A script to transform the results of a watch execution. */ public abstract class WatcherTransformScript { - public static final String[] PARAMETERS = {}; - - private static final Map DEPRECATIONS; - - static { - Map deprecations = new HashMap<>(); - deprecations.put( - "ctx", - "Accessing variable [ctx] via [params.ctx] from within a watcher_transform script " + - "is deprecated in favor of directly accessing [ctx]." - ); - DEPRECATIONS = Collections.unmodifiableMap(deprecations); - } private final Map params; // TODO: ctx should have its members extracted into execute parameters, but it needs to be a member bwc access in params private final Map ctx; public WatcherTransformScript(Map params, WatchExecutionContext watcherContext, Payload payload) { - Map paramsWithCtx = new HashMap<>(params); - Map ctx = Variables.createCtx(watcherContext, payload); - paramsWithCtx.put("ctx", ctx); - this.params = new ParameterMap(Collections.unmodifiableMap(paramsWithCtx), DEPRECATIONS); - this.ctx = ctx; + this.params = params; + this.ctx = Variables.createCtx(watcherContext, payload); } public abstract Object execute(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java index 1787904e98f0a..40a7d053e943f 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java @@ -206,7 +206,7 @@ public void testScriptConditionAccessCtx() throws Exception { assertThat(condition.execute(ctx).met(), is(true)); } - public void testParamsCtxDeprecated() throws Exception { + public void testParamsCtxNull() throws Exception { WatchExecutionContext watcherContext = mock(WatchExecutionContext.class); when(watcherContext.id()).thenReturn(mock(Wid.class)); when(watcherContext.watch()).thenReturn(mock(Watch.class)); @@ -216,13 +216,11 @@ public void testParamsCtxDeprecated() throws Exception { WatcherConditionScript watcherScript = new WatcherConditionScript(Collections.emptyMap(), watcherContext) { @Override public boolean execute() { - assertThat(getParams().get("ctx"), is(getCtx())); + assertNull(getParams().get("ctx")); return true; } }; - watcherScript.execute(); - assertWarnings("Accessing variable [ctx] via [params.ctx] from within a watcher_condition script " + - "is deprecated in favor of directly accessing [ctx]."); + assertTrue(watcherScript.execute()); } private static XContentBuilder createConditionContent(String script, String scriptLang, ScriptType scriptType) throws IOException { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java index 077e168c547ee..1b04868faf10d 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java @@ -202,9 +202,7 @@ public Object execute() { return getParams().get("ctx"); } }; - assertThat(watcherScript.execute(), is(watcherScript.getCtx())); - assertWarnings("Accessing variable [ctx] via [params.ctx] from within a watcher_transform script " + - "is deprecated in favor of directly accessing [ctx]."); + assertNull(watcherScript.execute()); } static String scriptTypeField(ScriptType type) { From 58e223d7ddc4ae9db7c90c2025113fa0f2b17a07 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Wed, 19 Dec 2018 20:14:56 -0600 Subject: [PATCH 2/8] Remove a redundant semicolon --- .../xpack/watcher/condition/WatcherConditionScript.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java index 2977a92bb5e41..90ce9c3da833c 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java @@ -25,7 +25,7 @@ public abstract class WatcherConditionScript { public WatcherConditionScript(Map params, WatchExecutionContext watcherContext) { this.params = params; - this.ctx = Variables.createCtx(watcherContext, watcherContext.payload());; + this.ctx = Variables.createCtx(watcherContext, watcherContext.payload()); } public abstract boolean execute(); From 0671a669bf1435b27ed22eb2e75be1defbcc55d3 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Thu, 20 Dec 2018 08:10:32 -0600 Subject: [PATCH 3/8] Also remove update scripts params.ctx --- .../org/elasticsearch/script/UpdateScript.java | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/script/UpdateScript.java b/server/src/main/java/org/elasticsearch/script/UpdateScript.java index e1eaf14bcb943..24029f607a2f7 100644 --- a/server/src/main/java/org/elasticsearch/script/UpdateScript.java +++ b/server/src/main/java/org/elasticsearch/script/UpdateScript.java @@ -31,17 +31,6 @@ public abstract class UpdateScript { public static final String[] PARAMETERS = { }; - private static final Map DEPRECATIONS; - static { - Map deprecations = new HashMap<>(); - deprecations.put( - "ctx", - "Accessing variable [ctx] via [params.ctx] from within a update script " + - "is deprecated in favor of directly accessing [ctx]." - ); - DEPRECATIONS = Collections.unmodifiableMap(deprecations); - } - /** The context used to compile {@link UpdateScript} factories. */ public static final ScriptContext CONTEXT = new ScriptContext<>("update", Factory.class); @@ -52,9 +41,7 @@ public abstract class UpdateScript { private final Map ctx; public UpdateScript(Map params, Map ctx) { - Map paramsWithCtx = new HashMap<>(params); - paramsWithCtx.put("ctx", ctx); - this.params = new ParameterMap(paramsWithCtx, DEPRECATIONS); + this.params = params; this.ctx = ctx; } From 9b0c5c1578837839d89400acd83367380a5e94ae Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Thu, 20 Dec 2018 08:20:26 -0600 Subject: [PATCH 4/8] precommit --- .../src/main/java/org/elasticsearch/script/UpdateScript.java | 2 -- .../xpack/watcher/condition/WatcherConditionScript.java | 3 --- .../xpack/watcher/transform/script/WatcherTransformScript.java | 3 --- 3 files changed, 8 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/script/UpdateScript.java b/server/src/main/java/org/elasticsearch/script/UpdateScript.java index 24029f607a2f7..9b9e79c7b74ba 100644 --- a/server/src/main/java/org/elasticsearch/script/UpdateScript.java +++ b/server/src/main/java/org/elasticsearch/script/UpdateScript.java @@ -20,8 +20,6 @@ package org.elasticsearch.script; -import java.util.Collections; -import java.util.HashMap; import java.util.Map; /** diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java index 90ce9c3da833c..c51bec10ab05f 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java @@ -5,13 +5,10 @@ */ package org.elasticsearch.xpack.watcher.condition; -import org.elasticsearch.script.ParameterMap; import org.elasticsearch.script.ScriptContext; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.watcher.support.Variables; -import java.util.Collections; -import java.util.HashMap; import java.util.Map; /** diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java index 27aec14f70c30..8be01ce9ff4a3 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java @@ -5,14 +5,11 @@ */ package org.elasticsearch.xpack.watcher.transform.script; -import org.elasticsearch.script.ParameterMap; import org.elasticsearch.script.ScriptContext; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.watch.Payload; import org.elasticsearch.xpack.watcher.support.Variables; -import java.util.Collections; -import java.util.HashMap; import java.util.Map; /** From f07518df1346b0625655a996291450dfe1a861e3 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Thu, 20 Dec 2018 09:49:32 -0600 Subject: [PATCH 5/8] Adding back PARAMETERS, needed by painless --- .../xpack/watcher/condition/WatcherConditionScript.java | 2 ++ .../xpack/watcher/transform/script/WatcherTransformScript.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java index c51bec10ab05f..4e39baf34f699 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java @@ -15,6 +15,8 @@ * A script to determine whether a watch should be run. */ public abstract class WatcherConditionScript { + + public static final String[] PARAMETERS = {}; private final Map params; // TODO: ctx should have its members extracted into execute parameters, but it needs to be a member for bwc access in params diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java index 8be01ce9ff4a3..57ee1e9f35c5d 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java @@ -17,6 +17,8 @@ */ public abstract class WatcherTransformScript { + public static final String[] PARAMETERS = {}; + private final Map params; // TODO: ctx should have its members extracted into execute parameters, but it needs to be a member bwc access in params private final Map ctx; From 4d284324fae591f3fe65ec9f9bec1a4192a89dbf Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Thu, 20 Dec 2018 10:26:11 -0600 Subject: [PATCH 6/8] thanks intellij --- .../xpack/watcher/condition/WatcherConditionScript.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java index 4e39baf34f699..1a5c8718bbd45 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java @@ -15,7 +15,7 @@ * A script to determine whether a watch should be run. */ public abstract class WatcherConditionScript { - + public static final String[] PARAMETERS = {}; private final Map params; From 4d375483ee461b6a334a203b584941a7b774e682 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Thu, 20 Dec 2018 14:40:08 -0600 Subject: [PATCH 7/8] Remove tests that are not relevant anymore --- .../watcher/condition/ScriptConditionTests.java | 17 ----------------- .../transform/script/ScriptTransformTests.java | 17 ----------------- 2 files changed, 34 deletions(-) diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java index 40a7d053e943f..57b936e07870f 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java @@ -206,23 +206,6 @@ public void testScriptConditionAccessCtx() throws Exception { assertThat(condition.execute(ctx).met(), is(true)); } - public void testParamsCtxNull() throws Exception { - WatchExecutionContext watcherContext = mock(WatchExecutionContext.class); - when(watcherContext.id()).thenReturn(mock(Wid.class)); - when(watcherContext.watch()).thenReturn(mock(Watch.class)); - when(watcherContext.triggerEvent()).thenReturn(mock(TriggerEvent.class)); - DateTime now = DateTime.now(DateTimeZone.UTC); - when(watcherContext.executionTime()).thenReturn(now); - WatcherConditionScript watcherScript = new WatcherConditionScript(Collections.emptyMap(), watcherContext) { - @Override - public boolean execute() { - assertNull(getParams().get("ctx")); - return true; - } - }; - assertTrue(watcherScript.execute()); - } - private static XContentBuilder createConditionContent(String script, String scriptLang, ScriptType scriptType) throws IOException { XContentBuilder builder = jsonBuilder(); if (scriptType == null) { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java index 1b04868faf10d..3f664f858c8ee 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java @@ -188,23 +188,6 @@ public void testScriptConditionParserBadLang() throws Exception { assertThat(e.getMessage(), containsString("script_lang not supported [not_a_valid_lang]")); } - public void testParamsCtxDeprecated() throws Exception { - WatchExecutionContext watcherContext = mock(WatchExecutionContext.class); - when(watcherContext.id()).thenReturn(mock(Wid.class)); - when(watcherContext.watch()).thenReturn(mock(Watch.class)); - when(watcherContext.triggerEvent()).thenReturn(mock(TriggerEvent.class)); - DateTime now = DateTime.now(DateTimeZone.UTC); - when(watcherContext.executionTime()).thenReturn(now); - Payload payload = mock(Payload.class); - WatcherTransformScript watcherScript = new WatcherTransformScript(Collections.emptyMap(), watcherContext, payload) { - @Override - public Object execute() { - return getParams().get("ctx"); - } - }; - assertNull(watcherScript.execute()); - } - static String scriptTypeField(ScriptType type) { switch (type) { case INLINE: return "source"; From f8e629a89a3cc06606e4b391aead4cc906d13d97 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Thu, 20 Dec 2018 19:14:48 -0600 Subject: [PATCH 8/8] doh, precommit --- .../xpack/watcher/condition/ScriptConditionTests.java | 6 ------ .../watcher/transform/script/ScriptTransformTests.java | 5 ----- 2 files changed, 11 deletions(-) diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java index 57b936e07870f..72ab01009bb99 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java @@ -30,10 +30,7 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.condition.ExecutableCondition; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; -import org.elasticsearch.xpack.core.watcher.execution.Wid; -import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent; import org.elasticsearch.xpack.core.watcher.watch.Payload; -import org.elasticsearch.xpack.core.watcher.watch.Watch; import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase; import org.elasticsearch.xpack.watcher.test.WatcherMockScriptPlugin; import org.joda.time.DateTime; @@ -41,7 +38,6 @@ import org.junit.Before; import java.io.IOException; -import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -54,8 +50,6 @@ import static org.elasticsearch.xpack.watcher.test.WatcherTestUtils.mockExecutionContext; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; public class ScriptConditionTests extends ESTestCase { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java index 3f664f858c8ee..3e2ec780fa4c0 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java @@ -16,14 +16,9 @@ import org.elasticsearch.script.ScriptType; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; -import org.elasticsearch.xpack.core.watcher.execution.Wid; import org.elasticsearch.xpack.core.watcher.transform.Transform; -import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent; import org.elasticsearch.xpack.core.watcher.watch.Payload; -import org.elasticsearch.xpack.core.watcher.watch.Watch; import org.elasticsearch.xpack.watcher.Watcher; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; import java.util.Collections; import java.util.HashMap;