Skip to content

HLRC: Add Get Lifecycle Policy API to HLRC #33323

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

Merged
merged 5 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyResponse;
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest;
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusResponse;
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
Expand All @@ -43,6 +45,35 @@ public class IndexLifecycleClient {
this.restHighLevelClient = restHighLevelClient;
}

/**
* Retrieve one or more lifecycle policy definition
* 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 GetLifecyclePolicyResponse getLifecyclePolicy(GetLifecyclePolicyRequest request,
RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::getLifecyclePolicy, options,
GetLifecyclePolicyResponse::fromXContent, emptySet());
}

/**
* Asynchronously retrieve one or more lifecycle policy definition
* 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 getLifecyclePolicyAsync(GetLifecyclePolicyRequest request, RequestOptions options,
ActionListener<GetLifecyclePolicyResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::getLifecyclePolicy, options,
GetLifecyclePolicyResponse::fromXContent, listener, emptySet());
}

/**
* Create or modify a lifecycle definition
* See <a href="https://fix-me-when-we-have-docs.com">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest;
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
Expand Down Expand Up @@ -1164,6 +1165,18 @@ static Request xpackUsage(XPackUsageRequest usageRequest) {
return request;
}

static Request getLifecyclePolicy(GetLifecyclePolicyRequest getLifecyclePolicyRequest) {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_ilm")
.addCommaSeparatedPathParts(getLifecyclePolicyRequest.getPolicyNames())
.build();
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
Params params = new Params(request);
params.withMasterTimeout(getLifecyclePolicyRequest.masterNodeTimeout());
params.withTimeout(getLifecyclePolicyRequest.timeout());
return request;
}

static Request putLifecyclePolicy(PutLifecyclePolicyRequest putLifecycleRequest) throws IOException {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_ilm")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.indexlifecycle;

import org.elasticsearch.client.TimedRequest;
import org.elasticsearch.common.Strings;

import java.util.Arrays;

public class GetLifecyclePolicyRequest extends TimedRequest {

private final String[] policyNames;

public GetLifecyclePolicyRequest(String... policyNames) {
if (policyNames == null) {
this.policyNames = Strings.EMPTY_ARRAY;
} else {
for (String name : policyNames) {
if (name == null) {
throw new IllegalArgumentException("cannot include null policy name");
}
}
this.policyNames = policyNames;
}
}

public String[] getPolicyNames() {
return policyNames;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GetLifecyclePolicyRequest request = (GetLifecyclePolicyRequest) o;
return Arrays.equals(getPolicyNames(), request.getPolicyNames());
}

@Override
public int hashCode() {
return Arrays.hashCode(getPolicyNames());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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.indexlifecycle;

import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;

public class GetLifecyclePolicyResponse implements ToXContentObject {

private final ImmutableOpenMap<String, LifecyclePolicyMetadata> policies;

public GetLifecyclePolicyResponse(ImmutableOpenMap<String, LifecyclePolicyMetadata> policies) {
this.policies = policies;
}

public ImmutableOpenMap<String, LifecyclePolicyMetadata> getPolicies() {
return policies;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this? I dont see it being used anywhere.

Copy link
Contributor Author

@gwbrown gwbrown Sep 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used to leverage AbstractXContentTestCase for testing parsing in GetLifecyclePolicyResponseTests. If there's a better way of doing that, please let me know!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont have a good answer for this yet. While we can totally test this, the value in the server -> client.fromXContent is greater than just testing the client.toXContent -> client.fromXContent. I think these kinds of tests are not really providing much value, and we also test the former in the IT tests.

builder.startObject();
for (ObjectObjectCursor<String, LifecyclePolicyMetadata> stringLifecyclePolicyObjectObjectCursor : policies) {
builder.field(stringLifecyclePolicyObjectObjectCursor.key, stringLifecyclePolicyObjectObjectCursor.value);
}
builder.endObject();
return builder;
}

public static GetLifecyclePolicyResponse fromXContent(XContentParser parser) throws IOException {
ImmutableOpenMap.Builder<String, LifecyclePolicyMetadata> policies = ImmutableOpenMap.builder();

if (parser.currentToken() == null) {
parser.nextToken();
}
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
parser.nextToken();

while (!parser.isClosed()) {
if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
String policyName = parser.currentName();
LifecyclePolicyMetadata policyDefinion = LifecyclePolicyMetadata.parse(parser, policyName);
policies.put(policyName, policyDefinion);
} else {
parser.nextToken();
}
}

return new GetLifecyclePolicyResponse(policies.build());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GetLifecyclePolicyResponse that = (GetLifecyclePolicyResponse) o;
return Objects.equals(getPolicies(), that.getPolicies());
}

@Override
public int hashCode() {
return Objects.hash(getPolicies());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.indexlifecycle;

import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.plugins.spi.NamedXContentProvider;

import java.util.Arrays;
import java.util.List;

public class IndexLifecycleNamedXContentProvider implements NamedXContentProvider {


@Override
public List<NamedXContentRegistry.Entry> getNamedXContentParsers() {
return Arrays.asList(
// ILM
new NamedXContentRegistry.Entry(LifecycleAction.class,
new ParseField(AllocateAction.NAME),
AllocateAction::parse),
new NamedXContentRegistry.Entry(LifecycleAction.class,
new ParseField(DeleteAction.NAME),
DeleteAction::parse),
new NamedXContentRegistry.Entry(LifecycleAction.class,
new ParseField(ForceMergeAction.NAME),
ForceMergeAction::parse),
new NamedXContentRegistry.Entry(LifecycleAction.class,
new ParseField(ReadOnlyAction.NAME),
ReadOnlyAction::parse),
new NamedXContentRegistry.Entry(LifecycleAction.class,
new ParseField(RolloverAction.NAME),
RolloverAction::parse),
new NamedXContentRegistry.Entry(LifecycleAction.class,
new ParseField(ShrinkAction.NAME),
ShrinkAction::parse)
);
}
}
Loading