Skip to content

Commit 4804da0

Browse files
authored
Switch x-pack:core to new style Requests (#32252)
In #29623 we added `Request` object flavored requests to the low level REST client and in #30315 we deprecated the old `performRequest`s. This changes all calls in the `x-pack:core` project to use the new versions.
1 parent d43c3a1 commit 4804da0

File tree

5 files changed

+44
-54
lines changed

5 files changed

+44
-54
lines changed

x-pack/plugin/core/src/test/java/org/elasticsearch/license/StartBasicLicenseTests.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.license;
77

8+
import org.elasticsearch.client.Request;
89
import org.elasticsearch.client.Response;
910
import org.elasticsearch.client.ResponseException;
1011
import org.elasticsearch.client.RestClient;
@@ -67,12 +68,14 @@ public void testStartBasicLicense() throws Exception {
6768
}
6869

6970
RestClient restClient = getRestClient();
70-
Response response = restClient.performRequest("GET", "/_xpack/license/basic_status");
71+
Response response = restClient.performRequest(new Request("GET", "/_xpack/license/basic_status"));
7172
String body = Streams.copyToString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8));
7273
assertEquals(200, response.getStatusLine().getStatusCode());
7374
assertEquals("{\"eligible_to_start_basic\":true}", body);
7475

75-
Response response2 = restClient.performRequest("POST", "/_xpack/license/start_basic?acknowledge=true");
76+
Request ackRequest = new Request("POST", "/_xpack/license/start_basic");
77+
ackRequest.addParameter("acknowledge", "true");
78+
Response response2 = restClient.performRequest(ackRequest);
7679
String body2 = Streams.copyToString(new InputStreamReader(response2.getEntity().getContent(), StandardCharsets.UTF_8));
7780
assertEquals(200, response2.getStatusLine().getStatusCode());
7881
assertTrue(body2.contains("\"acknowledged\":true"));
@@ -86,20 +89,19 @@ public void testStartBasicLicense() throws Exception {
8689
long expirationMillis = licensingClient.prepareGetLicense().get().license().expiryDate();
8790
assertEquals(LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS, expirationMillis);
8891

89-
Response response3 = restClient.performRequest("GET", "/_xpack/license");
92+
Response response3 = restClient.performRequest(new Request("GET", "/_xpack/license"));
9093
String body3 = Streams.copyToString(new InputStreamReader(response3.getEntity().getContent(), StandardCharsets.UTF_8));
9194
assertTrue(body3.contains("\"type\" : \"basic\""));
9295
assertFalse(body3.contains("expiry_date"));
9396
assertFalse(body3.contains("expiry_date_in_millis"));
9497

95-
96-
Response response4 = restClient.performRequest("GET", "/_xpack/license/basic_status");
98+
Response response4 = restClient.performRequest(new Request("GET", "/_xpack/license/basic_status"));
9799
String body4 = Streams.copyToString(new InputStreamReader(response4.getEntity().getContent(), StandardCharsets.UTF_8));
98100
assertEquals(200, response3.getStatusLine().getStatusCode());
99101
assertEquals("{\"eligible_to_start_basic\":false}", body4);
100102

101103
ResponseException ex = expectThrows(ResponseException.class,
102-
() -> restClient.performRequest("POST", "/_xpack/license/start_basic"));
104+
() -> restClient.performRequest(new Request("POST", "/_xpack/license/start_basic")));
103105
Response response5 = ex.getResponse();
104106
String body5 = Streams.copyToString(new InputStreamReader(response5.getEntity().getContent(), StandardCharsets.UTF_8));
105107
assertEquals(403, response5.getStatusLine().getStatusCode());
@@ -118,7 +120,7 @@ public void testUnacknowledgedStartBasicLicense() throws Exception {
118120
assertEquals("trial", getLicenseResponse.license().type());
119121
});
120122

121-
Response response2 = getRestClient().performRequest("POST", "/_xpack/license/start_basic");
123+
Response response2 = getRestClient().performRequest(new Request("POST", "/_xpack/license/start_basic"));
122124
String body2 = Streams.copyToString(new InputStreamReader(response2.getEntity().getContent(), StandardCharsets.UTF_8));
123125
assertEquals(200, response2.getStatusLine().getStatusCode());
124126
assertTrue(body2.contains("\"acknowledged\":false"));

x-pack/plugin/core/src/test/java/org/elasticsearch/license/StartTrialLicenseTests.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.license;
77

8+
import org.elasticsearch.client.Request;
89
import org.elasticsearch.client.Response;
910
import org.elasticsearch.client.ResponseException;
1011
import org.elasticsearch.client.RestClient;
@@ -54,13 +55,13 @@ public void testStartTrial() throws Exception {
5455
ensureStartingWithBasic();
5556

5657
RestClient restClient = getRestClient();
57-
Response response = restClient.performRequest("GET", "/_xpack/license/trial_status");
58+
Response response = restClient.performRequest(new Request("GET", "/_xpack/license/trial_status"));
5859
String body = Streams.copyToString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8));
5960
assertEquals(200, response.getStatusLine().getStatusCode());
6061
assertEquals("{\"eligible_to_start_trial\":true}", body);
6162

6263
// Test that starting will fail without acknowledgement
63-
Response response2 = restClient.performRequest("POST", "/_xpack/license/start_trial");
64+
Response response2 = restClient.performRequest(new Request("POST", "/_xpack/license/start_trial"));
6465
String body2 = Streams.copyToString(new InputStreamReader(response2.getEntity().getContent(), StandardCharsets.UTF_8));
6566
assertEquals(200, response2.getStatusLine().getStatusCode());
6667
assertTrue(body2.contains("\"trial_was_started\":false"));
@@ -74,7 +75,10 @@ public void testStartTrial() throws Exception {
7475

7576
String type = randomFrom(LicenseService.VALID_TRIAL_TYPES);
7677

77-
Response response3 = restClient.performRequest("POST", "/_xpack/license/start_trial?acknowledge=true&type=" + type);
78+
Request ackRequest = new Request("POST", "/_xpack/license/start_trial");
79+
ackRequest.addParameter("acknowledge", "true");
80+
ackRequest.addParameter("type", type);
81+
Response response3 = restClient.performRequest(ackRequest);
7882
String body3 = Streams.copyToString(new InputStreamReader(response3.getEntity().getContent(), StandardCharsets.UTF_8));
7983
assertEquals(200, response3.getStatusLine().getStatusCode());
8084
assertTrue(body3.contains("\"trial_was_started\":true"));
@@ -86,15 +90,17 @@ public void testStartTrial() throws Exception {
8690
assertEquals(type, postTrialLicenseResponse.license().type());
8791
});
8892

89-
Response response4 = restClient.performRequest("GET", "/_xpack/license/trial_status");
93+
Response response4 = restClient.performRequest(new Request("GET", "/_xpack/license/trial_status"));
9094
String body4 = Streams.copyToString(new InputStreamReader(response4.getEntity().getContent(), StandardCharsets.UTF_8));
9195
assertEquals(200, response4.getStatusLine().getStatusCode());
9296
assertEquals("{\"eligible_to_start_trial\":false}", body4);
9397

9498
String secondAttemptType = randomFrom(LicenseService.VALID_TRIAL_TYPES);
9599

96-
ResponseException ex = expectThrows(ResponseException.class,
97-
() -> restClient.performRequest("POST", "/_xpack/license/start_trial?acknowledge=true&type=" + secondAttemptType));
100+
Request startTrialWhenStartedRequest = new Request("POST", "/_xpack/license/start_trial");
101+
startTrialWhenStartedRequest.addParameter("acknowledge", "true");
102+
startTrialWhenStartedRequest.addParameter("type", secondAttemptType);
103+
ResponseException ex = expectThrows(ResponseException.class, () -> restClient.performRequest(startTrialWhenStartedRequest));
98104
Response response5 = ex.getResponse();
99105
String body5 = Streams.copyToString(new InputStreamReader(response5.getEntity().getContent(), StandardCharsets.UTF_8));
100106
assertEquals(403, response5.getStatusLine().getStatusCode());
@@ -105,8 +111,9 @@ public void testStartTrial() throws Exception {
105111
public void testInvalidType() throws Exception {
106112
ensureStartingWithBasic();
107113

108-
ResponseException ex = expectThrows(ResponseException.class, () ->
109-
getRestClient().performRequest("POST", "/_xpack/license/start_trial?type=basic"));
114+
Request request = new Request("POST", "/_xpack/license/start_trial");
115+
request.addParameter("type", "basic");
116+
ResponseException ex = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request));
110117
Response response = ex.getResponse();
111118
String body = Streams.copyToString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8));
112119
assertEquals(400, response.getStatusLine().getStatusCode());

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,11 @@ private void deleteAllDatafeeds() throws IOException {
4444
}
4545

4646
try {
47-
int statusCode = adminClient.performRequest("POST", "/_xpack/ml/datafeeds/_all/_stop")
48-
.getStatusLine().getStatusCode();
49-
if (statusCode != 200) {
50-
logger.error("Got status code " + statusCode + " when stopping datafeeds");
51-
}
47+
adminClient.performRequest(new Request("POST", "/_xpack/ml/datafeeds/_all/_stop"));
5248
} catch (Exception e1) {
5349
logger.warn("failed to stop all datafeeds. Forcing stop", e1);
5450
try {
55-
int statusCode = adminClient
56-
.performRequest("POST", "/_xpack/ml/datafeeds/_all/_stop?force=true")
57-
.getStatusLine().getStatusCode();
58-
if (statusCode != 200) {
59-
logger.error("Got status code " + statusCode + " when stopping datafeeds");
60-
}
51+
adminClient.performRequest(new Request("POST", "/_xpack/ml/datafeeds/_all/_stop?force=true"));
6152
} catch (Exception e2) {
6253
logger.warn("Force-closing all data feeds failed", e2);
6354
}
@@ -67,10 +58,7 @@ private void deleteAllDatafeeds() throws IOException {
6758

6859
for (Map<String, Object> datafeed : datafeeds) {
6960
String datafeedId = (String) datafeed.get("datafeed_id");
70-
int statusCode = adminClient.performRequest("DELETE", "/_xpack/ml/datafeeds/" + datafeedId).getStatusLine().getStatusCode();
71-
if (statusCode != 200) {
72-
logger.error("Got status code " + statusCode + " when deleting datafeed " + datafeedId);
73-
}
61+
adminClient.performRequest(new Request("DELETE", "/_xpack/ml/datafeeds/" + datafeedId));
7462
}
7563
}
7664

@@ -86,17 +74,11 @@ private void deleteAllJobs() throws IOException {
8674
}
8775

8876
try {
89-
int statusCode = adminClient
90-
.performRequest("POST", "/_xpack/ml/anomaly_detectors/_all/_close")
91-
.getStatusLine().getStatusCode();
92-
if (statusCode != 200) {
93-
logger.error("Got status code " + statusCode + " when closing all jobs");
94-
}
77+
adminClient.performRequest(new Request("POST", "/_xpack/ml/anomaly_detectors/_all/_close"));
9578
} catch (Exception e1) {
9679
logger.warn("failed to close all jobs. Forcing closed", e1);
9780
try {
98-
adminClient.performRequest("POST",
99-
"/_xpack/ml/anomaly_detectors/_all/_close?force=true");
81+
adminClient.performRequest(new Request("POST", "/_xpack/ml/anomaly_detectors/_all/_close?force=true"));
10082
} catch (Exception e2) {
10183
logger.warn("Force-closing all jobs failed", e2);
10284
}
@@ -106,10 +88,7 @@ private void deleteAllJobs() throws IOException {
10688

10789
for (Map<String, Object> jobConfig : jobConfigs) {
10890
String jobId = (String) jobConfig.get("job_id");
109-
int statusCode = adminClient.performRequest("DELETE", "/_xpack/ml/anomaly_detectors/" + jobId).getStatusLine().getStatusCode();
110-
if (statusCode != 200) {
111-
logger.error("Got status code " + statusCode + " when deleting job " + jobId);
112-
}
91+
adminClient.performRequest(new Request("DELETE", "/_xpack/ml/anomaly_detectors/" + jobId));
11392
}
11493
}
11594
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupRestTestStateCleaner.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.elasticsearch.xpack.core.rollup;
77

88
import org.apache.http.HttpStatus;
9+
import org.elasticsearch.client.Request;
910
import org.elasticsearch.client.Response;
1011
import org.elasticsearch.client.RestClient;
1112
import org.elasticsearch.common.xcontent.support.XContentMapValues;
@@ -17,7 +18,6 @@
1718
import java.io.IOException;
1819
import java.io.InputStreamReader;
1920
import java.nio.charset.StandardCharsets;
20-
import java.util.Collections;
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.stream.Collectors;
@@ -35,8 +35,9 @@ public static void clearRollupMetadata(RestClient adminClient) throws Exception
3535
private static void waitForPendingTasks(RestClient adminClient) throws Exception {
3636
ESTestCase.assertBusy(() -> {
3737
try {
38-
Response response = adminClient.performRequest("GET", "/_cat/tasks",
39-
Collections.singletonMap("detailed", "true"));
38+
Request request = new Request("GET", "/_cat/tasks");
39+
request.addParameter("detailed", "true");
40+
Response response = adminClient.performRequest(request);
4041
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
4142
try (BufferedReader responseReader = new BufferedReader(
4243
new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8))) {
@@ -63,7 +64,7 @@ private static void waitForPendingTasks(RestClient adminClient) throws Exception
6364

6465
@SuppressWarnings("unchecked")
6566
private static void deleteAllJobs(RestClient adminClient) throws Exception {
66-
Response response = adminClient.performRequest("GET", "/_xpack/rollup/job/_all");
67+
Response response = adminClient.performRequest(new Request("GET", "/_xpack/rollup/job/_all"));
6768
Map<String, Object> jobs = ESRestTestCase.entityAsMap(response);
6869
List<Map<String, Object>> jobConfigs =
6970
(List<Map<String, Object>>) XContentMapValues.extractValue("jobs", jobs);
@@ -75,7 +76,7 @@ private static void deleteAllJobs(RestClient adminClient) throws Exception {
7576
for (Map<String, Object> jobConfig : jobConfigs) {
7677
String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
7778
try {
78-
response = adminClient.performRequest("DELETE", "/_xpack/rollup/job/" + jobId);
79+
response = adminClient.performRequest(new Request("DELETE", "/_xpack/rollup/job/" + jobId));
7980
} catch (Exception e) {
8081
// ok
8182
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestTestHelper.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.apache.http.util.EntityUtils;
1111
import org.elasticsearch.Version;
1212
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksAction;
13+
import org.elasticsearch.client.Request;
1314
import org.elasticsearch.client.Response;
1415
import org.elasticsearch.client.ResponseException;
1516
import org.elasticsearch.client.RestClient;
@@ -25,12 +26,10 @@
2526
import java.io.InputStreamReader;
2627
import java.nio.charset.StandardCharsets;
2728
import java.util.Arrays;
28-
import java.util.Collections;
2929
import java.util.List;
3030
import java.util.Map;
3131
import java.util.concurrent.atomic.AtomicReference;
3232

33-
import static java.util.Collections.singletonMap;
3433
import static org.junit.Assert.assertEquals;
3534

3635
public final class XPackRestTestHelper {
@@ -47,8 +46,9 @@ public static void waitForMlTemplates(RestClient client) throws InterruptedExcep
4746
ESTestCase.awaitBusy(() -> {
4847
String response;
4948
try {
50-
response = EntityUtils
51-
.toString(client.performRequest("GET", "/_cat/nodes", singletonMap("h", "master,version")).getEntity());
49+
Request request = new Request("GET", "/_cat/nodes");
50+
request.addParameter("h", "master,version");
51+
response = EntityUtils.toString(client.performRequest(request).getEntity());
5252
} catch (IOException e) {
5353
throw new RuntimeException(e);
5454
}
@@ -67,7 +67,7 @@ public static void waitForMlTemplates(RestClient client) throws InterruptedExcep
6767
ESTestCase.awaitBusy(() -> {
6868
Map<?, ?> response;
6969
try {
70-
String string = EntityUtils.toString(client.performRequest("GET", "/_template/" + template).getEntity());
70+
String string = EntityUtils.toString(client.performRequest(new Request("GET", "/_template/" + template)).getEntity());
7171
response = XContentHelper.convertToMap(JsonXContent.jsonXContent, string, false);
7272
} catch (ResponseException e) {
7373
if (e.getResponse().getStatusLine().getStatusCode() == 404) {
@@ -89,8 +89,9 @@ public static void waitForMlTemplates(RestClient client) throws InterruptedExcep
8989
public static void waitForPendingTasks(RestClient adminClient) throws Exception {
9090
ESTestCase.assertBusy(() -> {
9191
try {
92-
Response response = adminClient.performRequest("GET", "/_cat/tasks",
93-
Collections.singletonMap("detailed", "true"));
92+
Request request = new Request("GET", "/_cat/tasks");
93+
request.addParameter("detailed", "true");
94+
Response response = adminClient.performRequest(request);
9495
// Check to see if there are tasks still active. We exclude the
9596
// list tasks
9697
// actions tasks form this otherwise we will always fail

0 commit comments

Comments
 (0)