Skip to content

Commit d435e9d

Browse files
committed
Add create rollup job api to high level rest client (#33521)
This commit 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 a17e0e8 commit d435e9d

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
@@ -217,6 +217,7 @@ public class RestHighLevelClient implements Closeable {
217217
private final MigrationClient migrationClient = new MigrationClient(this);
218218
private final MachineLearningClient machineLearningClient = new MachineLearningClient(this);
219219
private final SecurityClient securityClient = new SecurityClient(this);
220+
private final RollupClient rollupClient = new RollupClient(this);
220221

221222
/**
222223
* Creates a {@link RestHighLevelClient} given the low level {@link RestClientBuilder} that allows to build the
@@ -298,6 +299,18 @@ public final SnapshotClient snapshot() {
298299
return snapshotClient;
299300
}
300301

302+
/**
303+
* Provides methods for accessing the Elastic Licensed Rollup APIs that
304+
* are shipped with the default distribution of Elasticsearch. All of
305+
* these APIs will 404 if run against the OSS distribution of Elasticsearch.
306+
* <p>
307+
* See the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-apis.html">
308+
* Watcher APIs on elastic.co</a> for more information.
309+
*/
310+
public RollupClient rollup() {
311+
return rollupClient;
312+
}
313+
301314
/**
302315
* Provides a {@link TasksClient} which can be used to access the Tasks API.
303316
*
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)