This is the xpack side of elastic/elasticsearch#24627


Original commit: elastic/x-pack-elasticsearch@4d1c745d74
This commit is contained in:
Ryan Ernst 2017-05-17 14:42:46 -07:00 committed by GitHub
parent 0059180d78
commit f7705eac86
17 changed files with 16 additions and 54 deletions

View File

@ -72,7 +72,7 @@ file:
-------------------------------------------------- --------------------------------------------------
"condition" : { "condition" : {
"script" : { "script" : {
"file" : "my_script" "id" : "my_script"
} }
} }
-------------------------------------------------- --------------------------------------------------
@ -84,7 +84,7 @@ script language and parameters:
-------------------------------------------------- --------------------------------------------------
"condition" : { "condition" : {
"script" : { "script" : {
"file" : "my_script", "id" : "my_script",
"lang" : "javascript", "lang" : "javascript",
"params" : { "params" : {
"result" : true "result" : true

View File

@ -116,7 +116,7 @@ you can then reference it by name in the watch condition.
-------------------------------------------------- --------------------------------------------------
"condition" : { "condition" : {
"script" : { "script" : {
"file" : "threshold_hits", "id" : "threshold_hits",
"params" : { "params" : {
"threshold" : 0 <1> "threshold" : 0 <1>
} }
@ -181,7 +181,7 @@ PUT _xpack/watcher/watch/rss_watch
}, },
"condition" : { "condition" : {
"script" : { "script" : {
"file" : "threshold_hits", "id" : "threshold_hits",
"params" : { "params" : {
"threshold" : 0 "threshold" : 0
} }
@ -204,7 +204,7 @@ PUT _xpack/watcher/watch/rss_watch
} }
-------------------------------------------------- --------------------------------------------------
// CONSOLE // CONSOLE
// TEST[s/"file" : "threshold_hits"/"inline": "return ctx.payload.hits.total > params.threshold"/] // TEST[s/"id" : "threshold_hits"/"inline": "return ctx.payload.hits.total > params.threshold"/]
<1> Replace `<username>@<domainname>` with your email address to receive <1> Replace `<username>@<domainname>` with your email address to receive
notifications. notifications.

View File

@ -458,7 +458,7 @@ references the script file `threshold_hits.painless`:
-------------------------------------------------- --------------------------------------------------
"condition" : { "condition" : {
"script" : { "script" : {
"file" : "threshold_hits", "id" : "threshold_hits",
"params" : { "params" : {
"threshold" : 0 "threshold" : 0
} }

View File

@ -38,11 +38,6 @@ public class LatchScriptEngine implements ScriptEngine {
return NAME; return NAME;
} }
@Override
public String getExtension() {
return NAME;
}
@Override @Override
public Object compile(String scriptName, String scriptSource, Map<String, String> params) { public Object compile(String scriptName, String scriptSource, Map<String, String> params) {
return scriptSource; return scriptSource;

View File

@ -38,11 +38,6 @@ public class MockMustacheScriptEngine extends MockScriptEngine {
return NAME; return NAME;
} }
@Override
public String getExtension() {
return NAME;
}
@Override @Override
public Object compile(String name, String script, Map<String, String> params) { public Object compile(String name, String script, Map<String, String> params) {
if (script.contains("{{") && script.contains("}}")) { if (script.contains("{{") && script.contains("}}")) {

View File

@ -102,9 +102,6 @@ public class TextTemplateTests extends ESTestCase {
case INLINE: case INLINE:
builder.field("inline", template.getTemplate()); builder.field("inline", template.getTemplate());
break; break;
case FILE:
builder.field("file", template.getTemplate());
break;
case STORED: case STORED:
builder.field("stored", template.getTemplate()); builder.field("stored", template.getTemplate());
} }

View File

@ -38,11 +38,6 @@ public class MockPainlessScriptEngine extends MockScriptEngine {
return NAME; return NAME;
} }
@Override
public String getExtension() {
return NAME;
}
@Override @Override
public Object compile(String name, String script, Map<String, String> params) { public Object compile(String name, String script, Map<String, String> params) {
// We always return the script's source as it is // We always return the script's source as it is

View File

@ -92,11 +92,8 @@ public class ScriptConditionTests extends ESTestCase {
ScriptContextRegistry contextRegistry = new ScriptContextRegistry(singleton(new ScriptContext.Plugin("xpack", "watch"))); ScriptContextRegistry contextRegistry = new ScriptContextRegistry(singleton(new ScriptContext.Plugin("xpack", "watch")));
ScriptSettings scriptSettings = new ScriptSettings(registry, contextRegistry); ScriptSettings scriptSettings = new ScriptSettings(registry, contextRegistry);
Settings settings = Settings.builder()
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
.build();
scriptService = new ScriptService(settings, new Environment(settings), null, registry, contextRegistry, scriptSettings); scriptService = new ScriptService(Settings.EMPTY, registry, contextRegistry, scriptSettings);
ClusterState.Builder clusterState = new ClusterState.Builder(new ClusterName("_name")); ClusterState.Builder clusterState = new ClusterState.Builder(new ClusterName("_name"));
clusterState.metaData(MetaData.builder().putCustom(ScriptMetaData.TYPE, new ScriptMetaData.Builder(null).build())); clusterState.metaData(MetaData.builder().putCustom(ScriptMetaData.TYPE, new ScriptMetaData.Builder(null).build()));
ClusterState cs = clusterState.build(); ClusterState cs = clusterState.build();
@ -152,7 +149,7 @@ public class ScriptConditionTests extends ESTestCase {
fail("expected a condition exception trying to parse an invalid condition XContent"); fail("expected a condition exception trying to parse an invalid condition XContent");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertThat(e.getMessage(), assertThat(e.getMessage(),
containsString("must specify either code for an [inline] script or an id for a [stored] script or [file] script")); containsString("must specify either code for an [inline] script or an id for a [stored] script"));
} }
} }
@ -165,10 +162,6 @@ public class ScriptConditionTests extends ESTestCase {
expectedException = ResourceNotFoundException.class; expectedException = ResourceNotFoundException.class;
script = "nonExisting_script"; script = "nonExisting_script";
break; break;
case FILE:
expectedException = IllegalArgumentException.class;
script = "nonExisting_script";
break;
default: default:
expectedException = GeneralScriptException.class; expectedException = GeneralScriptException.class;
script = "foo = = 1"; script = "foo = = 1";
@ -228,9 +221,6 @@ public class ScriptConditionTests extends ESTestCase {
case INLINE: case INLINE:
builder.field("inline", script); builder.field("inline", script);
break; break;
case FILE:
builder.field("file", script);
break;
case STORED: case STORED:
builder.field("stored", script); builder.field("stored", script);
break; break;

View File

@ -24,7 +24,7 @@ public class WatcherSearchTemplateRequestTests extends ESTestCase {
} }
public void testFromXContentWithTemplateCustomLang() throws IOException { public void testFromXContentWithTemplateCustomLang() throws IOException {
String source = "{\"template\":{\"file\":\"custom-script\", \"lang\":\"painful\",\"params\":{\"bar\":\"baz\"}}}"; String source = "{\"template\":{\"inline\":\"custom-script\", \"lang\":\"painful\",\"params\":{\"bar\":\"baz\"}}}";
assertTemplate(source, "custom-script", "painful", singletonMap("bar", "baz")); assertTemplate(source, "custom-script", "painful", singletonMap("bar", "baz"));
} }

View File

@ -248,8 +248,7 @@ public final class WatcherTestUtils {
ScriptEngineRegistry scriptEngineRegistry = ScriptEngineRegistry scriptEngineRegistry =
new ScriptEngineRegistry(Collections.emptyList()); new ScriptEngineRegistry(Collections.emptyList());
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry); ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
return new ScriptService(settings, new Environment(settings), new ResourceWatcherService(settings, tp), return new ScriptService(settings, scriptEngineRegistry, scriptContextRegistry, scriptSettings);
scriptEngineRegistry, scriptContextRegistry, scriptSettings);
} }
public static SearchType getRandomSupportedSearchType() { public static SearchType getRandomSupportedSearchType() {

View File

@ -192,7 +192,7 @@ public class SearchTransformTests extends ESIntegTestCase {
builder.field("search_type", searchType.name()); builder.field("search_type", searchType.name());
} }
if (templateName != null) { if (templateName != null) {
TextTemplate template = new TextTemplate(templateName, null, ScriptType.FILE, null); TextTemplate template = new TextTemplate(templateName, null, ScriptType.INLINE, null);
builder.field("template", template); builder.field("template", template);
} }
@ -228,7 +228,7 @@ public class SearchTransformTests extends ESIntegTestCase {
} }
if (templateName != null) { if (templateName != null) {
assertThat(executable.transform().getRequest().getTemplate(), assertThat(executable.transform().getRequest().getTemplate(),
equalTo(new Script(ScriptType.FILE, "mustache", "template1", Collections.emptyMap()))); equalTo(new Script(ScriptType.INLINE, "mustache", "template1", Collections.emptyMap())));
} }
assertThat(executable.transform().getRequest().getSearchSource().utf8ToString(), equalTo("{\"query\":{\"match_all\":{}}}")); assertThat(executable.transform().getRequest().getSearchSource().utf8ToString(), equalTo("{\"query\":{\"match_all\":{}}}"));
assertThat(executable.transform().getTimeout(), equalTo(readTimeout)); assertThat(executable.transform().getTimeout(), equalTo(readTimeout));

View File

@ -130,7 +130,7 @@ public class TransformIntegrationTests extends AbstractWatcherIntegrationTestCas
if (randomBoolean()) { if (randomBoolean()) {
logger.info("testing script transform with an inline script"); logger.info("testing script transform with an inline script");
script = mockScript("['key3' : ctx.payload.key1 + ctx.payload.key2]"); script = mockScript("['key3' : ctx.payload.key1 + ctx.payload.key2]");
} else if (randomBoolean()) { } else {
logger.info("testing script transform with an indexed script"); logger.info("testing script transform with an indexed script");
assertAcked(client().admin().cluster().preparePutStoredScript() assertAcked(client().admin().cluster().preparePutStoredScript()
.setId("my-script") .setId("my-script")
@ -138,9 +138,6 @@ public class TransformIntegrationTests extends AbstractWatcherIntegrationTestCas
.setContent(new BytesArray("{\"script\" : \"['key3' : ctx.payload.key1 + ctx.payload.key2]\"}"), XContentType.JSON) .setContent(new BytesArray("{\"script\" : \"['key3' : ctx.payload.key1 + ctx.payload.key2]\"}"), XContentType.JSON)
.get()); .get());
script = new Script(ScriptType.STORED, "mockscript", "my-script", Collections.emptyMap()); script = new Script(ScriptType.STORED, "mockscript", "my-script", Collections.emptyMap());
} else {
logger.info("testing script transform with a file script");
script = new Script(ScriptType.FILE, "mockscript", "my-script", Collections.emptyMap());
} }
// put a watch that has watch level transform: // put a watch that has watch level transform:

View File

@ -213,7 +213,6 @@ public class ScriptTransformTests extends ESTestCase {
static String scriptTypeField(ScriptType type) { static String scriptTypeField(ScriptType type) {
switch (type) { switch (type) {
case INLINE: return "inline"; case INLINE: return "inline";
case FILE: return "file";
case STORED: return "stored"; case STORED: return "stored";
default: default:
throw illegalArgument("unsupported script type [{}]", type); throw illegalArgument("unsupported script type [{}]", type);

View File

@ -11,7 +11,6 @@ integTestCluster {
plugin ':x-pack-elasticsearch:plugin' plugin ':x-pack-elasticsearch:plugin'
setting 'xpack.watcher.enabled', 'false' setting 'xpack.watcher.enabled', 'false'
setting 'xpack.monitoring.enabled', 'false' setting 'xpack.monitoring.enabled', 'false'
setting 'path.scripts', "${project.buildDir}/resources/test/templates"
setupCommand 'setupDummyUser', setupCommand 'setupDummyUser',
'bin/x-pack/users', 'useradd', 'test_admin', '-p', 'changeme', '-r', 'superuser' 'bin/x-pack/users', 'useradd', 'test_admin', '-p', 'changeme', '-r', 'superuser'
waitCondition = { node, ant -> waitCondition = { node, ant ->

View File

@ -111,7 +111,7 @@ setup:
"privileges": ["all"], "privileges": ["all"],
"query" : { "query" : {
"template" : { "template" : {
"file" : "query" "id" : "query"
} }
} }
} }

View File

@ -111,7 +111,7 @@ setup:
"privileges": ["all"], "privileges": ["all"],
"query" : { "query" : {
"template" : { "template" : {
"file" : "query" "id" : "query"
} }
} }
} }

View File

@ -41,17 +41,13 @@ public class WatcherTemplateIT extends ESTestCase {
@Before @Before
public void init() throws Exception { public void init() throws Exception {
Settings setting = Settings.builder().put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING, true).build();
Environment environment = Mockito.mock(Environment.class);
ResourceWatcherService resourceWatcherService = Mockito.mock(ResourceWatcherService.class);
ScriptContextRegistry registry = new ScriptContextRegistry(Collections.singletonList(new ScriptContext.Plugin("xpack", "watch"))); ScriptContextRegistry registry = new ScriptContextRegistry(Collections.singletonList(new ScriptContext.Plugin("xpack", "watch")));
ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry( ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(
Collections.singleton(new MustacheScriptEngine()) Collections.singleton(new MustacheScriptEngine())
); );
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, registry); ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, registry);
ScriptService scriptService = new ScriptService(setting, environment, resourceWatcherService, scriptEngineRegistry, ScriptService scriptService = new ScriptService(Settings.EMPTY, scriptEngineRegistry, registry, scriptSettings);
registry, scriptSettings);
engine = new TextTemplateEngine(Settings.EMPTY, scriptService); engine = new TextTemplateEngine(Settings.EMPTY, scriptService);
} }