From f1f26abafe9dc53d57933bb9cc275f32d11393cd Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 15 Nov 2018 15:21:44 +0100 Subject: [PATCH] Add high-level REST client API for `_freeze` and `_unfreeze` This change adds support for `_freeze` and `_unfreeze` to the HLRC Relates to #34352 --- .../client/FreezeIndexRequest.java | 44 ++++++++++++++ .../elasticsearch/client/IndicesClient.java | 46 +++++++++++++++ .../client/IndicesRequestConverters.java | 16 ++++++ .../client/ShardAcknowledgedResponse.java | 57 +++++++++++++++++++ .../client/UnfreezeIndexRequest.java | 44 ++++++++++++++ .../elasticsearch/client/IndicesClientIT.java | 13 +++++ .../client/RestHighLevelClientTests.java | 3 +- 7 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/FreezeIndexRequest.java create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/ShardAcknowledgedResponse.java create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/UnfreezeIndexRequest.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/FreezeIndexRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/FreezeIndexRequest.java new file mode 100644 index 0000000000000..c9222d9c1d27c --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/FreezeIndexRequest.java @@ -0,0 +1,44 @@ +/* + * 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; + +import java.util.Objects; + +/** + * Request for the _freeze index API + */ +public final class FreezeIndexRequest implements Validatable { + + private final String indices; + + /** + * Creates a new freeze index request + * @param indices the index to freeze + */ + public FreezeIndexRequest(String indices) { + this.indices = Objects.requireNonNull(indices); + } + + /** + * Returns the indices to freeze + */ + public String getIndices() { + return indices; + } +} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java index 3811ba783445d..d9f70db29c39b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java @@ -836,4 +836,50 @@ public void analyzeAsync(AnalyzeRequest request, RequestOptions options, restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::analyze, options, AnalyzeResponse::fromXContent, listener, emptySet()); } + + /** + * Asynchronously calls the _freeze API + * + * @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 notified upon request completion + */ + public void freezeAsync(FreezeIndexRequest request, RequestOptions options, ActionListener listener) { + restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::freezeIndex, options, + ShardAcknowledgedResponse::fromXContent, listener, emptySet()); + } + + /** + * Asynchronously calls the _unfreeze API + * + * @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 notified upon request completion + */ + public void unfreezeAsync(UnfreezeIndexRequest request, RequestOptions options, ActionListener listener) { + restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::unFreezeIndex, options, + ShardAcknowledgedResponse::fromXContent, listener, emptySet()); + } + + /** + * Synchronously calls the _freeze API + * + * @param request the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + */ + public ShardAcknowledgedResponse freeze(FreezeIndexRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::freezeIndex, options, + ShardAcknowledgedResponse::fromXContent, emptySet()); + } + + /** + * Synchronously calls the _freeze API + * + * @param request the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + */ + public ShardAcknowledgedResponse unfreeze(UnfreezeIndexRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::unFreezeIndex, options, + ShardAcknowledgedResponse::fromXContent, emptySet()); + } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java index ea81c88f8fef7..2879a8962fc37 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java @@ -403,4 +403,20 @@ static Request analyze(AnalyzeRequest request) throws IOException { req.setEntity(RequestConverters.createEntity(request, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); return req; } + + static Request freezeIndex(FreezeIndexRequest freezeIndexRequest) { + String endpoint = new RequestConverters.EndpointBuilder() + .addPathPart(freezeIndexRequest.getIndices()) + .addPathPartAsIs("_freeze") + .build(); + return new Request(HttpPost.METHOD_NAME, endpoint); + } + + static Request unFreezeIndex(UnfreezeIndexRequest unFreezeIndexRequest) { + String endpoint = new RequestConverters.EndpointBuilder() + .addPathPart(unFreezeIndexRequest.getIndices()) + .addPathPartAsIs("_unfreeze") + .build(); + return new Request(HttpPost.METHOD_NAME, endpoint); + } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ShardAcknowledgedResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ShardAcknowledgedResponse.java new file mode 100644 index 0000000000000..8171fb2fb0459 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ShardAcknowledgedResponse.java @@ -0,0 +1,57 @@ +/* + * 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; + +import org.elasticsearch.client.core.AcknowledgedResponse; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; + +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; + +public class ShardAcknowledgedResponse extends AcknowledgedResponse implements Validatable { + + private static ConstructingObjectParser buildParser() { + + ConstructingObjectParser p = new ConstructingObjectParser<>("freeze", true, + args -> new ShardAcknowledgedResponse((boolean) args[0], (boolean) args[1])); + p.declareBoolean(constructorArg(), new ParseField(AcknowledgedResponse.PARSE_FIELD_NAME)); + p.declareBoolean(constructorArg(), new ParseField("shards_acknowledged")); + return p; + } + + private static final ConstructingObjectParser PARSER = buildParser(); + + private final boolean shardsAcknowledged; + + public ShardAcknowledgedResponse(boolean acknowledged, boolean shardsAcknowledged) { + super(acknowledged); + this.shardsAcknowledged = shardsAcknowledged; + } + + public boolean isShardsAcknowledged() { + return shardsAcknowledged; + } + + public static ShardAcknowledgedResponse fromXContent(XContentParser parser) throws IOException { + return PARSER.parse(parser, null); + } +} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/UnfreezeIndexRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/UnfreezeIndexRequest.java new file mode 100644 index 0000000000000..456053808320a --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/UnfreezeIndexRequest.java @@ -0,0 +1,44 @@ +/* + * 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; + +import java.util.Objects; + +/** + * Request for the _unfreeze index API + */ +public final class UnfreezeIndexRequest implements Validatable { + + private final String indices; + + /** + * Creates a new unfreeze index request + * @param indices the index to unfreeze + */ + public UnfreezeIndexRequest(String indices) { + this.indices = Objects.requireNonNull(indices); + } + + /** + * Returns the indices to unfreeze + */ + public String getIndices() { + return indices; + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 053f46f8496b0..e6e5f0494b7dc 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -1370,6 +1370,19 @@ public void testAnalyze() throws Exception { AnalyzeResponse detailsResponse = execute(detailsRequest, client.indices()::analyze, client.indices()::analyzeAsync); assertNotNull(detailsResponse.detail()); + } + + public void testFreezeAndUnfreeze() throws IOException { + createIndex("test", Settings.EMPTY); + RestHighLevelClient client = highLevelClient(); + + ShardAcknowledgedResponse freeze = execute(new FreezeIndexRequest("test"), client.indices()::freeze, client.indices()::freezeAsync); + assertTrue(freeze.isShardsAcknowledged()); + assertTrue(freeze.isAcknowledged()); + ShardAcknowledgedResponse unfreeze = execute(new UnfreezeIndexRequest("test"), client.indices()::unfreeze, + client.indices()::unfreezeAsync); + assertTrue(unfreeze.isShardsAcknowledged()); + assertTrue(unfreeze.isAcknowledged()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java index 56788e2eb769f..119a50695ec20 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java @@ -751,7 +751,8 @@ public void testApiNamingConventions() throws Exception { apiName.startsWith("migration.") == false && apiName.startsWith("security.") == false && apiName.startsWith("index_lifecycle.") == false && - apiName.startsWith("ccr.") == false) { + apiName.startsWith("ccr.") == false && + apiName.endsWith("freeze") == false) { apiNotFound.add(apiName); } }