Skip to content

Commit f98ecf4

Browse files
committed
Add create rollup job api to high level rest client
This pull request adds the Create Rollup Job API to the high level REST client. It supersedes #32703 and adds dedicated request/response objects so that it does not depend on server side components. Related #29827
1 parent c92ec1c commit f98ecf4

26 files changed

+2738
-11
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java

+13
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public class RestHighLevelClient implements Closeable {
219219
private final MigrationClient migrationClient = new MigrationClient(this);
220220
private final MachineLearningClient machineLearningClient = new MachineLearningClient(this);
221221
private final SecurityClient securityClient = new SecurityClient(this);
222+
private final RollupClient rollupClient = new RollupClient(this);
222223

223224
/**
224225
* Creates a {@link RestHighLevelClient} given the low level {@link RestClientBuilder} that allows to build the
@@ -300,6 +301,18 @@ public final SnapshotClient snapshot() {
300301
return snapshotClient;
301302
}
302303

304+
/**
305+
* Provides methods for accessing the Elastic Licensed Rollup APIs that
306+
* are shipped with the default distribution of Elasticsearch. All of
307+
* these APIs will 404 if run against the OSS distribution of Elasticsearch.
308+
* <p>
309+
* See the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-apis.html">
310+
* Watcher APIs on elastic.co</a> for more information.
311+
*/
312+
public RollupClient rollup() {
313+
return rollupClient;
314+
}
315+
303316
/**
304317
* Provides a {@link TasksClient} which can be used to access the Tasks API.
305318
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client;
21+
22+
import org.elasticsearch.action.ActionListener;
23+
import org.elasticsearch.client.rollup.PutRollupJobRequest;
24+
import org.elasticsearch.client.rollup.PutRollupJobResponse;
25+
26+
import java.io.IOException;
27+
import java.util.Collections;
28+
29+
/**
30+
* A wrapper for the {@link RestHighLevelClient} that provides methods for
31+
* accessing the Elastic Rollup-related methods
32+
* <p>
33+
* See the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-apis.html">
34+
* X-Pack Rollup APIs on elastic.co</a> for more information.
35+
*/
36+
public class RollupClient {
37+
38+
private final RestHighLevelClient restHighLevelClient;
39+
40+
RollupClient(final RestHighLevelClient restHighLevelClient) {
41+
this.restHighLevelClient = restHighLevelClient;
42+
}
43+
44+
/**
45+
* Put a rollup job into the cluster
46+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-put-job.html">
47+
* the docs</a> for more.
48+
* @param request the request
49+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
50+
* @return the response
51+
* @throws IOException in case there is a problem sending the request or parsing back the response
52+
*/
53+
public PutRollupJobResponse putRollupJob(PutRollupJobRequest request, RequestOptions options) throws IOException {
54+
return restHighLevelClient.performRequestAndParseEntity(request,
55+
RollupRequestConverters::putJob,
56+
options,
57+
PutRollupJobResponse::fromXContent,
58+
Collections.emptySet());
59+
}
60+
61+
/**
62+
* Asynchronously put a rollup job into the cluster
63+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-put-job.html">
64+
* the docs</a> for more.
65+
* @param request the request
66+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
67+
* @param listener the listener to be notified upon request completion
68+
*/
69+
public void putRollupJobAsync(PutRollupJobRequest request, RequestOptions options, ActionListener<PutRollupJobResponse> listener) {
70+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
71+
RollupRequestConverters::putJob,
72+
options,
73+
PutRollupJobResponse::fromXContent,
74+
listener, Collections.emptySet());
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client;
20+
21+
import org.apache.http.client.methods.HttpPut;
22+
import org.elasticsearch.client.rollup.PutRollupJobRequest;
23+
24+
import java.io.IOException;
25+
26+
import static org.elasticsearch.client.RequestConverters.REQUEST_BODY_CONTENT_TYPE;
27+
import static org.elasticsearch.client.RequestConverters.createEntity;
28+
29+
final class RollupRequestConverters {
30+
31+
private RollupRequestConverters() {
32+
}
33+
34+
static Request putJob(final PutRollupJobRequest putRollupJobRequest) throws IOException {
35+
String endpoint = new RequestConverters.EndpointBuilder()
36+
.addPathPartAsIs("_xpack")
37+
.addPathPartAsIs("rollup")
38+
.addPathPartAsIs("job")
39+
.addPathPart(putRollupJobRequest.getConfig().getId())
40+
.build();
41+
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
42+
request.setEntity(createEntity(putRollupJobRequest, REQUEST_BODY_CONTENT_TYPE));
43+
return request;
44+
}
45+
}

client/rest-high-level/src/main/java/org/elasticsearch/client/ValidationException.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.elasticsearch.client;
2020

21+
import org.elasticsearch.common.Nullable;
22+
2123
import java.util.ArrayList;
2224
import java.util.List;
2325

@@ -31,10 +33,23 @@ public class ValidationException extends IllegalArgumentException {
3133
* Add a new validation error to the accumulating validation errors
3234
* @param error the error to add
3335
*/
34-
public void addValidationError(String error) {
36+
public void addValidationError(final String error) {
3537
validationErrors.add(error);
3638
}
3739

40+
/**
41+
* Adds validation errors from an existing {@link ValidationException} to
42+
* the accumulating validation errors
43+
* @param exception the {@link ValidationException} to add errors from
44+
*/
45+
public final void addValidationErrors(final @Nullable ValidationException exception) {
46+
if (exception != null) {
47+
for (String error : exception.validationErrors()) {
48+
addValidationError(error);
49+
}
50+
}
51+
}
52+
3853
/**
3954
* Returns the validation errors accumulated
4055
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client.rollup;
20+
21+
import org.elasticsearch.client.Validatable;
22+
import org.elasticsearch.client.ValidationException;
23+
import org.elasticsearch.client.rollup.job.config.RollupJobConfig;
24+
import org.elasticsearch.common.xcontent.ToXContentObject;
25+
import org.elasticsearch.common.xcontent.XContentBuilder;
26+
27+
import java.io.IOException;
28+
import java.util.Objects;
29+
import java.util.Optional;
30+
31+
public class PutRollupJobRequest implements Validatable, ToXContentObject {
32+
33+
private final RollupJobConfig config;
34+
35+
public PutRollupJobRequest(final RollupJobConfig config) {
36+
this.config = Objects.requireNonNull(config, "rollup job configuration is required");
37+
}
38+
39+
public RollupJobConfig getConfig() {
40+
return config;
41+
}
42+
43+
@Override
44+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
45+
return config.toXContent(builder, params);
46+
}
47+
48+
@Override
49+
public Optional<ValidationException> validate() {
50+
return config.validate();
51+
}
52+
53+
@Override
54+
public boolean equals(Object o) {
55+
if (this == o) return true;
56+
if (o == null || getClass() != o.getClass()) return false;
57+
final PutRollupJobRequest that = (PutRollupJobRequest) o;
58+
return Objects.equals(config, that.config);
59+
}
60+
61+
@Override
62+
public int hashCode() {
63+
return Objects.hash(config);
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client.rollup;
20+
21+
import org.elasticsearch.common.ParseField;
22+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
23+
import org.elasticsearch.common.xcontent.ToXContentObject;
24+
import org.elasticsearch.common.xcontent.XContentBuilder;
25+
import org.elasticsearch.common.xcontent.XContentParser;
26+
27+
import java.io.IOException;
28+
import java.util.Objects;
29+
30+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
31+
32+
public class PutRollupJobResponse implements ToXContentObject {
33+
34+
private final boolean acknowledged;
35+
36+
public PutRollupJobResponse(final boolean acknowledged) {
37+
this.acknowledged = acknowledged;
38+
}
39+
40+
public boolean isAcknowledged() {
41+
return acknowledged;
42+
}
43+
44+
@Override
45+
public boolean equals(Object o) {
46+
if (this == o) {
47+
return true;
48+
}
49+
if (o == null || getClass() != o.getClass()) {
50+
return false;
51+
}
52+
final PutRollupJobResponse that = (PutRollupJobResponse) o;
53+
return isAcknowledged() == that.isAcknowledged();
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
return Objects.hash(acknowledged);
59+
}
60+
61+
@Override
62+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
63+
builder.startObject();
64+
{
65+
builder.field("acknowledged", isAcknowledged());
66+
}
67+
builder.endObject();
68+
return builder;
69+
}
70+
71+
private static final ConstructingObjectParser<PutRollupJobResponse, Void> PARSER
72+
= new ConstructingObjectParser<>("put_rollup_job_response", true, args -> new PutRollupJobResponse((boolean) args[0]));
73+
static {
74+
PARSER.declareBoolean(constructorArg(), new ParseField("acknowledged"));
75+
}
76+
77+
public static PutRollupJobResponse fromXContent(final XContentParser parser) throws IOException {
78+
return PARSER.parse(parser, null);
79+
}
80+
}

0 commit comments

Comments
 (0)