Skip to content

Commit

Permalink
Scripting: Remove deprecated params.ctx (#36848)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hub-cap committed Dec 21, 2018
1 parent fba7104 commit 7cbf03c
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 105 deletions.
17 changes: 1 addition & 16 deletions server/src/main/java/org/elasticsearch/script/UpdateScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

package org.elasticsearch.script;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -31,17 +29,6 @@ public abstract class UpdateScript {

public static final String[] PARAMETERS = { };

private static final Map<String, String> DEPRECATIONS;
static {
Map<String, String> 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<Factory> CONTEXT = new ScriptContext<>("update", Factory.class);

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

public UpdateScript(Map<String, Object> params, Map<String, Object> ctx) {
Map<String, Object> paramsWithCtx = new HashMap<>(params);
paramsWithCtx.put("ctx", ctx);
this.params = new ParameterMap(paramsWithCtx, DEPRECATIONS);
this.params = params;
this.ctx = ctx;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,26 @@
*/
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;

/**
* A script to determine whether a watch should be run.
*/
public abstract class WatcherConditionScript {
public static final String[] PARAMETERS = {};

private static final Map<String, String> DEPRECATIONS;

static {
Map<String, String> 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);
}
public static final String[] PARAMETERS = {};

private final Map<String, Object> 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<String, Object> ctx;

public WatcherConditionScript(Map<String, Object> params, WatchExecutionContext watcherContext) {
Map<String, Object> paramsWithCtx = new HashMap<>(params);
Map<String, Object> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,27 @@
*/
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;

/**
* A script to transform the results of a watch execution.
*/
public abstract class WatcherTransformScript {
public static final String[] PARAMETERS = {};

private static final Map<String, String> DEPRECATIONS;

static {
Map<String, String> 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);
}
public static final String[] PARAMETERS = {};

private final Map<String, Object> 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<String, Object> ctx;

public WatcherTransformScript(Map<String, Object> params, WatchExecutionContext watcherContext, Payload payload) {
Map<String, Object> paramsWithCtx = new HashMap<>(params);
Map<String, Object> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,14 @@
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;
import org.joda.time.DateTimeZone;
import org.junit.Before;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -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 {

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

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);
WatcherConditionScript watcherScript = new WatcherConditionScript(Collections.emptyMap(), watcherContext) {
@Override
public boolean execute() {
assertThat(getParams().get("ctx"), is(getCtx()));
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].");
}

private static XContentBuilder createConditionContent(String script, String scriptLang, ScriptType scriptType) throws IOException {
XContentBuilder builder = jsonBuilder();
if (scriptType == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -188,25 +183,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");
}
};
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].");
}

static String scriptTypeField(ScriptType type) {
switch (type) {
case INLINE: return "source";
Expand Down

0 comments on commit 7cbf03c

Please sign in to comment.