Remove groovy scripting language (elastic/elasticsearch#4162)

This is the xplugins side of elastic/elasticsearchelastic/elasticsearch#21607

Original commit: elastic/x-pack-elasticsearch@125843e814
This commit is contained in:
Ryan Ernst 2016-11-22 22:45:15 -08:00 committed by GitHub
parent ecbdd92c8c
commit 1dc839bd98
43 changed files with 86 additions and 818 deletions

View File

@ -216,13 +216,11 @@ public class Watcher implements ActionPlugin, ScriptPlugin {
}
final Map<String, ConditionFactory> 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));
parsers.put(ArrayCompareCondition.TYPE, (c, id, p, upgrade) -> ArrayCompareCondition.parse(c, id, p));
parsers.put(CompareCondition.TYPE, (c, id, p, upgrade) -> CompareCondition.parse(c, id, p));
String defaultLegacyScriptLanguage = ScriptSettings.getLegacyDefaultLang(settings);
parsers.put(ScriptCondition.TYPE, (c, id, p, upgrade) -> ScriptCondition.parse(scriptService, id, p, upgrade,
defaultLegacyScriptLanguage));
parsers.put(AlwaysCondition.TYPE, (c, id, p) -> AlwaysCondition.parse(id, p));
parsers.put(NeverCondition.TYPE, (c, id, p) -> NeverCondition.parse(id, p));
parsers.put(ArrayCompareCondition.TYPE, (c, id, p) -> ArrayCompareCondition.parse(c, id, p));
parsers.put(CompareCondition.TYPE, (c, id, p) -> CompareCondition.parse(c, id, p));
parsers.put(ScriptCondition.TYPE, (c, id, p) -> ScriptCondition.parse(scriptService, id, p));
final ConditionRegistry conditionRegistry = new ConditionRegistry(Collections.unmodifiableMap(parsers), clock);
final Map<String, TransformFactory> transformFactories = new HashMap<>();

View File

@ -41,7 +41,7 @@ public class ActionRegistry {
return parsers.get(type);
}
public List<ActionWrapper> parseActions(String watchId, XContentParser parser, boolean upgradeActionSource) throws IOException {
public List<ActionWrapper> parseActions(String watchId, XContentParser parser) throws IOException {
if (parser.currentToken() != XContentParser.Token.START_OBJECT) {
throw new ElasticsearchParseException("could not parse actions for watch [{}]. expected an object but found [{}] instead",
watchId, parser.currentToken());
@ -58,7 +58,7 @@ public class ActionRegistry {
watchId);
}
} else if (token == XContentParser.Token.START_OBJECT && id != null) {
actions.add(ActionWrapper.parse(watchId, id, parser, this, clock, licenseState, upgradeActionSource));
actions.add(ActionWrapper.parse(watchId, id, parser, this, clock, licenseState));
}
}
return actions;

View File

@ -194,7 +194,7 @@ public class ActionWrapper implements ToXContent {
}
static ActionWrapper parse(String watchId, String actionId, XContentParser parser, ActionRegistry actionRegistry, Clock clock,
XPackLicenseState licenseState, boolean upgradeActionSource) throws IOException {
XPackLicenseState licenseState) throws IOException {
assert parser.currentToken() == XContentParser.Token.START_OBJECT;
@ -210,9 +210,9 @@ public class ActionWrapper implements ToXContent {
currentFieldName = parser.currentName();
} else {
if (ParseFieldMatcher.STRICT.match(currentFieldName, Watch.Field.CONDITION)) {
condition = actionRegistry.getConditionRegistry().parseExecutable(watchId, parser, upgradeActionSource);
condition = actionRegistry.getConditionRegistry().parseExecutable(watchId, parser);
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Transform.Field.TRANSFORM)) {
transform = actionRegistry.getTransformRegistry().parse(watchId, parser, upgradeActionSource);
transform = actionRegistry.getTransformRegistry().parse(watchId, parser);
} 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)) {

View File

@ -17,11 +17,9 @@ public interface ConditionFactory {
/**
* Parses the given xcontent and creates a concrete condition
* @param watchId The id of the watch
* @param watchId The id of the watch
* @param parser The parsing that contains the condition content
* @param upgradeConditionSource Whether to upgrade the source related to condition if in legacy format
* Note: depending on the version, only conditions implementations that have a
*/
Condition parse(Clock clock, String watchId, XContentParser parser, boolean upgradeConditionSource) throws IOException;
Condition parse(Clock clock, String watchId, XContentParser parser) throws IOException;
}

View File

@ -34,11 +34,8 @@ public class ConditionRegistry {
*
* @param watchId The id of the watch
* @param parser The parsing that contains the condition content
* @param upgradeConditionSource Whether to upgrade the source related to condition if in legacy format
* Note: depending on the version, only conditions implementations that have a
* known legacy format will support this option, otherwise this is a noop.
*/
public Condition parseExecutable(String watchId, XContentParser parser, boolean upgradeConditionSource) throws IOException {
public Condition parseExecutable(String watchId, XContentParser parser) throws IOException {
Condition condition = null;
ConditionFactory factory;
@ -56,7 +53,7 @@ public class ConditionRegistry {
throw new ElasticsearchParseException("could not parse condition for watch [{}]. unknown condition type [{}]",
watchId, type);
}
condition = factory.parse(clock, watchId, parser, upgradeConditionSource);
condition = factory.parse(clock, watchId, parser);
}
}
if (condition == null) {

View File

@ -53,15 +53,9 @@ public final class ScriptCondition extends Condition {
return script;
}
public static ScriptCondition parse(ScriptService scriptService, String watchId, XContentParser parser, boolean upgradeConditionSource,
String defaultLegacyScriptLanguage) throws IOException {
public static ScriptCondition parse(ScriptService scriptService, String watchId, XContentParser parser) throws IOException {
try {
Script script;
if (upgradeConditionSource) {
script = Script.parse(parser, ParseFieldMatcher.STRICT, defaultLegacyScriptLanguage);
} else {
script = Script.parse(parser, ParseFieldMatcher.STRICT);
}
Script script = Script.parse(parser, ParseFieldMatcher.STRICT);
return new ScriptCondition(script, scriptService);
} catch (ElasticsearchParseException pe) {
throw new ElasticsearchParseException("could not parse [{}] condition for watch [{}]. failed to parse script", pe, TYPE,

View File

@ -31,19 +31,16 @@ public abstract class InputFactory<I extends Input, R extends Input.Result, E ex
*
* @param watchId The id of the watch
* @param parser The parser containing the input content of the watch
* @param upgradeInputSource Whether to upgrade the source related to the inpit if that source is in legacy format
* Note: depending on the version, only input implementations that have a known legacy
* format will support this option, otherwise this is a noop.
*/
public abstract I parseInput(String watchId, XContentParser parser, boolean upgradeInputSource) throws IOException;
public abstract I parseInput(String watchId, XContentParser parser) throws IOException;
/**
* Creates an executable input out of the given input.
*/
public abstract E createExecutable(I input);
public E parseExecutable(String watchId, XContentParser parser, boolean upgradeInputSource) throws IOException {
I input = parseInput(watchId, parser, upgradeInputSource);
public E parseExecutable(String watchId, XContentParser parser) throws IOException {
I input = parseInput(watchId, parser);
return createExecutable(input);
}
}

View File

@ -26,24 +26,13 @@ public class InputRegistry {
this.factories = Collections.unmodifiableMap(map);
}
/**
* Reads the contents of parser to create the correct Input
*
* @param parser The parser containing the input definition
* @return A new input instance from the parser
*/
public ExecutableInput parse(String watchId, XContentParser parser) throws IOException {
return parse(watchId, parser, false);
}
/**
* Reads the contents of parser to create the correct Input
*
* @param parser The parser containing the input definition
* @param upgradeInputSource Whether to upgrade the source related to input if in legacy format.
* @return A new input instance from the parser
*/
public ExecutableInput parse(String watchId, XContentParser parser, boolean upgradeInputSource) throws IOException {
public ExecutableInput parse(String watchId, XContentParser parser) throws IOException {
String type = null;
if (parser.currentToken() != XContentParser.Token.START_OBJECT) {
@ -64,7 +53,7 @@ public class InputRegistry {
if (factory == null) {
throw new ElasticsearchParseException("could not parse input for watch [{}]. unknown input type [{}]", watchId, type);
}
input = factory.parseExecutable(watchId, parser, upgradeInputSource);
input = factory.parseExecutable(watchId, parser);
} else {
throw new ElasticsearchParseException("could not parse input for watch [{}]. expected an object representing input [{}], " +
"but found [{}] instead", watchId, type, token);

View File

@ -33,7 +33,7 @@ public class ChainInputFactory extends InputFactory<ChainInput, ChainInput.Resul
}
@Override
public ChainInput parseInput(String watchId, XContentParser parser, boolean upgradeInputSource) throws IOException {
public ChainInput parseInput(String watchId, XContentParser parser) throws IOException {
return ChainInput.parse(watchId, parser, inputRegistry);
}

View File

@ -35,7 +35,7 @@ public final class HttpInputFactory extends InputFactory<HttpInput, HttpInput.Re
}
@Override
public HttpInput parseInput(String watchId, XContentParser parser, boolean upgradeInputSource) throws IOException {
public HttpInput parseInput(String watchId, XContentParser parser) throws IOException {
return HttpInput.parse(watchId, parser, requestTemplateParser);
}

View File

@ -24,7 +24,7 @@ public class NoneInputFactory extends InputFactory<NoneInput, NoneInput.Result,
}
@Override
public NoneInput parseInput(String watchId, XContentParser parser, boolean upgradeInputSource) throws IOException {
public NoneInput parseInput(String watchId, XContentParser parser) throws IOException {
return NoneInput.parse(watchId, parser);
}

View File

@ -109,8 +109,6 @@ public class SearchInput implements Input {
}
public static SearchInput parse(Logger inputLogger, String watchId, XContentParser parser,
boolean upgradeInputSource,
String defaultLegacyScriptLanguage,
ParseFieldMatcher parseFieldMatcher,
SearchRequestParsers searchRequestParsers) throws IOException {
WatcherSearchTemplateRequest request = null;
@ -126,7 +124,7 @@ public class SearchInput implements Input {
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.REQUEST)) {
try {
request = WatcherSearchTemplateRequest.fromXContent(inputLogger, parser, ExecutableSearchInput.DEFAULT_SEARCH_TYPE,
upgradeInputSource, defaultLegacyScriptLanguage, parseFieldMatcher, searchRequestParsers);
parseFieldMatcher, searchRequestParsers);
} catch (ElasticsearchParseException srpe) {
throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. failed to parse [{}]", srpe, TYPE,
watchId, currentFieldName);

View File

@ -52,9 +52,8 @@ public class SearchInputFactory extends InputFactory<SearchInput, SearchInput.Re
}
@Override
public SearchInput parseInput(String watchId, XContentParser parser, boolean upgradeInputSource) throws IOException {
String defaultLegacyScriptLanguage = ScriptSettings.getLegacyDefaultLang(settings);
return SearchInput.parse(inputLogger, watchId, parser, upgradeInputSource, defaultLegacyScriptLanguage,
public SearchInput parseInput(String watchId, XContentParser parser) throws IOException {
return SearchInput.parse(inputLogger, watchId, parser,
parseFieldMatcher, searchRequestParsers);
}

View File

@ -24,7 +24,7 @@ public class SimpleInputFactory extends InputFactory<SimpleInput, SimpleInput.Re
}
@Override
public SimpleInput parseInput(String watchId, XContentParser parser, boolean upgradeInputSource) throws IOException {
public SimpleInput parseInput(String watchId, XContentParser parser) throws IOException {
return SimpleInput.parse(watchId, parser);
}

View File

@ -167,8 +167,6 @@ public class WatcherSearchTemplateRequest implements ToXContent {
*/
public static WatcherSearchTemplateRequest fromXContent(Logger logger, XContentParser parser,
SearchType searchType,
boolean upgradeSearchSource,
String defaultLegacyScriptLanguage,
ParseFieldMatcher parseFieldMatcher,
SearchRequestParsers searchRequestParsers) throws IOException {
List<String> indices = new ArrayList<>();
@ -210,20 +208,6 @@ public class WatcherSearchTemplateRequest implements ToXContent {
try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) {
builder.copyCurrentStructure(parser);
searchSource = builder.bytes();
if (upgradeSearchSource) {
XContentParser searchSourceParser = XContentHelper.createParser(searchSource);
QueryParseContext context = new QueryParseContext(defaultLegacyScriptLanguage,
searchRequestParsers.queryParsers, searchSourceParser, parseFieldMatcher);
try (XContentBuilder upgradeBuilder = XContentBuilder.builder(parser.contentType().xContent())) {
SearchSourceBuilder sourceBuilder = SearchSourceBuilder.fromXContent(context,
searchRequestParsers.aggParsers, searchRequestParsers.suggesters,
searchRequestParsers.searchExtParsers);
upgradeBuilder.value(sourceBuilder);
searchSource = upgradeBuilder.bytes();
} catch (Exception e) {
logger.warn("Unable to upgrade search source: [" + searchSource.utf8ToString() + "]", e);
}
}
}
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, INDICES_OPTIONS_FIELD)) {
boolean expandOpen = DEFAULT_INDICES_OPTIONS.expandWildcardsOpen();

View File

@ -28,19 +28,16 @@ public abstract class TransformFactory<T extends Transform, R extends Transform.
*
* @param watchId The id of the watch
* @param parser The parsing that contains the condition content
* @param upgradeTransformSource Whether to upgrade the source related to transform if in legacy format
* Note: depending on the version, only transform implementations that have a
* known legacy format will support this option, otherwise this is a noop.
*/
public abstract T parseTransform(String watchId, XContentParser parser, boolean upgradeTransformSource) throws IOException;
public abstract T parseTransform(String watchId, XContentParser parser) throws IOException;
/**
* Creates an executable transform out of the given transform.
*/
public abstract E createExecutable(T transform);
public E parseExecutable(String watchId, XContentParser parser, boolean upgradeTransformSource) throws IOException {
T transform = parseTransform(watchId, parser, upgradeTransformSource);
public E parseExecutable(String watchId, XContentParser parser) throws IOException {
T transform = parseTransform(watchId, parser);
return createExecutable(transform);
}
}

View File

@ -30,7 +30,7 @@ public class TransformRegistry {
return factories.get(type);
}
public ExecutableTransform parse(String watchId, XContentParser parser, boolean upgradeTransformSource) throws IOException {
public ExecutableTransform parse(String watchId, XContentParser parser) throws IOException {
String type = null;
XContentParser.Token token;
ExecutableTransform transform = null;
@ -38,26 +38,25 @@ public class TransformRegistry {
if (token == XContentParser.Token.FIELD_NAME) {
type = parser.currentName();
} else if (type != null) {
transform = parse(watchId, type, parser, upgradeTransformSource);
transform = parse(watchId, type, parser);
}
}
return transform;
}
private ExecutableTransform parse(String watchId, String type, XContentParser parser,
boolean upgradeTransformSource) throws IOException {
private ExecutableTransform parse(String watchId, String type, XContentParser parser) throws IOException {
TransformFactory factory = factories.get(type);
if (factory == null) {
throw new ElasticsearchParseException("could not parse transform for watch [{}], unknown transform type [{}]", watchId, type);
}
return factory.parseExecutable(watchId, parser, upgradeTransformSource);
return factory.parseExecutable(watchId, parser);
}
public Transform parseTransform(String watchId, String type, XContentParser parser, boolean upgradeSource) throws IOException {
public Transform parseTransform(String watchId, String type, XContentParser parser) throws IOException {
TransformFactory factory = factories.get(type);
if (factory == null) {
throw new ElasticsearchParseException("could not parse transform for watch [{}], unknown transform type [{}]", watchId, type);
}
return factory.parseTransform(watchId, parser, upgradeSource);
return factory.parseTransform(watchId, parser);
}
}

View File

@ -68,8 +68,7 @@ public class ChainTransform implements Transform {
return builder.endArray();
}
static ChainTransform parse(String watchId, XContentParser parser, TransformRegistry transformRegistry,
boolean upgradeSource) throws IOException {
static ChainTransform parse(String watchId, XContentParser parser, TransformRegistry transformRegistry) throws IOException {
XContentParser.Token token = parser.currentToken();
if (token != XContentParser.Token.START_ARRAY) {
throw new ElasticsearchParseException("could not parse [{}] transform for watch [{}]. expected an array of transform objects," +
@ -88,7 +87,7 @@ public class ChainTransform implements Transform {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else {
transforms.add(transformRegistry.parseTransform(watchId, currentFieldName, parser, upgradeSource));
transforms.add(transformRegistry.parseTransform(watchId, currentFieldName, parser));
}
}
}

View File

@ -31,8 +31,8 @@ public final class ChainTransformFactory extends TransformFactory<ChainTransform
}
@Override
public ChainTransform parseTransform(String watchId, XContentParser parser, boolean upgradeTransformSource) throws IOException {
return ChainTransform.parse(watchId, parser, registry, upgradeTransformSource);
public ChainTransform parseTransform(String watchId, XContentParser parser) throws IOException {
return ChainTransform.parse(watchId, parser, registry);
}
@Override

View File

@ -54,15 +54,9 @@ public class ScriptTransform implements Transform {
return script.toXContent(builder, params);
}
public static ScriptTransform parse(String watchId, XContentParser parser, boolean upgradeSource,
String defaultLegacyScriptLanguage) throws IOException {
public static ScriptTransform parse(String watchId, XContentParser parser) throws IOException {
try {
Script script;
if (upgradeSource) {
script = Script.parse(parser, ParseFieldMatcher.STRICT, defaultLegacyScriptLanguage);
} else {
script = Script.parse(parser, ParseFieldMatcher.STRICT);
}
Script script = Script.parse(parser, ParseFieldMatcher.STRICT);
return new ScriptTransform(script);
} catch (ElasticsearchParseException pe) {
throw new ElasticsearchParseException("could not parse [{}] transform for watch [{}]. failed to parse script", pe, TYPE,

View File

@ -31,9 +31,8 @@ public class ScriptTransformFactory extends TransformFactory<ScriptTransform, Sc
}
@Override
public ScriptTransform parseTransform(String watchId, XContentParser parser, boolean upgradeTransformSource) throws IOException {
String defaultLegacyScriptLanguage = ScriptSettings.getLegacyDefaultLang(settings);
return ScriptTransform.parse(watchId, parser, upgradeTransformSource, defaultLegacyScriptLanguage);
public ScriptTransform parseTransform(String watchId, XContentParser parser) throws IOException {
return ScriptTransform.parse(watchId, parser);
}
@Override

View File

@ -93,8 +93,6 @@ public class SearchTransform implements Transform {
public static SearchTransform parse(Logger transformLogger, String watchId,
XContentParser parser,
boolean upgradeTransformSource,
String defaultLegacyScriptLanguage,
ParseFieldMatcher parseFieldMatcher,
SearchRequestParsers searchRequestParsers) throws IOException {
WatcherSearchTemplateRequest request = null;
@ -108,10 +106,8 @@ public class SearchTransform implements Transform {
currentFieldName = parser.currentName();
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.REQUEST)) {
try {
request = WatcherSearchTemplateRequest.fromXContent(
transformLogger, parser, ExecutableSearchTransform.DEFAULT_SEARCH_TYPE, upgradeTransformSource,
defaultLegacyScriptLanguage, parseFieldMatcher, searchRequestParsers
);
request = WatcherSearchTemplateRequest.fromXContent(transformLogger, parser,
ExecutableSearchTransform.DEFAULT_SEARCH_TYPE, parseFieldMatcher, searchRequestParsers);
} catch (ElasticsearchParseException srpe) {
throw new ElasticsearchParseException("could not parse [{}] transform for watch [{}]. failed to parse [{}]", srpe,
TYPE, watchId, currentFieldName);

View File

@ -52,9 +52,8 @@ public class SearchTransformFactory extends TransformFactory<SearchTransform, Se
}
@Override
public SearchTransform parseTransform(String watchId, XContentParser parser, boolean upgradeTransformSource) throws IOException {
String defaultLegacyScriptLanguage = ScriptSettings.getLegacyDefaultLang(settings);
return SearchTransform.parse(transformLogger, watchId, parser, upgradeTransformSource, defaultLegacyScriptLanguage,
public SearchTransform parseTransform(String watchId, XContentParser parser) throws IOException {
return SearchTransform.parse(transformLogger, watchId, parser,
parseFieldMatcher, searchRequestParsers);
}

View File

@ -301,11 +301,11 @@ public class Watch implements TriggerEngine.Job, ToXContent {
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.TRIGGER)) {
trigger = triggerService.parseTrigger(id, parser);
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.INPUT)) {
input = inputRegistry.parse(id, parser, upgradeWatchSource);
input = inputRegistry.parse(id, parser);
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.CONDITION)) {
condition = actionRegistry.getConditionRegistry().parseExecutable(id, parser, upgradeWatchSource);
condition = actionRegistry.getConditionRegistry().parseExecutable(id, parser);
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.TRANSFORM)) {
transform = actionRegistry.getTransformRegistry().parse(id, parser, upgradeWatchSource);
transform = actionRegistry.getTransformRegistry().parse(id, parser);
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.THROTTLE_PERIOD)) {
throttlePeriod = timeValueMillis(parser.longValue());
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.THROTTLE_PERIOD_HUMAN)) {
@ -317,7 +317,7 @@ public class Watch implements TriggerEngine.Job, ToXContent {
pe, id, currentFieldName);
}
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.ACTIONS)) {
actions = actionRegistry.parseActions(id, parser, upgradeWatchSource);
actions = actionRegistry.parseActions(id, parser);
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.METADATA)) {
metatdata = parser.map();
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.STATUS)) {

View File

@ -53,7 +53,6 @@ import static org.hamcrest.Matchers.is;
public class ScriptConditionTests extends ESTestCase {
private ScriptService scriptService;
private String defaultScriptLang = ScriptSettings.getLegacyDefaultLang(Settings.EMPTY);
@Before
public void init() throws IOException {
@ -118,7 +117,7 @@ public class ScriptConditionTests extends ESTestCase {
XContentParser parser = XContentFactory.xContent(builder.bytes()).createParser(builder.bytes());
parser.nextToken();
ScriptCondition executable = ScriptCondition.parse(scriptService, "_watch", parser, false, defaultScriptLang);
ScriptCondition executable = ScriptCondition.parse(scriptService, "_watch", parser);
SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 500L, new ShardSearchFailure[0]);
WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.XContent(response));
@ -129,7 +128,7 @@ public class ScriptConditionTests extends ESTestCase {
builder = createConditionContent("return true", null, ScriptType.INLINE);
parser = XContentFactory.xContent(builder.bytes()).createParser(builder.bytes());
parser.nextToken();
executable = ScriptCondition.parse(scriptService, "_watch", parser, false, defaultScriptLang);
executable = ScriptCondition.parse(scriptService, "_watch", parser);
ctx = mockExecutionContext("_name", new Payload.XContent(response));
@ -142,7 +141,7 @@ public class ScriptConditionTests extends ESTestCase {
XContentParser parser = XContentFactory.xContent(builder.bytes()).createParser(builder.bytes());
parser.nextToken();
try {
ScriptCondition.parse(scriptService, "_id", parser, false, defaultScriptLang);
ScriptCondition.parse(scriptService, "_id", parser);
fail("expected a condition exception trying to parse an invalid condition XContent");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(),
@ -162,11 +161,11 @@ public class ScriptConditionTests extends ESTestCase {
default:
script = "foo = = 1";
}
XContentBuilder builder = createConditionContent(script, "groovy", scriptType);
XContentBuilder builder = createConditionContent(script, "painless", scriptType);
XContentParser parser = XContentFactory.xContent(builder.bytes()).createParser(builder.bytes());
parser.nextToken();
expectThrows(IllegalArgumentException.class,
() -> ScriptCondition.parse(scriptService, "_watch", parser, false, defaultScriptLang));
() -> ScriptCondition.parse(scriptService, "_watch", parser));
}
public void testScriptConditionParser_badLang() throws Exception {
@ -175,7 +174,7 @@ public class ScriptConditionTests extends ESTestCase {
XContentParser parser = XContentFactory.xContent(builder.bytes()).createParser(builder.bytes());
parser.nextToken();
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class,
() -> ScriptCondition.parse(scriptService, "_watch", parser, false, defaultScriptLang));
() -> ScriptCondition.parse(scriptService, "_watch", parser));
assertThat(exception.getMessage(), containsString("script_lang not supported [not_a_valid_lang]"));
}

View File

@ -84,7 +84,7 @@ public class ChainInputTests extends ESTestCase {
// first pass JSON and check for correct inputs
XContentParser parser = XContentFactory.xContent(builder.bytes()).createParser(builder.bytes());
parser.nextToken();
ChainInput chainInput = chainInputFactory.parseInput("test", parser, false);
ChainInput chainInput = chainInputFactory.parseInput("test", parser);
assertThat(chainInput.getInputs(), hasSize(2));
assertThat(chainInput.getInputs().get(0).v1(), is("first"));
@ -195,7 +195,7 @@ public class ChainInputTests extends ESTestCase {
XContentParser parser = XContentFactory.xContent(builder.bytes()).createParser(builder.bytes());
parser.nextToken();
ElasticsearchParseException e =
expectThrows(ElasticsearchParseException.class, () -> chainInputFactory.parseInput("test", parser, false));
expectThrows(ElasticsearchParseException.class, () -> chainInputFactory.parseInput("test", parser));
assertThat(e.getMessage(),
containsString("Expected closing JSON object after parsing input [simple] named [first] in watch [test]"));
}
@ -225,7 +225,7 @@ public class ChainInputTests extends ESTestCase {
XContentParser parser = XContentFactory.xContent(builder.bytes()).createParser(builder.bytes());
parser.nextToken();
ElasticsearchParseException e =
expectThrows(ElasticsearchParseException.class, () -> chainInputFactory.parseInput("test", parser, false));
expectThrows(ElasticsearchParseException.class, () -> chainInputFactory.parseInput("test", parser));
assertThat(e.getMessage(), containsString("Expected starting JSON object after [first] in watch [test]"));
}

View File

@ -187,7 +187,7 @@ public class HttpInputTests extends ESTestCase {
BytesReference source = jsonBuilder().value(inputBuilder.build()).bytes();
XContentParser parser = XContentHelper.createParser(source);
parser.nextToken();
HttpInput result = httpParser.parseInput("_id", parser, false);
HttpInput result = httpParser.parseInput("_id", parser);
assertThat(result.type(), equalTo(HttpInput.TYPE));
assertThat(result.getRequest().scheme(), equalTo(scheme != null ? scheme : Scheme.HTTP)); // http is the default
@ -225,7 +225,7 @@ public class HttpInputTests extends ESTestCase {
XContentParser parser = XContentHelper.createParser(builder.bytes());
parser.nextToken();
try {
httpParser.parseInput("_id", parser, false);
httpParser.parseInput("_id", parser);
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), is("unsupported http method [_METHOD]"));

View File

@ -46,7 +46,7 @@ public class SimpleInputTests extends ESTestCase {
InputFactory parser = new SimpleInputFactory(Settings.builder().build());
XContentParser xContentParser = JsonXContent.jsonXContent.createParser(jsonBuilder.bytes());
xContentParser.nextToken();
ExecutableInput input = parser.parseExecutable("_id", xContentParser, false);
ExecutableInput input = parser.parseExecutable("_id", xContentParser);
assertEquals(input.type(), SimpleInput.TYPE);
@ -63,7 +63,7 @@ public class SimpleInputTests extends ESTestCase {
XContentParser xContentParser = JsonXContent.jsonXContent.createParser(jsonBuilder.bytes());
xContentParser.nextToken();
try {
parser.parseInput("_id", xContentParser, false);
parser.parseInput("_id", xContentParser);
fail("[simple] input parse should fail with an InputException for an empty json object");
} catch (ElasticsearchParseException e) {
assertThat(e.getMessage(), containsString("expected an object but found [VALUE_STRING] instead"));

View File

@ -123,8 +123,7 @@ public class WatcherUtilsTests extends ESTestCase {
request.toXContent(builder, ToXContent.EMPTY_PARAMS);
XContentParser parser = XContentHelper.createParser(builder.bytes());
assertThat(parser.nextToken(), equalTo(XContentParser.Token.START_OBJECT));
WatcherSearchTemplateRequest result = WatcherSearchTemplateRequest.fromXContent(logger, parser, DEFAULT_SEARCH_TYPE,
false, null, null, null);
WatcherSearchTemplateRequest result = WatcherSearchTemplateRequest.fromXContent(logger, parser, DEFAULT_SEARCH_TYPE, null, null);
assertThat(result.getIndices(), arrayContainingInAnyOrder(expectedIndices != null ? expectedIndices : new String[0]));
assertThat(result.getTypes(), arrayContainingInAnyOrder(expectedTypes != null ? expectedTypes : new String[0]));
@ -213,8 +212,7 @@ public class WatcherUtilsTests extends ESTestCase {
XContentParser parser = XContentHelper.createParser(builder.bytes());
assertThat(parser.nextToken(), equalTo(XContentParser.Token.START_OBJECT));
WatcherSearchTemplateRequest result = WatcherSearchTemplateRequest.fromXContent(logger, parser, DEFAULT_SEARCH_TYPE,
false, null, null, null);
WatcherSearchTemplateRequest result = WatcherSearchTemplateRequest.fromXContent(logger, parser, DEFAULT_SEARCH_TYPE, null, null);
assertThat(result.getIndices(), arrayContainingInAnyOrder(indices));
assertThat(result.getTypes(), arrayContainingInAnyOrder(types));

View File

@ -42,7 +42,7 @@ public class WatcherSearchTemplateRequestTests extends ESTestCase {
parser.nextToken();
WatcherSearchTemplateRequest result = WatcherSearchTemplateRequest.fromXContent(
logger, parser, randomFrom(SearchType.values()), false, null, null, null);
logger, parser, randomFrom(SearchType.values()), null, null);
assertNotNull(result.getTemplate());
assertThat(result.getTemplate().getIdOrCode(), equalTo(expectedScript));
assertThat(result.getTemplate().getLang(), equalTo(expectedLang));
@ -91,7 +91,7 @@ public class WatcherSearchTemplateRequestTests extends ESTestCase {
SearchRequestParsers searchRequestParsers = new SearchModule(Settings.EMPTY, false, Collections.emptyList())
.getSearchRequestParsers();
WatcherSearchTemplateRequest result = WatcherSearchTemplateRequest.fromXContent(
logger, parser, SearchType.DEFAULT, true, "your_legacy_lang", ParseFieldMatcher.STRICT, searchRequestParsers);
logger, parser, SearchType.DEFAULT, ParseFieldMatcher.STRICT, searchRequestParsers);
Map<String, Object> parsedResult = XContentHelper.convertToMap(result.getSearchSource(), true).v2();
// after upgrading the language must be equal to legacy language, because no language was defined explicitly in these scripts:
assertThat(XContentMapValues.extractValue("query.script.script.lang", parsedResult), equalTo("your_legacy_lang"));

View File

@ -150,7 +150,7 @@ public class SearchInputTests extends ESIntegTestCase {
SearchInputFactory factory = new SearchInputFactory(Settings.EMPTY, WatcherClientProxy.of(client()),
searchParsers, scriptService());
SearchInput searchInput = factory.parseInput("_id", parser, false);
SearchInput searchInput = factory.parseInput("_id", parser);
assertEquals(SearchInput.TYPE, searchInput.type());
assertThat(searchInput.getTimeout(), equalTo(timeout));
}

View File

@ -218,7 +218,7 @@ public class SearchTransformTests extends ESIntegTestCase {
SearchRequestParsers searchRequestParsers = internalCluster().getInstance(SearchRequestParsers.class);
SearchTransformFactory transformFactory = new SearchTransformFactory(Settings.EMPTY, WatcherClientProxy.of(client()),
searchRequestParsers, scriptService());
ExecutableSearchTransform executable = transformFactory.parseExecutable("_id", parser, false);
ExecutableSearchTransform executable = transformFactory.parseExecutable("_id", parser);
assertThat(executable, notNullValue());
assertThat(executable.type(), is(SearchTransform.TYPE));

View File

@ -123,7 +123,7 @@ public class ChainTransformTests extends ESTestCase {
XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes());
parser.nextToken();
ExecutableChainTransform executable = transformParser.parseExecutable("_id", parser, false);
ExecutableChainTransform executable = transformParser.parseExecutable("_id", parser);
assertThat(executable, notNullValue());
assertThat(executable.transform().getTransforms(), notNullValue());
assertThat(executable.transform().getTransforms(), hasSize(4));
@ -201,7 +201,7 @@ public class ChainTransformTests extends ESTestCase {
}
@Override
public Transform parseTransform(String watchId, XContentParser parser, boolean upgradeTransformSource) throws IOException {
public Transform parseTransform(String watchId, XContentParser parser) throws IOException {
if (parser.currentToken() == XContentParser.Token.VALUE_STRING) {
return new Transform(parser.text());
}
@ -270,7 +270,7 @@ public class ChainTransformTests extends ESTestCase {
}
@Override
public Transform parseTransform(String watchId, XContentParser parser, boolean upgradeTransformSource) throws IOException {
public Transform parseTransform(String watchId, XContentParser parser) throws IOException {
assert parser.currentToken() == XContentParser.Token.START_OBJECT;
XContentParser.Token token = parser.nextToken();
assert token == XContentParser.Token.END_OBJECT;

View File

@ -154,7 +154,7 @@ public class ScriptTransformTests extends ESTestCase {
XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes());
parser.nextToken();
ExecutableScriptTransform transform = new ScriptTransformFactory(Settings.EMPTY, service).parseExecutable("_id", parser, false);
ExecutableScriptTransform transform = new ScriptTransformFactory(Settings.EMPTY, service).parseExecutable("_id", parser);
Script script = new Script(type, "_lang", "_script", singletonMap("key", "value"));
assertThat(transform.transform().getScript(), equalTo(script));
}
@ -165,7 +165,7 @@ public class ScriptTransformTests extends ESTestCase {
XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes());
parser.nextToken();
ExecutableScriptTransform transform = new ScriptTransformFactory(Settings.EMPTY, service).parseExecutable("_id", parser, false);
ExecutableScriptTransform transform = new ScriptTransformFactory(Settings.EMPTY, service).parseExecutable("_id", parser);
assertThat(transform.transform().getScript(), equalTo(new Script("_script")));
}
@ -185,7 +185,7 @@ public class ScriptTransformTests extends ESTestCase {
XContentParser parser = XContentFactory.xContent(builder.bytes()).createParser(builder.bytes());
parser.nextToken();
ScriptTransform scriptTransform = transformFactory.parseTransform("_watch", parser, false);
ScriptTransform scriptTransform = transformFactory.parseTransform("_watch", parser);
Exception e = expectThrows(ScriptException.class, () -> transformFactory.createExecutable(scriptTransform));
assertThat(e.getMessage(), containsString(errorMessage));
}
@ -203,7 +203,7 @@ public class ScriptTransformTests extends ESTestCase {
XContentParser parser = XContentFactory.xContent(builder.bytes()).createParser(builder.bytes());
parser.nextToken();
ScriptTransform scriptCondition = transformFactory.parseTransform("_watch", parser, false);
ScriptTransform scriptCondition = transformFactory.parseTransform("_watch", parser);
Exception e = expectThrows(IllegalArgumentException.class, () -> transformFactory.createExecutable(scriptCondition));
assertThat(e.getMessage(), containsString("script_lang not supported [not_a_valid_lang]"));
}

View File

@ -345,10 +345,10 @@ public class WatchTests extends ESTestCase {
// parse in legacy mode:
watch = watchParser.parse("_id", false, builder.bytes(), true);
assertThat(((ScriptCondition) watch.condition()).getScript().getLang(), equalTo("groovy"));
assertThat(((ScriptCondition) watch.condition()).getScript().getLang(), equalTo("painless"));
request = ((SearchInput) watch.input().input()).getRequest();
searchRequest = searchTemplateService.toSearchRequest(request);
assertThat(((ScriptQueryBuilder) searchRequest.source().query()).script().getLang(), equalTo("groovy"));
assertThat(((ScriptQueryBuilder) searchRequest.source().query()).script().getLang(), equalTo("painless"));
}
private static Schedule randomSchedule() {
@ -437,13 +437,11 @@ public class WatchTests extends ESTestCase {
private ConditionRegistry conditionRegistry() {
Map<String, ConditionFactory> 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));
parsers.put(ArrayCompareCondition.TYPE, (c, id, p, upgrade) -> ArrayCompareCondition.parse(c, id, p));
parsers.put(CompareCondition.TYPE, (c, id, p, upgrade) -> CompareCondition.parse(c, id, p));
String defaultLegacyScriptLanguage = ScriptSettings.getLegacyDefaultLang(settings);
parsers.put(ScriptCondition.TYPE, (c, id, p, upgrade) -> ScriptCondition.parse(scriptService, id, p, upgrade,
defaultLegacyScriptLanguage));
parsers.put(AlwaysCondition.TYPE, (c, id, p) -> AlwaysCondition.parse(id, p));
parsers.put(NeverCondition.TYPE, (c, id, p) -> NeverCondition.parse(id, p));
parsers.put(ArrayCompareCondition.TYPE, (c, id, p) -> ArrayCompareCondition.parse(c, id, p));
parsers.put(CompareCondition.TYPE, (c, id, p) -> CompareCondition.parse(c, id, p));
parsers.put(ScriptCondition.TYPE, (c, id, p) -> ScriptCondition.parse(scriptService, id, p));
return new ConditionRegistry(parsers, new ClockMock());
}

View File

@ -1,15 +0,0 @@
apply plugin: 'elasticsearch.rest-test'
dependencies {
testCompile project(path: ':x-plugins:elasticsearch', configuration: 'runtime')
testCompile project(path: ':modules:lang-groovy', configuration: 'runtime')
}
integTest {
cluster {
plugin ':x-plugins:elasticsearch'
setting 'script.inline', 'true'
setting 'xpack.security.enabled', 'false'
setting 'xpack.monitoring.enabled', 'false'
}
}

View File

@ -1,42 +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.smoketest;
import com.carrotsearch.randomizedtesting.annotations.Name;
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
import org.elasticsearch.test.rest.yaml.parser.ClientYamlTestParseException;
import org.junit.After;
import org.junit.Before;
import java.io.IOException;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
public abstract class WatcherRestTestCase extends ESClientYamlSuiteTestCase {
public WatcherRestTestCase(@Name("yaml") ClientYamlTestCandidate testCandidate) {
super(testCandidate);
}
@ParametersFactory
public static Iterable<Object[]> parameters() throws IOException, ClientYamlTestParseException {
return ESClientYamlSuiteTestCase.createParameters();
}
@Before
public void startWatcher() throws Exception {
getAdminExecutionContext().callApi("xpack.watcher.start", emptyMap(), emptyList(), emptyMap());
}
@After
public void stopWatcher() throws Exception {
getAdminExecutionContext().callApi("xpack.watcher.stop", emptyMap(), emptyList(), emptyMap());
}
}

View File

@ -1,17 +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.smoketest;
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
/** Runs rest tests against external cluster */
public class WatcherWithGroovyIT extends WatcherRestTestCase {
public WatcherWithGroovyIT(ClientYamlTestCandidate testCandidate) {
super(testCandidate);
}
}

View File

@ -1,89 +0,0 @@
---
"Test execute watch api":
- do:
cluster.health:
wait_for_status: green
- do:
xpack.watcher.put_watch:
id: "my_exe_watch"
body: >
{
"trigger" : {
"schedule" : { "cron" : "0 0 0 1 * ? 2099" }
},
"input" : {
"search" : {
"request" : {
"indices" : [ "logstash*" ],
"body" : {
"query" : {
"bool": {
"must" : {
"match": {
"response": 404
}
},
"filter": {
"range": {
"@timestamp" : {
"from": "{{ctx.trigger.scheduled_time}}||-5m",
"to": "{{ctx.trigger.triggered_time}}"
}
}
}
}
}
}
}
}
},
"condition" : {
"script" : {
"inline" : "ctx.payload.hits.total > 1"
}
},
"actions" : {
"email_admin" : {
"email" : {
"to" : "someone@domain.host.com",
"subject" : "404 recently encountered"
}
}
}
}
- match: { _id: "my_exe_watch" }
- do:
xpack.watcher.execute_watch:
id: "my_exe_watch"
body: >
{
"trigger_data" : {
"scheduled_time" : "2015-05-05T20:58:02.443Z",
"triggered_time" : "2015-05-05T20:58:02.443Z"
},
"alternative_input" : {
"foo" : "bar"
},
"ignore_condition" : true,
"action_modes" : {
"_all" : "force_simulate"
},
"record_execution" : true
}
- match: { "watch_record.watch_id": "my_exe_watch" }
- match: { "watch_record.state": "executed" }
- match: { "watch_record.trigger_event.type": "manual" }
- match: { "watch_record.trigger_event.triggered_time": "2015-05-05T20:58:02.443Z" }
- match: { "watch_record.trigger_event.manual.schedule.scheduled_time": "2015-05-05T20:58:02.443Z" }
- match: { "watch_record.result.input.type": "simple" }
- match: { "watch_record.result.input.status": "success" }
- match: { "watch_record.result.input.payload.foo": "bar" }
- match: { "watch_record.result.condition.type": "always" }
- match: { "watch_record.result.condition.status": "success" }
- match: { "watch_record.result.condition.met": true }
- match: { "watch_record.result.actions.0.id" : "email_admin" }
- match: { "watch_record.result.actions.0.status" : "simulated" }
- match: { "watch_record.result.actions.0.type" : "email" }
- match: { "watch_record.result.actions.0.email.message.subject" : "404 recently encountered" }

View File

@ -1,48 +0,0 @@
---
"Test execute watch api with minimal body":
- do:
cluster.health:
wait_for_status: green
- do:
xpack.watcher.put_watch:
id: "my_logging_watch"
body: >
{
"trigger" : {
"schedule" : { "cron" : "0 0 0 1 * ? 2099" }
},
"input" : {
"simple" : {
"count" : 1
}
},
"condition" : {
"script" : "ctx.payload.count == 1"
},
"actions" : {
"logging" : {
"logging" : {
"text" : "foobar"
}
}
}
}
- match: { _id: "my_logging_watch" }
- do:
xpack.watcher.execute_watch:
id: "my_logging_watch"
- match: { "watch_record.watch_id": "my_logging_watch" }
- match: { "watch_record.state": "executed" }
- match: { "watch_record.result.input.type": "simple" }
- match: { "watch_record.result.input.status": "success" }
- match: { "watch_record.result.input.payload.count": 1 }
- match: { "watch_record.result.condition.type": "script" }
- match: { "watch_record.result.condition.status": "success" }
- match: { "watch_record.result.condition.met": true }
- match: { "watch_record.result.actions.0.id" : "logging" }
- match: { "watch_record.result.actions.0.type" : "logging" }
- match: { "watch_record.result.actions.0.status" : "success" }
- match: { "watch_record.result.actions.0.logging.logged_text" : "foobar" }

View File

@ -1,74 +0,0 @@
---
"Test execute watch api with an inline watch":
- do:
cluster.health:
wait_for_status: green
- do:
xpack.watcher.execute_watch:
body: >
{
"trigger_data" : {
"scheduled_time" : "2015-05-05T20:58:02.443Z",
"triggered_time" : "2015-05-05T20:58:02.443Z"
},
"alternative_input" : {
"foo" : "bar"
},
"ignore_condition" : true,
"action_modes" : {
"_all" : "force_simulate"
},
"watch" : {
"trigger" : {
"schedule" : { "cron" : "0 0 0 1 * ? 2099" }
},
"input" : {
"search" : {
"request" : {
"indices" : [ "logstash*" ],
"body" : {
"query" : {
"bool" : {
"must": {
"match": {
"response": 404
}
},
"filter": {
"range": {
"@timestamp" : {
"from": "{{ctx.trigger.scheduled_time}}||-5m",
"to": "{{ctx.trigger.triggered_time}}"
}
}
}
}
}
}
}
}
},
"condition" : {
"script" : {
"inline" : "ctx.payload.hits.total > 1"
}
},
"actions" : {
"email_admin" : {
"email" : {
"to" : "someone@domain.host.com",
"subject" : "404 recently encountered"
}
}
}
}
}
- match: { "watch_record.state": "executed" }
- match: { "watch_record.trigger_event.manual.schedule.scheduled_time": "2015-05-05T20:58:02.443Z" }
- match: { "watch_record.result.input.type": "simple" }
- match: { "watch_record.result.input.payload.foo": "bar" }
- match: { "watch_record.result.condition.met": true }
- match: { "watch_record.result.actions.0.id" : "email_admin" }
- match: { "watch_record.result.actions.0.status" : "simulated" }
- match: { "watch_record.result.actions.0.email.message.subject" : "404 recently encountered" }

View File

@ -1,238 +0,0 @@
---
"Test simple input to index action":
- do:
xpack.watcher.put_watch:
id: my_watch
body: >
{
"trigger" : { "schedule" : { "cron" : "0/1 * * * * ? 2020" } },
"input" : { "simple" : { "foo": "bar" } },
"actions" : {
"index_action" : {
"index" : {
"index" : "idx",
"doc_type" : "type",
"execution_time_field" : "@timestamp"
}
}
}
}
- match: { _id: "my_watch" }
- do:
xpack.watcher.execute_watch:
id: "my_watch"
body: >
{
"trigger_data" : {
"triggered_time" : "2016-07-07T09:00:00Z",
"scheduled_time" : "2016-07-07T09:00:00Z"
}
}
- match: { "watch_record.state": "executed" }
- do:
indices.refresh: {}
- do:
search:
index: idx
type: type
- match: { hits.total: 1 }
- match: { hits.hits.0._source.foo: bar }
- gte: { hits.hits.0._source.@timestamp: '2016-07-08' }
---
"Test simple input with document field":
- do:
xpack.watcher.put_watch:
id: my_watch
body: >
{
"trigger" : { "schedule" : { "cron" : "0/1 * * * * ? 2020" } },
"input" : { "simple" : { "foo": "bar" } },
"actions" : {
"index_action" : {
"transform" : { "script" : { "inline": "return [ '_doc' : ctx.payload ]" } },
"index" : {
"index" : "idx",
"doc_type" : "type",
"execution_time_field" : "@timestamp"
}
}
}
}
- match: { _id: "my_watch" }
- do:
xpack.watcher.execute_watch:
id: "my_watch"
body: >
{
"trigger_data" : {
"triggered_time" : "2016-07-07T09:00:00Z",
"scheduled_time" : "2016-07-07T09:00:00Z"
}
}
- match: { "watch_record.state": "executed" }
- do:
indices.refresh: {}
- do:
search:
index: idx
type: type
- match: { hits.total: 1 }
- match: { hits.hits.0._source.foo: bar }
- gte: { hits.hits.0._source.@timestamp: '2016-07-08"' }
---
"Test simple input with wrong document results in error":
- do:
xpack.watcher.put_watch:
id: my_watch
body: >
{
"trigger" : { "schedule" : { "cron" : "0/1 * * * * ? 2020" } },
"input" : { "simple" : { "foo": "bar" } },
"actions" : {
"index_action" : {
"transform" : { "script" : { "inline": "return [ '_doc' : 1 ]" } },
"index" : {
"index" : "idx",
"doc_type" : "type",
"execution_time_field" : "@timestamp"
}
}
}
}
- match: { _id: "my_watch" }
- do:
xpack.watcher.execute_watch:
id: "my_watch"
body: >
{
"record_execution" : true,
"trigger_data" : {
"triggered_time" : "2016-07-07T09:00:00Z",
"scheduled_time" : "2016-07-07T09:00:00Z"
}
}
- match: { "watch_record.state": "executed" }
- do:
indices.refresh: {}
- do:
indices.exists:
index: idx
- is_false: ''
- do:
search:
index: .watcher-history-*
type: watch_record
body: >
{
"query" : {
"match" : {
"result.actions.status": "failure"
}
}
}
- match: { hits.total: 1 }
---
"Test search input to index action with aggs":
- do:
bulk:
refresh: true
body:
- '{"index": {"_index": "idx", "_type": "type", "_id": "1"}}'
- '{"@timestamp": "2016-07-07" }'
- '{"index": {"_index": "idx", "_type": "type", "_id": "2"}}'
- '{"@timestamp": "2016-07-08" }'
- '{"index": {"_index": "idx", "_type": "type", "_id": "3"}}'
- '{"@timestamp": "2016-07-09" }'
- do:
xpack.watcher.put_watch:
id: my_watch
body: >
{
"trigger" : { "schedule" : { "cron" : "0/1 * * * * ? 2020" } },
"input" : {
"search" : {
"request": {
"indices" : [ "idx" ],
"types" : [ "type" ],
"body" : {
"aggs" : {
"trend" : {
"date_histogram" : {
"field" : "@timestamp",
"interval" : "day"
}
}
}
}
}
}
},
"actions" : {
"index_action" : {
"transform" : { "script" : { "inline": "return [ '_doc' : ctx.payload.aggregations.trend.buckets]" } },
"index" : {
"index" : "idx",
"doc_type" : "bucket",
"execution_time_field" : "@timestamp"
}
}
}
}
- match: { _id: "my_watch" }
- do:
xpack.watcher.execute_watch:
id: "my_watch"
body: >
{
"trigger_data" : {
"triggered_time" : "2016-07-07T09:00:00Z",
"scheduled_time" : "2016-07-07T09:00:00Z"
}
}
- match: { "watch_record.state": "executed" }
- do:
indices.refresh: {}
- do:
search:
index: idx
type: bucket
- match: { hits.total: 3 }

View File

@ -1,141 +0,0 @@
---
"Test the execution of a Groovy closure in script condition":
- do:
bulk:
refresh: true
body: |
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "green", "@timestamp": "2005-01-01T00:00:00" }
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "yellow", "@timestamp": "2005-01-01T00:00:05" }
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "green", "@timestamp": "2005-01-01T00:00:55" }
- do:
xpack.watcher.put_watch:
id: "watch_with_groovy_closure"
body: >
{
"trigger" : {
"schedule" : { "cron" : "0 0 0 1 * ? 2099" }
},
"input" : {
"search" : {
"request" : {
"indices" : [ ".monitoring" ],
"body" : {
"query" : {
"match_all" : {
}
},
"aggregations" : {
"minutes" : {
"date_histogram" : {
"field" : "@timestamp",
"interval" : "5s",
"order" : {
"_count" : "desc"
}
},
"aggregations": {
"status" : {
"terms" : {
"field" : "status.keyword",
"size" : 3
}
}
}
}
}
}
}
}
},
"condition" : {
"script" : {
"inline" : "if (ctx.payload.hits.total < 1) return false; def rows = ctx.payload.hits.hits; if (ctx.payload.aggregations.minutes.buckets.size() < 12) return false; def last60Seconds = ctx.payload.aggregations.minutes.buckets[-12..-1]; return last60Seconds.every { it.status.buckets.every { s -> s.key == 'red' } }",
"lang": "groovy"
}
},
"actions" : {
"log" : {
"logging" : {
"text" : "executed at {{ctx.execution_time}}"
}
}
}
}
- match: { _id: "watch_with_groovy_closure" }
- do:
warnings:
- '[groovy] scripts are deprecated, use [painless] scripts instead'
xpack.watcher.execute_watch:
id: "watch_with_groovy_closure"
body: >
{
"trigger_data" : {
"scheduled_time" : "2015-05-05T20:58:02.443Z",
"triggered_time" : "2015-05-05T20:58:02.443Z"
},
"ignore_condition" : false,
"action_modes" : {
"_all" : "force_simulate"
},
"record_execution" : false
}
- match: { "watch_record.state": "execution_not_needed" }
- match: { "watch_record.result.condition.met": false }
- do:
bulk:
refresh: true
body: |
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "red", "@timestamp": "2005-01-01T00:01:00" }
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "red", "@timestamp": "2005-01-01T00:01:05" }
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "red", "@timestamp": "2005-01-01T00:01:10" }
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "red", "@timestamp": "2005-01-01T00:01:15" }
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "red", "@timestamp": "2005-01-01T00:01:20" }
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "red", "@timestamp": "2005-01-01T00:01:25" }
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "red", "@timestamp": "2005-01-01T00:01:30" }
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "red", "@timestamp": "2005-01-01T00:01:35" }
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "red", "@timestamp": "2005-01-01T00:01:40" }
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "red", "@timestamp": "2005-01-01T00:01:45" }
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "red", "@timestamp": "2005-01-01T00:01:50" }
{"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
{ "status": "red", "@timestamp": "2005-01-01T00:01:55" }
- do:
warnings:
- '[groovy] scripts are deprecated, use [painless] scripts instead'
xpack.watcher.execute_watch:
id: "watch_with_groovy_closure"
body: >
{
"trigger_data" : {
"scheduled_time" : "2015-05-05T20:58:02.443Z",
"triggered_time" : "2015-05-05T20:58:02.443Z"
},
"ignore_condition" : false,
"action_modes" : {
"_all" : "force_simulate"
},
"record_execution" : false
}
- match: { "watch_record.state": "executed" }
- match: { "watch_record.result.condition.met": true }