From 376d641776e6838a2dbc6b5fded01768c65e10ba Mon Sep 17 00:00:00 2001 From: dblock Date: Tue, 24 Oct 2023 11:17:10 -0400 Subject: [PATCH] Override put_mapping definition to make index name optional. Signed-off-by: dblock --- dev-requirements.txt | 1 + opensearchpy/_async/client/indices.py | 9 ++++----- opensearchpy/_async/client/indices.pyi | 2 +- opensearchpy/client/indices.py | 9 ++++----- opensearchpy/client/indices.pyi | 2 +- utils/generate-api.py | 18 ++++++++++++++--- .../overrides/indices/put_mapping.json | 20 +++++++++++++++++++ 7 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 utils/templates/overrides/indices/put_mapping.json diff --git a/dev-requirements.txt b/dev-requirements.txt index 04cfb3e8..a79a1a0b 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -7,6 +7,7 @@ sphinx<7.3 sphinx_rtd_theme jinja2 pytz +deepmerge # No wheels for Python 3.10 yet! numpy; python_version<"3.10" diff --git a/opensearchpy/_async/client/indices.py b/opensearchpy/_async/client/indices.py index 5825f151..cfc48db4 100644 --- a/opensearchpy/_async/client/indices.py +++ b/opensearchpy/_async/client/indices.py @@ -381,14 +381,14 @@ async def exists(self, index, params=None, headers=None): "timeout", "write_index_only", ) - async def put_mapping(self, index, body, params=None, headers=None): + async def put_mapping(self, body, index=None, params=None, headers=None): """ Updates the index mappings. + :arg body: The mapping definition :arg index: Comma-separated list of indices; use `_all` or empty string to perform the operation on all indices. - :arg body: The mapping definition :arg allow_no_indices: Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified). @@ -406,9 +406,8 @@ async def put_mapping(self, index, body, params=None, headers=None): :arg write_index_only: When true, applies mappings only to the write index of an alias or data stream. Default is false. """ - for param in (index, body): - if param in SKIP_IN_PATH: - raise ValueError("Empty value passed for a required argument.") + if body in SKIP_IN_PATH: + raise ValueError("Empty value passed for a required argument 'body'.") if index in SKIP_IN_PATH: index = "_all" diff --git a/opensearchpy/_async/client/indices.pyi b/opensearchpy/_async/client/indices.pyi index 0ecab94e..0d9b5953 100644 --- a/opensearchpy/_async/client/indices.pyi +++ b/opensearchpy/_async/client/indices.pyi @@ -264,9 +264,9 @@ class IndicesClient(NamespacedClient): ) -> bool: ... async def put_mapping( self, - index: Any, *, body: Any, + index: Optional[Any] = ..., allow_no_indices: Optional[Any] = ..., cluster_manager_timeout: Optional[Any] = ..., expand_wildcards: Optional[Any] = ..., diff --git a/opensearchpy/client/indices.py b/opensearchpy/client/indices.py index 171cbc66..9db06dd6 100644 --- a/opensearchpy/client/indices.py +++ b/opensearchpy/client/indices.py @@ -381,14 +381,14 @@ def exists(self, index, params=None, headers=None): "timeout", "write_index_only", ) - def put_mapping(self, index, body, params=None, headers=None): + def put_mapping(self, body, index=None, params=None, headers=None): """ Updates the index mappings. + :arg body: The mapping definition :arg index: Comma-separated list of indices; use `_all` or empty string to perform the operation on all indices. - :arg body: The mapping definition :arg allow_no_indices: Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified). @@ -406,9 +406,8 @@ def put_mapping(self, index, body, params=None, headers=None): :arg write_index_only: When true, applies mappings only to the write index of an alias or data stream. Default is false. """ - for param in (index, body): - if param in SKIP_IN_PATH: - raise ValueError("Empty value passed for a required argument.") + if body in SKIP_IN_PATH: + raise ValueError("Empty value passed for a required argument 'body'.") if index in SKIP_IN_PATH: index = "_all" diff --git a/opensearchpy/client/indices.pyi b/opensearchpy/client/indices.pyi index 4e8eb5b9..51751d53 100644 --- a/opensearchpy/client/indices.pyi +++ b/opensearchpy/client/indices.pyi @@ -264,9 +264,9 @@ class IndicesClient(NamespacedClient): ) -> bool: ... def put_mapping( self, - index: Any, *, body: Any, + index: Optional[Any] = ..., allow_no_indices: Optional[Any] = ..., cluster_manager_timeout: Optional[Any] = ..., expand_wildcards: Optional[Any] = ..., diff --git a/utils/generate-api.py b/utils/generate-api.py index fffd0e82..07360ae6 100644 --- a/utils/generate-api.py +++ b/utils/generate-api.py @@ -25,6 +25,7 @@ # specific language governing permissions and limitations # under the License. +import json import os import re from functools import lru_cache @@ -33,6 +34,7 @@ from pathlib import Path import black +import deepmerge import requests import unasync import urllib3 @@ -301,9 +303,10 @@ def all_parts(self): parts.update(url.get("parts", {})) for p in parts: - parts[p]["required"] = all( - p in url.get("parts", {}) for url in self._def["url"]["paths"] - ) + if "required" not in parts[p]: + parts[p]["required"] = all( + p in url.get("parts", {}) for url in self._def["url"]["paths"] + ) parts[p]["type"] = "Any" # This piece of logic corresponds to calling @@ -638,6 +641,15 @@ def read_modules(): api.update({"url": {"paths": paths}}) + # override a spec + override_file_path = ( + CODE_ROOT / "utils/templates/overrides" / namespace / f"{name}.json" + ) + if os.path.exists(override_file_path): + with open(override_file_path) as f: + override_json = json.load(f) + api = deepmerge.always_merger.merge(api, override_json) + if namespace not in modules: modules[namespace] = Module(namespace) diff --git a/utils/templates/overrides/indices/put_mapping.json b/utils/templates/overrides/indices/put_mapping.json new file mode 100644 index 00000000..4409c446 --- /dev/null +++ b/utils/templates/overrides/indices/put_mapping.json @@ -0,0 +1,20 @@ +{ + "url": { + "paths": [ + { + "path": "/{index}/_mapping", + "methods": [ + "POST", + "PUT" + ], + "parts": { + "index": { + "type": "string", + "description": "Comma-separated list of indices; use `_all` or empty string to perform the operation on all indices.", + "required": false + } + } + } + ] + } +} \ No newline at end of file