-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HLRC: Move ML request converters into their own class (#32906)
- Loading branch information
1 parent
518cd52
commit 9c0540a
Showing
8 changed files
with
188 additions
and
80 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.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,78 @@ | ||
/* | ||
* 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.HttpPost; | ||
import org.apache.http.client.methods.HttpPut; | ||
import org.elasticsearch.client.RequestConverters.EndpointBuilder; | ||
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest; | ||
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest; | ||
import org.elasticsearch.protocol.xpack.ml.PutJobRequest; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.elasticsearch.client.RequestConverters.REQUEST_BODY_CONTENT_TYPE; | ||
import static org.elasticsearch.client.RequestConverters.createEntity; | ||
|
||
final class MLRequestConverters { | ||
|
||
private MLRequestConverters() {} | ||
|
||
static Request putJob(PutJobRequest putJobRequest) throws IOException { | ||
String endpoint = new EndpointBuilder() | ||
.addPathPartAsIs("_xpack") | ||
.addPathPartAsIs("ml") | ||
.addPathPartAsIs("anomaly_detectors") | ||
.addPathPart(putJobRequest.getJob().getId()) | ||
.build(); | ||
Request request = new Request(HttpPut.METHOD_NAME, endpoint); | ||
request.setEntity(createEntity(putJobRequest, REQUEST_BODY_CONTENT_TYPE)); | ||
return request; | ||
} | ||
|
||
static Request openJob(OpenJobRequest openJobRequest) throws IOException { | ||
String endpoint = new EndpointBuilder() | ||
.addPathPartAsIs("_xpack") | ||
.addPathPartAsIs("ml") | ||
.addPathPartAsIs("anomaly_detectors") | ||
.addPathPart(openJobRequest.getJobId()) | ||
.addPathPartAsIs("_open") | ||
.build(); | ||
Request request = new Request(HttpPost.METHOD_NAME, endpoint); | ||
request.setJsonEntity(openJobRequest.toString()); | ||
return request; | ||
} | ||
|
||
static Request deleteJob(DeleteJobRequest deleteJobRequest) { | ||
String endpoint = new EndpointBuilder() | ||
.addPathPartAsIs("_xpack") | ||
.addPathPartAsIs("ml") | ||
.addPathPartAsIs("anomaly_detectors") | ||
.addPathPart(deleteJobRequest.getJobId()) | ||
.build(); | ||
Request request = new Request(HttpDelete.METHOD_NAME, endpoint); | ||
|
||
RequestConverters.Params params = new RequestConverters.Params(request); | ||
params.putParam("force", Boolean.toString(deleteJobRequest.isForce())); | ||
|
||
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
90 changes: 90 additions & 0 deletions
90
client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.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,90 @@ | ||
/* | ||
* 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.HttpPost; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.json.JsonXContent; | ||
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest; | ||
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest; | ||
import org.elasticsearch.protocol.xpack.ml.PutJobRequest; | ||
import org.elasticsearch.protocol.xpack.ml.job.config.AnalysisConfig; | ||
import org.elasticsearch.protocol.xpack.ml.job.config.Detector; | ||
import org.elasticsearch.protocol.xpack.ml.job.config.Job; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class MLRequestConvertersTests extends ESTestCase { | ||
|
||
public void testPutJob() throws IOException { | ||
Job job = createValidJob("foo"); | ||
PutJobRequest putJobRequest = new PutJobRequest(job); | ||
|
||
Request request = MLRequestConverters.putJob(putJobRequest); | ||
|
||
assertThat(request.getEndpoint(), equalTo("/_xpack/ml/anomaly_detectors/foo")); | ||
try (XContentParser parser = createParser(JsonXContent.jsonXContent, request.getEntity().getContent())) { | ||
Job parsedJob = Job.PARSER.apply(parser, null).build(); | ||
assertThat(parsedJob, equalTo(job)); | ||
} | ||
} | ||
|
||
public void testOpenJob() throws Exception { | ||
String jobId = "some-job-id"; | ||
OpenJobRequest openJobRequest = new OpenJobRequest(jobId); | ||
openJobRequest.setTimeout(TimeValue.timeValueMinutes(10)); | ||
|
||
Request request = MLRequestConverters.openJob(openJobRequest); | ||
assertEquals(HttpPost.METHOD_NAME, request.getMethod()); | ||
assertEquals("/_xpack/ml/anomaly_detectors/" + jobId + "/_open", request.getEndpoint()); | ||
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
request.getEntity().writeTo(bos); | ||
assertEquals(bos.toString("UTF-8"), "{\"job_id\":\""+ jobId +"\",\"timeout\":\"10m\"}"); | ||
} | ||
|
||
public void testDeleteJob() { | ||
String jobId = randomAlphaOfLength(10); | ||
DeleteJobRequest deleteJobRequest = new DeleteJobRequest(jobId); | ||
|
||
Request request = MLRequestConverters.deleteJob(deleteJobRequest); | ||
assertEquals(HttpDelete.METHOD_NAME, request.getMethod()); | ||
assertEquals("/_xpack/ml/anomaly_detectors/" + jobId, request.getEndpoint()); | ||
assertEquals(Boolean.toString(false), request.getParameters().get("force")); | ||
|
||
deleteJobRequest.setForce(true); | ||
request = MLRequestConverters.deleteJob(deleteJobRequest); | ||
assertEquals(Boolean.toString(true), request.getParameters().get("force")); | ||
} | ||
|
||
private static Job createValidJob(String jobId) { | ||
AnalysisConfig.Builder analysisConfig = AnalysisConfig.builder(Collections.singletonList( | ||
Detector.builder().setFunction("count").build())); | ||
Job.Builder jobBuilder = Job.builder(jobId); | ||
jobBuilder.setAnalysisConfig(analysisConfig); | ||
return jobBuilder.build(); | ||
} | ||
} |
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
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