Skip to content

Commit fab45a4

Browse files
committed
HLRC: split ingest request converters (#33435)
In an effort to encapsulate the different clients, the request converters are being shuffled around. This splits the IngestClient request converters.
1 parent 9371eb7 commit fab45a4

File tree

6 files changed

+219
-150
lines changed

6 files changed

+219
-150
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public final class IngestClient {
5555
* @throws IOException in case there is a problem sending the request or parsing back the response
5656
*/
5757
public AcknowledgedResponse putPipeline(PutPipelineRequest request, RequestOptions options) throws IOException {
58-
return restHighLevelClient.performRequestAndParseEntity( request, RequestConverters::putPipeline, options,
58+
return restHighLevelClient.performRequestAndParseEntity( request, IngestRequestConverters::putPipeline, options,
5959
AcknowledgedResponse::fromXContent, emptySet());
6060
}
6161

@@ -68,7 +68,7 @@ public AcknowledgedResponse putPipeline(PutPipelineRequest request, RequestOptio
6868
* @param listener the listener to be notified upon request completion
6969
*/
7070
public void putPipelineAsync(PutPipelineRequest request, RequestOptions options, ActionListener<AcknowledgedResponse> listener) {
71-
restHighLevelClient.performRequestAsyncAndParseEntity( request, RequestConverters::putPipeline, options,
71+
restHighLevelClient.performRequestAsyncAndParseEntity( request, IngestRequestConverters::putPipeline, options,
7272
AcknowledgedResponse::fromXContent, listener, emptySet());
7373
}
7474

@@ -82,7 +82,7 @@ public void putPipelineAsync(PutPipelineRequest request, RequestOptions options,
8282
* @throws IOException in case there is a problem sending the request or parsing back the response
8383
*/
8484
public GetPipelineResponse getPipeline(GetPipelineRequest request, RequestOptions options) throws IOException {
85-
return restHighLevelClient.performRequestAndParseEntity( request, RequestConverters::getPipeline, options,
85+
return restHighLevelClient.performRequestAndParseEntity( request, IngestRequestConverters::getPipeline, options,
8686
GetPipelineResponse::fromXContent, emptySet());
8787
}
8888

@@ -95,7 +95,7 @@ public GetPipelineResponse getPipeline(GetPipelineRequest request, RequestOption
9595
* @param listener the listener to be notified upon request completion
9696
*/
9797
public void getPipelineAsync(GetPipelineRequest request, RequestOptions options, ActionListener<GetPipelineResponse> listener) {
98-
restHighLevelClient.performRequestAsyncAndParseEntity( request, RequestConverters::getPipeline, options,
98+
restHighLevelClient.performRequestAsyncAndParseEntity( request, IngestRequestConverters::getPipeline, options,
9999
GetPipelineResponse::fromXContent, listener, emptySet());
100100
}
101101

@@ -110,7 +110,7 @@ public void getPipelineAsync(GetPipelineRequest request, RequestOptions options,
110110
* @throws IOException in case there is a problem sending the request or parsing back the response
111111
*/
112112
public AcknowledgedResponse deletePipeline(DeletePipelineRequest request, RequestOptions options) throws IOException {
113-
return restHighLevelClient.performRequestAndParseEntity( request, RequestConverters::deletePipeline, options,
113+
return restHighLevelClient.performRequestAndParseEntity( request, IngestRequestConverters::deletePipeline, options,
114114
AcknowledgedResponse::fromXContent, emptySet());
115115
}
116116

@@ -124,7 +124,7 @@ public AcknowledgedResponse deletePipeline(DeletePipelineRequest request, Reques
124124
* @param listener the listener to be notified upon request completion
125125
*/
126126
public void deletePipelineAsync(DeletePipelineRequest request, RequestOptions options, ActionListener<AcknowledgedResponse> listener) {
127-
restHighLevelClient.performRequestAsyncAndParseEntity( request, RequestConverters::deletePipeline, options,
127+
restHighLevelClient.performRequestAsyncAndParseEntity( request, IngestRequestConverters::deletePipeline, options,
128128
AcknowledgedResponse::fromXContent, listener, emptySet());
129129
}
130130

@@ -140,7 +140,7 @@ public void deletePipelineAsync(DeletePipelineRequest request, RequestOptions op
140140
* @throws IOException in case there is a problem sending the request or parsing back the response
141141
*/
142142
public SimulatePipelineResponse simulate(SimulatePipelineRequest request, RequestOptions options) throws IOException {
143-
return restHighLevelClient.performRequestAndParseEntity( request, RequestConverters::simulatePipeline, options,
143+
return restHighLevelClient.performRequestAndParseEntity( request, IngestRequestConverters::simulatePipeline, options,
144144
SimulatePipelineResponse::fromXContent, emptySet());
145145
}
146146

@@ -157,7 +157,7 @@ public SimulatePipelineResponse simulate(SimulatePipelineRequest request, Reques
157157
public void simulateAsync(SimulatePipelineRequest request,
158158
RequestOptions options,
159159
ActionListener<SimulatePipelineResponse> listener) {
160-
restHighLevelClient.performRequestAsyncAndParseEntity( request, RequestConverters::simulatePipeline, options,
160+
restHighLevelClient.performRequestAsyncAndParseEntity( request, IngestRequestConverters::simulatePipeline, options,
161161
SimulatePipelineResponse::fromXContent, listener, emptySet());
162162
}
163163
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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.apache.http.client.methods.HttpDelete;
23+
import org.apache.http.client.methods.HttpGet;
24+
import org.apache.http.client.methods.HttpPost;
25+
import org.apache.http.client.methods.HttpPut;
26+
import org.elasticsearch.action.ingest.DeletePipelineRequest;
27+
import org.elasticsearch.action.ingest.GetPipelineRequest;
28+
import org.elasticsearch.action.ingest.PutPipelineRequest;
29+
import org.elasticsearch.action.ingest.SimulatePipelineRequest;
30+
31+
import java.io.IOException;
32+
33+
public class IngestRequestConverters {
34+
35+
static Request getPipeline(GetPipelineRequest getPipelineRequest) {
36+
String endpoint = new RequestConverters.EndpointBuilder()
37+
.addPathPartAsIs("_ingest/pipeline")
38+
.addCommaSeparatedPathParts(getPipelineRequest.getIds())
39+
.build();
40+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
41+
42+
RequestConverters.Params parameters = new RequestConverters.Params(request);
43+
parameters.withMasterTimeout(getPipelineRequest.masterNodeTimeout());
44+
return request;
45+
}
46+
47+
static Request putPipeline(PutPipelineRequest putPipelineRequest) throws IOException {
48+
String endpoint = new RequestConverters.EndpointBuilder()
49+
.addPathPartAsIs("_ingest/pipeline")
50+
.addPathPart(putPipelineRequest.getId())
51+
.build();
52+
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
53+
54+
RequestConverters.Params parameters = new RequestConverters.Params(request);
55+
parameters.withTimeout(putPipelineRequest.timeout());
56+
parameters.withMasterTimeout(putPipelineRequest.masterNodeTimeout());
57+
58+
request.setEntity(RequestConverters.createEntity(putPipelineRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
59+
return request;
60+
}
61+
62+
static Request deletePipeline(DeletePipelineRequest deletePipelineRequest) {
63+
String endpoint = new RequestConverters.EndpointBuilder()
64+
.addPathPartAsIs("_ingest/pipeline")
65+
.addPathPart(deletePipelineRequest.getId())
66+
.build();
67+
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
68+
69+
RequestConverters.Params parameters = new RequestConverters.Params(request);
70+
parameters.withTimeout(deletePipelineRequest.timeout());
71+
parameters.withMasterTimeout(deletePipelineRequest.masterNodeTimeout());
72+
73+
return request;
74+
}
75+
76+
static Request simulatePipeline(SimulatePipelineRequest simulatePipelineRequest) throws IOException {
77+
RequestConverters.EndpointBuilder builder = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ingest/pipeline");
78+
if (simulatePipelineRequest.getId() != null && !simulatePipelineRequest.getId().isEmpty()) {
79+
builder.addPathPart(simulatePipelineRequest.getId());
80+
}
81+
builder.addPathPartAsIs("_simulate");
82+
String endpoint = builder.build();
83+
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
84+
RequestConverters.Params params = new RequestConverters.Params(request);
85+
params.putParam("verbose", Boolean.toString(simulatePipelineRequest.isVerbose()));
86+
request.setEntity(RequestConverters.createEntity(simulatePipelineRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
87+
return request;
88+
}
89+
}

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

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@
6464
import org.elasticsearch.action.get.GetRequest;
6565
import org.elasticsearch.action.get.MultiGetRequest;
6666
import org.elasticsearch.action.index.IndexRequest;
67-
import org.elasticsearch.action.ingest.DeletePipelineRequest;
68-
import org.elasticsearch.action.ingest.GetPipelineRequest;
69-
import org.elasticsearch.action.ingest.PutPipelineRequest;
70-
import org.elasticsearch.action.ingest.SimulatePipelineRequest;
7167
import org.elasticsearch.action.search.ClearScrollRequest;
7268
import org.elasticsearch.action.search.MultiSearchRequest;
7369
import org.elasticsearch.action.search.SearchRequest;
@@ -706,47 +702,6 @@ private static Request resize(ResizeRequest resizeRequest) throws IOException {
706702
return request;
707703
}
708704

709-
static Request getPipeline(GetPipelineRequest getPipelineRequest) {
710-
String endpoint = new EndpointBuilder()
711-
.addPathPartAsIs("_ingest/pipeline")
712-
.addCommaSeparatedPathParts(getPipelineRequest.getIds())
713-
.build();
714-
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
715-
716-
Params parameters = new Params(request);
717-
parameters.withMasterTimeout(getPipelineRequest.masterNodeTimeout());
718-
return request;
719-
}
720-
721-
static Request putPipeline(PutPipelineRequest putPipelineRequest) throws IOException {
722-
String endpoint = new EndpointBuilder()
723-
.addPathPartAsIs("_ingest/pipeline")
724-
.addPathPart(putPipelineRequest.getId())
725-
.build();
726-
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
727-
728-
Params parameters = new Params(request);
729-
parameters.withTimeout(putPipelineRequest.timeout());
730-
parameters.withMasterTimeout(putPipelineRequest.masterNodeTimeout());
731-
732-
request.setEntity(createEntity(putPipelineRequest, REQUEST_BODY_CONTENT_TYPE));
733-
return request;
734-
}
735-
736-
static Request deletePipeline(DeletePipelineRequest deletePipelineRequest) {
737-
String endpoint = new EndpointBuilder()
738-
.addPathPartAsIs("_ingest/pipeline")
739-
.addPathPart(deletePipelineRequest.getId())
740-
.build();
741-
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
742-
743-
Params parameters = new Params(request);
744-
parameters.withTimeout(deletePipelineRequest.timeout());
745-
parameters.withMasterTimeout(deletePipelineRequest.masterNodeTimeout());
746-
747-
return request;
748-
}
749-
750705
static Request reindex(ReindexRequest reindexRequest) throws IOException {
751706
String endpoint = new EndpointBuilder().addPathPart("_reindex").build();
752707
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
@@ -923,20 +878,6 @@ static Request validateQuery(ValidateQueryRequest validateQueryRequest) throws I
923878
return request;
924879
}
925880

926-
static Request simulatePipeline(SimulatePipelineRequest simulatePipelineRequest) throws IOException {
927-
EndpointBuilder builder = new EndpointBuilder().addPathPartAsIs("_ingest/pipeline");
928-
if (simulatePipelineRequest.getId() != null && !simulatePipelineRequest.getId().isEmpty()) {
929-
builder.addPathPart(simulatePipelineRequest.getId());
930-
}
931-
builder.addPathPartAsIs("_simulate");
932-
String endpoint = builder.build();
933-
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
934-
Params params = new Params(request);
935-
params.putParam("verbose", Boolean.toString(simulatePipelineRequest.isVerbose()));
936-
request.setEntity(createEntity(simulatePipelineRequest, REQUEST_BODY_CONTENT_TYPE));
937-
return request;
938-
}
939-
940881
static Request getAlias(GetAliasesRequest getAliasesRequest) {
941882
String[] indices = getAliasesRequest.indices() == null ? Strings.EMPTY_ARRAY : getAliasesRequest.indices();
942883
String[] aliases = getAliasesRequest.aliases() == null ? Strings.EMPTY_ARRAY : getAliasesRequest.aliases();

client/rest-high-level/src/test/java/org/elasticsearch/client/ESRestHighLevelClientTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ protected static void createPipeline(String pipelineId) throws IOException {
168168
}
169169

170170
protected static void createPipeline(PutPipelineRequest putPipelineRequest) throws IOException {
171-
assertOK(client().performRequest(RequestConverters.putPipeline(putPipelineRequest)));
171+
assertTrue(execute(
172+
putPipelineRequest, highLevelClient().ingest()::putPipeline, highLevelClient().ingest()::putPipelineAsync).isAcknowledged());
172173
}
173174

174175
protected static void clusterUpdateSettings(Settings persistentSettings,
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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.apache.http.client.methods.HttpDelete;
23+
import org.apache.http.client.methods.HttpGet;
24+
import org.apache.http.client.methods.HttpPost;
25+
import org.apache.http.client.methods.HttpPut;
26+
import org.elasticsearch.action.ingest.DeletePipelineRequest;
27+
import org.elasticsearch.action.ingest.GetPipelineRequest;
28+
import org.elasticsearch.action.ingest.PutPipelineRequest;
29+
import org.elasticsearch.action.ingest.SimulatePipelineRequest;
30+
import org.elasticsearch.action.support.master.AcknowledgedRequest;
31+
import org.elasticsearch.common.bytes.BytesArray;
32+
import org.elasticsearch.common.xcontent.XContentType;
33+
import org.elasticsearch.test.ESTestCase;
34+
import org.junit.Assert;
35+
36+
import java.io.IOException;
37+
import java.nio.charset.StandardCharsets;
38+
import java.util.HashMap;
39+
import java.util.Map;
40+
import java.util.StringJoiner;
41+
42+
public class IngestRequestConvertersTests extends ESTestCase {
43+
44+
public void testPutPipeline() throws IOException {
45+
String pipelineId = "some_pipeline_id";
46+
PutPipelineRequest request = new PutPipelineRequest(
47+
"some_pipeline_id",
48+
new BytesArray("{}".getBytes(StandardCharsets.UTF_8)),
49+
XContentType.JSON
50+
);
51+
Map<String, String> expectedParams = new HashMap<>();
52+
RequestConvertersTests.setRandomMasterTimeout(request, expectedParams);
53+
RequestConvertersTests.setRandomTimeout(request::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
54+
55+
Request expectedRequest = IngestRequestConverters.putPipeline(request);
56+
StringJoiner endpoint = new StringJoiner("/", "/", "");
57+
endpoint.add("_ingest/pipeline");
58+
endpoint.add(pipelineId);
59+
Assert.assertEquals(endpoint.toString(), expectedRequest.getEndpoint());
60+
Assert.assertEquals(HttpPut.METHOD_NAME, expectedRequest.getMethod());
61+
Assert.assertEquals(expectedParams, expectedRequest.getParameters());
62+
}
63+
64+
public void testGetPipeline() {
65+
String pipelineId = "some_pipeline_id";
66+
Map<String, String> expectedParams = new HashMap<>();
67+
GetPipelineRequest request = new GetPipelineRequest("some_pipeline_id");
68+
RequestConvertersTests.setRandomMasterTimeout(request, expectedParams);
69+
Request expectedRequest = IngestRequestConverters.getPipeline(request);
70+
StringJoiner endpoint = new StringJoiner("/", "/", "");
71+
endpoint.add("_ingest/pipeline");
72+
endpoint.add(pipelineId);
73+
Assert.assertEquals(endpoint.toString(), expectedRequest.getEndpoint());
74+
Assert.assertEquals(HttpGet.METHOD_NAME, expectedRequest.getMethod());
75+
Assert.assertEquals(expectedParams, expectedRequest.getParameters());
76+
}
77+
78+
public void testDeletePipeline() {
79+
String pipelineId = "some_pipeline_id";
80+
Map<String, String> expectedParams = new HashMap<>();
81+
DeletePipelineRequest request = new DeletePipelineRequest(pipelineId);
82+
RequestConvertersTests.setRandomMasterTimeout(request, expectedParams);
83+
RequestConvertersTests.setRandomTimeout(request::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
84+
Request expectedRequest = IngestRequestConverters.deletePipeline(request);
85+
StringJoiner endpoint = new StringJoiner("/", "/", "");
86+
endpoint.add("_ingest/pipeline");
87+
endpoint.add(pipelineId);
88+
Assert.assertEquals(endpoint.toString(), expectedRequest.getEndpoint());
89+
Assert.assertEquals(HttpDelete.METHOD_NAME, expectedRequest.getMethod());
90+
Assert.assertEquals(expectedParams, expectedRequest.getParameters());
91+
}
92+
93+
public void testSimulatePipeline() throws IOException {
94+
String pipelineId = ESTestCase.randomBoolean() ? "some_pipeline_id" : null;
95+
boolean verbose = ESTestCase.randomBoolean();
96+
String json = "{\"pipeline\":{" +
97+
"\"description\":\"_description\"," +
98+
"\"processors\":[{\"set\":{\"field\":\"field2\",\"value\":\"_value\"}}]}," +
99+
"\"docs\":[{\"_index\":\"index\",\"_type\":\"_doc\",\"_id\":\"id\",\"_source\":{\"foo\":\"rab\"}}]}";
100+
SimulatePipelineRequest request = new SimulatePipelineRequest(
101+
new BytesArray(json.getBytes(StandardCharsets.UTF_8)),
102+
XContentType.JSON
103+
);
104+
request.setId(pipelineId);
105+
request.setVerbose(verbose);
106+
Map<String, String> expectedParams = new HashMap<>();
107+
expectedParams.put("verbose", Boolean.toString(verbose));
108+
109+
Request expectedRequest = IngestRequestConverters.simulatePipeline(request);
110+
StringJoiner endpoint = new StringJoiner("/", "/", "");
111+
endpoint.add("_ingest/pipeline");
112+
if (pipelineId != null && !pipelineId.isEmpty())
113+
endpoint.add(pipelineId);
114+
endpoint.add("_simulate");
115+
Assert.assertEquals(endpoint.toString(), expectedRequest.getEndpoint());
116+
Assert.assertEquals(HttpPost.METHOD_NAME, expectedRequest.getMethod());
117+
Assert.assertEquals(expectedParams, expectedRequest.getParameters());
118+
RequestConvertersTests.assertToXContentBody(request, expectedRequest.getEntity());
119+
}
120+
}

0 commit comments

Comments
 (0)