Merge pull request #57 from hashicorp/f-api-sort
api: sort all list responses
This commit is contained in:
commit
e293e354c1
|
@ -1,6 +1,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -21,6 +22,7 @@ func (a *Allocations) List(q *QueryOptions) ([]*AllocationListStub, *QueryMeta,
|
|||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
sort.Sort(AllocIndexSort(resp))
|
||||
return resp, qm, nil
|
||||
}
|
||||
|
||||
|
@ -84,3 +86,18 @@ type AllocationListStub struct {
|
|||
CreateIndex uint64
|
||||
ModifyIndex uint64
|
||||
}
|
||||
|
||||
// AllocIndexSort reverse sorts allocs by CreateIndex.
|
||||
type AllocIndexSort []*AllocationListStub
|
||||
|
||||
func (a AllocIndexSort) Len() int {
|
||||
return len(a)
|
||||
}
|
||||
|
||||
func (a AllocIndexSort) Less(i, j int) bool {
|
||||
return a[i].CreateIndex > a[j].CreateIndex
|
||||
}
|
||||
|
||||
func (a AllocIndexSort) Swap(i, j int) {
|
||||
a[i], a[j] = a[j], a[i]
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -49,3 +51,21 @@ func TestAllocations_List(t *testing.T) {
|
|||
t.Fatalf("bad: %#v", allocs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllocations_CreateIndexSort(t *testing.T) {
|
||||
allocs := []*AllocationListStub{
|
||||
&AllocationListStub{CreateIndex: 2},
|
||||
&AllocationListStub{CreateIndex: 1},
|
||||
&AllocationListStub{CreateIndex: 5},
|
||||
}
|
||||
sort.Sort(AllocIndexSort(allocs))
|
||||
|
||||
expect := []*AllocationListStub{
|
||||
&AllocationListStub{CreateIndex: 5},
|
||||
&AllocationListStub{CreateIndex: 2},
|
||||
&AllocationListStub{CreateIndex: 1},
|
||||
}
|
||||
if !reflect.DeepEqual(allocs, expect) {
|
||||
t.Fatalf("\n\n%#v\n\n%#v", allocs, expect)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -21,6 +22,7 @@ func (e *Evaluations) List(q *QueryOptions) ([]*Evaluation, *QueryMeta, error) {
|
|||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
sort.Sort(EvalIndexSort(resp))
|
||||
return resp, qm, nil
|
||||
}
|
||||
|
||||
|
@ -42,6 +44,7 @@ func (e *Evaluations) Allocations(evalID string, q *QueryOptions) ([]*Allocation
|
|||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
sort.Sort(AllocIndexSort(resp))
|
||||
return resp, qm, nil
|
||||
}
|
||||
|
||||
|
@ -60,4 +63,22 @@ type Evaluation struct {
|
|||
Wait time.Duration
|
||||
NextEval string
|
||||
PreviousEval string
|
||||
CreateIndex uint64
|
||||
ModifyIndex uint64
|
||||
}
|
||||
|
||||
// EvalIndexSort is a wrapper to sort evaluations by CreateIndex.
|
||||
// We reverse the test so that we get the highest index first.
|
||||
type EvalIndexSort []*Evaluation
|
||||
|
||||
func (e EvalIndexSort) Len() int {
|
||||
return len(e)
|
||||
}
|
||||
|
||||
func (e EvalIndexSort) Less(i, j int) bool {
|
||||
return e[i].CreateIndex > e[j].CreateIndex
|
||||
}
|
||||
|
||||
func (e EvalIndexSort) Swap(i, j int) {
|
||||
e[i], e[j] = e[j], e[i]
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -94,3 +96,21 @@ func TestEvaluations_Allocations(t *testing.T) {
|
|||
t.Fatalf("expected 0 allocs, got: %d", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluations_Sort(t *testing.T) {
|
||||
evals := []*Evaluation{
|
||||
&Evaluation{CreateIndex: 2},
|
||||
&Evaluation{CreateIndex: 1},
|
||||
&Evaluation{CreateIndex: 5},
|
||||
}
|
||||
sort.Sort(EvalIndexSort(evals))
|
||||
|
||||
expect := []*Evaluation{
|
||||
&Evaluation{CreateIndex: 5},
|
||||
&Evaluation{CreateIndex: 2},
|
||||
&Evaluation{CreateIndex: 1},
|
||||
}
|
||||
if !reflect.DeepEqual(evals, expect) {
|
||||
t.Fatalf("\n\n%#v\n\n%#v", evals, expect)
|
||||
}
|
||||
}
|
||||
|
|
22
api/jobs.go
22
api/jobs.go
|
@ -1,5 +1,9 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
const (
|
||||
// JobTypeService indicates a long-running processes
|
||||
JobTypeService = "service"
|
||||
|
@ -38,6 +42,7 @@ func (j *Jobs) List(q *QueryOptions) ([]*JobListStub, *QueryMeta, error) {
|
|||
if err != nil {
|
||||
return nil, qm, err
|
||||
}
|
||||
sort.Sort(JobIDSort(resp))
|
||||
return resp, qm, nil
|
||||
}
|
||||
|
||||
|
@ -59,6 +64,7 @@ func (j *Jobs) Allocations(jobID string, q *QueryOptions) ([]*AllocationListStub
|
|||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
sort.Sort(AllocIndexSort(resp))
|
||||
return resp, qm, nil
|
||||
}
|
||||
|
||||
|
@ -70,6 +76,7 @@ func (j *Jobs) Evaluations(jobID string, q *QueryOptions) ([]*Evaluation, *Query
|
|||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
sort.Sort(EvalIndexSort(resp))
|
||||
return resp, qm, nil
|
||||
}
|
||||
|
||||
|
@ -123,6 +130,21 @@ type JobListStub struct {
|
|||
ModifyIndex uint64
|
||||
}
|
||||
|
||||
// JobIDSort is used to sort jobs by their job ID's.
|
||||
type JobIDSort []*JobListStub
|
||||
|
||||
func (j JobIDSort) Len() int {
|
||||
return len(j)
|
||||
}
|
||||
|
||||
func (j JobIDSort) Less(a, b int) bool {
|
||||
return j[a].ID < j[b].ID
|
||||
}
|
||||
|
||||
func (j JobIDSort) Swap(a, b int) {
|
||||
j[a], j[b] = j[b], j[a]
|
||||
}
|
||||
|
||||
// NewServiceJob creates and returns a new service-style job
|
||||
// for long-lived processes using the provided name, ID, and
|
||||
// relative job priority.
|
||||
|
|
|
@ -2,6 +2,7 @@ package api
|
|||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -303,3 +304,21 @@ func TestJobs_Constrain(t *testing.T) {
|
|||
t.Fatalf("expect: %#v, got: %#v", expect, job.Constraints)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobs_Sort(t *testing.T) {
|
||||
jobs := []*JobListStub{
|
||||
&JobListStub{ID: "job2"},
|
||||
&JobListStub{ID: "job0"},
|
||||
&JobListStub{ID: "job1"},
|
||||
}
|
||||
sort.Sort(JobIDSort(jobs))
|
||||
|
||||
expect := []*JobListStub{
|
||||
&JobListStub{ID: "job0"},
|
||||
&JobListStub{ID: "job1"},
|
||||
&JobListStub{ID: "job2"},
|
||||
}
|
||||
if !reflect.DeepEqual(jobs, expect) {
|
||||
t.Fatalf("\n\n%#v\n\n%#v", jobs, expect)
|
||||
}
|
||||
}
|
||||
|
|
20
api/nodes.go
20
api/nodes.go
|
@ -1,6 +1,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
|
@ -16,11 +17,12 @@ func (c *Client) Nodes() *Nodes {
|
|||
|
||||
// List is used to list out all of the nodes
|
||||
func (n *Nodes) List(q *QueryOptions) ([]*NodeListStub, *QueryMeta, error) {
|
||||
var resp []*NodeListStub
|
||||
var resp NodeIndexSort
|
||||
qm, err := n.client.query("/v1/nodes", &resp, q)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
sort.Sort(NodeIndexSort(resp))
|
||||
return resp, qm, nil
|
||||
}
|
||||
|
||||
|
@ -51,6 +53,7 @@ func (n *Nodes) Allocations(nodeID string, q *QueryOptions) ([]*AllocationListSt
|
|||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
sort.Sort(AllocIndexSort(resp))
|
||||
return resp, qm, nil
|
||||
}
|
||||
|
||||
|
@ -95,6 +98,21 @@ type NodeListStub struct {
|
|||
ModifyIndex uint64
|
||||
}
|
||||
|
||||
// NodeIndexSort reverse sorts nodes by CreateIndex
|
||||
type NodeIndexSort []*NodeListStub
|
||||
|
||||
func (n NodeIndexSort) Len() int {
|
||||
return len(n)
|
||||
}
|
||||
|
||||
func (n NodeIndexSort) Less(i, j int) bool {
|
||||
return n[i].CreateIndex > n[j].CreateIndex
|
||||
}
|
||||
|
||||
func (n NodeIndexSort) Swap(i, j int) {
|
||||
n[i], n[j] = n[j], n[i]
|
||||
}
|
||||
|
||||
// nodeEvalResponse is used to decode a force-eval.
|
||||
type nodeEvalResponse struct {
|
||||
EvalID string
|
||||
|
|
|
@ -2,6 +2,8 @@ package api
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
@ -201,3 +203,21 @@ func TestNodes_ForceEvaluate(t *testing.T) {
|
|||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodes_Sort(t *testing.T) {
|
||||
nodes := []*NodeListStub{
|
||||
&NodeListStub{CreateIndex: 2},
|
||||
&NodeListStub{CreateIndex: 1},
|
||||
&NodeListStub{CreateIndex: 5},
|
||||
}
|
||||
sort.Sort(NodeIndexSort(nodes))
|
||||
|
||||
expect := []*NodeListStub{
|
||||
&NodeListStub{CreateIndex: 5},
|
||||
&NodeListStub{CreateIndex: 2},
|
||||
&NodeListStub{CreateIndex: 1},
|
||||
}
|
||||
if !reflect.DeepEqual(nodes, expect) {
|
||||
t.Fatalf("\n\n%#v\n\n%#v", nodes, expect)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue