From 4d8667077266fe48cb376661fd50c305ce6716c9 Mon Sep 17 00:00:00 2001 From: Zachary Tong Date: Wed, 7 Dec 2016 12:48:33 -0500 Subject: [PATCH] Make jobId and from/size mutually exclusive options (elastic/elasticsearch#477) Make jobId and from/size mutually exclusive options. This approach has the main properties of not allowing an invalid Request to be built, and alerting the user if they set an incorrect configuration. It has the downside that PageParams can be null so the consumer will have to check for it. Since jobId could be null before, this seemed acceptable. Original commit: elastic/x-pack-elasticsearch@106dcdf61aace4c35e8f6f04cc883eb5dc707da9 --- .../xpack/prelert/action/GetJobsAction.java | 22 +++++--- .../prelert/rest/job/RestGetJobsAction.java | 14 ++++-- .../action/GetJobActionRequestTests.java | 3 +- .../rest-api-spec/test/jobs_crud.yaml | 50 +++++++++++++++++++ 4 files changed, 77 insertions(+), 12 deletions(-) diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetJobsAction.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetJobsAction.java index 12867013cfe..b8d68eec3c8 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetJobsAction.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetJobsAction.java @@ -93,8 +93,6 @@ public class GetJobsAction extends Action PARSER = new ObjectParser<>(NAME, Request::new); public static final ParseField METRIC = new ParseField("metric"); - - static { PARSER.declareString(Request::setJobId, Job.ID); PARSER.declareObject(Request::setPageParams, PageParams.PARSER, PageParams.PAGE); @@ -104,20 +102,23 @@ public class GetJobsAction extends Action(Collections.singletonList(jobInfo), 1, Job.RESULTS_FIELD); - } else { + } else if (request.getPageParams() != null) { // Multiple Jobs - QueryPage jobsPage = jobManager.getJobs(request.pageParams.getFrom(), request.pageParams.getSize(), state); + + PageParams pageParams = request.getPageParams(); + QueryPage jobsPage = jobManager.getJobs(pageParams.getFrom(), pageParams.getSize(), state); List jobInfoList = new ArrayList<>(); for (Job job : jobsPage.results()) { Job jobConfig = request.config() ? job : null; @@ -523,6 +529,8 @@ public class GetJobsAction extends Action(jobInfoList, jobsPage.count(), Job.RESULTS_FIELD); + } else { + throw new IllegalStateException("Both jobId and pageParams are null"); } listener.onResponse(new Response(response)); diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/rest/job/RestGetJobsAction.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/rest/job/RestGetJobsAction.java index d5e5d3f957a..7148a2a5059 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/rest/job/RestGetJobsAction.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/rest/job/RestGetJobsAction.java @@ -57,13 +57,21 @@ public class RestGetJobsAction extends BaseRestHandler { XContentParser parser = XContentFactory.xContent(bodyBytes).createParser(bodyBytes); request = GetJobsAction.Request.PARSER.apply(parser, () -> parseFieldMatcher); } else { + String jobId = restRequest.param(Job.ID.getPreferredName()); request = new GetJobsAction.Request(); - request.setJobId(restRequest.param(Job.ID.getPreferredName())); + if (jobId != null && !jobId.isEmpty()) { + request.setJobId(jobId); + } + if (restRequest.hasParam(PageParams.FROM.getPreferredName()) + || restRequest.hasParam(PageParams.SIZE.getPreferredName()) + || jobId == null) { + request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + } Set stats = Strings.splitStringByCommaToSet( restRequest.param(GetJobsAction.Request.METRIC.getPreferredName(), "config")); request.setStats(stats); - request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + } return channel -> transportGetJobAction.execute(request, new RestStatusToXContentListener<>(channel)); diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/action/GetJobActionRequestTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/action/GetJobActionRequestTests.java index 8fbeb0ed7e3..009c38b9428 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/action/GetJobActionRequestTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/action/GetJobActionRequestTests.java @@ -24,8 +24,7 @@ public class GetJobActionRequestTests extends AbstractStreamableTestCase