forked from elastic/elasticsearch
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add high level rest client support for SetIndexLifecyclePolicy
This adds HLRC support for the ILM operation of setting an index's lifecycle policy. It also includes extracting and renaming a number of classes (like the request and response objects) as well as the addition of a new `IndexLifecycleClient` for the HLRC. This is a prerequisite to making the `index.lifecycle.name` setting internal only, because we require a dedicated REST endpoint to change the policy, and our tests currently set this setting with the REST client multiple places. A subsequent PR will change the setting to be internal and move those uses over to this new API. This misses some links to the documentation because I don't think ILM has any documentation available yet. Relates to elastic#29827 and elastic#29823
- Loading branch information
Showing
16 changed files
with
622 additions
and
314 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyRequest; | ||
import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyResponse; | ||
|
||
import java.io.IOException; | ||
|
||
import static java.util.Collections.emptySet; | ||
|
||
public class IndexLifecycleClient { | ||
private final RestHighLevelClient restHighLevelClient; | ||
|
||
IndexLifecycleClient(RestHighLevelClient restHighLevelClient) { | ||
this.restHighLevelClient = restHighLevelClient; | ||
} | ||
|
||
/** | ||
* Set the index lifecycle policy for an index | ||
* See <a href="https://fix-me-when-we-have-docs.com"> | ||
* 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 in case there is a problem sending the request or parsing back the response | ||
*/ | ||
public SetIndexLifecyclePolicyResponse setIndexLifecyclePolicy(SetIndexLifecyclePolicyRequest request, | ||
RequestOptions options) throws IOException { | ||
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::setIndexLifecyclePolicy, options, | ||
SetIndexLifecyclePolicyResponse::fromXContent, emptySet()); | ||
} | ||
|
||
/** | ||
* Asynchronously set the index lifecycle policy for an index | ||
* See <a href="https://fix-me-when-we-have-docs.com"> | ||
* 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 notified upon request completion | ||
*/ | ||
public void setIndexLifecyclePolicyAsync(SetIndexLifecyclePolicyRequest request, RequestOptions options, | ||
ActionListener<SetIndexLifecyclePolicyResponse> listener) { | ||
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::setIndexLifecyclePolicy, options, | ||
SetIndexLifecyclePolicyResponse::fromXContent, listener, emptySet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* 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.apache.http.HttpEntity; | ||
import org.apache.http.entity.ContentType; | ||
import org.apache.http.nio.entity.NStringEntity; | ||
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; | ||
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyRequest; | ||
import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyResponse; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class IndexLifecycleIT extends ESRestHighLevelClientTestCase { | ||
|
||
public void testSetIndexLifecyclePolicy() throws Exception { | ||
String policy = randomAlphaOfLength(10); | ||
|
||
// TODO: convert this to using the high level client once there are APIs for it | ||
String jsonString = "{\n" + | ||
" \"policy\": {\n" + | ||
" \"type\": \"timeseries\",\n" + | ||
" \"phases\": {\n" + | ||
" \"hot\": {\n" + | ||
" \"after\": \"60s\",\n" + | ||
" \"actions\": {\n" + | ||
" \"rollover\": {\n" + | ||
" \"max_age\": \"500s\"\n" + | ||
" } \n" + | ||
" }\n" + | ||
" },\n" + | ||
" \"warm\": {\n" + | ||
" \"after\": \"1000s\",\n" + | ||
" \"actions\": {\n" + | ||
" \"allocate\": {\n" + | ||
" \"require\": { \"_name\": \"node-1\" },\n" + | ||
" \"include\": {},\n" + | ||
" \"exclude\": {}\n" + | ||
" },\n" + | ||
" \"shrink\": {\n" + | ||
" \"number_of_shards\": 1\n" + | ||
" },\n" + | ||
" \"forcemerge\": {\n" + | ||
" \"max_num_segments\": 1000\n" + | ||
" }\n" + | ||
" }\n" + | ||
" },\n" + | ||
" \"cold\": {\n" + | ||
" \"after\": \"2000s\",\n" + | ||
" \"actions\": {\n" + | ||
" \"replicas\": {\n" + | ||
" \"number_of_replicas\": 0\n" + | ||
" }\n" + | ||
" }\n" + | ||
" },\n" + | ||
" \"delete\": {\n" + | ||
" \"after\": \"3000s\",\n" + | ||
" \"actions\": {\n" + | ||
" \"delete\": {}\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}"; | ||
HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON); | ||
Request request = new Request("PUT", "/_xpack/index_lifecycle/" + policy); | ||
request.setEntity(entity); | ||
client().performRequest(request); | ||
|
||
createIndex("foo", Settings.builder().put("index.lifecycle.name", "bar").build()); | ||
createIndex("baz", Settings.builder().put("index.lifecycle.name", "eggplant").build()); | ||
SetIndexLifecyclePolicyRequest req = new SetIndexLifecyclePolicyRequest(policy, "foo", "baz"); | ||
SetIndexLifecyclePolicyResponse response = execute(req, highLevelClient().xpack().indexLifecycle()::setIndexLifecyclePolicy, | ||
highLevelClient().xpack().indexLifecycle()::setIndexLifecyclePolicyAsync); | ||
assertThat(response.hasFailures(), is(false)); | ||
assertThat(response.getFailedIndexes().isEmpty(), is(true)); | ||
|
||
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices("foo", "baz"); | ||
GetSettingsResponse settingsResponse = highLevelClient().indices().getSettings(getSettingsRequest, RequestOptions.DEFAULT); | ||
assertThat(settingsResponse.getSetting("foo", "index.lifecycle.name"), equalTo(policy)); | ||
assertThat(settingsResponse.getSetting("baz", "index.lifecycle.name"), equalTo(policy)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...ava/org/elasticsearch/xpack/core/indexlifecycle/action/SetIndexLifecyclePolicyAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.indexlifecycle.action; | ||
|
||
import org.elasticsearch.action.Action; | ||
import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyResponse; | ||
|
||
public class SetIndexLifecyclePolicyAction extends Action<SetIndexLifecyclePolicyResponse> { | ||
|
||
public static final SetIndexLifecyclePolicyAction INSTANCE = new SetIndexLifecyclePolicyAction(); | ||
public static final String NAME = "indices:admin/xpack/index_lifecycle/set_index_policy"; | ||
|
||
protected SetIndexLifecyclePolicyAction() { | ||
super(NAME); | ||
} | ||
|
||
@Override | ||
public SetIndexLifecyclePolicyResponse newResponse() { | ||
return new SetIndexLifecyclePolicyResponse(); | ||
} | ||
} |
Oops, something went wrong.