Skip to content

Commit

Permalink
Override put_mapping definition to make index name optional.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Oct 24, 2023
1 parent 3a6c075 commit 376d641
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 15 deletions.
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 4 additions & 5 deletions opensearchpy/_async/client/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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"

Check warning on line 413 in opensearchpy/_async/client/indices.py

View check run for this annotation

Codecov / codecov/patch

opensearchpy/_async/client/indices.py#L412-L413

Added lines #L412 - L413 were not covered by tests
Expand Down
2 changes: 1 addition & 1 deletion opensearchpy/_async/client/indices.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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] = ...,
Expand Down
9 changes: 4 additions & 5 deletions opensearchpy/client/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion opensearchpy/client/indices.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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] = ...,
Expand Down
18 changes: 15 additions & 3 deletions utils/generate-api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# specific language governing permissions and limitations
# under the License.

import json
import os
import re
from functools import lru_cache
Expand All @@ -33,6 +34,7 @@
from pathlib import Path

import black
import deepmerge
import requests
import unasync
import urllib3
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
20 changes: 20 additions & 0 deletions utils/templates/overrides/indices/put_mapping.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
]
}
}

0 comments on commit 376d641

Please sign in to comment.