Skip to content

Commit c36d192

Browse files
authored
[Monitoring] Remove include_type_name parameter from GET _template request (#38926)
Backport of #38818 to `7.0`. Original description: The HTTP exporter code in the Monitoring plugin makes `GET _template` requests to check for existence of templates. These requests don't need to pass the `include_type_name` query parameter so this PR removes it from the request. This should remove the following deprecation log entries on the Monitoring cluster in 7.0.0 onwards: ``` [types removal] Specifying include_type_name in get index template requests is deprecated. ```
1 parent 37cd832 commit c36d192

12 files changed

+99
-55
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected void doCheck(final RestClient client, final ActionListener<Boolean> li
105105
@Override
106106
protected void doPublish(final RestClient client, final ActionListener<Boolean> listener) {
107107
putResource(client, listener, logger,
108-
"/_watcher/watch", watchId.get(), this::watchToHttpEntity, "monitoring cluster alert",
108+
"/_watcher/watch", watchId.get(), Collections.emptyMap(), this::watchToHttpEntity, "monitoring cluster alert",
109109
resourceOwnerName, "monitoring cluster");
110110
}
111111

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.common.xcontent.XContentType;
1818
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils;
1919

20+
import java.util.Collections;
2021
import java.util.Objects;
2122
import java.util.function.Supplier;
2223

@@ -72,7 +73,7 @@ protected void doCheck(final RestClient client, final ActionListener<Boolean> li
7273
@Override
7374
protected void doPublish(final RestClient client, final ActionListener<Boolean> listener) {
7475
putResource(client, listener, logger,
75-
"/_ingest/pipeline", pipelineName, this::pipelineToHttpEntity, "monitoring pipeline",
76+
"/_ingest/pipeline", pipelineName, Collections.emptyMap(), this::pipelineToHttpEntity, "monitoring pipeline",
7677
resourceOwnerName, "monitoring cluster");
7778
}
7879

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public abstract class PublishableHttpResource extends HttpResource {
7070
/**
7171
* The default parameters to use for any request.
7272
*/
73-
protected final Map<String, String> parameters;
73+
protected final Map<String, String> defaultParameters;
7474

7575
/**
7676
* Create a new {@link PublishableHttpResource} that {@linkplain #isDirty() is dirty}.
@@ -102,9 +102,9 @@ protected PublishableHttpResource(final String resourceOwnerName, @Nullable fina
102102
parameters.putAll(baseParameters);
103103
parameters.put("master_timeout", masterTimeout.toString());
104104

105-
this.parameters = Collections.unmodifiableMap(parameters);
105+
this.defaultParameters = Collections.unmodifiableMap(parameters);
106106
} else {
107-
this.parameters = baseParameters;
107+
this.defaultParameters = baseParameters;
108108
}
109109
}
110110

@@ -113,8 +113,8 @@ protected PublishableHttpResource(final String resourceOwnerName, @Nullable fina
113113
*
114114
* @return Never {@code null}.
115115
*/
116-
public Map<String, String> getParameters() {
117-
return parameters;
116+
public Map<String, String> getDefaultParameters() {
117+
return defaultParameters;
118118
}
119119

120120
/**
@@ -221,7 +221,8 @@ protected void checkForResource(final RestClient client,
221221
logger.trace("checking if {} [{}] exists on the [{}] {}", resourceType, resourceName, resourceOwnerName, resourceOwnerType);
222222

223223
final Request request = new Request("GET", resourceBasePath + "/" + resourceName);
224-
addParameters(request);
224+
addDefaultParameters(request);
225+
225226
// avoid exists and DNE parameters from being an exception by default
226227
final Set<Integer> expectedResponseCodes = Sets.union(exists, doesNotExist);
227228
request.addParameter("ignore", expectedResponseCodes.stream().map(i -> i.toString()).collect(Collectors.joining(",")));
@@ -299,6 +300,7 @@ public void onFailure(final Exception exception) {
299300
* @param logger The logger to use for status messages.
300301
* @param resourceBasePath The base path/endpoint to check for the resource (e.g., "/_template").
301302
* @param resourceName The name of the resource (e.g., "template123").
303+
* @param parameters Map of query string parameters, if any.
302304
* @param body The {@link HttpEntity} that makes up the body of the request.
303305
* @param resourceType The type of resource (e.g., "monitoring template").
304306
* @param resourceOwnerName The user-recognizeable resource owner.
@@ -309,6 +311,7 @@ protected void putResource(final RestClient client,
309311
final Logger logger,
310312
final String resourceBasePath,
311313
final String resourceName,
314+
final Map<String, String> parameters,
312315
final java.util.function.Supplier<HttpEntity> body,
313316
final String resourceType,
314317
final String resourceOwnerName,
@@ -317,7 +320,8 @@ protected void putResource(final RestClient client,
317320

318321

319322
final Request request = new Request("PUT", resourceBasePath + "/" + resourceName);
320-
addParameters(request);
323+
addDefaultParameters(request);
324+
addParameters(request, parameters);
321325
request.setEntity(body.get());
322326

323327
client.performRequestAsync(request, new ResponseListener() {
@@ -376,9 +380,9 @@ protected void deleteResource(final RestClient client,
376380
logger.trace("deleting {} [{}] from the [{}] {}", resourceType, resourceName, resourceOwnerName, resourceOwnerType);
377381

378382
final Request request = new Request("DELETE", resourceBasePath + "/" + resourceName);
379-
addParameters(request);
383+
addDefaultParameters(request);
380384

381-
if (false == parameters.containsKey("ignore")) {
385+
if (false == defaultParameters.containsKey("ignore")) {
382386
// avoid 404 being an exception by default
383387
request.addParameter("ignore", Integer.toString(RestStatus.NOT_FOUND.getStatus()));
384388
}
@@ -463,7 +467,11 @@ protected boolean alwaysReplaceResource(final Response response) {
463467
return true;
464468
}
465469

466-
private void addParameters(final Request request) {
470+
private void addDefaultParameters(final Request request) {
471+
this.addParameters(request, defaultParameters);
472+
}
473+
474+
private void addParameters(final Request request, final Map<String, String> parameters) {
467475
for (final Map.Entry<String, String> param : parameters.entrySet()) {
468476
request.addParameter(param.getKey(), param.getValue());
469477
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public class TemplateHttpResource extends PublishableHttpResource {
4141
static {
4242
Map<String, String> parameters = new TreeMap<>();
4343
parameters.put("filter_path", FILTER_PATH_RESOURCE_VERSION);
44-
parameters.put(INCLUDE_TYPE_NAME_PARAMETER, "true");
4544
PARAMETERS = Collections.unmodifiableMap(parameters);
4645
}
4746

@@ -88,8 +87,9 @@ protected void doCheck(final RestClient client, final ActionListener<Boolean> li
8887
*/
8988
@Override
9089
protected void doPublish(final RestClient client, final ActionListener<Boolean> listener) {
90+
Map<String, String> parameters = Collections.singletonMap(INCLUDE_TYPE_NAME_PARAMETER, "true");
9191
putResource(client, listener, logger,
92-
"/_template", templateName, this::templateToHttpEntity, "monitoring template",
92+
"/_template", templateName, parameters, this::templateToHttpEntity, "monitoring template",
9393
resourceOwnerName, "monitoring cluster");
9494
}
9595

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.IOException;
2828
import java.util.Map;
2929
import java.util.Set;
30+
import java.util.HashMap;
3031
import java.util.function.Predicate;
3132
import java.util.stream.Collectors;
3233

@@ -81,7 +82,7 @@ protected void assertCheckDoesNotExist(final PublishableHttpResource resource,
8182
*/
8283
protected void assertCheckWithException(final PublishableHttpResource resource,
8384
final String resourceBasePath, final String resourceName) {
84-
assertCheckWithException(resource, getParameters(resource.getParameters()), resourceBasePath, resourceName);
85+
assertCheckWithException(resource, getParameters(resource.getDefaultParameters()), resourceBasePath, resourceName);
8586
}
8687

8788
/**
@@ -140,7 +141,7 @@ protected void assertCheckAsDeleteWithException(final PublishableHttpResource re
140141
final Exception e = randomFrom(new IOException("expected"), new RuntimeException("expected"), responseException);
141142

142143
final Request request = new Request("DELETE", endpoint);
143-
addParameters(request, deleteParameters(resource.getParameters()));
144+
addParameters(request, deleteParameters(resource.getDefaultParameters()));
144145
whenPerformRequestAsyncWith(client, request, e);
145146

146147
resource.doCheck(client, listener);
@@ -155,11 +156,13 @@ protected void assertCheckAsDeleteWithException(final PublishableHttpResource re
155156
* @param resource The resource to execute.
156157
* @param resourceBasePath The base endpoint (e.g., "/_template")
157158
* @param resourceName The resource name (e.g., the template or pipeline name).
159+
* @param parameters Map of query string parameters, if any.
158160
* @param bodyType The request body provider's type.
159161
*/
160162
protected void assertPublishSucceeds(final PublishableHttpResource resource, final String resourceBasePath, final String resourceName,
163+
Map<String, String> parameters,
161164
final Class<? extends HttpEntity> bodyType) {
162-
doPublishWithStatusCode(resource, resourceBasePath, resourceName, bodyType, successfulPublishStatus(), true);
165+
doPublishWithStatusCode(resource, resourceBasePath, resourceName, parameters, bodyType, successfulPublishStatus(), true);
163166
}
164167

165168
/**
@@ -168,10 +171,12 @@ protected void assertPublishSucceeds(final PublishableHttpResource resource, fin
168171
*
169172
* @param resource The resource to execute.
170173
* @param resourceBasePath The base endpoint (e.g., "/_template")
174+
* @param parameters Map of query string parameters, if any.
171175
* @param resourceName The resource name (e.g., the template or pipeline name).
172176
*/
173177
protected void assertPublishWithException(final PublishableHttpResource resource,
174178
final String resourceBasePath, final String resourceName,
179+
Map<String, String> parameters,
175180
final Class<? extends HttpEntity> bodyType) {
176181
final String endpoint = concatenateEndpoint(resourceBasePath, resourceName);
177182
final Exception e = randomFrom(new IOException("expected"), new RuntimeException("expected"));
@@ -182,16 +187,20 @@ protected void assertPublishWithException(final PublishableHttpResource resource
182187

183188
verifyListener(null);
184189

190+
Map <String, String> allParameters = new HashMap<>();
191+
allParameters.putAll(resource.getDefaultParameters());
192+
allParameters.putAll(parameters);
193+
185194
final ArgumentCaptor<Request> request = ArgumentCaptor.forClass(Request.class);
186195
verify(client).performRequestAsync(request.capture(), any(ResponseListener.class));
187196
assertThat(request.getValue().getMethod(), is("PUT"));
188197
assertThat(request.getValue().getEndpoint(), is(endpoint));
189-
assertThat(request.getValue().getParameters(), is(resource.getParameters()));
198+
assertThat(request.getValue().getParameters(), is(allParameters));
190199
assertThat(request.getValue().getEntity(), instanceOf(bodyType));
191200
}
192201

193202
protected void assertParameters(final PublishableHttpResource resource) {
194-
final Map<String, String> parameters = new HashMap<>(resource.getParameters());
203+
final Map<String, String> parameters = new HashMap<>(resource.getDefaultParameters());
195204

196205
if (masterTimeout != null && TimeValue.MINUS_ONE.equals(masterTimeout) == false) {
197206
assertThat(parameters.remove("master_timeout"), is(masterTimeout.toString()));
@@ -204,7 +213,7 @@ protected void assertParameters(final PublishableHttpResource resource) {
204213
}
205214

206215
protected void assertVersionParameters(final PublishableHttpResource resource) {
207-
final Map<String, String> parameters = new HashMap<>(resource.getParameters());
216+
final Map<String, String> parameters = new HashMap<>(resource.getDefaultParameters());
208217

209218
if (masterTimeout != null && TimeValue.MINUS_ONE.equals(masterTimeout) == false) {
210219
assertThat(parameters.remove("master_timeout"), is(masterTimeout.toString()));
@@ -244,7 +253,7 @@ protected void doCheckWithStatusCode(final PublishableHttpResource resource, fin
244253
final String endpoint = concatenateEndpoint(resourceBasePath, resourceName);
245254
final Response response = response("GET", endpoint, status, entity);
246255

247-
doCheckWithStatusCode(resource, getParameters(resource.getParameters(), exists, doesNotExist), endpoint, expected, response);
256+
doCheckWithStatusCode(resource, getParameters(resource.getDefaultParameters(), exists, doesNotExist), endpoint, expected, response);
248257
}
249258

250259
protected void doCheckWithStatusCode(final PublishableHttpResource resource, final Map<String, String> expectedParameters,
@@ -262,6 +271,7 @@ protected void doCheckWithStatusCode(final PublishableHttpResource resource, fin
262271
}
263272

264273
private void doPublishWithStatusCode(final PublishableHttpResource resource, final String resourceBasePath, final String resourceName,
274+
Map<String, String> parameters,
265275
final Class<? extends HttpEntity> bodyType,
266276
final RestStatus status,
267277
final boolean errorFree) {
@@ -277,9 +287,13 @@ private void doPublishWithStatusCode(final PublishableHttpResource resource, fin
277287
final ArgumentCaptor<Request> request = ArgumentCaptor.forClass(Request.class);
278288
verify(client).performRequestAsync(request.capture(), any(ResponseListener.class));
279289

290+
Map <String, String> allParameters = new HashMap<>();
291+
allParameters.putAll(resource.getDefaultParameters());
292+
allParameters.putAll(parameters);
293+
280294
assertThat(request.getValue().getMethod(), is("PUT"));
281295
assertThat(request.getValue().getEndpoint(), is(endpoint));
282-
assertThat(request.getValue().getParameters(), is(resource.getParameters()));
296+
assertThat(request.getValue().getParameters(), is(allParameters));
283297
assertThat(request.getValue().getEntity(), instanceOf(bodyType));
284298
}
285299

@@ -297,7 +311,7 @@ protected void doCheckAsDeleteWithStatusCode(final PublishableHttpResource resou
297311
final String endpoint, final Boolean expected,
298312
final Response response) {
299313
final Request request = new Request("DELETE", endpoint);
300-
addParameters(request, deleteParameters(resource.getParameters()));
314+
addParameters(request, deleteParameters(resource.getDefaultParameters()));
301315
whenPerformRequestAsyncWith(client, request, response);
302316

303317
resource.doCheck(client, listener);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.io.IOException;
99
import java.io.InputStream;
10+
import java.util.Collections;
1011
import java.util.HashMap;
1112
import java.util.Map;
1213
import org.apache.http.HttpEntity;
@@ -127,11 +128,11 @@ public void testDoCheckWithExceptionAsDeleteWatchError() throws IOException {
127128
}
128129

129130
public void testDoPublishTrue() throws IOException {
130-
assertPublishSucceeds(resource, "/_watcher/watch", watchId, StringEntity.class);
131+
assertPublishSucceeds(resource, "/_watcher/watch", watchId, Collections.emptyMap(), StringEntity.class);
131132
}
132133

133134
public void testDoPublishFalseWithException() throws IOException {
134-
assertPublishWithException(resource, "/_watcher/watch", watchId, StringEntity.class);
135+
assertPublishWithException(resource, "/_watcher/watch", watchId, Collections.emptyMap(), StringEntity.class);
135136
}
136137

137138
public void testShouldReplaceClusterAlertRethrowsIOException() throws IOException {
@@ -181,7 +182,7 @@ public void testShouldReplaceCheckAlertChecksVersion() throws IOException {
181182
}
182183

183184
public void testParameters() {
184-
final Map<String, String> parameters = new HashMap<>(resource.getParameters());
185+
final Map<String, String> parameters = new HashMap<>(resource.getDefaultParameters());
185186

186187
assertThat(parameters.remove("filter_path"), is("metadata.xpack.version_created"));
187188
assertThat(parameters.isEmpty(), is(true));

0 commit comments

Comments
 (0)