|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client.watcher; |
| 21 | + |
| 22 | +import org.elasticsearch.client.Validatable; |
| 23 | +import org.elasticsearch.common.Nullable; |
| 24 | +import org.elasticsearch.common.bytes.BytesArray; |
| 25 | +import org.elasticsearch.common.bytes.BytesReference; |
| 26 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 27 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 28 | +import org.elasticsearch.common.xcontent.XContentType; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.Objects; |
| 34 | + |
| 35 | +/** |
| 36 | + * An execute watch request to execute a watch by id or inline |
| 37 | + */ |
| 38 | +public class ExecuteWatchRequest implements Validatable, ToXContentObject { |
| 39 | + |
| 40 | + public enum ActionExecutionMode { |
| 41 | + SIMULATE, FORCE_SIMULATE, EXECUTE, FORCE_EXECUTE, SKIP |
| 42 | + } |
| 43 | + |
| 44 | + private final String id; |
| 45 | + private final BytesReference watchContent; |
| 46 | + |
| 47 | + private boolean ignoreCondition = false; |
| 48 | + private boolean recordExecution = false; |
| 49 | + private boolean debug = false; |
| 50 | + |
| 51 | + @Nullable |
| 52 | + private BytesReference triggerData = null; |
| 53 | + |
| 54 | + @Nullable |
| 55 | + private BytesReference alternativeInput = null; |
| 56 | + |
| 57 | + private Map<String, ActionExecutionMode> actionModes = new HashMap<>(); |
| 58 | + |
| 59 | + /** |
| 60 | + * Execute an existing watch on the cluster |
| 61 | + * |
| 62 | + * @param id the id of the watch to execute |
| 63 | + */ |
| 64 | + public static ExecuteWatchRequest byId(String id) { |
| 65 | + return new ExecuteWatchRequest(Objects.requireNonNull(id, "Watch id cannot be null"), null); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Execute an inline watch |
| 70 | + * @param watchContent the JSON definition of the watch |
| 71 | + */ |
| 72 | + public static ExecuteWatchRequest inline(String watchContent) { |
| 73 | + return new ExecuteWatchRequest(null, Objects.requireNonNull(watchContent, "Watch content cannot be null")); |
| 74 | + } |
| 75 | + |
| 76 | + private ExecuteWatchRequest(String id, String watchContent) { |
| 77 | + this.id = id; |
| 78 | + this.watchContent = watchContent == null ? null : new BytesArray(watchContent); |
| 79 | + } |
| 80 | + |
| 81 | + public String getId() { |
| 82 | + return this.id; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param ignoreCondition set if the condition for this execution be ignored |
| 87 | + */ |
| 88 | + public void setIgnoreCondition(boolean ignoreCondition) { |
| 89 | + this.ignoreCondition = ignoreCondition; |
| 90 | + } |
| 91 | + |
| 92 | + public boolean ignoreCondition() { |
| 93 | + return ignoreCondition; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param recordExecution Sets if this execution be recorded in the history index |
| 98 | + */ |
| 99 | + public void setRecordExecution(boolean recordExecution) { |
| 100 | + if (watchContent != null && recordExecution) { |
| 101 | + throw new IllegalArgumentException("The execution of an inline watch cannot be recorded"); |
| 102 | + } |
| 103 | + this.recordExecution = recordExecution; |
| 104 | + } |
| 105 | + |
| 106 | + public boolean recordExecution() { |
| 107 | + return recordExecution; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param alternativeInput Sets the alternative input |
| 112 | + */ |
| 113 | + public void setAlternativeInput(String alternativeInput) { |
| 114 | + this.alternativeInput = new BytesArray(alternativeInput); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @param data A JSON string representing the data that should be associated with the trigger event. |
| 119 | + */ |
| 120 | + public void setTriggerData(String data) { |
| 121 | + this.triggerData = new BytesArray(data); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Sets the action execution mode for the give action (identified by its id). |
| 126 | + * |
| 127 | + * @param actionId the action id. |
| 128 | + * @param actionMode the execution mode of the action. |
| 129 | + */ |
| 130 | + public void setActionMode(String actionId, ActionExecutionMode actionMode) { |
| 131 | + Objects.requireNonNull(actionId, "actionId cannot be null"); |
| 132 | + actionModes.put(actionId, actionMode); |
| 133 | + } |
| 134 | + |
| 135 | + public Map<String, ActionExecutionMode> getActionModes() { |
| 136 | + return this.actionModes; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @param debug indicates whether the watch should execute in debug mode. In debug mode the |
| 141 | + * returned watch record will hold the execution {@code vars} |
| 142 | + */ |
| 143 | + public void setDebug(boolean debug) { |
| 144 | + this.debug = debug; |
| 145 | + } |
| 146 | + |
| 147 | + public boolean isDebug() { |
| 148 | + return debug; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public String toString() { |
| 153 | + return "execute[" + id + "]"; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 158 | + builder.startObject(); |
| 159 | + if (triggerData != null) { |
| 160 | + builder.rawField("trigger_data", triggerData.streamInput(), XContentType.JSON); |
| 161 | + } |
| 162 | + if (alternativeInput != null) { |
| 163 | + builder.rawField("alternative_input", alternativeInput.streamInput(), XContentType.JSON); |
| 164 | + } |
| 165 | + if (actionModes.size() > 0) { |
| 166 | + builder.field("action_modes", actionModes); |
| 167 | + } |
| 168 | + if (watchContent != null) { |
| 169 | + builder.rawField("watch", watchContent.streamInput(), XContentType.JSON); |
| 170 | + } |
| 171 | + builder.endObject(); |
| 172 | + return builder; |
| 173 | + } |
| 174 | +} |
| 175 | + |
0 commit comments