Skip to content

Commit ad4bbb1

Browse files
committed
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 b982e1a commit ad4bbb1

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

+9-7
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;
@@ -64,12 +65,14 @@ public void testStartBasicLicense() throws Exception {
6465
}
6566

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

72-
Response response2 = restClient.performRequest("POST", "/_xpack/license/start_basic?acknowledge=true");
73+
Request ackRequest = new Request("POST", "/_xpack/license/start_basic");
74+
ackRequest.addParameter("acknowledge", "true");
75+
Response response2 = restClient.performRequest(ackRequest);
7376
String body2 = Streams.copyToString(new InputStreamReader(response2.getEntity().getContent(), StandardCharsets.UTF_8));
7477
assertEquals(200, response2.getStatusLine().getStatusCode());
7578
assertTrue(body2.contains("\"acknowledged\":true"));
@@ -83,20 +86,19 @@ public void testStartBasicLicense() throws Exception {
8386
long expirationMillis = licensingClient.prepareGetLicense().get().license().expiryDate();
8487
assertEquals(LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS, expirationMillis);
8588

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

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

98100
ResponseException ex = expectThrows(ResponseException.class,
99-
() -> restClient.performRequest("POST", "/_xpack/license/start_basic"));
101+
() -> restClient.performRequest(new Request("POST", "/_xpack/license/start_basic")));
100102
Response response5 = ex.getResponse();
101103
String body5 = Streams.copyToString(new InputStreamReader(response5.getEntity().getContent(), StandardCharsets.UTF_8));
102104
assertEquals(403, response5.getStatusLine().getStatusCode());
@@ -115,7 +117,7 @@ public void testUnacknowledgedStartBasicLicense() throws Exception {
115117
assertEquals("trial", getLicenseResponse.license().type());
116118
});
117119

118-
Response response2 = getRestClient().performRequest("POST", "/_xpack/license/start_basic");
120+
Response response2 = getRestClient().performRequest(new Request("POST", "/_xpack/license/start_basic"));
119121
String body2 = Streams.copyToString(new InputStreamReader(response2.getEntity().getContent(), StandardCharsets.UTF_8));
120122
assertEquals(200, response2.getStatusLine().getStatusCode());
121123
assertTrue(body2.contains("\"acknowledged\":false"));

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

+15-8
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;
@@ -51,13 +52,13 @@ public void testStartTrial() throws Exception {
5152
ensureStartingWithBasic();
5253

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

5960
// Test that starting will fail without acknowledgement
60-
Response response2 = restClient.performRequest("POST", "/_xpack/license/start_trial");
61+
Response response2 = restClient.performRequest(new Request("POST", "/_xpack/license/start_trial"));
6162
String body2 = Streams.copyToString(new InputStreamReader(response2.getEntity().getContent(), StandardCharsets.UTF_8));
6263
assertEquals(200, response2.getStatusLine().getStatusCode());
6364
assertTrue(body2.contains("\"trial_was_started\":false"));
@@ -71,7 +72,10 @@ public void testStartTrial() throws Exception {
7172

7273
String type = randomFrom(LicenseService.VALID_TRIAL_TYPES);
7374

74-
Response response3 = restClient.performRequest("POST", "/_xpack/license/start_trial?acknowledge=true&type=" + type);
75+
Request ackRequest = new Request("POST", "/_xpack/license/start_trial");
76+
ackRequest.addParameter("acknowledge", "true");
77+
ackRequest.addParameter("type", type);
78+
Response response3 = restClient.performRequest(ackRequest);
7579
String body3 = Streams.copyToString(new InputStreamReader(response3.getEntity().getContent(), StandardCharsets.UTF_8));
7680
assertEquals(200, response3.getStatusLine().getStatusCode());
7781
assertTrue(body3.contains("\"trial_was_started\":true"));
@@ -83,15 +87,17 @@ public void testStartTrial() throws Exception {
8387
assertEquals(type, postTrialLicenseResponse.license().type());
8488
});
8589

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

9195
String secondAttemptType = randomFrom(LicenseService.VALID_TRIAL_TYPES);
9296

93-
ResponseException ex = expectThrows(ResponseException.class,
94-
() -> restClient.performRequest("POST", "/_xpack/license/start_trial?acknowledge=true&type=" + secondAttemptType));
97+
Request startTrialWhenStartedRequest = new Request("POST", "/_xpack/license/start_trial");
98+
startTrialWhenStartedRequest.addParameter("acknowledge", "true");
99+
startTrialWhenStartedRequest.addParameter("type", secondAttemptType);
100+
ResponseException ex = expectThrows(ResponseException.class, () -> restClient.performRequest(startTrialWhenStartedRequest));
95101
Response response5 = ex.getResponse();
96102
String body5 = Streams.copyToString(new InputStreamReader(response5.getEntity().getContent(), StandardCharsets.UTF_8));
97103
assertEquals(403, response5.getStatusLine().getStatusCode());
@@ -102,8 +108,9 @@ public void testStartTrial() throws Exception {
102108
public void testInvalidType() throws Exception {
103109
ensureStartingWithBasic();
104110

105-
ResponseException ex = expectThrows(ResponseException.class, () ->
106-
getRestClient().performRequest("POST", "/_xpack/license/start_trial?type=basic"));
111+
Request request = new Request("POST", "/_xpack/license/start_trial");
112+
request.addParameter("type", "basic");
113+
ResponseException ex = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request));
107114
Response response = ex.getResponse();
108115
String body = Streams.copyToString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8));
109116
assertEquals(400, response.getStatusLine().getStatusCode());

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

+6-27
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,11 @@ private void deleteAllDatafeeds() throws IOException {
4545
}
4646

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

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

@@ -87,17 +75,11 @@ private void deleteAllJobs() throws IOException {
8775
}
8876

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

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

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

+6-5
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
@SuppressWarnings("unchecked")
6970
List<Map<String, Object>> jobConfigs =
@@ -76,7 +77,7 @@ private static void deleteAllJobs(RestClient adminClient) throws Exception {
7677
for (Map<String, Object> jobConfig : jobConfigs) {
7778
String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
7879
try {
79-
response = adminClient.performRequest("DELETE", "/_xpack/rollup/job/" + jobId);
80+
response = adminClient.performRequest(new Request("DELETE", "/_xpack/rollup/job/" + jobId));
8081
} catch (Exception e) {
8182
// ok
8283
}

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

+8-7
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)