Skip to content

Commit bb1d4aa

Browse files
authored
Watcher: Fix put watch action (#31524)
If no version is specified when putting a watch, the index API should be used instead of the update API, so that the whole watch gets overwritten instead of being merged with the existing one. Merging only happens when a version is specified, so that credentials can be omitted, which is important for the watcher UI.
1 parent adfcea2 commit bb1d4aa

File tree

2 files changed

+94
-8
lines changed

2 files changed

+94
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
"Test put watch api without version overwrites watch":
3+
- do:
4+
cluster.health:
5+
wait_for_status: yellow
6+
7+
- do:
8+
xpack.watcher.put_watch:
9+
id: "my_watch"
10+
body: >
11+
{
12+
"trigger": {
13+
"schedule": {
14+
"hourly": {
15+
"minute": [ 0, 5 ]
16+
}
17+
}
18+
},
19+
"input": {
20+
"simple": {
21+
"foo": "bar"
22+
}
23+
},
24+
"actions": {
25+
"logging": {
26+
"logging": {
27+
"text": "yaml test"
28+
}
29+
}
30+
}
31+
}
32+
- match: { _id: "my_watch" }
33+
34+
- do:
35+
xpack.watcher.get_watch:
36+
id: "my_watch"
37+
- match: { watch.input.simple.foo: "bar" }
38+
39+
# change the simple input fields, then ensure the old
40+
# field does not exist on get
41+
- do:
42+
xpack.watcher.put_watch:
43+
id: "my_watch"
44+
body: >
45+
{
46+
"trigger": {
47+
"schedule": {
48+
"hourly": {
49+
"minute": [ 0, 5 ]
50+
}
51+
}
52+
},
53+
"input": {
54+
"simple": {
55+
"spam": "eggs"
56+
}
57+
},
58+
"actions": {
59+
"logging": {
60+
"logging": {
61+
"text": "yaml test"
62+
}
63+
}
64+
}
65+
}
66+
- match: { _id: "my_watch" }
67+
68+
- do:
69+
xpack.watcher.get_watch:
70+
id: "my_watch"
71+
- match: { watch.input.simple.spam: "eggs" }
72+
- is_false: watch.input.simple.foo
73+

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/put/TransportPutWatchAction.java

+21-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import org.elasticsearch.action.ActionListener;
99
import org.elasticsearch.action.DocWriteResponse;
10+
import org.elasticsearch.action.index.IndexRequest;
11+
import org.elasticsearch.action.index.IndexResponse;
1012
import org.elasticsearch.action.support.ActionFilters;
1113
import org.elasticsearch.action.support.WriteRequest;
1214
import org.elasticsearch.action.update.UpdateRequest;
@@ -89,18 +91,29 @@ protected void doExecute(PutWatchRequest request, ActionListener<PutWatchRespons
8991
try (XContentBuilder builder = jsonBuilder()) {
9092
watch.toXContent(builder, DEFAULT_PARAMS);
9193

92-
UpdateRequest updateRequest = new UpdateRequest(Watch.INDEX, Watch.DOC_TYPE, request.getId());
93-
updateRequest.docAsUpsert(isUpdate == false);
94-
updateRequest.version(request.getVersion());
95-
updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
96-
updateRequest.doc(builder);
94+
if (isUpdate) {
95+
UpdateRequest updateRequest = new UpdateRequest(Watch.INDEX, Watch.DOC_TYPE, request.getId());
96+
updateRequest.version(request.getVersion());
97+
updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
98+
updateRequest.doc(builder);
9799

98-
executeAsyncWithOrigin(client.threadPool().getThreadContext(), WATCHER_ORIGIN, updateRequest,
99-
ActionListener.<UpdateResponse>wrap(response -> {
100+
executeAsyncWithOrigin(client.threadPool().getThreadContext(), WATCHER_ORIGIN, updateRequest,
101+
ActionListener.<UpdateResponse>wrap(response -> {
102+
boolean created = response.getResult() == DocWriteResponse.Result.CREATED;
103+
listener.onResponse(new PutWatchResponse(response.getId(), response.getVersion(), created));
104+
}, listener::onFailure),
105+
client::update);
106+
} else {
107+
IndexRequest indexRequest = new IndexRequest(Watch.INDEX, Watch.DOC_TYPE, request.getId());
108+
indexRequest.source(builder);
109+
indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
110+
executeAsyncWithOrigin(client.threadPool().getThreadContext(), WATCHER_ORIGIN, indexRequest,
111+
ActionListener.<IndexResponse>wrap(response -> {
100112
boolean created = response.getResult() == DocWriteResponse.Result.CREATED;
101113
listener.onResponse(new PutWatchResponse(response.getId(), response.getVersion(), created));
102114
}, listener::onFailure),
103-
client::update);
115+
client::index);
116+
}
104117
}
105118
} catch (Exception e) {
106119
listener.onFailure(e);

0 commit comments

Comments
 (0)