Skip to content

Commit 2da239f

Browse files
authored
[HLRC] Add GetRollupIndexCaps API (#35102)
Also refactors the caps response tests a bit to share the same abstract class to reduce duplication of test code
1 parent e7896bc commit 2da239f

12 files changed

+668
-134
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.elasticsearch.action.ActionListener;
2323
import org.elasticsearch.client.rollup.DeleteRollupJobRequest;
2424
import org.elasticsearch.client.rollup.DeleteRollupJobResponse;
25+
import org.elasticsearch.client.rollup.GetRollupIndexCapsRequest;
26+
import org.elasticsearch.client.rollup.GetRollupIndexCapsResponse;
2527
import org.elasticsearch.client.rollup.GetRollupJobRequest;
2628
import org.elasticsearch.client.rollup.GetRollupJobResponse;
2729
import org.elasticsearch.client.rollup.GetRollupCapsRequest;
@@ -219,4 +221,40 @@ public void getRollupCapabilitiesAsync(GetRollupCapsRequest request, RequestOpti
219221
listener,
220222
Collections.emptySet());
221223
}
224+
225+
/**
226+
* Get the Rollup Index Capabilities of a rollup index or pattern
227+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/master/rollup-get-rollup-index-caps.html">
228+
* the docs</a> for more.
229+
* @param request the request
230+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
231+
* @return the response
232+
* @throws IOException in case there is a problem sending the request or parsing back the response
233+
*/
234+
public GetRollupIndexCapsResponse getRollupIndexCapabilities(GetRollupIndexCapsRequest request,
235+
RequestOptions options) throws IOException {
236+
return restHighLevelClient.performRequestAndParseEntity(request,
237+
RollupRequestConverters::getRollupIndexCaps,
238+
options,
239+
GetRollupIndexCapsResponse::fromXContent,
240+
Collections.emptySet());
241+
}
242+
243+
/**
244+
* Asynchronously Get the Rollup Index Capabilities of a rollup index or pattern
245+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/master/rollup-get-rollup-index-caps.html">
246+
* the docs</a> for more.
247+
* @param request the request
248+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
249+
* @param listener the listener to be notified upon request completion
250+
*/
251+
public void getRollupIndexCapabilitiesAsync(GetRollupIndexCapsRequest request, RequestOptions options,
252+
ActionListener<GetRollupIndexCapsResponse> listener) {
253+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
254+
RollupRequestConverters::getRollupIndexCaps,
255+
options,
256+
GetRollupIndexCapsResponse::fromXContent,
257+
listener,
258+
Collections.emptySet());
259+
}
222260
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.http.client.methods.HttpPut;
2525
import org.elasticsearch.client.rollup.DeleteRollupJobRequest;
2626
import org.elasticsearch.client.rollup.GetRollupCapsRequest;
27+
import org.elasticsearch.client.rollup.GetRollupIndexCapsRequest;
2728
import org.elasticsearch.client.rollup.GetRollupJobRequest;
2829
import org.elasticsearch.client.rollup.PutRollupJobRequest;
2930
import org.elasticsearch.client.rollup.StartRollupJobRequest;
@@ -85,4 +86,14 @@ static Request getRollupCaps(final GetRollupCapsRequest getRollupCapsRequest) th
8586
request.setEntity(createEntity(getRollupCapsRequest, REQUEST_BODY_CONTENT_TYPE));
8687
return request;
8788
}
89+
90+
static Request getRollupIndexCaps(final GetRollupIndexCapsRequest getRollupIndexCapsRequest) throws IOException {
91+
String endpoint = new RequestConverters.EndpointBuilder()
92+
.addCommaSeparatedPathParts(getRollupIndexCapsRequest.indices())
93+
.addPathPartAsIs("_xpack", "rollup", "data")
94+
.build();
95+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
96+
request.setEntity(createEntity(getRollupIndexCapsRequest, REQUEST_BODY_CONTENT_TYPE));
97+
return request;
98+
}
8899
}

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

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
*/
1919
package org.elasticsearch.client.rollup;
2020

21-
import org.elasticsearch.common.Strings;
22-
import org.elasticsearch.common.xcontent.ToXContent;
23-
import org.elasticsearch.common.xcontent.ToXContentObject;
24-
import org.elasticsearch.common.xcontent.XContentBuilder;
2521
import org.elasticsearch.common.xcontent.XContentParser;
2622

2723
import java.io.IOException;
@@ -30,7 +26,7 @@
3026
import java.util.Map;
3127
import java.util.Objects;
3228

33-
public class GetRollupCapsResponse implements ToXContentObject {
29+
public class GetRollupCapsResponse {
3430

3531
private final Map<String, RollableIndexCaps> jobs;
3632

@@ -42,16 +38,6 @@ public Map<String, RollableIndexCaps> getJobs() {
4238
return jobs;
4339
}
4440

45-
@Override
46-
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
47-
builder.startObject();
48-
for (Map.Entry<String, RollableIndexCaps> entry : jobs.entrySet()) {
49-
entry.getValue().toXContent(builder, params);
50-
}
51-
builder.endObject();
52-
return builder;
53-
}
54-
5541
public static GetRollupCapsResponse fromXContent(final XContentParser parser) throws IOException {
5642
Map<String, RollableIndexCaps> jobs = new HashMap<>();
5743
XContentParser.Token token = parser.nextToken();
@@ -84,9 +70,4 @@ public boolean equals(Object obj) {
8470
GetRollupCapsResponse other = (GetRollupCapsResponse) obj;
8571
return Objects.equals(jobs, other.jobs);
8672
}
87-
88-
@Override
89-
public final String toString() {
90-
return Strings.toString(this);
91-
}
9273
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.action.support.IndicesOptions;
22+
import org.elasticsearch.client.Validatable;
23+
import org.elasticsearch.common.Strings;
24+
import org.elasticsearch.common.xcontent.ToXContentObject;
25+
import org.elasticsearch.common.xcontent.XContentBuilder;
26+
27+
import java.io.IOException;
28+
import java.util.Arrays;
29+
import java.util.Objects;
30+
31+
public class GetRollupIndexCapsRequest implements Validatable, ToXContentObject {
32+
private static final String INDICES = "indices";
33+
private static final String INDICES_OPTIONS = "indices_options";
34+
35+
private String[] indices;
36+
private IndicesOptions options;
37+
38+
public GetRollupIndexCapsRequest(final String... indices) {
39+
this(indices, IndicesOptions.STRICT_EXPAND_OPEN_FORBID_CLOSED);
40+
}
41+
42+
public GetRollupIndexCapsRequest(final String[] indices, final IndicesOptions options) {
43+
if (indices == null || indices.length == 0) {
44+
throw new IllegalArgumentException("[indices] must not be null or empty");
45+
}
46+
for (String index : indices) {
47+
if (Strings.isNullOrEmpty(index)) {
48+
throw new IllegalArgumentException("[index] must not be null or empty");
49+
}
50+
}
51+
this.indices = indices;
52+
this.options = Objects.requireNonNull(options);
53+
}
54+
55+
public IndicesOptions indicesOptions() {
56+
return options;
57+
}
58+
59+
public String[] indices() {
60+
return indices;
61+
}
62+
63+
@Override
64+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
65+
builder.startObject();
66+
{
67+
builder.array(INDICES, indices);
68+
builder.startObject(INDICES_OPTIONS);
69+
{
70+
options.toXContent(builder, params);
71+
}
72+
builder.endObject();
73+
}
74+
builder.endObject();
75+
return builder;
76+
}
77+
78+
@Override
79+
public int hashCode() {
80+
return Objects.hash(Arrays.hashCode(indices), options);
81+
}
82+
83+
@Override
84+
public boolean equals(Object obj) {
85+
if (obj == null) {
86+
return false;
87+
}
88+
if (getClass() != obj.getClass()) {
89+
return false;
90+
}
91+
GetRollupIndexCapsRequest other = (GetRollupIndexCapsRequest) obj;
92+
return Arrays.equals(indices, other.indices)
93+
&& Objects.equals(options, other.options);
94+
}
95+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.xcontent.XContentParser;
22+
23+
import java.io.IOException;
24+
import java.util.Collections;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
import java.util.Objects;
28+
29+
public class GetRollupIndexCapsResponse {
30+
31+
private final Map<String, RollableIndexCaps> jobs;
32+
33+
public GetRollupIndexCapsResponse(final Map<String, RollableIndexCaps> jobs) {
34+
this.jobs = Collections.unmodifiableMap(Objects.requireNonNull(jobs));
35+
}
36+
37+
public Map<String, RollableIndexCaps> getJobs() {
38+
return jobs;
39+
}
40+
41+
public static GetRollupIndexCapsResponse fromXContent(final XContentParser parser) throws IOException {
42+
Map<String, RollableIndexCaps> jobs = new HashMap<>();
43+
XContentParser.Token token = parser.nextToken();
44+
if (token.equals(XContentParser.Token.START_OBJECT)) {
45+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
46+
if (token.equals(XContentParser.Token.FIELD_NAME)) {
47+
String pattern = parser.currentName();
48+
49+
RollableIndexCaps cap = RollableIndexCaps.PARSER.apply(pattern).apply(parser, null);
50+
jobs.put(pattern, cap);
51+
}
52+
}
53+
}
54+
return new GetRollupIndexCapsResponse(jobs);
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
return Objects.hash(jobs);
60+
}
61+
62+
@Override
63+
public boolean equals(Object obj) {
64+
if (obj == null) {
65+
return false;
66+
}
67+
if (getClass() != obj.getClass()) {
68+
return false;
69+
}
70+
GetRollupIndexCapsResponse other = (GetRollupIndexCapsResponse) obj;
71+
return Objects.equals(jobs, other.jobs);
72+
}
73+
}

0 commit comments

Comments
 (0)