From 9b20a30b0f7bd683008aa30fa4adc64d4035f5e5 Mon Sep 17 00:00:00 2001 From: Seth Michael Larson Date: Fri, 13 Mar 2020 16:20:54 -0500 Subject: [PATCH] Restore and deprecate client.tasks.get() without task_id --- elasticsearch/client/tasks.py | 9 ++++++-- test_elasticsearch/test_client/__init__.py | 25 ++++++++++++++++++++++ utils/generate_api.py | 7 ++++++ utils/templates/overrides/tasks/get | 12 +++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 utils/templates/overrides/tasks/get diff --git a/elasticsearch/client/tasks.py b/elasticsearch/client/tasks.py index 0f253ca1..4c4f6d1f 100644 --- a/elasticsearch/client/tasks.py +++ b/elasticsearch/client/tasks.py @@ -1,3 +1,4 @@ +import warnings from .utils import NamespacedClient, query_params, _make_path, SKIP_IN_PATH @@ -58,7 +59,7 @@ class TasksClient(NamespacedClient): ) @query_params("timeout", "wait_for_completion") - def get(self, task_id, params=None, headers=None): + def get(self, task_id=None, params=None, headers=None): """ Returns information about a task. ``_ @@ -70,7 +71,11 @@ class TasksClient(NamespacedClient): complete (default: false) """ if task_id in SKIP_IN_PATH: - raise ValueError("Empty value passed for a required argument 'task_id'.") + warnings.warn( + "Calling client.tasks.get() without a task_id is deprecated " + "and will be removed in v8.0. Use client.tasks.list() instead.", + DeprecationWarning, + ) return self.transport.perform_request( "GET", _make_path("_tasks", task_id), params=params, headers=headers diff --git a/test_elasticsearch/test_client/__init__.py b/test_elasticsearch/test_client/__init__.py index b7fa18ad..5fa01f76 100644 --- a/test_elasticsearch/test_client/__init__.py +++ b/test_elasticsearch/test_client/__init__.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals +import warnings from elasticsearch.client import _normalize_hosts, Elasticsearch @@ -110,3 +111,27 @@ class TestClient(ElasticsearchTestCase): self.client.index(index="my-index", id=0, body={}) self.assert_url_called("PUT", "/my-index/_doc/0") + + def test_tasks_get_without_task_id_deprecated(self): + warnings.simplefilter("always", DeprecationWarning) + with warnings.catch_warnings(record=True) as w: + self.client.tasks.get() + + self.assert_url_called("GET", "/_tasks") + self.assertEquals(len(w), 1) + self.assertIs(w[0].category, DeprecationWarning) + self.assertEquals( + str(w[0].message), + "Calling client.tasks.get() without a task_id is deprecated " + "and will be removed in v8.0. Use client.tasks.list() instead.", + ) + + def test_tasks_get_with_task_id_not_deprecated(self): + warnings.simplefilter("always", DeprecationWarning) + with warnings.catch_warnings(record=True) as w: + self.client.tasks.get("task-1") + self.client.tasks.get(task_id="task-2") + + self.assert_url_called("GET", "/_tasks/task-1") + self.assert_url_called("GET", "/_tasks/task-2") + self.assertEquals(len(w), 0) diff --git a/utils/generate_api.py b/utils/generate_api.py index 0e76617d..183934e4 100644 --- a/utils/generate_api.py +++ b/utils/generate_api.py @@ -146,6 +146,13 @@ class API: p in url.get("parts", {}) for url in self._def["url"]["paths"] ) + # This piece of logic corresponds to calling + # client.tasks.get() w/o a task_id which was erroneously + # allowed in the 7.1 client library. This functionality + # is deprecated and will be removed in 8.x. + if self.namespace == "tasks" and self.name == "get": + parts["task_id"]["required"] = False + for k, sub in SUBSTITUTIONS.items(): if k in parts: parts[sub] = parts.pop(k) diff --git a/utils/templates/overrides/tasks/get b/utils/templates/overrides/tasks/get new file mode 100644 index 00000000..ca855ab9 --- /dev/null +++ b/utils/templates/overrides/tasks/get @@ -0,0 +1,12 @@ +{% extends "base" %} +{% block request %} + if task_id in SKIP_IN_PATH: + warnings.warn( + "Calling client.tasks.get() without a task_id is deprecated " + "and will be removed in v8.0. Use client.tasks.list() instead.", + DeprecationWarning, + ) + + {{ super()|trim }} +{% endblock %} +