Skip to content

Commit 55a2d3e

Browse files
authored
Switch monitoring to new style Requests (#32255)
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/plugin/monitoring` project to use the new versions.
1 parent fe6bb75 commit 55a2d3e

File tree

6 files changed

+198
-137
lines changed

6 files changed

+198
-137
lines changed

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/PublishableHttpResource.java

+24-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.apache.logging.log4j.Logger;
1010
import org.apache.logging.log4j.message.ParameterizedMessage;
1111
import org.apache.logging.log4j.util.Supplier;
12+
import org.elasticsearch.client.Request;
1213
import org.elasticsearch.client.Response;
1314
import org.elasticsearch.client.ResponseException;
1415
import org.elasticsearch.client.RestClient;
@@ -311,13 +312,15 @@ protected Tuple<CheckResponse, Response> checkForResource(final RestClient clien
311312
final Set<Integer> exists, final Set<Integer> doesNotExist) {
312313
logger.trace("checking if {} [{}] exists on the [{}] {}", resourceType, resourceName, resourceOwnerName, resourceOwnerType);
313314

314-
final Set<Integer> expectedResponseCodes = Sets.union(exists, doesNotExist);
315+
316+
final Request request = new Request("GET", resourceBasePath + "/" + resourceName);
317+
addParameters(request);
315318
// avoid exists and DNE parameters from being an exception by default
316-
final Map<String, String> getParameters = new HashMap<>(parameters);
317-
getParameters.put("ignore", expectedResponseCodes.stream().map(i -> i.toString()).collect(Collectors.joining(",")));
319+
final Set<Integer> expectedResponseCodes = Sets.union(exists, doesNotExist);
320+
request.addParameter("ignore", expectedResponseCodes.stream().map(i -> i.toString()).collect(Collectors.joining(",")));
318321

319322
try {
320-
final Response response = client.performRequest("GET", resourceBasePath + "/" + resourceName, getParameters);
323+
final Response response = client.performRequest(request);
321324
final int statusCode = response.getStatusLine().getStatusCode();
322325

323326
// checking the content is the job of whoever called this function by checking the tuple's response
@@ -385,8 +388,12 @@ protected boolean putResource(final RestClient client, final Logger logger,
385388

386389
boolean success = false;
387390

391+
final Request request = new Request("PUT", resourceBasePath + "/" + resourceName);
392+
addParameters(request);
393+
request.setEntity(body.get());
394+
388395
try {
389-
final Response response = client.performRequest("PUT", resourceBasePath + "/" + resourceName, parameters, body.get());
396+
final Response response = client.performRequest(request);
390397
final int statusCode = response.getStatusLine().getStatusCode();
391398

392399
// 200 or 201
@@ -431,12 +438,15 @@ protected boolean deleteResource(final RestClient client, final Logger logger,
431438

432439
boolean success = false;
433440

434-
// avoid 404 being an exception by default
435-
final Map<String, String> deleteParameters = new HashMap<>(parameters);
436-
deleteParameters.putIfAbsent("ignore", Integer.toString(RestStatus.NOT_FOUND.getStatus()));
441+
Request request = new Request("DELETE", resourceBasePath + "/" + resourceName);
442+
addParameters(request);
443+
if (false == parameters.containsKey("ignore")) {
444+
// avoid 404 being an exception by default
445+
request.addParameter("ignore", Integer.toString(RestStatus.NOT_FOUND.getStatus()));
446+
}
437447

438448
try {
439-
final Response response = client.performRequest("DELETE", resourceBasePath + "/" + resourceName, deleteParameters);
449+
final Response response = client.performRequest(request);
440450
final int statusCode = response.getStatusLine().getStatusCode();
441451

442452
// 200 or 404 (not found is just as good as deleting it!)
@@ -498,4 +508,9 @@ protected boolean shouldReplaceResource(final Response response, final XContent
498508
return true;
499509
}
500510

511+
private void addParameters(Request request) {
512+
for (Map.Entry<String, String> param : parameters.entrySet()) {
513+
request.addParameter(param.getKey(), param.getValue());
514+
}
515+
}
501516
}

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/VersionHttpResource.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import org.apache.logging.log4j.message.ParameterizedMessage;
1010
import org.apache.logging.log4j.util.Supplier;
1111
import org.elasticsearch.Version;
12+
import org.elasticsearch.client.Request;
1213
import org.elasticsearch.client.Response;
1314
import org.elasticsearch.client.RestClient;
1415
import org.elasticsearch.common.logging.Loggers;
1516
import org.elasticsearch.common.xcontent.XContentHelper;
1617
import org.elasticsearch.common.xcontent.json.JsonXContent;
1718

1819
import java.io.IOException;
19-
import java.util.Collections;
2020
import java.util.Map;
2121
import java.util.Objects;
2222

@@ -27,11 +27,6 @@ public class VersionHttpResource extends HttpResource {
2727

2828
private static final Logger logger = Loggers.getLogger(VersionHttpResource.class);
2929

30-
/**
31-
* The parameters to pass with every version request to limit the output to just the version number.
32-
*/
33-
public static final Map<String, String> PARAMETERS = Collections.singletonMap("filter_path", "version.number");
34-
3530
/**
3631
* The minimum supported version of Elasticsearch.
3732
*/
@@ -59,7 +54,9 @@ protected boolean doCheckAndPublish(final RestClient client) {
5954
logger.trace("checking [{}] to ensure that it supports the minimum version [{}]", resourceOwnerName, minimumVersion);
6055

6156
try {
62-
return validateVersion(client.performRequest("GET", "/", PARAMETERS));
57+
Request request = new Request("GET", "/");
58+
request.addParameter("filter_path", "version.number");
59+
return validateVersion(client.performRequest(request));
6360
} catch (IOException | RuntimeException e) {
6461
logger.error(
6562
(Supplier<?>)() ->

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/AbstractPublishableHttpResourceTestCase.java

+35-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.http.StatusLine;
1212
import org.apache.http.entity.ContentType;
1313
import org.apache.http.entity.StringEntity;
14+
import org.elasticsearch.client.Request;
1415
import org.elasticsearch.client.Response;
1516
import org.elasticsearch.client.ResponseException;
1617
import org.elasticsearch.client.RestClient;
@@ -20,6 +21,8 @@
2021
import org.elasticsearch.rest.RestStatus;
2122
import org.elasticsearch.test.ESTestCase;
2223
import org.elasticsearch.xpack.monitoring.exporter.http.PublishableHttpResource.CheckResponse;
24+
import org.mockito.ArgumentCaptor;
25+
import org.mockito.Mockito;
2326

2427
import java.io.IOException;
2528
import java.util.Map;
@@ -30,10 +33,10 @@
3033
import static org.elasticsearch.xpack.monitoring.exporter.http.PublishableHttpResource.GET_DOES_NOT_EXIST;
3134
import static org.elasticsearch.xpack.monitoring.exporter.http.PublishableHttpResource.GET_EXISTS;
3235
import static org.hamcrest.Matchers.is;
33-
import static org.mockito.Matchers.any;
34-
import static org.mockito.Matchers.eq;
36+
import static org.hamcrest.Matchers.instanceOf;
3537
import static org.mockito.Mockito.mock;
3638
import static org.mockito.Mockito.when;
39+
import static org.mockito.Mockito.verify;
3740

3841
/**
3942
* Base test helper for any {@link PublishableHttpResource}.
@@ -87,7 +90,9 @@ protected void assertCheckWithException(final PublishableHttpResource resource,
8790
final ResponseException responseException = responseException("GET", endpoint, failedCheckStatus());
8891
final Exception e = randomFrom(new IOException("expected"), new RuntimeException("expected"), responseException);
8992

90-
when(client.performRequest("GET", endpoint, getParameters(resource.getParameters()))).thenThrow(e);
93+
Request request = new Request("GET", endpoint);
94+
addParameters(request, getParameters(resource.getParameters()));
95+
when(client.performRequest(request)).thenThrow(e);
9196

9297
assertThat(resource.doCheck(client), is(CheckResponse.ERROR));
9398
}
@@ -123,7 +128,9 @@ protected void assertCheckAsDeleteWithException(final PublishableHttpResource re
123128
final ResponseException responseException = responseException("DELETE", endpoint, failedCheckStatus());
124129
final Exception e = randomFrom(new IOException("expected"), new RuntimeException("expected"), responseException);
125130

126-
when(client.performRequest("DELETE", endpoint, deleteParameters(resource.getParameters()))).thenThrow(e);
131+
Request request = new Request("DELETE", endpoint);
132+
addParameters(request, deleteParameters(resource.getParameters()));
133+
when(client.performRequest(request)).thenThrow(e);
127134

128135
assertThat(resource.doCheck(client), is(CheckResponse.ERROR));
129136
}
@@ -173,9 +180,15 @@ protected void assertPublishWithException(final PublishableHttpResource resource
173180
final String endpoint = concatenateEndpoint(resourceBasePath, resourceName);
174181
final Exception e = randomFrom(new IOException("expected"), new RuntimeException("expected"));
175182

176-
when(client.performRequest(eq("PUT"), eq(endpoint), eq(resource.getParameters()), any(bodyType))).thenThrow(e);
183+
when(client.performRequest(Mockito.any(Request.class))).thenThrow(e);
177184

178185
assertThat(resource.doPublish(client), is(false));
186+
ArgumentCaptor<Request> request = ArgumentCaptor.forClass(Request.class);
187+
verify(client).performRequest(request.capture());
188+
assertThat(request.getValue().getMethod(), is("PUT"));
189+
assertThat(request.getValue().getEndpoint(), is(endpoint));
190+
assertThat(request.getValue().getParameters(), is(resource.getParameters()));
191+
assertThat(request.getValue().getEntity(), instanceOf(bodyType));
179192
}
180193

181194
protected void assertParameters(final PublishableHttpResource resource) {
@@ -244,7 +257,9 @@ protected void doCheckWithStatusCode(final PublishableHttpResource resource, fin
244257
final String endpoint, final CheckResponse expected,
245258
final Response response)
246259
throws IOException {
247-
when(client.performRequest("GET", endpoint, expectedParameters)).thenReturn(response);
260+
Request request = new Request("GET", endpoint);
261+
addParameters(request, expectedParameters);
262+
when(client.performRequest(request)).thenReturn(response);
248263

249264
assertThat(resource.doCheck(client), is(expected));
250265
}
@@ -257,9 +272,14 @@ private void doPublishWithStatusCode(final PublishableHttpResource resource, fin
257272
final String endpoint = concatenateEndpoint(resourceBasePath, resourceName);
258273
final Response response = response("GET", endpoint, status);
259274

260-
when(client.performRequest(eq("PUT"), eq(endpoint), eq(resource.getParameters()), any(bodyType))).thenReturn(response);
275+
ArgumentCaptor<Request> request = ArgumentCaptor.forClass(Request.class);
276+
when(client.performRequest(request.capture())).thenReturn(response);
261277

262278
assertThat(resource.doPublish(client), is(expected));
279+
assertThat(request.getValue().getMethod(), is("PUT"));
280+
assertThat(request.getValue().getEndpoint(), is(endpoint));
281+
assertThat(request.getValue().getParameters(), is(resource.getParameters()));
282+
assertThat(request.getValue().getEntity(), instanceOf(bodyType));
263283
}
264284

265285
protected void doCheckAsDeleteWithStatusCode(final PublishableHttpResource resource,
@@ -277,7 +297,9 @@ protected void doCheckAsDeleteWithStatusCode(final PublishableHttpResource resou
277297
final String endpoint, final CheckResponse expected,
278298
final Response response)
279299
throws IOException {
280-
when(client.performRequest("DELETE", endpoint, deleteParameters(resource.getParameters()))).thenReturn(response);
300+
Request request = new Request("DELETE", endpoint);
301+
addParameters(request, deleteParameters(resource.getParameters()));
302+
when(client.performRequest(request)).thenReturn(response);
281303

282304
assertThat(resource.doCheck(client), is(expected));
283305
}
@@ -427,4 +449,9 @@ protected HttpEntity entityForClusterAlert(final CheckResponse expected, final i
427449
return entity;
428450
}
429451

452+
protected void addParameters(Request request, Map<String, String> parameters) {
453+
for (Map.Entry<String, String> param : parameters.entrySet()) {
454+
request.addParameter(param.getKey(), param.getValue());
455+
}
456+
}
430457
}

0 commit comments

Comments
 (0)