// JobLister is a handler which passess requests for listing jobs to jobmanager.
func (a *APIDefaults) JobLister(params jobs.JobListerParams) middleware.Responder {
- if (params.After != nil) && (params.Before != nil) {
- return jobs.NewJobListerBadRequest().WithPayload(
- &weles.ErrResponse{Message: weles.ErrBeforeAfterNotAllowed.Error()})
- }
-
- var jobInfoReceived []weles.JobInfo
- var listInfo weles.ListInfo
- var err error
paginator := weles.JobPagination{}
-
if a.PageLimit != 0 {
+ if (params.After != nil) && (params.Before != nil) {
+ return jobs.NewJobListerBadRequest().WithPayload(
+ &weles.ErrResponse{Message: weles.ErrBeforeAfterNotAllowed.Error()})
+ }
paginator = setJobPaginator(params, a.PageLimit)
}
-
- if params.JobFilterAndSort.Filter != nil || params.JobFilterAndSort.Sorter != nil {
- jobInfoReceived, listInfo, err = a.Managers.JM.ListJobs(
- *params.JobFilterAndSort.Filter, *params.JobFilterAndSort.Sorter, paginator)
- } else {
- jobInfoReceived, listInfo, err = a.Managers.JM.ListJobs(
- weles.JobFilter{}, weles.JobSorter{}, paginator)
+ filter := weles.JobFilter{}
+ if params.JobFilterAndSort.Filter != nil {
+ filter = *params.JobFilterAndSort.Filter
}
+ sorter := setJobSorter(params.JobFilterAndSort.Sorter)
+
+ jobInfoReceived, listInfo, err := a.Managers.JM.ListJobs(filter, sorter, paginator)
if err != nil {
// due to weles.ErrInvalidArgument implementing error interface rather than being error
// (which is intentional as we want to pass underlying error) switch err.(type) checks only
return responder200(listInfo, paginator, jobInfoReturned, a.PageLimit)
}
return responder206(listInfo, paginator, jobInfoReturned, a.PageLimit)
-
}
func responder206(listInfo weles.ListInfo, paginator weles.JobPagination,
return
}
+// setJobSorter sets default sorter values.
+func setJobSorter(si *weles.JobSorter) (so weles.JobSorter) {
+ if si == nil {
+ return weles.JobSorter{
+ SortOrder: weles.SortOrderAscending,
+ SortBy: weles.JobSortByID,
+ }
+ }
+ if si.SortOrder == "" {
+ so.SortOrder = weles.SortOrderAscending
+ } else {
+ so.SortOrder = si.SortOrder
+ }
+ if si.SortBy == "" {
+ so.SortBy = weles.JobSortByID
+ } else {
+ so.SortBy = si.SortBy
+ }
+ return
+}
+
//jobInfoReceivedToReturned is a function which changes the slice of values to slice of pointers.
//It is required due to fact that swagger generates responses as slices of pointers rather than
//slices of values that the interface provides.
emptyFilter := weles.JobFilter{}
filledSorter1 := weles.JobSorter{
- SortBy: weles.JobSortByCreatedDate,
+ SortBy: weles.JobSortByID,
SortOrder: weles.SortOrderAscending}
filledSorter2 := weles.JobSorter{
listInfo := weles.ListInfo{
TotalRecords: uint64(len(jobInfoSlice420)),
RemainingRecords: 0}
-
+ if sorter.SortOrder == "" {
+ sorter.SortOrder = weles.SortOrderAscending
+ }
+ if sorter.SortBy == "" {
+ sorter.SortBy = weles.JobSortByID
+ }
mockJobManager.EXPECT().ListJobs(
filter, sorter, emptyPaginator).Return(
jobInfoSlice420, listInfo, nil)
TotalRecords: uint64(len(jobInfo)),
RemainingRecords: 0}
+ if sorter.SortOrder == "" {
+ sorter.SortOrder = weles.SortOrderAscending
+ }
+ if sorter.SortBy == "" {
+ sorter.SortBy = weles.JobSortByID
+ }
mockJobManager.EXPECT().ListJobs(
filter, sorter, paginator).Return(
jobInfo, listInfo, nil)
jobInfoStartingPage)),
}
+ if sorter.SortOrder == "" {
+ sorter.SortOrder = weles.SortOrderAscending
+ }
+ if sorter.SortBy == "" {
+ sorter.SortBy = weles.JobSortByID
+ }
first := mockJobManager.EXPECT().ListJobs(filter, sorter, paginator).Return(
jobInfoStartingPage, listInfo, nil)
}
+ if sorter.SortOrder == "" {
+ sorter.SortOrder = weles.SortOrderAscending
+ }
+ if sorter.SortBy == "" {
+ sorter.SortBy = weles.JobSortByID
+ }
first := mockJobManager.EXPECT().ListJobs(
filter, sorter, paginator).Return(jobInfoStartingPage, listInfo, nil)
}
listInfo := weles.ListInfo{TotalRecords: uint64(aviJobs), RemainingRecords: 0}
+ if sorter.SortOrder == "" {
+ sorter.SortOrder = weles.SortOrderAscending
+ }
+ if sorter.SortBy == "" {
+ sorter.SortBy = weles.JobSortByID
+ }
mockJobManager.EXPECT().ListJobs(filter, sorter, paginator).Return(
jobInfo, listInfo, jmerr)
reqBody := filterSorterReqBody(filter, sorter, JSON)
Expect(resp.Header.Get("RemainingRecords")).To(Equal(""))
},
- Entry("json, pagination off",
- int32(0), "?before=10&after=20", JSON, JSON, emptyFilter, emptySorter),
Entry("json, pagination on",
int32(100), "?before=10&after=20", JSON, JSON, emptyFilter, emptySorter),
)