Skip to content

Commit 7cbf03c

Browse files
authored
Scripting: Remove deprecated params.ctx (#36848)
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
1 parent fba7104 commit 7cbf03c

File tree

5 files changed

+7
-105
lines changed

5 files changed

+7
-105
lines changed

server/src/main/java/org/elasticsearch/script/UpdateScript.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
package org.elasticsearch.script;
2222

23-
import java.util.Collections;
24-
import java.util.HashMap;
2523
import java.util.Map;
2624

2725
/**
@@ -31,17 +29,6 @@ public abstract class UpdateScript {
3129

3230
public static final String[] PARAMETERS = { };
3331

34-
private static final Map<String, String> DEPRECATIONS;
35-
static {
36-
Map<String, String> deprecations = new HashMap<>();
37-
deprecations.put(
38-
"ctx",
39-
"Accessing variable [ctx] via [params.ctx] from within a update script " +
40-
"is deprecated in favor of directly accessing [ctx]."
41-
);
42-
DEPRECATIONS = Collections.unmodifiableMap(deprecations);
43-
}
44-
4532
/** The context used to compile {@link UpdateScript} factories. */
4633
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("update", Factory.class);
4734

@@ -52,9 +39,7 @@ public abstract class UpdateScript {
5239
private final Map<String, Object> ctx;
5340

5441
public UpdateScript(Map<String, Object> params, Map<String, Object> ctx) {
55-
Map<String, Object> paramsWithCtx = new HashMap<>(params);
56-
paramsWithCtx.put("ctx", ctx);
57-
this.params = new ParameterMap(paramsWithCtx, DEPRECATIONS);
42+
this.params = params;
5843
this.ctx = ctx;
5944
}
6045

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,26 @@
55
*/
66
package org.elasticsearch.xpack.watcher.condition;
77

8-
import org.elasticsearch.script.ParameterMap;
98
import org.elasticsearch.script.ScriptContext;
109
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
1110
import org.elasticsearch.xpack.watcher.support.Variables;
1211

13-
import java.util.Collections;
14-
import java.util.HashMap;
1512
import java.util.Map;
1613

1714
/**
1815
* A script to determine whether a watch should be run.
1916
*/
2017
public abstract class WatcherConditionScript {
21-
public static final String[] PARAMETERS = {};
22-
23-
private static final Map<String, String> DEPRECATIONS;
2418

25-
static {
26-
Map<String, String> deprecations = new HashMap<>();
27-
deprecations.put(
28-
"ctx",
29-
"Accessing variable [ctx] via [params.ctx] from within a watcher_condition script " +
30-
"is deprecated in favor of directly accessing [ctx]."
31-
);
32-
DEPRECATIONS = Collections.unmodifiableMap(deprecations);
33-
}
19+
public static final String[] PARAMETERS = {};
3420

3521
private final Map<String, Object> params;
3622
// TODO: ctx should have its members extracted into execute parameters, but it needs to be a member for bwc access in params
3723
private final Map<String, Object> ctx;
3824

3925
public WatcherConditionScript(Map<String, Object> params, WatchExecutionContext watcherContext) {
40-
Map<String, Object> paramsWithCtx = new HashMap<>(params);
41-
Map<String, Object> ctx = Variables.createCtx(watcherContext, watcherContext.payload());
42-
paramsWithCtx.put("ctx", ctx);
43-
this.params = new ParameterMap(Collections.unmodifiableMap(paramsWithCtx), DEPRECATIONS);
44-
this.ctx = ctx;
26+
this.params = params;
27+
this.ctx = Variables.createCtx(watcherContext, watcherContext.payload());
4528
}
4629

4730
public abstract boolean execute();

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,27 @@
55
*/
66
package org.elasticsearch.xpack.watcher.transform.script;
77

8-
import org.elasticsearch.script.ParameterMap;
98
import org.elasticsearch.script.ScriptContext;
109
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
1110
import org.elasticsearch.xpack.core.watcher.watch.Payload;
1211
import org.elasticsearch.xpack.watcher.support.Variables;
1312

14-
import java.util.Collections;
15-
import java.util.HashMap;
1613
import java.util.Map;
1714

1815
/**
1916
* A script to transform the results of a watch execution.
2017
*/
2118
public abstract class WatcherTransformScript {
22-
public static final String[] PARAMETERS = {};
23-
24-
private static final Map<String, String> DEPRECATIONS;
2519

26-
static {
27-
Map<String, String> deprecations = new HashMap<>();
28-
deprecations.put(
29-
"ctx",
30-
"Accessing variable [ctx] via [params.ctx] from within a watcher_transform script " +
31-
"is deprecated in favor of directly accessing [ctx]."
32-
);
33-
DEPRECATIONS = Collections.unmodifiableMap(deprecations);
34-
}
20+
public static final String[] PARAMETERS = {};
3521

3622
private final Map<String, Object> params;
3723
// TODO: ctx should have its members extracted into execute parameters, but it needs to be a member bwc access in params
3824
private final Map<String, Object> ctx;
3925

4026
public WatcherTransformScript(Map<String, Object> params, WatchExecutionContext watcherContext, Payload payload) {
41-
Map<String, Object> paramsWithCtx = new HashMap<>(params);
42-
Map<String, Object> ctx = Variables.createCtx(watcherContext, payload);
43-
paramsWithCtx.put("ctx", ctx);
44-
this.params = new ParameterMap(Collections.unmodifiableMap(paramsWithCtx), DEPRECATIONS);
45-
this.ctx = ctx;
27+
this.params = params;
28+
this.ctx = Variables.createCtx(watcherContext, payload);
4629
}
4730

4831
public abstract Object execute();

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,14 @@
3030
import org.elasticsearch.test.ESTestCase;
3131
import org.elasticsearch.xpack.core.watcher.condition.ExecutableCondition;
3232
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
33-
import org.elasticsearch.xpack.core.watcher.execution.Wid;
34-
import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent;
3533
import org.elasticsearch.xpack.core.watcher.watch.Payload;
36-
import org.elasticsearch.xpack.core.watcher.watch.Watch;
3734
import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase;
3835
import org.elasticsearch.xpack.watcher.test.WatcherMockScriptPlugin;
3936
import org.joda.time.DateTime;
4037
import org.joda.time.DateTimeZone;
4138
import org.junit.Before;
4239

4340
import java.io.IOException;
44-
import java.util.Collections;
4541
import java.util.Date;
4642
import java.util.HashMap;
4743
import java.util.Map;
@@ -54,8 +50,6 @@
5450
import static org.elasticsearch.xpack.watcher.test.WatcherTestUtils.mockExecutionContext;
5551
import static org.hamcrest.Matchers.containsString;
5652
import static org.hamcrest.Matchers.is;
57-
import static org.mockito.Mockito.mock;
58-
import static org.mockito.Mockito.when;
5953

6054
public class ScriptConditionTests extends ESTestCase {
6155

@@ -206,25 +200,6 @@ public void testScriptConditionAccessCtx() throws Exception {
206200
assertThat(condition.execute(ctx).met(), is(true));
207201
}
208202

209-
public void testParamsCtxDeprecated() throws Exception {
210-
WatchExecutionContext watcherContext = mock(WatchExecutionContext.class);
211-
when(watcherContext.id()).thenReturn(mock(Wid.class));
212-
when(watcherContext.watch()).thenReturn(mock(Watch.class));
213-
when(watcherContext.triggerEvent()).thenReturn(mock(TriggerEvent.class));
214-
DateTime now = DateTime.now(DateTimeZone.UTC);
215-
when(watcherContext.executionTime()).thenReturn(now);
216-
WatcherConditionScript watcherScript = new WatcherConditionScript(Collections.emptyMap(), watcherContext) {
217-
@Override
218-
public boolean execute() {
219-
assertThat(getParams().get("ctx"), is(getCtx()));
220-
return true;
221-
}
222-
};
223-
watcherScript.execute();
224-
assertWarnings("Accessing variable [ctx] via [params.ctx] from within a watcher_condition script " +
225-
"is deprecated in favor of directly accessing [ctx].");
226-
}
227-
228203
private static XContentBuilder createConditionContent(String script, String scriptLang, ScriptType scriptType) throws IOException {
229204
XContentBuilder builder = jsonBuilder();
230205
if (scriptType == null) {

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@
1616
import org.elasticsearch.script.ScriptType;
1717
import org.elasticsearch.test.ESTestCase;
1818
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
19-
import org.elasticsearch.xpack.core.watcher.execution.Wid;
2019
import org.elasticsearch.xpack.core.watcher.transform.Transform;
21-
import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent;
2220
import org.elasticsearch.xpack.core.watcher.watch.Payload;
23-
import org.elasticsearch.xpack.core.watcher.watch.Watch;
2421
import org.elasticsearch.xpack.watcher.Watcher;
25-
import org.joda.time.DateTime;
26-
import org.joda.time.DateTimeZone;
2722

2823
import java.util.Collections;
2924
import java.util.HashMap;
@@ -188,25 +183,6 @@ public void testScriptConditionParserBadLang() throws Exception {
188183
assertThat(e.getMessage(), containsString("script_lang not supported [not_a_valid_lang]"));
189184
}
190185

191-
public void testParamsCtxDeprecated() throws Exception {
192-
WatchExecutionContext watcherContext = mock(WatchExecutionContext.class);
193-
when(watcherContext.id()).thenReturn(mock(Wid.class));
194-
when(watcherContext.watch()).thenReturn(mock(Watch.class));
195-
when(watcherContext.triggerEvent()).thenReturn(mock(TriggerEvent.class));
196-
DateTime now = DateTime.now(DateTimeZone.UTC);
197-
when(watcherContext.executionTime()).thenReturn(now);
198-
Payload payload = mock(Payload.class);
199-
WatcherTransformScript watcherScript = new WatcherTransformScript(Collections.emptyMap(), watcherContext, payload) {
200-
@Override
201-
public Object execute() {
202-
return getParams().get("ctx");
203-
}
204-
};
205-
assertThat(watcherScript.execute(), is(watcherScript.getCtx()));
206-
assertWarnings("Accessing variable [ctx] via [params.ctx] from within a watcher_transform script " +
207-
"is deprecated in favor of directly accessing [ctx].");
208-
}
209-
210186
static String scriptTypeField(ScriptType type) {
211187
switch (type) {
212188
case INLINE: return "source";

0 commit comments

Comments
 (0)