Skip to content

Commit

Permalink
[Backport 2.x] Fix create or update alias API doesn't throw exception…
Browse files Browse the repository at this point in the history
… for unsupported parameters (#14756)

* Fix create or update alias API doesn't throw exception for unsupported parameters (#14719)

* Fix create or update alias API doesn't throw exception for unsupported parameters

Signed-off-by: Gao Binlong <gbinlong@amazon.com>

* Update version check in yml test

Signed-off-by: Gao Binlong <gbinlong@amazon.com>

* modify change log

Signed-off-by: Gao Binlong <gbinlong@amazon.com>

---------

Signed-off-by: Gao Binlong <gbinlong@amazon.com>
(cherry picked from commit 29a3e2c)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Update 10_basic.yml

Signed-off-by: Andriy Redko <andriy.redko@aiven.io>

---------

Signed-off-by: Gao Binlong <gbinlong@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Andriy Redko <andriy.redko@aiven.io>
  • Loading branch information
3 people committed Jul 15, 2024
1 parent fd3d162 commit 697396b
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix FuzzyQuery in keyword field will use IndexOrDocValuesQuery when both of index and doc_value are true ([#14378](https://github.com/opensearch-project/OpenSearch/pull/14378))
- Fix file cache initialization ([#14004](https://github.com/opensearch-project/OpenSearch/pull/14004))
- Handle NPE in GetResult if "found" field is missing ([#14552](https://github.com/opensearch-project/OpenSearch/pull/14552))
- Fix create or update alias API doesn't throw exception for unsupported parameters ([#14719](https://github.com/opensearch-project/OpenSearch/pull/14719))
- Refactoring FilterPath.parse by using an iterative approach ([#14200](https://github.com/opensearch-project/OpenSearch/pull/14200))
- Refactoring Grok.validatePatternBank by using an iterative approach ([#14206](https://github.com/opensearch-project/OpenSearch/pull/14206))
- Update help output for _cat ([#14722](https://github.com/opensearch-project/OpenSearch/pull/14722))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,62 @@
"description":"The name of the alias to be created or updated"
}
}
},
{
"path":"/{index}/_alias",
"methods":[
"PUT"
],
"parts":{
"index":{
"type":"list",
"description":"A comma-separated list of index names the alias should point to (supports wildcards); use `_all` to perform the operation on all indices."
}
}
},
{
"path":"/{index}/_aliases",
"methods":[
"PUT"
],
"parts":{
"index":{
"type":"list",
"description":"A comma-separated list of index names the alias should point to (supports wildcards); use `_all` to perform the operation on all indices."
}
}
},
{
"path":"/_alias/{name}",
"methods":[
"PUT",
"POST"
],
"parts":{
"name":{
"type":"string",
"description":"The name of the alias to be created or updated"
}
}
},
{
"path":"/_aliases/{name}",
"methods":[
"PUT",
"POST"
],
"parts":{
"name":{
"type":"string",
"description":"The name of the alias to be created or updated"
}
}
},
{
"path":"/_alias",
"methods":[
"PUT"
]
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@

- match: {test_index.aliases.test_alias: {}}

- do:
indices.put_alias:
index: test_index
body: {"alias": "test_alias_1"}

- do:
indices.get_alias:
index: test_index
name: test_alias_1

- match: {test_index.aliases.test_alias_1: {}}

- do:
indices.put_alias:
name: test_alias_2
body: {"index": "test_index"}

- do:
indices.get_alias:
index: test_index
name: test_alias_2

- match: {test_index.aliases.test_alias_2: {}}

- do:
catch: bad_request
indices.put_alias:
index: null
name: null

---
"Can't create alias with invalid characters":

Expand Down Expand Up @@ -102,3 +132,179 @@
index: test_index
name: test_alias
- match: {test_index.aliases.test_alias: {"filter": {"range": {"date_nanos_field": {"gt": "now-7d/d"}}}}}

---
"Can set index_routing":
- do:
indices.create:
index: test_index

- do:
indices.put_alias:
index: test_index
name: test_alias
body:
index_routing: "test"

- do:
indices.get_alias:
index: test_index
name: test_alias
- match: {test_index.aliases.test_alias: { 'index_routing': "test" }}

---
"Can set routing":
- do:
indices.create:
index: test_index

- do:
indices.put_alias:
index: test_index
name: test_alias
body:
routing: "test"

- do:
indices.get_alias:
index: test_index
name: test_alias
- match: {test_index.aliases.test_alias: { 'index_routing': "test", 'search_routing': "test" }}

---
"Can set search_routing":
- do:
indices.create:
index: test_index

- do:
indices.put_alias:
index: test_index
name: test_alias
body:
search_routing: "test"

- do:
indices.get_alias:
index: test_index
name: test_alias
- match: {test_index.aliases.test_alias: { 'search_routing': "test" }}

---
"Index parameter supports multiple values":
- do:
indices.create:
index: test_index
- do:
indices.create:
index: test_index1

- do:
indices.put_alias:
index: test_index,test_index1
name: test_alias

- do:
indices.get_alias:
index: test_index
name: test_alias
- match: {test_index.aliases.test_alias: { }}
- do:
indices.get_alias:
index: test_index1
name: test_alias
- match: {test_index1.aliases.test_alias: { }}

- do:
indices.put_alias:
body: {"index": "test_index,test_index1", "alias": "test_alias_1"}

- do:
indices.get_alias:
index: test_index
name: test_alias_1
- match: {test_index.aliases.test_alias_1: { }}
- do:
indices.get_alias:
index: test_index1
name: test_alias_1
- match: {test_index1.aliases.test_alias_1: { }}

---
"Index and alias in request body can override path parameters":
- do:
indices.create:
index: test_index

- do:
indices.put_alias:
index: test_index_unknown
name: test_alias
body: {"index": "test_index"}

- do:
indices.get_alias:
index: test_index
name: test_alias
- match: {test_index.aliases.test_alias: { }}

- do:
indices.put_alias:
index: test_index
name: test_alias_unknown
body: {"alias": "test_alias_2"}

- do:
indices.get_alias:
index: test_index
name: test_alias_2
- match: {test_index.aliases.test_alias_2: { }}

- do:
indices.put_alias:
body: {"index": "test_index", "alias": "test_alias_3"}

- do:
indices.get_alias:
index: test_index
name: test_alias_3
- match: {test_index.aliases.test_alias_3: { }}

---
"Can set is_hidden":
- skip:
version: " - 2.15.99"
reason: "Fix was introduced in 2.16.0"
- do:
indices.create:
index: test_index

- do:
indices.put_alias:
index: test_index
name: test_alias
body:
is_hidden: true

- do:
indices.get_alias:
index: test_index
name: test_alias
- match: {test_index.aliases.test_alias: { 'is_hidden': true }}

---
"Throws exception with invalid parameters":
- skip:
version: " - 2.15.99"
reason: "Fix was introduced in 2.16.0"

- do:
indices.create:
index: test_index

- do:
catch: /unknown field \[abc\]/
indices.put_alias:
index: test_index
name: test_alias
body: {"abc": 1}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
String indexRouting = null;
String searchRouting = null;
Boolean writeIndex = null;
Boolean isHidden = null;

if (request.hasContent()) {
try (XContentParser parser = request.contentParser()) {
Expand Down Expand Up @@ -120,10 +121,16 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
searchRouting = parser.textOrNull();
} else if ("is_write_index".equals(currentFieldName)) {
writeIndex = parser.booleanValue();
} else if ("is_hidden".equals(currentFieldName)) {
isHidden = parser.booleanValue();
} else {
throw new IllegalArgumentException("unknown field [" + currentFieldName + "]");
}
} else if (token == XContentParser.Token.START_OBJECT) {
if ("filter".equals(currentFieldName)) {
filter = parser.mapOrdered();
} else {
throw new IllegalArgumentException("unknown field [" + currentFieldName + "]");
}
}
}
Expand Down Expand Up @@ -153,6 +160,9 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
if (writeIndex != null) {
aliasAction.writeIndex(writeIndex);
}
if (isHidden != null) {
aliasAction.isHidden(isHidden);
}
indicesAliasesRequest.addAliasAction(aliasAction);
return channel -> client.admin().indices().aliases(indicesAliasesRequest, new RestToXContentListener<>(channel));
}
Expand Down

0 comments on commit 697396b

Please sign in to comment.