2017-07-28 21:48:15 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2017-08-03 14:47:20 +00:00
|
|
|
"fmt"
|
2017-07-28 21:48:15 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Resources struct {
|
|
|
|
srv *Server
|
|
|
|
}
|
|
|
|
|
2017-08-02 21:55:48 +00:00
|
|
|
func getMatches(iter memdb.ResultIterator, context, prefix string) ([]string, bool) {
|
|
|
|
var matches []string
|
|
|
|
isTruncated := false
|
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
raw := iter.Next()
|
|
|
|
if raw == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
getID := func(i interface{}) string {
|
|
|
|
switch i.(type) {
|
|
|
|
case *structs.Job:
|
|
|
|
return i.(*structs.Job).ID
|
|
|
|
case *structs.Evaluation:
|
|
|
|
return i.(*structs.Evaluation).ID
|
2017-08-03 14:28:38 +00:00
|
|
|
case *structs.Allocation:
|
|
|
|
return i.(*structs.Allocation).ID
|
|
|
|
case *structs.Node:
|
|
|
|
return i.(*structs.Node).ID
|
2017-08-02 21:55:48 +00:00
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
id := getID(raw)
|
|
|
|
if id == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
matches = append(matches, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if iter.Next() != nil {
|
|
|
|
isTruncated = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches, isTruncated
|
|
|
|
}
|
|
|
|
|
2017-07-28 21:48:15 +00:00
|
|
|
// List is used to list the jobs registered in the system
|
|
|
|
// TODO if no context, return all
|
|
|
|
func (r *Resources) List(args *structs.ResourcesRequest,
|
|
|
|
reply *structs.ResourcesResponse) error {
|
2017-08-02 21:55:48 +00:00
|
|
|
reply.Matches = make(map[string][]string)
|
|
|
|
reply.Truncations = make(map[string]bool)
|
2017-07-28 21:48:15 +00:00
|
|
|
|
|
|
|
// Setup the blocking query
|
|
|
|
opts := blockingOptions{
|
|
|
|
queryMeta: &reply.QueryMeta,
|
2017-08-02 15:57:19 +00:00
|
|
|
queryOpts: &structs.QueryOptions{},
|
2017-07-28 21:48:15 +00:00
|
|
|
run: func(ws memdb.WatchSet, state *state.StateStore) error {
|
|
|
|
|
|
|
|
// return jobs matching given prefix
|
|
|
|
var err error
|
|
|
|
var iter memdb.ResultIterator
|
2017-08-02 21:55:48 +00:00
|
|
|
res := make([]string, 0)
|
|
|
|
isTrunc := false
|
2017-08-02 17:21:58 +00:00
|
|
|
|
2017-08-03 14:47:20 +00:00
|
|
|
switch args.Context {
|
|
|
|
case "job":
|
2017-08-02 17:21:58 +00:00
|
|
|
iter, err = state.JobsByIDPrefix(ws, args.Prefix)
|
2017-08-03 14:47:20 +00:00
|
|
|
case "eval":
|
2017-08-02 17:21:58 +00:00
|
|
|
iter, err = state.EvalsByIDPrefix(ws, args.Prefix)
|
2017-08-03 14:47:20 +00:00
|
|
|
case "alloc":
|
2017-08-03 14:28:38 +00:00
|
|
|
iter, err = state.AllocsByIDPrefix(ws, args.Prefix)
|
2017-08-03 14:47:20 +00:00
|
|
|
case "node":
|
2017-08-03 14:28:38 +00:00
|
|
|
iter, err = state.NodesByIDPrefix(ws, args.Prefix)
|
2017-08-03 14:47:20 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid context")
|
2017-07-28 21:48:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 21:55:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-03 14:28:38 +00:00
|
|
|
|
2017-08-02 21:55:48 +00:00
|
|
|
res, isTrunc = getMatches(iter, args.Context, args.Prefix)
|
|
|
|
reply.Matches[args.Context] = res
|
|
|
|
reply.Truncations[args.Context] = isTrunc
|
2017-07-28 21:48:15 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}}
|
|
|
|
return r.srv.blockingRPC(&opts)
|
|
|
|
}
|