Change `meta` to metadata in the code.

The meta data field was known and `metadata` in the docs but the parsing logic expected `meta`.
This change unifies everything to use `metadata`. Also clean up Watch parsing to fail in a more consistent way when unknown fields are encountered.

Fixes elastic/elasticsearch#329

Original commit: elastic/x-pack-elasticsearch@a9013127d8
This commit is contained in:
Brian Murphy 2015-05-02 12:50:16 -04:00
parent 0d6fb1081a
commit 7d8dc38c29
7 changed files with 52 additions and 13 deletions

View File

@ -213,7 +213,7 @@ public class WatchRecord implements ToXContent {
public static final ParseField TRIGGER_EVENT_FIELD = new ParseField("trigger_event"); public static final ParseField TRIGGER_EVENT_FIELD = new ParseField("trigger_event");
public static final ParseField MESSAGE_FIELD = new ParseField("message"); public static final ParseField MESSAGE_FIELD = new ParseField("message");
public static final ParseField STATE_FIELD = new ParseField("state"); public static final ParseField STATE_FIELD = new ParseField("state");
public static final ParseField METADATA_FIELD = new ParseField("meta"); public static final ParseField METADATA_FIELD = new ParseField("metadata");
public static final ParseField WATCH_EXECUTION_FIELD = new ParseField("watch_execution"); public static final ParseField WATCH_EXECUTION_FIELD = new ParseField("watch_execution");
private final ConditionRegistry conditionRegistry; private final ConditionRegistry conditionRegistry;

View File

@ -64,7 +64,7 @@ public class ManualTriggerEvent extends TriggerEvent {
} else { } else {
throw new ParseException("could not parse trigger event for [" + context + "]. unknown string value field [" + currentFieldName + "]"); throw new ParseException("could not parse trigger event for [" + context + "]. unknown string value field [" + currentFieldName + "]");
} }
} if (token == XContentParser.Token.START_OBJECT) { } else if (token == XContentParser.Token.START_OBJECT) {
if (TRIGGER_DATA_FIELD.match(currentFieldName)) { if (TRIGGER_DATA_FIELD.match(currentFieldName)) {
triggerData = parser.map(); triggerData = parser.map();
} else { } else {

View File

@ -195,7 +195,7 @@ public class Watch implements TriggerEngine.Job, ToXContent {
public static final ParseField CONDITION_FIELD = new ParseField("condition"); public static final ParseField CONDITION_FIELD = new ParseField("condition");
public static final ParseField ACTIONS_FIELD = new ParseField("actions"); public static final ParseField ACTIONS_FIELD = new ParseField("actions");
public static final ParseField TRANSFORM_FIELD = new ParseField("transform"); public static final ParseField TRANSFORM_FIELD = new ParseField("transform");
public static final ParseField META_FIELD = new ParseField("meta"); public static final ParseField META_FIELD = new ParseField("metadata");
public static final ParseField STATUS_FIELD = new ParseField("status"); public static final ParseField STATUS_FIELD = new ParseField("status");
public static final ParseField THROTTLE_PERIOD_FIELD = new ParseField("throttle_period"); public static final ParseField THROTTLE_PERIOD_FIELD = new ParseField("throttle_period");
@ -304,8 +304,11 @@ public class Watch implements TriggerEngine.Job, ToXContent {
transform = transformRegistry.parse(id, parser); transform = transformRegistry.parse(id, parser);
} else if (META_FIELD.match(currentFieldName)) { } else if (META_FIELD.match(currentFieldName)) {
metatdata = parser.map(); metatdata = parser.map();
} else if (STATUS_FIELD.match(currentFieldName) && includeStatus) { } else if (STATUS_FIELD.match(currentFieldName)) {
status = Status.parse(parser); Status parsedStatus= Status.parse(parser);
if (includeStatus) {
status = parsedStatus;
}
} else if (THROTTLE_PERIOD_FIELD.match(currentFieldName)) { } else if (THROTTLE_PERIOD_FIELD.match(currentFieldName)) {
if (token == XContentParser.Token.VALUE_STRING) { if (token == XContentParser.Token.VALUE_STRING) {
throttlePeriod = TimeValue.parseTimeValue(parser.text(), null); throttlePeriod = TimeValue.parseTimeValue(parser.text(), null);
@ -314,6 +317,8 @@ public class Watch implements TriggerEngine.Job, ToXContent {
} else { } else {
throw new WatcherSettingsException("could not parse watch [" + id + "] throttle period. could not parse token [" + token + "] as time value (must either be string or number)"); throw new WatcherSettingsException("could not parse watch [" + id + "] throttle period. could not parse token [" + token + "] as time value (must either be string or number)");
} }
} else {
throw new WatcherSettingsException("could not parse watch [" + id + "]. unexpected field [" + currentFieldName + "]");
} }
} }
} }

View File

@ -47,7 +47,7 @@
} }
} }
}, },
"meta" : { "metadata" : {
"type" : "object", "type" : "object",
"dynamic": true "dynamic": true
} }

View File

@ -47,7 +47,7 @@
"enabled" : false, "enabled" : false,
"dynamic" : true "dynamic" : true
}, },
"meta" : { "metadata" : {
"type" : "object", "type" : "object",
"dynamic": true "dynamic": true
} }

View File

@ -6,9 +6,15 @@
package org.elasticsearch.watcher.test.integration; package org.elasticsearch.watcher.test.integration;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.watcher.actions.logging.LoggingAction;
import org.elasticsearch.watcher.actions.logging.LoggingLevel;
import org.elasticsearch.watcher.condition.always.AlwaysCondition;
import org.elasticsearch.watcher.history.HistoryStore; import org.elasticsearch.watcher.history.HistoryStore;
import org.elasticsearch.watcher.history.WatchRecord;
import org.elasticsearch.watcher.support.template.Template;
import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTests; import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTests;
import org.elasticsearch.watcher.test.WatcherTestUtils; import org.elasticsearch.watcher.test.WatcherTestUtils;
import org.elasticsearch.watcher.transport.actions.execute.ExecuteWatchResponse;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
@ -16,15 +22,17 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static org.elasticsearch.watcher.client.WatchSourceBuilders.watchBuilder;
import static org.elasticsearch.watcher.condition.ConditionBuilders.scriptCondition;
import static org.elasticsearch.watcher.input.InputBuilders.searchInput;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.elasticsearch.watcher.client.WatchSourceBuilders.watchBuilder;
import static org.elasticsearch.watcher.condition.ConditionBuilders.scriptCondition;
import static org.elasticsearch.watcher.input.InputBuilders.searchInput;
import static org.elasticsearch.watcher.trigger.TriggerBuilders.schedule; import static org.elasticsearch.watcher.trigger.TriggerBuilders.schedule;
import static org.elasticsearch.watcher.trigger.schedule.Schedules.cron; import static org.elasticsearch.watcher.trigger.schedule.Schedules.cron;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.instanceOf;
/** /**
* *
@ -59,9 +67,35 @@ public class WatchMetadataTests extends AbstractWatcherIntegrationTests {
refresh(); refresh();
SearchResponse searchResponse = client().prepareSearch(HistoryStore.INDEX_PREFIX + "*") SearchResponse searchResponse = client().prepareSearch(HistoryStore.INDEX_PREFIX + "*")
.setQuery(termQuery("meta.foo", "bar")) .setQuery(termQuery("metadata.foo", "bar"))
.get(); .get();
assertThat(searchResponse.getHits().getTotalHits(), greaterThan(0L)); assertThat(searchResponse.getHits().getTotalHits(), greaterThan(0L));
} }
@Test
public void testWatchMetadataAvailableAtExecution() throws Exception {
Map<String, Object> metadata = new HashMap<>();
metadata.put("foo", "bar");
metadata.put("logtext", "This is a test");
LoggingAction loggingAction = new LoggingAction(new Template("{{ctx.metadata.logtext}}"), LoggingLevel.DEBUG, "test");
watcherClient().preparePutWatch("_name")
.setSource(watchBuilder()
.trigger(schedule(cron("0/5 * * * * ? *")))
.input(searchInput(WatcherTestUtils.newInputSearchRequest("my-index").source(searchSource().query(matchAllQuery()))))
.condition(new AlwaysCondition())
.addAction("testLogger", loggingAction)
.metadata(metadata))
.get();
WatchRecord.Parser parser = getInstanceFromMaster(WatchRecord.Parser.class);
ExecuteWatchResponse executeWatchResponse = watcherClient().prepareExecuteWatch("_name").addSimulatedActions("_all").get();
WatchRecord record = parser.parse("test_run", 1, executeWatchResponse.getWatchRecordSource());
assertThat(record.metadata().get("foo").toString(), equalTo("bar"));
assertThat(record.execution().actionsResults().get("testLogger").action(), instanceOf(LoggingAction.Result.Simulated.class));
LoggingAction.Result.Simulated simulatedResult = (LoggingAction.Result.Simulated) (record.execution().actionsResults().get("testLogger").action());
assertThat(simulatedResult.loggedText(), equalTo("This is a test"));
}
} }

View File

@ -125,7 +125,7 @@ public class WatchTests extends ElasticsearchTestCase {
public void testParser_SelfGenerated() throws Exception { public void testParser_SelfGenerated() throws Exception {
TransformRegistry transformRegistry = transformRegistry(); TransformRegistry transformRegistry = transformRegistry();
boolean includeStatus = randomBoolean();
Schedule schedule = randomSchedule(); Schedule schedule = randomSchedule();
Trigger trigger = new ScheduleTrigger(schedule); Trigger trigger = new ScheduleTrigger(schedule);
ScheduleRegistry scheduleRegistry = registry(schedule); ScheduleRegistry scheduleRegistry = registry(schedule);
@ -156,7 +156,7 @@ public class WatchTests extends ElasticsearchTestCase {
logger.info(bytes.toUtf8()); logger.info(bytes.toUtf8());
Watch.Parser watchParser = new Watch.Parser(settings, mock(LicenseService.class), conditionRegistry, triggerService, transformRegistry, actionRegistry, inputRegistry, SystemClock.INSTANCE, secretService); Watch.Parser watchParser = new Watch.Parser(settings, mock(LicenseService.class), conditionRegistry, triggerService, transformRegistry, actionRegistry, inputRegistry, SystemClock.INSTANCE, secretService);
boolean includeStatus = randomBoolean();
Watch parsedWatch = watchParser.parse("_name", includeStatus, bytes); Watch parsedWatch = watchParser.parse("_name", includeStatus, bytes);
if (includeStatus) { if (includeStatus) {