From e26f3fc28c39ff907a93625ba2fcbae52968538c Mon Sep 17 00:00:00 2001 From: Jeppe Fihl-Pearson Date: Thu, 22 Aug 2024 13:33:55 +0100 Subject: [PATCH] Add override for code generator to change `indices.put_alias` argument order (#804) * Add currently-failing test to validate the original argument order Signed-off-by: Jeppe Fihl-Pearson * Add workaround for breaking API change inadvertently added in v2.7.0 Signed-off-by: Jeppe Fihl-Pearson * Generate new version of the client code with the correct argument order Signed-off-by: Jeppe Fihl-Pearson --------- Signed-off-by: Jeppe Fihl-Pearson --- CHANGELOG.md | 1 + opensearchpy/_async/client/indices.py | 114 +++++++++--------- opensearchpy/client/indices.py | 114 +++++++++--------- test_opensearchpy/test_client/test_indices.py | 5 + utils/templates/base | 2 +- utils/templates/overrides/indices/put_alias | 2 + .../overrides/indices/put_alias-func_params | 24 ++++ 7 files changed, 147 insertions(+), 115 deletions(-) create mode 100644 utils/templates/overrides/indices/put_alias create mode 100644 utils/templates/overrides/indices/put_alias-func_params diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d28ec813..0a7b93fd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Fixed the use of `minimum_should_match` with `Bool` to allow the use of string-based value (percent string, combination). ([#780](https://github.com/opensearch-project/opensearch-py/pull/780)) - Fixed incorrect `retry_on_conflict` type ([#795](https://github.com/opensearch-project/opensearch-py/pull/795)) ### Updated APIs +- Updated opensearch-py APIs to reflect [opensearch-api-specification@b28c1cf](https://github.com/opensearch-project/opensearch-api-specification/commit/b28c1cfaf4a76d786c789929eaa7fabffd09cb99) - Updated opensearch-py APIs to reflect [opensearch-api-specification@9d3bc34](https://github.com/opensearch-project/opensearch-api-specification/commit/9d3bc340ccd7d049e7d6e14a4aff2293780cb446) ### Security ### Dependencies diff --git a/opensearchpy/_async/client/indices.py b/opensearchpy/_async/client/indices.py index e00068894..2c692fe8b 100644 --- a/opensearchpy/_async/client/indices.py +++ b/opensearchpy/_async/client/indices.py @@ -852,63 +852,6 @@ async def get_field_mapping( headers=headers, ) - @query_params( - "cluster_manager_timeout", - "error_trace", - "filter_path", - "human", - "master_timeout", - "pretty", - "source", - "timeout", - ) - async def put_alias( - self, - body: Any = None, - index: Any = None, - name: Any = None, - params: Any = None, - headers: Any = None, - ) -> Any: - """ - Creates or updates an alias. - - - :arg body: The settings for the alias, such as `routing` or - `filter` - :arg index: Comma-separated list of data streams or indices to - add. Supports wildcards (`*`). Wildcard patterns that match both data - streams and indices return an error. - :arg name: Alias to update. If the alias doesn't exist, the - request creates it. Index alias names support date math. - :arg cluster_manager_timeout: Operation timeout for connection - to cluster-manager node. - :arg error_trace: Whether to include the stack trace of returned - errors. - :arg filter_path: Comma-separated list of filters used to reduce - the response. - :arg human: Whether to return human readable values for - statistics. - :arg master_timeout (Deprecated: To promote inclusive language, - use 'cluster_manager_timeout' instead.): Period to wait for a connection - to the master node. If no response is received before the timeout - expires, the request fails and returns an error. - :arg pretty: Whether to pretty format the returned JSON - response. - :arg source: The URL-encoded request definition. Useful for - libraries that do not accept a request body for non-POST requests. - :arg timeout: Period to wait for a response. If no response is - received before the timeout expires, the request fails and returns an - error. - """ - return await self.transport.perform_request( - "PUT", - _make_path(index, "_alias", name), - params=params, - headers=headers, - body=body, - ) - @query_params( "allow_no_indices", "error_trace", @@ -2766,3 +2709,60 @@ async def data_streams_stats( params=params, headers=headers, ) + + @query_params( + "cluster_manager_timeout", + "error_trace", + "filter_path", + "human", + "master_timeout", + "pretty", + "source", + "timeout", + ) + async def put_alias( + self, + index: Any = None, + name: Any = None, + body: Any = None, + params: Any = None, + headers: Any = None, + ) -> Any: + """ + Creates or updates an alias. + + + :arg body: The settings for the alias, such as `routing` or + `filter` + :arg index: Comma-separated list of data streams or indices to + add. Supports wildcards (`*`). Wildcard patterns that match both data + streams and indices return an error. + :arg name: Alias to update. If the alias doesn't exist, the + request creates it. Index alias names support date math. + :arg cluster_manager_timeout: Operation timeout for connection + to cluster-manager node. + :arg error_trace: Whether to include the stack trace of returned + errors. + :arg filter_path: Comma-separated list of filters used to reduce + the response. + :arg human: Whether to return human readable values for + statistics. + :arg master_timeout (Deprecated: To promote inclusive language, + use 'cluster_manager_timeout' instead.): Period to wait for a connection + to the master node. If no response is received before the timeout + expires, the request fails and returns an error. + :arg pretty: Whether to pretty format the returned JSON + response. + :arg source: The URL-encoded request definition. Useful for + libraries that do not accept a request body for non-POST requests. + :arg timeout: Period to wait for a response. If no response is + received before the timeout expires, the request fails and returns an + error. + """ + return await self.transport.perform_request( + "PUT", + _make_path(index, "_alias", name), + params=params, + headers=headers, + body=body, + ) diff --git a/opensearchpy/client/indices.py b/opensearchpy/client/indices.py index a98944f77..e4ed6fa5e 100644 --- a/opensearchpy/client/indices.py +++ b/opensearchpy/client/indices.py @@ -852,63 +852,6 @@ def get_field_mapping( headers=headers, ) - @query_params( - "cluster_manager_timeout", - "error_trace", - "filter_path", - "human", - "master_timeout", - "pretty", - "source", - "timeout", - ) - def put_alias( - self, - body: Any = None, - index: Any = None, - name: Any = None, - params: Any = None, - headers: Any = None, - ) -> Any: - """ - Creates or updates an alias. - - - :arg body: The settings for the alias, such as `routing` or - `filter` - :arg index: Comma-separated list of data streams or indices to - add. Supports wildcards (`*`). Wildcard patterns that match both data - streams and indices return an error. - :arg name: Alias to update. If the alias doesn't exist, the - request creates it. Index alias names support date math. - :arg cluster_manager_timeout: Operation timeout for connection - to cluster-manager node. - :arg error_trace: Whether to include the stack trace of returned - errors. - :arg filter_path: Comma-separated list of filters used to reduce - the response. - :arg human: Whether to return human readable values for - statistics. - :arg master_timeout (Deprecated: To promote inclusive language, - use 'cluster_manager_timeout' instead.): Period to wait for a connection - to the master node. If no response is received before the timeout - expires, the request fails and returns an error. - :arg pretty: Whether to pretty format the returned JSON - response. - :arg source: The URL-encoded request definition. Useful for - libraries that do not accept a request body for non-POST requests. - :arg timeout: Period to wait for a response. If no response is - received before the timeout expires, the request fails and returns an - error. - """ - return self.transport.perform_request( - "PUT", - _make_path(index, "_alias", name), - params=params, - headers=headers, - body=body, - ) - @query_params( "allow_no_indices", "error_trace", @@ -2766,3 +2709,60 @@ def data_streams_stats( params=params, headers=headers, ) + + @query_params( + "cluster_manager_timeout", + "error_trace", + "filter_path", + "human", + "master_timeout", + "pretty", + "source", + "timeout", + ) + def put_alias( + self, + index: Any = None, + name: Any = None, + body: Any = None, + params: Any = None, + headers: Any = None, + ) -> Any: + """ + Creates or updates an alias. + + + :arg body: The settings for the alias, such as `routing` or + `filter` + :arg index: Comma-separated list of data streams or indices to + add. Supports wildcards (`*`). Wildcard patterns that match both data + streams and indices return an error. + :arg name: Alias to update. If the alias doesn't exist, the + request creates it. Index alias names support date math. + :arg cluster_manager_timeout: Operation timeout for connection + to cluster-manager node. + :arg error_trace: Whether to include the stack trace of returned + errors. + :arg filter_path: Comma-separated list of filters used to reduce + the response. + :arg human: Whether to return human readable values for + statistics. + :arg master_timeout (Deprecated: To promote inclusive language, + use 'cluster_manager_timeout' instead.): Period to wait for a connection + to the master node. If no response is received before the timeout + expires, the request fails and returns an error. + :arg pretty: Whether to pretty format the returned JSON + response. + :arg source: The URL-encoded request definition. Useful for + libraries that do not accept a request body for non-POST requests. + :arg timeout: Period to wait for a response. If no response is + received before the timeout expires, the request fails and returns an + error. + """ + return self.transport.perform_request( + "PUT", + _make_path(index, "_alias", name), + params=params, + headers=headers, + body=body, + ) diff --git a/test_opensearchpy/test_client/test_indices.py b/test_opensearchpy/test_client/test_indices.py index d45405e52..8705a4909 100644 --- a/test_opensearchpy/test_client/test_indices.py +++ b/test_opensearchpy/test_client/test_indices.py @@ -45,3 +45,8 @@ def test_passing_empty_value_for_required_param_raises_exception(self) -> None: self.assertRaises(ValueError, self.client.indices.exists, index=None) self.assertRaises(ValueError, self.client.indices.exists, index=[]) self.assertRaises(ValueError, self.client.indices.exists, index="") + + def test_create_alias(self) -> None: + self.client.indices.create("test-index") + self.client.indices.put_alias("test-index", "test-alias") + self.assert_url_called("PUT", "/test-index/_alias/test-alias") diff --git a/utils/templates/base b/utils/templates/base index 7e6be8751..c6cebe606 100644 --- a/utils/templates/base +++ b/utils/templates/base @@ -1,6 +1,6 @@ @query_params({{ api.query_params|map("tojson")|join(", ")}}) - async def {{ api.name }}(self, {% include "func_params" %}) -> Any: + async def {{ api.name }}(self, {% block func_params %}{% include "func_params" %}{% endblock %}) -> Any: """ {% if api.description %} {{ api.description|replace("\n", " ")|wordwrap(wrapstring="\n ") }} diff --git a/utils/templates/overrides/indices/put_alias b/utils/templates/overrides/indices/put_alias new file mode 100644 index 000000000..f055c6bfa --- /dev/null +++ b/utils/templates/overrides/indices/put_alias @@ -0,0 +1,2 @@ +{% extends "base" %} +{% block func_params %}{% include "overrides/indices/put_alias-func_params" %}{% endblock %} diff --git a/utils/templates/overrides/indices/put_alias-func_params b/utils/templates/overrides/indices/put_alias-func_params new file mode 100644 index 000000000..d47aebada --- /dev/null +++ b/utils/templates/overrides/indices/put_alias-func_params @@ -0,0 +1,24 @@ +{# Work around for https://github.com/opensearch-project/opensearch-py/issues/803 #} + +{% set first_params = ["index", "name"] %} + +{% for p, info in api.all_parts.items() %} + {% if info.required %}{{ p }}: {{ info.type }}, {% endif %} +{% endfor %} + +{% for p, info in api.all_parts.items() if p in first_params %} + {% if not info.type == 'Any' %}{{ p }}: Optional[{{ info.type }}]=None, {% endif %} + {% if info.type == 'Any' %}{{ p }}: {{ info.type }}=None, {% endif %} +{% endfor %} + +{% if api.body %} + body{% if not api.body.required %}: Any=None{% else %}: Any{% endif %}, +{% endif %} + +{% for p, info in api.all_parts.items() if p not in first_params %} + {% if not info.required and not info.type == 'Any' %}{{ p }}: Optional[{{ info.type }}]=None, {% endif %} + {% if not info.required and info.type == 'Any' %}{{ p }}: {{ info.type }}=None, {% endif %} +{% endfor %} + +params: Any=None, +headers: Any=None,