github.com/quite/nomad@v0.8.6/command/agent/eval_endpoint.go (about)

     1  package agent
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  )
     9  
    10  func (s *HTTPServer) EvalsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    11  	if req.Method != "GET" {
    12  		return nil, CodedError(405, ErrInvalidMethod)
    13  	}
    14  
    15  	args := structs.EvalListRequest{}
    16  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
    17  		return nil, nil
    18  	}
    19  
    20  	var out structs.EvalListResponse
    21  	if err := s.agent.RPC("Eval.List", &args, &out); err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	setMeta(resp, &out.QueryMeta)
    26  	if out.Evaluations == nil {
    27  		out.Evaluations = make([]*structs.Evaluation, 0)
    28  	}
    29  	return out.Evaluations, nil
    30  }
    31  
    32  func (s *HTTPServer) EvalSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    33  	path := strings.TrimPrefix(req.URL.Path, "/v1/evaluation/")
    34  	switch {
    35  	case strings.HasSuffix(path, "/allocations"):
    36  		evalID := strings.TrimSuffix(path, "/allocations")
    37  		return s.evalAllocations(resp, req, evalID)
    38  	default:
    39  		return s.evalQuery(resp, req, path)
    40  	}
    41  }
    42  
    43  func (s *HTTPServer) evalAllocations(resp http.ResponseWriter, req *http.Request, evalID string) (interface{}, error) {
    44  	if req.Method != "GET" {
    45  		return nil, CodedError(405, ErrInvalidMethod)
    46  	}
    47  
    48  	args := structs.EvalSpecificRequest{
    49  		EvalID: evalID,
    50  	}
    51  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
    52  		return nil, nil
    53  	}
    54  
    55  	var out structs.EvalAllocationsResponse
    56  	if err := s.agent.RPC("Eval.Allocations", &args, &out); err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	setMeta(resp, &out.QueryMeta)
    61  	if out.Allocations == nil {
    62  		out.Allocations = make([]*structs.AllocListStub, 0)
    63  	}
    64  	return out.Allocations, nil
    65  }
    66  
    67  func (s *HTTPServer) evalQuery(resp http.ResponseWriter, req *http.Request, evalID string) (interface{}, error) {
    68  	if req.Method != "GET" {
    69  		return nil, CodedError(405, ErrInvalidMethod)
    70  	}
    71  
    72  	args := structs.EvalSpecificRequest{
    73  		EvalID: evalID,
    74  	}
    75  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
    76  		return nil, nil
    77  	}
    78  
    79  	var out structs.SingleEvalResponse
    80  	if err := s.agent.RPC("Eval.GetEval", &args, &out); err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	setMeta(resp, &out.QueryMeta)
    85  	if out.Eval == nil {
    86  		return nil, CodedError(404, "eval not found")
    87  	}
    88  	return out.Eval, nil
    89  }