Skip to content

Commit 6658822

Browse files
committed
Add a validation rule to _rollover requests
Unlike with an ILM rollover action, a max_* condition is not strictly required -- an unconditional rollover is valid. However, if any conditions are present, then at least one of them must be a max_* condition.
1 parent e346d57 commit 6658822

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.rollover/10_basic.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,12 @@
174174

175175
# perform alias rollover with no result
176176
- do:
177+
catch: bad_request
177178
indices.rollover:
178179
alias: "logs_search"
179180
wait_for_active_shards: 1
180181
body:
181182
conditions:
182183
min_age: "0s"
183184
min_docs: 1
184-
185-
- match: { rolled_over: false }
186-
- match: { dry_run: false }
187-
- match: { conditions: { "[min_age: 0s]": true, "[min_docs: 1]": true } }
185+
- match: { error.reason: "Validation Failed: 1: at least one max_* rollover condition must be set when using min_* conditions;" }

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequest.java

+10
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,16 @@ public ActionRequestValidationException validate() {
193193
if (rolloverTarget == null) {
194194
validationException = addValidationError("rollover target is missing", validationException);
195195
}
196+
197+
// max_* conditions are the isRequired = false ones -- the request must have at least one max_* condition
198+
boolean noMaxConditions = conditions.values().stream().noneMatch(Predicate.not(Condition::isRequired));
199+
if (conditions.size() > 0 && noMaxConditions) {
200+
validationException = addValidationError(
201+
"at least one max_* rollover condition must be set when using min_* conditions",
202+
validationException
203+
);
204+
}
205+
196206
return validationException;
197207
}
198208

server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestTests.java

+30-6
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,36 @@ public void testSameConditionCanOnlyBeAddedOnce() {
209209
}
210210

211211
public void testValidation() {
212-
RolloverRequest rolloverRequest = new RolloverRequest();
213-
assertNotNull(rolloverRequest.getCreateIndexRequest());
214-
ActionRequestValidationException validationException = rolloverRequest.validate();
215-
assertNotNull(validationException);
216-
assertEquals(1, validationException.validationErrors().size());
217-
assertEquals("rollover target is missing", validationException.validationErrors().get(0));
212+
{
213+
RolloverRequest rolloverRequest = new RolloverRequest();
214+
assertNotNull(rolloverRequest.getCreateIndexRequest());
215+
ActionRequestValidationException validationException = rolloverRequest.validate();
216+
assertNotNull(validationException);
217+
assertEquals(1, validationException.validationErrors().size());
218+
assertEquals("rollover target is missing", validationException.validationErrors().get(0));
219+
}
220+
221+
{
222+
RolloverRequest rolloverRequest = new RolloverRequest("alias-index", "new-index-name");
223+
rolloverRequest.addMinIndexDocsCondition(1L);
224+
ActionRequestValidationException validationException = rolloverRequest.validate();
225+
assertNotNull(validationException);
226+
assertEquals(1, validationException.validationErrors().size());
227+
assertEquals(
228+
"at least one max_* rollover condition must be set when using min_* conditions",
229+
validationException.validationErrors().get(0)
230+
);
231+
}
232+
233+
{
234+
RolloverRequest rolloverRequest = new RolloverRequest("alias-index", "new-index-name");
235+
if (randomBoolean()) {
236+
rolloverRequest.addMaxIndexAgeCondition(TimeValue.timeValueHours(1));
237+
rolloverRequest.addMinIndexDocsCondition(1L);
238+
}
239+
ActionRequestValidationException validationException = rolloverRequest.validate();
240+
assertNull(validationException);
241+
}
218242
}
219243

220244
public void testParsingWithType() throws Exception {

0 commit comments

Comments
 (0)