From ce446e5e2a07535eb20109955f80fbd8a917e283 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Sun, 6 Sep 2015 16:18:25 -0700 Subject: [PATCH] http: complete the eval endpoints --- command/agent/eval_endpoint.go | 33 ++++++++++++++++++++- command/agent/eval_endpoint_test.go | 46 +++++++++++++++++++++++++++++ command/agent/job_endpoint_test.go | 2 +- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/command/agent/eval_endpoint.go b/command/agent/eval_endpoint.go index 7bd4dfc08..32cddcd16 100644 --- a/command/agent/eval_endpoint.go +++ b/command/agent/eval_endpoint.go @@ -27,7 +27,38 @@ func (s *HTTPServer) EvalsRequest(resp http.ResponseWriter, req *http.Request) ( } func (s *HTTPServer) EvalSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { - evalID := strings.TrimPrefix(req.URL.Path, "/v1/evaluation/") + 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) + return out.Allocations, nil +} + +func (s *HTTPServer) evalQuery(resp http.ResponseWriter, req *http.Request, evalID string) (interface{}, error) { if req.Method != "GET" { return nil, CodedError(405, ErrInvalidMethod) } diff --git a/command/agent/eval_endpoint_test.go b/command/agent/eval_endpoint_test.go index 53882bc16..22adce52a 100644 --- a/command/agent/eval_endpoint_test.go +++ b/command/agent/eval_endpoint_test.go @@ -53,6 +53,52 @@ func TestHTTP_EvalList(t *testing.T) { }) } +func TestHTTP_EvalAllocations(t *testing.T) { + httpTest(t, nil, func(s *TestServer) { + // Directly manipulate the state + state := s.Agent.server.State() + alloc1 := mock.Alloc() + alloc2 := mock.Alloc() + alloc2.EvalID = alloc1.EvalID + err := state.UpdateAllocations(1000, + []*structs.Allocation{alloc1, alloc2}) + if err != nil { + t.Fatalf("err: %v", err) + } + + // Make the HTTP request + req, err := http.NewRequest("GET", + "/v1/evaluation/"+alloc1.EvalID+"/allocations", nil) + if err != nil { + t.Fatalf("err: %v", err) + } + respW := httptest.NewRecorder() + + // Make the request + obj, err := s.Server.EvalSpecificRequest(respW, req) + if err != nil { + t.Fatalf("err: %v", err) + } + + // Check for the index + if respW.HeaderMap.Get("X-Nomad-Index") == "" { + t.Fatalf("missing index") + } + if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" { + t.Fatalf("missing known leader") + } + if respW.HeaderMap.Get("X-Nomad-LastContact") == "" { + t.Fatalf("missing last contact") + } + + // Check the ouptput + allocs := obj.([]*structs.AllocListStub) + if len(allocs) != 2 { + t.Fatalf("bad: %#v", allocs) + } + }) +} + func TestHTTP_EvalQuery(t *testing.T) { httpTest(t, nil, func(s *TestServer) { // Directly manipulate the state diff --git a/command/agent/job_endpoint_test.go b/command/agent/job_endpoint_test.go index 3d4bfb31a..78927a31c 100644 --- a/command/agent/job_endpoint_test.go +++ b/command/agent/job_endpoint_test.go @@ -373,7 +373,7 @@ func TestHTTP_JobAllocations(t *testing.T) { } // Check the response - allocs := obj.([]*structs.Allocation) + allocs := obj.([]*structs.AllocListStub) if len(allocs) != 1 && allocs[0].ID != alloc1.ID { t.Fatalf("bad: %v", allocs) }