From 57a69685a28b16fb66d2dfedafebf60b622808ea Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Sun, 6 Sep 2015 15:37:21 -0700 Subject: [PATCH] http: adding allocs list endpoint --- command/agent/alloc_endpoint.go | 51 ++++++++++++++++++++++++++ command/agent/alloc_endpoint_test.go | 54 ++++++++++++++++++++++++++++ command/agent/http.go | 3 ++ 3 files changed, 108 insertions(+) create mode 100644 command/agent/alloc_endpoint.go create mode 100644 command/agent/alloc_endpoint_test.go diff --git a/command/agent/alloc_endpoint.go b/command/agent/alloc_endpoint.go new file mode 100644 index 000000000..7203464bd --- /dev/null +++ b/command/agent/alloc_endpoint.go @@ -0,0 +1,51 @@ +package agent + +import ( + "net/http" + "strings" + + "github.com/hashicorp/nomad/nomad/structs" +) + +func (s *HTTPServer) AllocsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { + if req.Method != "GET" { + return nil, CodedError(405, ErrInvalidMethod) + } + + args := structs.AllocListRequest{} + if s.parse(resp, req, &args.Region, &args.QueryOptions) { + return nil, nil + } + + var out structs.AllocListResponse + if err := s.agent.RPC("Alloc.List", &args, &out); err != nil { + return nil, err + } + + setMeta(resp, &out.QueryMeta) + return out.Allocations, nil +} + +func (s *HTTPServer) AllocSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { + allocID := strings.TrimPrefix(req.URL.Path, "/v1/allocation/") + switch req.Method { + case "GET": + return s.allocQuery(resp, req, allocID) + case "DELETE": + return s.allocDelete(resp, req, allocID) + default: + return nil, CodedError(405, ErrInvalidMethod) + } +} + +func (s *HTTPServer) allocQuery(resp http.ResponseWriter, req *http.Request, + allocID string) (interface{}, error) { + // TODO + return nil, nil +} + +func (s *HTTPServer) allocDelete(resp http.ResponseWriter, req *http.Request, + allocID string) (interface{}, error) { + // TODO + return nil, nil +} diff --git a/command/agent/alloc_endpoint_test.go b/command/agent/alloc_endpoint_test.go new file mode 100644 index 000000000..ab9553121 --- /dev/null +++ b/command/agent/alloc_endpoint_test.go @@ -0,0 +1,54 @@ +package agent + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/hashicorp/nomad/nomad/mock" + "github.com/hashicorp/nomad/nomad/structs" +) + +func TestHTTP_AllocsList(t *testing.T) { + httpTest(t, nil, func(s *TestServer) { + // Directly manipulate the state + state := s.Agent.server.State() + alloc1 := mock.Alloc() + alloc2 := mock.Alloc() + 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/allocations", nil) + if err != nil { + t.Fatalf("err: %v", err) + } + respW := httptest.NewRecorder() + + // Make the request + obj, err := s.Server.AllocsRequest(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 job + n := obj.([]*structs.AllocListStub) + if len(n) != 2 { + t.Fatalf("bad: %#v", n) + } + }) +} diff --git a/command/agent/http.go b/command/agent/http.go index 2ed502309..86df51d2c 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -69,6 +69,9 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) { s.mux.HandleFunc("/v1/nodes", s.wrap(s.NodesRequest)) s.mux.HandleFunc("/v1/node/", s.wrap(s.NodeSpecificRequest)) + s.mux.HandleFunc("/v1/allocations", s.wrap(s.AllocsRequest)) + s.mux.HandleFunc("/v1/allocation/", s.wrap(s.AllocSpecificRequest)) + if enableDebug { s.mux.HandleFunc("/debug/pprof/", pprof.Index) s.mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)