Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HLRC: execute watch API #35868

Merged
merged 31 commits into from Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
601a3c7
broekn
romseygeek Oct 4, 2018
fd1687c
Merge remote-tracking branch 'origin/master' into hlrc-execute-watch
romseygeek Oct 17, 2018
3262fcd
WIP
romseygeek Oct 18, 2018
7a462df
Move ObjectPath, XContentSoure and XContentUtils to core
romseygeek Oct 24, 2018
89b3903
Merge remote-tracking branch 'origin/master' into hlrc-execute-watch
romseygeek Oct 26, 2018
c0bb8c3
Revert "Move ObjectPath, XContentSoure and XContentUtils to core"
romseygeek Oct 26, 2018
6330da1
Move ObjectPath and XContentUtils into lib/x-content
romseygeek Oct 26, 2018
42328d4
Merge branch 'xcontentutils' into hlrc-execute-watch
romseygeek Oct 26, 2018
6d5232c
Merge remote-tracking branch 'origin/master' into hlrc-execute-watch
romseygeek Nov 16, 2018
9c0aaed
WIP
romseygeek Nov 20, 2018
98ddc40
Merge remote-tracking branch 'origin/master' into hlrc-execute-watch
romseygeek Nov 20, 2018
d5a4db8
warnings
romseygeek Nov 21, 2018
54225ca
WIP
romseygeek Nov 21, 2018
e8ddc04
WIP
romseygeek Nov 22, 2018
696fb6b
HLRC comparison tests
romseygeek Nov 23, 2018
62da9c5
Merge remote-tracking branch 'origin/master' into hlrc-execute-watch
romseygeek Nov 23, 2018
aaadc9f
Tests; docs
romseygeek Nov 23, 2018
933bc40
javadoc
romseygeek Nov 23, 2018
3e90aa9
license headers; imports
romseygeek Nov 26, 2018
c611744
imports
romseygeek Nov 27, 2018
a1dfd0a
imports
romseygeek Nov 27, 2018
a6d4c0d
Merge remote-tracking branch 'origin/master' into hlrc-execute-watch
romseygeek Nov 27, 2018
c4e3fe4
Fix docs test
romseygeek Nov 28, 2018
ba11a6f
Check record-execution validation
romseygeek Nov 28, 2018
a8abe2b
Remove 404 exclusions
romseygeek Dec 3, 2018
492b507
Merge remote-tracking branch 'origin/master' into hlrc-execute-watch
romseygeek Dec 3, 2018
3d67c6a
Merge remote-tracking branch 'origin/master' into hlrc-execute-watch
romseygeek Dec 4, 2018
c779b34
feedback
romseygeek Dec 4, 2018
5b231e7
feedback
romseygeek Dec 4, 2018
64541a6
Merge remote-tracking branch 'origin/master' into hlrc-execute-watch
romseygeek Dec 5, 2018
72a9a29
Merge remote-tracking branch 'origin/master' into hlrc-execute-watch
romseygeek Dec 5, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -26,6 +26,8 @@
import org.elasticsearch.client.watcher.ActivateWatchResponse;
import org.elasticsearch.client.watcher.AckWatchRequest;
import org.elasticsearch.client.watcher.AckWatchResponse;
import org.elasticsearch.client.watcher.ExecuteWatchRequest;
import org.elasticsearch.client.watcher.ExecuteWatchResponse;
import org.elasticsearch.client.watcher.GetWatchRequest;
import org.elasticsearch.client.watcher.GetWatchResponse;
import org.elasticsearch.client.watcher.StartWatchServiceRequest;
Expand Down Expand Up @@ -269,6 +271,33 @@ public void activateWatchAsync(ActivateWatchRequest request, RequestOptions opti
ActivateWatchResponse::fromXContent, listener, singleton(404));
}

/**
* Execute a watch on the cluster
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-execute-watch.html">
* the docs</a> for more.
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response
* @throws IOException if there is a problem sending the request or parsing the response
*/
public ExecuteWatchResponse executeWatch(ExecuteWatchRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::executeWatch, options,
ExecuteWatchResponse::fromXContent, emptySet());
}

/**
* Asynchronously execute a watch on the cluster
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-execute-watch.html">
* the docs</a> for more.
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notifed upon request completion
*/
public void executeWatchAsync(ExecuteWatchRequest request, RequestOptions options, ActionListener<ExecuteWatchResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::executeWatch, options,
ExecuteWatchResponse::fromXContent, listener, emptySet());
}

/**
* Get the watcher stats
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-stats.html">
Expand Down
Expand Up @@ -25,16 +25,20 @@
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.elasticsearch.client.watcher.DeactivateWatchRequest;
import org.elasticsearch.client.watcher.ActivateWatchRequest;
import org.elasticsearch.client.watcher.AckWatchRequest;
import org.elasticsearch.client.watcher.ActivateWatchRequest;
import org.elasticsearch.client.watcher.DeactivateWatchRequest;
import org.elasticsearch.client.watcher.DeleteWatchRequest;
import org.elasticsearch.client.watcher.ExecuteWatchRequest;
import org.elasticsearch.client.watcher.GetWatchRequest;
import org.elasticsearch.client.watcher.PutWatchRequest;
import org.elasticsearch.client.watcher.StartWatchServiceRequest;
import org.elasticsearch.client.watcher.StopWatchServiceRequest;
import org.elasticsearch.client.watcher.WatcherStatsRequest;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

final class WatcherRequestConverters {

Expand Down Expand Up @@ -108,6 +112,28 @@ static Request deleteWatch(DeleteWatchRequest deleteWatchRequest) {
return request;
}

static Request executeWatch(ExecuteWatchRequest executeWatchRequest) throws IOException {
RequestConverters.EndpointBuilder builder = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_xpack", "watcher", "watch");
builder.addPathPart(executeWatchRequest.getId()); // will ignore if ID is null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_xpack", "watcher", "watch")
.addPathPart(...getId())
.addPathPartAsIs("_execute").build()

String endpoint = builder.addPathPart("_execute").build();

Request request = new Request(HttpPost.METHOD_NAME, endpoint);
RequestConverters.Params params = new RequestConverters.Params(request);
if (executeWatchRequest.isDebug()) {
params.putParam("debug", "true");
}
if (executeWatchRequest.ignoreCondition()) {
params.putParam("ignore_condition", "true");
}
if (executeWatchRequest.recordExecution()) {
params.putParam("record_execution", "true");
}

request.setEntity(RequestConverters.createEntity(executeWatchRequest, XContentType.JSON));
return request;
}

public static Request ackWatch(AckWatchRequest ackWatchRequest) {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_xpack", "watcher", "watch")
Expand Down
@@ -0,0 +1,85 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client.common;

import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* Encapsulates the xcontent source
*/
public class XContentSource {

private final Object data;

/**
* Constructs a new XContentSource out of the given parser
*/
public XContentSource(XContentParser parser) throws IOException {
this.data = XContentUtils.readValue(parser, parser.nextToken());
}

/**
* @return true if the top level value of the source is a map
*/
public boolean isMap() {
return data instanceof Map;
}

/**
* @return The source as a map
*/
@SuppressWarnings("unchecked")
public Map<String, Object> getAsMap() {
return (Map<String, Object>) data;
}

/**
* @return true if the top level value of the source is a list
*/
public boolean isList() {
return data instanceof List;
}

/**
* @return The source as a list
*/
@SuppressWarnings("unchecked")
public List<Object> getAsList() {
return (List<Object>) data;
}

/**
* Extracts a value identified by the given path in the source.
*
* @param path a dot notation path to the requested value
* @return The extracted value or {@code null} if no value is associated with the given path
*/
@SuppressWarnings("unchecked")
public <T> T getValue(String path) {
return (T) ObjectPath.eval(path, data);
}

}
@@ -0,0 +1,175 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client.watcher;

import org.elasticsearch.client.Validatable;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* An execute watch request to execute a watch by id or inline
*/
public class ExecuteWatchRequest implements Validatable, ToXContentObject {

public enum ActionExecutionMode {
SIMULATE, FORCE_SIMULATE, EXECUTE, FORCE_EXECUTE, SKIP
}

private final String id;
private final BytesReference watchContent;

private boolean ignoreCondition = false;
private boolean recordExecution = false;
private boolean debug = false;

@Nullable
private BytesReference triggerData = null;

@Nullable
private BytesReference alternativeInput = null;

private Map<String, ActionExecutionMode> actionModes = new HashMap<>();

/**
* Execute an existing watch on the cluster
*
* @param id the id of the watch to execute
*/
public static ExecuteWatchRequest byId(String id) {
return new ExecuteWatchRequest(Objects.requireNonNull(id, "Watch id cannot be null"), null);
}

/**
* Execute an inline watch
* @param watchContent the JSON definition of the watch
*/
public static ExecuteWatchRequest inline(String watchContent) {
return new ExecuteWatchRequest(null, Objects.requireNonNull(watchContent, "Watch content cannot be null"));
}

private ExecuteWatchRequest(String id, String watchContent) {
this.id = id;
this.watchContent = watchContent == null ? null : new BytesArray(watchContent);
}

public String getId() {
return this.id;
}

/**
* @param ignoreCondition set if the condition for this execution be ignored
*/
public void setIgnoreCondition(boolean ignoreCondition) {
this.ignoreCondition = ignoreCondition;
}

public boolean ignoreCondition() {
return ignoreCondition;
}

/**
* @param recordExecution Sets if this execution be recorded in the history index
*/
public void setRecordExecution(boolean recordExecution) {
if (watchContent != null && recordExecution) {
throw new IllegalArgumentException("The execution of an inline watch cannot be recorded");
}
this.recordExecution = recordExecution;
}

public boolean recordExecution() {
return recordExecution;
}

/**
* @param alternativeInput Sets the alternative input
*/
public void setAlternativeInput(String alternativeInput) {
this.alternativeInput = new BytesArray(alternativeInput);
}

/**
* @param data A JSON string representing the data that should be associated with the trigger event.
*/
public void setTriggerData(String data) {
this.triggerData = new BytesArray(data);
}

/**
* Sets the action execution mode for the give action (identified by its id).
*
* @param actionId the action id.
* @param actionMode the execution mode of the action.
*/
public void setActionMode(String actionId, ActionExecutionMode actionMode) {
Objects.requireNonNull(actionId, "actionId cannot be null");
actionModes.put(actionId, actionMode);
}

public Map<String, ActionExecutionMode> getActionModes() {
return this.actionModes;
}

/**
* @param debug indicates whether the watch should execute in debug mode. In debug mode the
* returned watch record will hold the execution {@code vars}
*/
public void setDebug(boolean debug) {
this.debug = debug;
}

public boolean isDebug() {
return debug;
}

@Override
public String toString() {
return "execute[" + id + "]";
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
if (triggerData != null) {
builder.rawField("trigger_data", triggerData.streamInput(), XContentType.JSON);
}
if (alternativeInput != null) {
builder.rawField("alternative_input", alternativeInput.streamInput(), XContentType.JSON);
}
if (actionModes.size() > 0) {
builder.field("action_modes", actionModes);
}
if (watchContent != null) {
builder.rawField("watch", watchContent.streamInput(), XContentType.JSON);
}
builder.endObject();
return builder;
}
}