-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
6 changed files
with
219 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
client/rest-high-level/src/main/java/org/elasticsearch/client/IngestRequestConverters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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; | ||
|
||
import org.apache.http.client.methods.HttpDelete; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.client.methods.HttpPut; | ||
import org.elasticsearch.action.ingest.DeletePipelineRequest; | ||
import org.elasticsearch.action.ingest.GetPipelineRequest; | ||
import org.elasticsearch.action.ingest.PutPipelineRequest; | ||
import org.elasticsearch.action.ingest.SimulatePipelineRequest; | ||
|
||
import java.io.IOException; | ||
|
||
public class IngestRequestConverters { | ||
|
||
static Request getPipeline(GetPipelineRequest getPipelineRequest) { | ||
String endpoint = new RequestConverters.EndpointBuilder() | ||
.addPathPartAsIs("_ingest/pipeline") | ||
.addCommaSeparatedPathParts(getPipelineRequest.getIds()) | ||
.build(); | ||
Request request = new Request(HttpGet.METHOD_NAME, endpoint); | ||
|
||
RequestConverters.Params parameters = new RequestConverters.Params(request); | ||
parameters.withMasterTimeout(getPipelineRequest.masterNodeTimeout()); | ||
return request; | ||
} | ||
|
||
static Request putPipeline(PutPipelineRequest putPipelineRequest) throws IOException { | ||
String endpoint = new RequestConverters.EndpointBuilder() | ||
.addPathPartAsIs("_ingest/pipeline") | ||
.addPathPart(putPipelineRequest.getId()) | ||
.build(); | ||
Request request = new Request(HttpPut.METHOD_NAME, endpoint); | ||
|
||
RequestConverters.Params parameters = new RequestConverters.Params(request); | ||
parameters.withTimeout(putPipelineRequest.timeout()); | ||
parameters.withMasterTimeout(putPipelineRequest.masterNodeTimeout()); | ||
|
||
request.setEntity(RequestConverters.createEntity(putPipelineRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); | ||
return request; | ||
} | ||
|
||
static Request deletePipeline(DeletePipelineRequest deletePipelineRequest) { | ||
String endpoint = new RequestConverters.EndpointBuilder() | ||
.addPathPartAsIs("_ingest/pipeline") | ||
.addPathPart(deletePipelineRequest.getId()) | ||
.build(); | ||
Request request = new Request(HttpDelete.METHOD_NAME, endpoint); | ||
|
||
RequestConverters.Params parameters = new RequestConverters.Params(request); | ||
parameters.withTimeout(deletePipelineRequest.timeout()); | ||
parameters.withMasterTimeout(deletePipelineRequest.masterNodeTimeout()); | ||
|
||
return request; | ||
} | ||
|
||
static Request simulatePipeline(SimulatePipelineRequest simulatePipelineRequest) throws IOException { | ||
RequestConverters.EndpointBuilder builder = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ingest/pipeline"); | ||
if (simulatePipelineRequest.getId() != null && !simulatePipelineRequest.getId().isEmpty()) { | ||
builder.addPathPart(simulatePipelineRequest.getId()); | ||
} | ||
builder.addPathPartAsIs("_simulate"); | ||
String endpoint = builder.build(); | ||
Request request = new Request(HttpPost.METHOD_NAME, endpoint); | ||
RequestConverters.Params params = new RequestConverters.Params(request); | ||
params.putParam("verbose", Boolean.toString(simulatePipelineRequest.isVerbose())); | ||
request.setEntity(RequestConverters.createEntity(simulatePipelineRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); | ||
return request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
.../rest-high-level/src/test/java/org/elasticsearch/client/IngestRequestConvertersTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* 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; | ||
|
||
import org.apache.http.client.methods.HttpDelete; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.client.methods.HttpPut; | ||
import org.elasticsearch.action.ingest.DeletePipelineRequest; | ||
import org.elasticsearch.action.ingest.GetPipelineRequest; | ||
import org.elasticsearch.action.ingest.PutPipelineRequest; | ||
import org.elasticsearch.action.ingest.SimulatePipelineRequest; | ||
import org.elasticsearch.action.support.master.AcknowledgedRequest; | ||
import org.elasticsearch.common.bytes.BytesArray; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.junit.Assert; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.StringJoiner; | ||
|
||
public class IngestRequestConvertersTests extends ESTestCase { | ||
|
||
public void testPutPipeline() throws IOException { | ||
String pipelineId = "some_pipeline_id"; | ||
PutPipelineRequest request = new PutPipelineRequest( | ||
"some_pipeline_id", | ||
new BytesArray("{}".getBytes(StandardCharsets.UTF_8)), | ||
XContentType.JSON | ||
); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
RequestConvertersTests.setRandomMasterTimeout(request, expectedParams); | ||
RequestConvertersTests.setRandomTimeout(request::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); | ||
|
||
Request expectedRequest = IngestRequestConverters.putPipeline(request); | ||
StringJoiner endpoint = new StringJoiner("/", "/", ""); | ||
endpoint.add("_ingest/pipeline"); | ||
endpoint.add(pipelineId); | ||
Assert.assertEquals(endpoint.toString(), expectedRequest.getEndpoint()); | ||
Assert.assertEquals(HttpPut.METHOD_NAME, expectedRequest.getMethod()); | ||
Assert.assertEquals(expectedParams, expectedRequest.getParameters()); | ||
} | ||
|
||
public void testGetPipeline() { | ||
String pipelineId = "some_pipeline_id"; | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
GetPipelineRequest request = new GetPipelineRequest("some_pipeline_id"); | ||
RequestConvertersTests.setRandomMasterTimeout(request, expectedParams); | ||
Request expectedRequest = IngestRequestConverters.getPipeline(request); | ||
StringJoiner endpoint = new StringJoiner("/", "/", ""); | ||
endpoint.add("_ingest/pipeline"); | ||
endpoint.add(pipelineId); | ||
Assert.assertEquals(endpoint.toString(), expectedRequest.getEndpoint()); | ||
Assert.assertEquals(HttpGet.METHOD_NAME, expectedRequest.getMethod()); | ||
Assert.assertEquals(expectedParams, expectedRequest.getParameters()); | ||
} | ||
|
||
public void testDeletePipeline() { | ||
String pipelineId = "some_pipeline_id"; | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
DeletePipelineRequest request = new DeletePipelineRequest(pipelineId); | ||
RequestConvertersTests.setRandomMasterTimeout(request, expectedParams); | ||
RequestConvertersTests.setRandomTimeout(request::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); | ||
Request expectedRequest = IngestRequestConverters.deletePipeline(request); | ||
StringJoiner endpoint = new StringJoiner("/", "/", ""); | ||
endpoint.add("_ingest/pipeline"); | ||
endpoint.add(pipelineId); | ||
Assert.assertEquals(endpoint.toString(), expectedRequest.getEndpoint()); | ||
Assert.assertEquals(HttpDelete.METHOD_NAME, expectedRequest.getMethod()); | ||
Assert.assertEquals(expectedParams, expectedRequest.getParameters()); | ||
} | ||
|
||
public void testSimulatePipeline() throws IOException { | ||
String pipelineId = ESTestCase.randomBoolean() ? "some_pipeline_id" : null; | ||
boolean verbose = ESTestCase.randomBoolean(); | ||
String json = "{\"pipeline\":{" + | ||
"\"description\":\"_description\"," + | ||
"\"processors\":[{\"set\":{\"field\":\"field2\",\"value\":\"_value\"}}]}," + | ||
"\"docs\":[{\"_index\":\"index\",\"_type\":\"_doc\",\"_id\":\"id\",\"_source\":{\"foo\":\"rab\"}}]}"; | ||
SimulatePipelineRequest request = new SimulatePipelineRequest( | ||
new BytesArray(json.getBytes(StandardCharsets.UTF_8)), | ||
XContentType.JSON | ||
); | ||
request.setId(pipelineId); | ||
request.setVerbose(verbose); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
expectedParams.put("verbose", Boolean.toString(verbose)); | ||
|
||
Request expectedRequest = IngestRequestConverters.simulatePipeline(request); | ||
StringJoiner endpoint = new StringJoiner("/", "/", ""); | ||
endpoint.add("_ingest/pipeline"); | ||
if (pipelineId != null && !pipelineId.isEmpty()) | ||
endpoint.add(pipelineId); | ||
endpoint.add("_simulate"); | ||
Assert.assertEquals(endpoint.toString(), expectedRequest.getEndpoint()); | ||
Assert.assertEquals(HttpPost.METHOD_NAME, expectedRequest.getMethod()); | ||
Assert.assertEquals(expectedParams, expectedRequest.getParameters()); | ||
RequestConvertersTests.assertToXContentBody(request, expectedRequest.getEntity()); | ||
} | ||
} |
Oops, something went wrong.