Skip to content

Commit 697396b

Browse files
opensearch-trigger-bot[bot]github-actions[bot]reta
authored
[Backport 2.x] Fix create or update alias API doesn't throw exception 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 <[email protected]> * Update version check in yml test Signed-off-by: Gao Binlong <[email protected]> * modify change log Signed-off-by: Gao Binlong <[email protected]> --------- Signed-off-by: Gao Binlong <[email protected]> (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 <[email protected]> --------- Signed-off-by: Gao Binlong <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Signed-off-by: Andriy Redko <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Andriy Redko <[email protected]>
1 parent fd3d162 commit 697396b

File tree

4 files changed

+273
-0
lines changed

4 files changed

+273
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6464
- 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))
6565
- Fix file cache initialization ([#14004](https://github.com/opensearch-project/OpenSearch/pull/14004))
6666
- Handle NPE in GetResult if "found" field is missing ([#14552](https://github.com/opensearch-project/OpenSearch/pull/14552))
67+
- Fix create or update alias API doesn't throw exception for unsupported parameters ([#14719](https://github.com/opensearch-project/OpenSearch/pull/14719))
6768
- Refactoring FilterPath.parse by using an iterative approach ([#14200](https://github.com/opensearch-project/OpenSearch/pull/14200))
6869
- Refactoring Grok.validatePatternBank by using an iterative approach ([#14206](https://github.com/opensearch-project/OpenSearch/pull/14206))
6970
- Update help output for _cat ([#14722](https://github.com/opensearch-project/OpenSearch/pull/14722))

rest-api-spec/src/main/resources/rest-api-spec/api/indices.put_alias.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,62 @@
4040
"description":"The name of the alias to be created or updated"
4141
}
4242
}
43+
},
44+
{
45+
"path":"/{index}/_alias",
46+
"methods":[
47+
"PUT"
48+
],
49+
"parts":{
50+
"index":{
51+
"type":"list",
52+
"description":"A comma-separated list of index names the alias should point to (supports wildcards); use `_all` to perform the operation on all indices."
53+
}
54+
}
55+
},
56+
{
57+
"path":"/{index}/_aliases",
58+
"methods":[
59+
"PUT"
60+
],
61+
"parts":{
62+
"index":{
63+
"type":"list",
64+
"description":"A comma-separated list of index names the alias should point to (supports wildcards); use `_all` to perform the operation on all indices."
65+
}
66+
}
67+
},
68+
{
69+
"path":"/_alias/{name}",
70+
"methods":[
71+
"PUT",
72+
"POST"
73+
],
74+
"parts":{
75+
"name":{
76+
"type":"string",
77+
"description":"The name of the alias to be created or updated"
78+
}
79+
}
80+
},
81+
{
82+
"path":"/_aliases/{name}",
83+
"methods":[
84+
"PUT",
85+
"POST"
86+
],
87+
"parts":{
88+
"name":{
89+
"type":"string",
90+
"description":"The name of the alias to be created or updated"
91+
}
92+
}
93+
},
94+
{
95+
"path":"/_alias",
96+
"methods":[
97+
"PUT"
98+
]
4399
}
44100
]
45101
},

rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_alias/10_basic.yml

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,36 @@
2828

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

31+
- do:
32+
indices.put_alias:
33+
index: test_index
34+
body: {"alias": "test_alias_1"}
35+
36+
- do:
37+
indices.get_alias:
38+
index: test_index
39+
name: test_alias_1
40+
41+
- match: {test_index.aliases.test_alias_1: {}}
42+
43+
- do:
44+
indices.put_alias:
45+
name: test_alias_2
46+
body: {"index": "test_index"}
47+
48+
- do:
49+
indices.get_alias:
50+
index: test_index
51+
name: test_alias_2
52+
53+
- match: {test_index.aliases.test_alias_2: {}}
54+
55+
- do:
56+
catch: bad_request
57+
indices.put_alias:
58+
index: null
59+
name: null
60+
3161
---
3262
"Can't create alias with invalid characters":
3363

@@ -102,3 +132,179 @@
102132
index: test_index
103133
name: test_alias
104134
- match: {test_index.aliases.test_alias: {"filter": {"range": {"date_nanos_field": {"gt": "now-7d/d"}}}}}
135+
136+
---
137+
"Can set index_routing":
138+
- do:
139+
indices.create:
140+
index: test_index
141+
142+
- do:
143+
indices.put_alias:
144+
index: test_index
145+
name: test_alias
146+
body:
147+
index_routing: "test"
148+
149+
- do:
150+
indices.get_alias:
151+
index: test_index
152+
name: test_alias
153+
- match: {test_index.aliases.test_alias: { 'index_routing': "test" }}
154+
155+
---
156+
"Can set routing":
157+
- do:
158+
indices.create:
159+
index: test_index
160+
161+
- do:
162+
indices.put_alias:
163+
index: test_index
164+
name: test_alias
165+
body:
166+
routing: "test"
167+
168+
- do:
169+
indices.get_alias:
170+
index: test_index
171+
name: test_alias
172+
- match: {test_index.aliases.test_alias: { 'index_routing': "test", 'search_routing': "test" }}
173+
174+
---
175+
"Can set search_routing":
176+
- do:
177+
indices.create:
178+
index: test_index
179+
180+
- do:
181+
indices.put_alias:
182+
index: test_index
183+
name: test_alias
184+
body:
185+
search_routing: "test"
186+
187+
- do:
188+
indices.get_alias:
189+
index: test_index
190+
name: test_alias
191+
- match: {test_index.aliases.test_alias: { 'search_routing': "test" }}
192+
193+
---
194+
"Index parameter supports multiple values":
195+
- do:
196+
indices.create:
197+
index: test_index
198+
- do:
199+
indices.create:
200+
index: test_index1
201+
202+
- do:
203+
indices.put_alias:
204+
index: test_index,test_index1
205+
name: test_alias
206+
207+
- do:
208+
indices.get_alias:
209+
index: test_index
210+
name: test_alias
211+
- match: {test_index.aliases.test_alias: { }}
212+
- do:
213+
indices.get_alias:
214+
index: test_index1
215+
name: test_alias
216+
- match: {test_index1.aliases.test_alias: { }}
217+
218+
- do:
219+
indices.put_alias:
220+
body: {"index": "test_index,test_index1", "alias": "test_alias_1"}
221+
222+
- do:
223+
indices.get_alias:
224+
index: test_index
225+
name: test_alias_1
226+
- match: {test_index.aliases.test_alias_1: { }}
227+
- do:
228+
indices.get_alias:
229+
index: test_index1
230+
name: test_alias_1
231+
- match: {test_index1.aliases.test_alias_1: { }}
232+
233+
---
234+
"Index and alias in request body can override path parameters":
235+
- do:
236+
indices.create:
237+
index: test_index
238+
239+
- do:
240+
indices.put_alias:
241+
index: test_index_unknown
242+
name: test_alias
243+
body: {"index": "test_index"}
244+
245+
- do:
246+
indices.get_alias:
247+
index: test_index
248+
name: test_alias
249+
- match: {test_index.aliases.test_alias: { }}
250+
251+
- do:
252+
indices.put_alias:
253+
index: test_index
254+
name: test_alias_unknown
255+
body: {"alias": "test_alias_2"}
256+
257+
- do:
258+
indices.get_alias:
259+
index: test_index
260+
name: test_alias_2
261+
- match: {test_index.aliases.test_alias_2: { }}
262+
263+
- do:
264+
indices.put_alias:
265+
body: {"index": "test_index", "alias": "test_alias_3"}
266+
267+
- do:
268+
indices.get_alias:
269+
index: test_index
270+
name: test_alias_3
271+
- match: {test_index.aliases.test_alias_3: { }}
272+
273+
---
274+
"Can set is_hidden":
275+
- skip:
276+
version: " - 2.15.99"
277+
reason: "Fix was introduced in 2.16.0"
278+
- do:
279+
indices.create:
280+
index: test_index
281+
282+
- do:
283+
indices.put_alias:
284+
index: test_index
285+
name: test_alias
286+
body:
287+
is_hidden: true
288+
289+
- do:
290+
indices.get_alias:
291+
index: test_index
292+
name: test_alias
293+
- match: {test_index.aliases.test_alias: { 'is_hidden': true }}
294+
295+
---
296+
"Throws exception with invalid parameters":
297+
- skip:
298+
version: " - 2.15.99"
299+
reason: "Fix was introduced in 2.16.0"
300+
301+
- do:
302+
indices.create:
303+
index: test_index
304+
305+
- do:
306+
catch: /unknown field \[abc\]/
307+
indices.put_alias:
308+
index: test_index
309+
name: test_alias
310+
body: {"abc": 1}

server/src/main/java/org/opensearch/rest/action/admin/indices/RestIndexPutAliasAction.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
9292
String indexRouting = null;
9393
String searchRouting = null;
9494
Boolean writeIndex = null;
95+
Boolean isHidden = null;
9596

9697
if (request.hasContent()) {
9798
try (XContentParser parser = request.contentParser()) {
@@ -120,10 +121,16 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
120121
searchRouting = parser.textOrNull();
121122
} else if ("is_write_index".equals(currentFieldName)) {
122123
writeIndex = parser.booleanValue();
124+
} else if ("is_hidden".equals(currentFieldName)) {
125+
isHidden = parser.booleanValue();
126+
} else {
127+
throw new IllegalArgumentException("unknown field [" + currentFieldName + "]");
123128
}
124129
} else if (token == XContentParser.Token.START_OBJECT) {
125130
if ("filter".equals(currentFieldName)) {
126131
filter = parser.mapOrdered();
132+
} else {
133+
throw new IllegalArgumentException("unknown field [" + currentFieldName + "]");
127134
}
128135
}
129136
}
@@ -153,6 +160,9 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
153160
if (writeIndex != null) {
154161
aliasAction.writeIndex(writeIndex);
155162
}
163+
if (isHidden != null) {
164+
aliasAction.isHidden(isHidden);
165+
}
156166
indicesAliasesRequest.addAliasAction(aliasAction);
157167
return channel -> client.admin().indices().aliases(indicesAliasesRequest, new RestToXContentListener<>(channel));
158168
}

0 commit comments

Comments
 (0)