-
Notifications
You must be signed in to change notification settings - Fork 25.6k
HLRC: Deactivate Watch API #34192
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: Deactivate Watch API #34192
Changes from 14 commits
6a6df68
cb92865
22ebecb
f52c5fa
be75157
590ae61
e829881
6efaed5
476dc7b
b0c977a
b616452
8c3d516
0214d1b
eb1be97
3d1b605
2076f94
4bc9bcb
5cdb181
707434a
e441b66
febaee0
61a9388
f7b7b16
345cecc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * 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.client.ValidationException; | ||
| import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public class DeactivateWatchRequest implements Validatable { | ||
| private final String watchId; | ||
|
|
||
| public DeactivateWatchRequest(String watchId) { | ||
|
|
||
| if (watchId == null) { | ||
| ValidationException exception = new ValidationException(); | ||
| exception.addValidationError("watch id is missing"); | ||
| throw exception; | ||
| } else if (PutWatchRequest.isValidId(watchId) == false) { | ||
| ValidationException exception = new ValidationException(); | ||
| exception.addValidationError("watch id contains whitespace"); | ||
| throw exception; | ||
| } | ||
|
|
||
| this.watchId = watchId; | ||
| } | ||
|
|
||
| public String getWatchId() { | ||
| return watchId; | ||
| } | ||
|
|
||
| // as per discussion https://github.com/elastic/elasticsearch/pull/34192/files#r221994527, keeping validate method as a no-op relic | ||
|
||
| @Override | ||
| public Optional<ValidationException> validate() { | ||
| return Optional.empty(); // empty indicates no validation errors | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * 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.common.ParseField; | ||
| import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
|
|
||
| public class DeactivateWatchResponse { | ||
| private WatchStatus status; | ||
|
|
||
| private static final ParseField STATUS_FIELD = new ParseField("status"); | ||
| private static final ConstructingObjectParser<DeactivateWatchResponse, Void> PARSER | ||
| = new ConstructingObjectParser<>("x_pack_deactivate_watch_response", true, | ||
| (fields) -> new DeactivateWatchResponse((WatchStatus) fields[0])); | ||
| static { | ||
| PARSER.declareObject(ConstructingObjectParser.constructorArg(), | ||
| (parser, context) -> WatchStatus.parse(parser), | ||
| STATUS_FIELD); | ||
| } | ||
|
|
||
| public static DeactivateWatchResponse fromXContent(XContentParser parser) throws IOException { | ||
| return PARSER.parse(parser, null); | ||
| } | ||
|
|
||
| public DeactivateWatchResponse(WatchStatus status) { | ||
| this.status = status; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| DeactivateWatchResponse that = (DeactivateWatchResponse) o; | ||
| return Objects.equals(status, that.status); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(status); | ||
| } | ||
|
|
||
| public WatchStatus getStatus() { | ||
| return status; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,13 +18,17 @@ | |
| */ | ||
| package org.elasticsearch.client; | ||
|
|
||
| import org.elasticsearch.ElasticsearchStatusException; | ||
| import org.elasticsearch.client.watcher.DeactivateWatchRequest; | ||
| import org.elasticsearch.client.watcher.DeactivateWatchResponse; | ||
| import org.elasticsearch.common.bytes.BytesArray; | ||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.xcontent.XContentType; | ||
| import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; | ||
| import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse; | ||
| import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; | ||
| import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; | ||
| import org.elasticsearch.rest.RestStatus; | ||
|
|
||
| import static org.hamcrest.Matchers.is; | ||
|
|
||
|
|
@@ -49,6 +53,23 @@ private PutWatchResponse createWatch(String watchId) throws Exception { | |
| return highLevelClient().watcher().putWatch(putWatchRequest, RequestOptions.DEFAULT); | ||
| } | ||
|
|
||
| public void testDeactivateWatch() throws Exception { | ||
| // Deactivate a watch that exists | ||
| String watchId = randomAlphaOfLength(10); | ||
| createWatch(watchId); | ||
| DeactivateWatchResponse response = highLevelClient().watcher().deactivateWatch( | ||
| new DeactivateWatchRequest(watchId), RequestOptions.DEFAULT); | ||
| assertThat(response.getStatus().state().isActive(), is(false)); | ||
| } | ||
| public void testDeactivateWatch404() throws Exception { | ||
| // Deactivate a watch that does not exist | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. worth adding a second test case for imho |
||
| String watchId = randomAlphaOfLength(10); | ||
| ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, | ||
| () -> highLevelClient().watcher().deactivateWatch(new DeactivateWatchRequest(watchId), RequestOptions.DEFAULT)); | ||
| assertEquals(RestStatus.NOT_FOUND, exception.status()); | ||
|
|
||
| } | ||
|
|
||
| public void testDeleteWatch() throws Exception { | ||
| // delete watch that exists | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * 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.ValidationException; | ||
| import org.elasticsearch.test.ESTestCase; | ||
|
|
||
| import static org.hamcrest.Matchers.hasItem; | ||
|
|
||
| public class DeactivateWatchRequestTests extends ESTestCase { | ||
|
|
||
| public void testNullId() { | ||
| ValidationException actual = expectThrows(ValidationException.class, () -> new DeactivateWatchRequest(null)); | ||
| assertNotNull(actual); | ||
| assertThat(actual.validationErrors(), hasItem("watch id is missing")); | ||
| } | ||
|
|
||
| public void testInvalidId() { | ||
| ValidationException actual = expectThrows(ValidationException.class, () -> new DeactivateWatchRequest("Watch id has spaces")); | ||
| assertNotNull(actual); | ||
| assertThat(actual.validationErrors(), hasItem("watch id contains whitespace")); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * 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.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentFactory; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.common.xcontent.XContentType; | ||
| import org.elasticsearch.test.ESTestCase; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class DeactivateWatchResponseTests extends ESTestCase { | ||
|
|
||
| public void testBasicParsing() throws IOException { | ||
| XContentType contentType = randomFrom(XContentType.values()); | ||
| int version = randomInt(); | ||
| ExecutionState executionState = randomFrom(ExecutionState.values()); | ||
| XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() | ||
| .startObject("status") | ||
| .field("version", version) | ||
| .field("execution_state", executionState) | ||
| .endObject() | ||
| .endObject(); | ||
| BytesReference bytes = BytesReference.bytes(builder); | ||
| DeactivateWatchResponse response = parse(contentType, bytes); | ||
| WatchStatus status = response.getStatus(); | ||
| assertNotNull(status); | ||
| assertEquals(version, status.version()); | ||
| assertEquals(executionState, status.getExecutionState()); | ||
| } | ||
|
|
||
| private DeactivateWatchResponse parse(XContentType contentType, BytesReference bytes) throws IOException { | ||
| XContentParser parser = XContentFactory.xContent(contentType) | ||
| .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); | ||
| parser.nextToken(); | ||
| return DeactivateWatchResponse.fromXContent(parser); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I did not explain myself well enuf. This is good, but we dont need the ValidationException here at all, really. You can instead just use
Objects.requireNotNull(obj, msg)and it will throw exceptions for you. its more concise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so does that mean we don't need to check for white space in the watch id (via
PutWatchRequest.isValidId())?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should also validate this as well in the constructor.