2015-09-06 23:02:53 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2022-07-06 14:30:11 +00:00
|
|
|
"fmt"
|
2015-09-06 23:02:53 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2022-07-06 14:30:11 +00:00
|
|
|
// EvalsRequest is the entry point for /v1/evaluations and is responsible for
|
|
|
|
// handling both the listing of evaluations, and the bulk deletion of
|
|
|
|
// evaluations. The latter is a dangerous operation and should use the
|
|
|
|
// eval delete command to perform this.
|
2015-09-06 23:02:53 +00:00
|
|
|
func (s *HTTPServer) EvalsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2022-07-06 14:30:11 +00:00
|
|
|
switch req.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
return s.evalsListRequest(resp, req)
|
|
|
|
case http.MethodDelete:
|
|
|
|
return s.evalsDeleteRequest(resp, req)
|
|
|
|
default:
|
|
|
|
return nil, CodedError(http.StatusMethodNotAllowed, ErrInvalidMethod)
|
2015-09-06 23:02:53 +00:00
|
|
|
}
|
2022-07-06 14:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) evalsListRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2015-09-06 23:02:53 +00:00
|
|
|
|
|
|
|
args := structs.EvalListRequest{}
|
|
|
|
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-12-10 18:43:03 +00:00
|
|
|
query := req.URL.Query()
|
|
|
|
args.FilterEvalStatus = query.Get("status")
|
|
|
|
args.FilterJobID = query.Get("job")
|
|
|
|
|
2015-09-06 23:02:53 +00:00
|
|
|
var out structs.EvalListResponse
|
|
|
|
if err := s.agent.RPC("Eval.List", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
2015-09-07 17:03:10 +00:00
|
|
|
if out.Evaluations == nil {
|
|
|
|
out.Evaluations = make([]*structs.Evaluation, 0)
|
|
|
|
}
|
2015-09-06 23:02:53 +00:00
|
|
|
return out.Evaluations, nil
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:30:11 +00:00
|
|
|
func (s *HTTPServer) evalsDeleteRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
|
|
|
|
var args structs.EvalDeleteRequest
|
|
|
|
|
|
|
|
if err := decodeBody(req, &args); err != nil {
|
|
|
|
return nil, CodedError(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
numIDs := len(args.EvalIDs)
|
|
|
|
|
|
|
|
// Ensure the number of evaluation IDs included in the request is within
|
|
|
|
// bounds.
|
|
|
|
if numIDs < 1 {
|
|
|
|
return nil, CodedError(http.StatusBadRequest, "request does not include any evaluation IDs")
|
|
|
|
} else if numIDs > structs.MaxUUIDsPerWriteRequest {
|
|
|
|
return nil, CodedError(http.StatusBadRequest, fmt.Sprintf(
|
|
|
|
"request includes %v evaluations IDs, must be %v or fewer",
|
|
|
|
numIDs, structs.MaxUUIDsPerWriteRequest))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the write request to populate all meta fields.
|
|
|
|
s.parseWriteRequest(req, &args.WriteRequest)
|
|
|
|
|
|
|
|
var reply structs.EvalDeleteResponse
|
|
|
|
if err := s.agent.RPC(structs.EvalDeleteRPCMethod, &args, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
setIndex(resp, reply.Index)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-09-06 23:02:53 +00:00
|
|
|
func (s *HTTPServer) EvalSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2015-09-06 23:18:25 +00:00
|
|
|
path := strings.TrimPrefix(req.URL.Path, "/v1/evaluation/")
|
|
|
|
switch {
|
|
|
|
case strings.HasSuffix(path, "/allocations"):
|
|
|
|
evalID := strings.TrimSuffix(path, "/allocations")
|
|
|
|
return s.evalAllocations(resp, req, evalID)
|
|
|
|
default:
|
|
|
|
return s.evalQuery(resp, req, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) evalAllocations(resp http.ResponseWriter, req *http.Request, evalID string) (interface{}, error) {
|
|
|
|
if req.Method != "GET" {
|
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := structs.EvalSpecificRequest{
|
|
|
|
EvalID: evalID,
|
|
|
|
}
|
|
|
|
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.EvalAllocationsResponse
|
|
|
|
if err := s.agent.RPC("Eval.Allocations", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
2015-09-07 17:03:10 +00:00
|
|
|
if out.Allocations == nil {
|
|
|
|
out.Allocations = make([]*structs.AllocListStub, 0)
|
|
|
|
}
|
2015-09-06 23:18:25 +00:00
|
|
|
return out.Allocations, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) evalQuery(resp http.ResponseWriter, req *http.Request, evalID string) (interface{}, error) {
|
2015-09-06 23:02:53 +00:00
|
|
|
if req.Method != "GET" {
|
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := structs.EvalSpecificRequest{
|
|
|
|
EvalID: evalID,
|
|
|
|
}
|
|
|
|
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:56:14 +00:00
|
|
|
query := req.URL.Query()
|
|
|
|
args.IncludeRelated = query.Get("related") == "true"
|
|
|
|
|
2015-09-06 23:02:53 +00:00
|
|
|
var out structs.SingleEvalResponse
|
|
|
|
if err := s.agent.RPC("Eval.GetEval", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
|
|
|
if out.Eval == nil {
|
|
|
|
return nil, CodedError(404, "eval not found")
|
|
|
|
}
|
|
|
|
return out.Eval, nil
|
|
|
|
}
|